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
9 changes: 9 additions & 0 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@
// if it's an inline module, or the first char of a the file if it's an external module.
// - `location` will always point to the token "foo" in `mod foo` regardless of whether
// it's inline or external.
// Eventually the location put in `ModuleData` is used for codelenses about `contract`s,

Check warning on line 929 in compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (codelenses)
// so we keep using `location` so that it continues to work as usual.
let location = Location::new(mod_name.span(), mod_location.file);
let new_module = ModuleData::new(
Expand Down Expand Up @@ -1030,6 +1030,15 @@
interner.register_function(func_id, &function.def);
}

if is_entry_point_function {
if let Some(generic) = function.def.generics.first() {
let name = name.to_string();
let location = generic.location();
let error = DefCollectorErrorKind::EntryPointWithGenerics { name, location };
errors.push(error.into());
}
}

if !is_test
&& !is_fuzzing_harness
&& !is_entry_point_function
Expand Down
10 changes: 9 additions & 1 deletion compiler/noirc_frontend/src/hir/def_collector/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ pub enum DefCollectorErrorKind {
TestOnlyFailWithWithoutParameters { location: Location },
#[error("The `#[fuzz]` attribute may only be used on functions with parameters")]
FuzzingHarnessWithoutParameters { location: Location },
#[error("`{name}` entry-point function is not allowed to have generic parameters")]
EntryPointWithGenerics { name: String, location: Location },
}

impl DefCollectorErrorKind {
Expand Down Expand Up @@ -112,7 +114,8 @@ impl DefCollectorErrorKind {
| DefCollectorErrorKind::TraitMissingMethod { trait_impl_location: location, .. }
| DefCollectorErrorKind::ForeignImpl { location, .. }
| DefCollectorErrorKind::TestOnlyFailWithWithoutParameters { location }
| DefCollectorErrorKind::FuzzingHarnessWithoutParameters { location } => *location,
| DefCollectorErrorKind::FuzzingHarnessWithoutParameters { location }
| DefCollectorErrorKind::EntryPointWithGenerics { location, .. } => *location,
DefCollectorErrorKind::NotATrait { not_a_trait_name: path }
| DefCollectorErrorKind::TraitNotFound { trait_path: path } => path.location,
DefCollectorErrorKind::UnsupportedNumericGenericType(
Expand Down Expand Up @@ -297,6 +300,11 @@ impl<'a> From<&'a DefCollectorErrorKind> for Diagnostic {
String::new(),
*location,
),
DefCollectorErrorKind::EntryPointWithGenerics { name, location } => Diagnostic::simple_error(
format!("`{name}` entry-point function is not allowed to have generic parameters"),
String::new(),
*location,
),
}
}
}
6 changes: 4 additions & 2 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1268,16 +1268,18 @@
match self {
// Type::Error is allowed as usual since it indicates an error was already issued and
// we don't need to issue further errors about this likely unresolved type
// TypeVariable and Generic are allowed here too as they can only result from
// generics being declared on the function itself, but we produce a different error in that case.
Type::FieldElement
| Type::Integer(_, _)
| Type::Bool
| Type::Unit
| Type::Constant(_, _)
| Type::TypeVariable(_)
| Type::NamedGeneric(_)
| Type::Error => true,

Type::FmtString(_, _)
| Type::TypeVariable(_)
| Type::NamedGeneric(_)
| Type::Function(_, _, _, _)
| Type::Reference(..)
| Type::Forall(_, _)
Expand Down Expand Up @@ -2397,7 +2399,7 @@
}

let recur_on_binding = |id, replacement: &Type| {
// Prevent recuring forever if there's a `T := T` binding

Check warning on line 2402 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (recuring)
if replacement.type_variable_id() == Some(id) {
replacement.clone()
} else {
Expand Down Expand Up @@ -2483,7 +2485,7 @@
Type::Tuple(fields)
}
Type::Forall(typevars, typ) => {
// Trying to substitute_helper a variable de, substitute_bound_typevarsfined within a nested Forall

Check warning on line 2488 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
// is usually impossible and indicative of an error in the type checker somewhere.
for var in typevars {
assert!(!type_bindings.contains_key(&var.id()));
Expand Down Expand Up @@ -2650,7 +2652,7 @@
}
}

/// Follow bindings if this is a type variable or generic to the first non-typevariable

Check warning on line 2655 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevariable)
/// type. Unlike `follow_bindings`, this won't recursively follow any bindings on any
/// fields or arguments of this type.
pub fn follow_bindings_shallow(&self) -> Cow<Type> {
Expand All @@ -2675,7 +2677,7 @@

/// Replace any `Type::NamedGeneric` in this type with a `Type::TypeVariable`
/// using to the same inner `TypeVariable`. This is used during monomorphization
/// to bind to named generics since they are unbindable during type checking.

Check warning on line 2680 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (unbindable)
pub fn replace_named_generics_with_type_variables(&mut self) {
match self {
Type::FieldElement
Expand Down Expand Up @@ -3221,7 +3223,7 @@
len.hash(state);
env.hash(state);
}
Type::Tuple(elems) => elems.hash(state),

Check warning on line 3226 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)

Check warning on line 3226 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
Type::DataType(def, args) => {
def.hash(state);
args.hash(state);
Expand Down
7 changes: 7 additions & 0 deletions test_programs/compile_failure/main_with_generics/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "main_with_generics"
type = "bin"
authors = [""]
compiler_version = ">=0.33.0"

[dependencies]
3 changes: 3 additions & 0 deletions test_programs/compile_failure/main_with_generics/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main<let F: u32>(x: [Field; F]) {
assert(x[0] != x[1]);
}
8 changes: 8 additions & 0 deletions test_programs/compile_failure/main_with_generics/stderr.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: `main` entry-point function is not allowed to have generic parameters
┌─ src/main.nr:1:13
1 │ fn main<let F: u32>(x: [Field; F]) {
│ ------

Aborting due to 1 previous error
Loading