Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
node_interner::{GlobalValue, QuotedTypeId},
token::SecondaryAttributeKind,
usage_tracker::UsageTracker,
validity::length_is_zero,
};
use crate::{
EnumVariant, Shared, Type, TypeVariable,
Expand Down Expand Up @@ -1021,6 +1022,16 @@ impl<'context> Elaborator<'context> {

let return_type = Box::new(self.use_type(func.return_type(), wildcard_allowed));

// Temporary allow slices for contract functions, until contracts are re-factored.
if !func.attributes().has_contract_library_method() {
self.check_if_type_is_valid_for_program_output(
&return_type,
is_entry_point || is_test_or_fuzz,
has_inline_attribute,
location,
);
}

let mut typ = Type::Function(
parameter_types,
return_type,
Expand Down Expand Up @@ -1181,6 +1192,32 @@ impl<'context> Elaborator<'context> {
}
}

fn check_if_type_is_valid_for_program_output(
&mut self,
typ: &Type,
is_entry_point: bool,
has_inline_attribute: bool,
location: Location,
) {
match typ {
Type::Unit => return,
Type::Array(length, _) | Type::String(length) => {
if length_is_zero(length) {
//returning zero length arrays is allowed
return;
}
}
_ => (),
}

self.check_if_type_is_valid_for_program_input(
typ,
is_entry_point,
has_inline_attribute,
location,
);
}

/// True if the `pub` keyword is allowed on parameters in this function
/// `pub` on function parameters is only allowed for entry point functions
fn pub_allowed(&self, func: &NoirFunction, in_contract: bool) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/hir_def/types/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@ impl Type {
}
}

fn length_is_zero(length: &Type) -> bool {
pub(crate) fn length_is_zero(length: &Type) -> bool {
length.evaluate_to_u32(Location::dummy()).unwrap_or(0) == 0
}
13 changes: 13 additions & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4514,6 +4514,19 @@ fn cannot_assign_to_nested_struct() {
check_errors!(src);
}

#[test]
fn cannot_return_slice_from_main() {
let src = r#"
fn main() -> pub [Field]{
^^^^ Invalid type found in the entry point to a program
~~~~ Slice is not a valid entry point type. Found: [Field]
&[1,2]

}
"#;
check_errors!(src);
}

#[test]
fn disallows_references_in_globals() {
let src = r#"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

[package]
name = "noirc_frontend_tests_cannot_return_slice_from_main"
type = "bin"
authors = [""]

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

fn main() -> pub [Field]{
&[1,2]

}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1631102025577823237

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading