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
6 changes: 5 additions & 1 deletion compiler/noirc_frontend/src/elaborator/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ impl Elaborator<'_> {
self.declare_methods(self_type, &function_ids);
}
} else {
self.push_err(DefCollectorErrorKind::NonStructTypeInImpl { location });
let is_primitive = self_type.is_primitive();
self.push_err(DefCollectorErrorKind::NonStructTypeInImpl {
location,
is_primitive,
});
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions compiler/noirc_frontend/src/hir/def_collector/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum DefCollectorErrorKind {
#[error("Cannot re-export {item_name} because it has less visibility than this use statement")]
CannotReexportItemWithLessVisibility { item_name: Ident, desired_visibility: ItemVisibility },
#[error("Non-struct type used in impl")]
NonStructTypeInImpl { location: Location },
NonStructTypeInImpl { location: Location, is_primitive: bool },
#[error("Cannot implement trait on a reference type")]
ReferenceInTraitImpl { location: Location },
#[error("Impl for type `{typ}` overlaps with existing impl")]
Expand Down Expand Up @@ -101,7 +101,7 @@ impl DefCollectorErrorKind {
}
| DefCollectorErrorKind::TestOnAssociatedFunction { location }
| DefCollectorErrorKind::ExportOnAssociatedFunction { location }
| DefCollectorErrorKind::NonStructTypeInImpl { location }
| DefCollectorErrorKind::NonStructTypeInImpl { location, .. }
| DefCollectorErrorKind::ReferenceInTraitImpl { location }
| DefCollectorErrorKind::OverlappingImpl { location, .. }
| DefCollectorErrorKind::ModuleAlreadyPartOfCrate { location, .. }
Expand Down Expand Up @@ -193,11 +193,21 @@ impl<'a> From<&'a DefCollectorErrorKind> for Diagnostic {
format!("consider marking {item_name} as {desired_visibility}"),
item_name.location())
}
DefCollectorErrorKind::NonStructTypeInImpl { location } => Diagnostic::simple_error(
"Non-struct type used in impl".into(),
"Only struct types may have implementation methods".into(),
*location,
),
DefCollectorErrorKind::NonStructTypeInImpl { location, is_primitive } =>{
if *is_primitive {
Diagnostic::simple_error(
"Cannot define inherent `impl` for primitive types".into(),
"Primitive types can only have implementation methods defined in the standard library".into(),
*location,
)
}else{
Diagnostic::simple_error(
"Non-struct type used in impl".into(),
"Only struct types may have implementation methods".into(),
*location,
)
}
}
DefCollectorErrorKind::ReferenceInTraitImpl { location } => Diagnostic::simple_error(
"Trait impls are not allowed on reference types".into(),
"Try using a struct type here instead".into(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/tests/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ fn errors_on_impl_for_primitive_type_outside_stdlib() {
let src = r#"
// User code (not stdlib) cannot impl methods on primitive types
impl Field {
^^^^^ Non-struct type used in impl
~~~~~ Only struct types may have implementation methods
^^^^^ Cannot define inherent `impl` for primitive types
~~~~~ Primitive types can only have implementation methods defined in the standard library
fn my_method(self) -> Field {
self
}
Expand Down

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

Loading