diff --git a/compiler/noirc_driver/src/lib.rs b/compiler/noirc_driver/src/lib.rs index 8a554879e9f..a2c069eac68 100644 --- a/compiler/noirc_driver/src/lib.rs +++ b/compiler/noirc_driver/src/lib.rs @@ -252,7 +252,7 @@ pub fn check_crate( let mut errors = vec![]; let diagnostics = CrateDefMap::collect_defs(crate_id, context, macros); errors.extend(diagnostics.into_iter().map(|(error, file_id)| { - let diagnostic: CustomDiagnostic = error.into(); + let diagnostic = CustomDiagnostic::from(&error); diagnostic.in_file(file_id) })); diff --git a/compiler/noirc_frontend/src/hir/comptime/errors.rs b/compiler/noirc_frontend/src/hir/comptime/errors.rs index 8de444252ab..af5ba9a44cf 100644 --- a/compiler/noirc_frontend/src/hir/comptime/errors.rs +++ b/compiler/noirc_frontend/src/hir/comptime/errors.rs @@ -7,7 +7,7 @@ use super::value::Value; /// The possible errors that can halt the interpreter. #[derive(Debug, Clone)] pub enum InterpreterError { - ArgumentCountMismatch { expected: usize, actual: usize, call_location: Location }, + ArgumentCountMismatch { expected: usize, actual: usize, location: Location }, TypeMismatch { expected: Type, value: Value, location: Location }, NonComptimeVarReferenced { name: String, location: Location }, IntegerOutOfRangeForType { value: FieldElement, typ: Type, location: Location }, @@ -60,7 +60,7 @@ impl InterpreterError { pub fn get_location(&self) -> Location { match self { - InterpreterError::ArgumentCountMismatch { call_location: location, .. } + InterpreterError::ArgumentCountMismatch { location, .. } | InterpreterError::TypeMismatch { location, .. } | InterpreterError::NonComptimeVarReferenced { location, .. } | InterpreterError::IntegerOutOfRangeForType { location, .. } @@ -98,20 +98,20 @@ impl InterpreterError { } } -impl From for CustomDiagnostic { - fn from(error: InterpreterError) -> Self { +impl<'a> From<&'a InterpreterError> for CustomDiagnostic { + fn from(error: &'a InterpreterError) -> Self { match error { - InterpreterError::ArgumentCountMismatch { expected, actual, call_location } => { + InterpreterError::ArgumentCountMismatch { expected, actual, location } => { let only = if expected > actual { "only " } else { "" }; - let plural = if expected == 1 { "" } else { "s" }; - let was_were = if actual == 1 { "was" } else { "were" }; + let plural = if *expected == 1 { "" } else { "s" }; + let was_were = if *actual == 1 { "was" } else { "were" }; let msg = format!( "Expected {expected} argument{plural}, but {only}{actual} {was_were} provided" ); let few_many = if actual < expected { "few" } else { "many" }; let secondary = format!("Too {few_many} arguments"); - CustomDiagnostic::simple_error(msg, secondary, call_location.span) + CustomDiagnostic::simple_error(msg, secondary, location.span) } InterpreterError::TypeMismatch { expected, value, location } => { let typ = value.get_type(); diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index 4bdd4eaec9a..94456f3037a 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -70,20 +70,21 @@ impl<'a> Interpreter<'a> { &mut self, function: FuncId, arguments: Vec<(Value, Location)>, - call_location: Location, + location: Location, ) -> IResult { let previous_state = self.enter_function(); let meta = self.interner.function_meta(&function); if meta.kind != FunctionKind::Normal { - todo!("Evaluation for {:?} is unimplemented", meta.kind); + let item = "Evaluation for builtin functions"; + return Err(InterpreterError::Unimplemented { item, location }); } if meta.parameters.len() != arguments.len() { return Err(InterpreterError::ArgumentCountMismatch { expected: meta.parameters.len(), actual: arguments.len(), - call_location, + location, }); } @@ -113,7 +114,7 @@ impl<'a> Interpreter<'a> { return Err(InterpreterError::ArgumentCountMismatch { expected: closure.parameters.len(), actual: arguments.len(), - call_location, + location: call_location, }); } diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index d7af4643192..6845c6ac5a9 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -116,7 +116,7 @@ impl Value { } Value::Closure(_lambda, _env, _typ) => { // TODO: How should a closure's environment be inlined? - let item = "returning closures from a comptime fn"; + let item = "Returning closures from a comptime fn"; return Err(InterpreterError::Unimplemented { item, location }); } Value::Tuple(fields) => { diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs index d8839b33ff4..d98b8a521a6 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs @@ -158,8 +158,8 @@ pub enum CompilationError { InterpreterError(InterpreterError), } -impl From for CustomDiagnostic { - fn from(value: CompilationError) -> Self { +impl<'a> From<&'a CompilationError> for CustomDiagnostic { + fn from(value: &'a CompilationError) -> Self { match value { CompilationError::ParseError(error) => error.into(), CompilationError::DefinitionError(error) => error.into(), diff --git a/compiler/noirc_frontend/src/hir/def_collector/errors.rs b/compiler/noirc_frontend/src/hir/def_collector/errors.rs index 59a3051ac70..edeb463e10d 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/errors.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/errors.rs @@ -77,7 +77,7 @@ pub struct MacroError { } impl DefCollectorErrorKind { - pub fn into_file_diagnostic(self, file: fm::FileId) -> FileDiagnostic { + pub fn into_file_diagnostic(&self, file: fm::FileId) -> FileDiagnostic { Diagnostic::from(self).in_file(file) } } @@ -99,8 +99,8 @@ impl fmt::Display for DuplicateType { } } -impl From for Diagnostic { - fn from(error: DefCollectorErrorKind) -> Diagnostic { +impl<'a> From<&'a DefCollectorErrorKind> for Diagnostic { + fn from(error: &'a DefCollectorErrorKind) -> Diagnostic { match error { DefCollectorErrorKind::Duplicate { typ, first_def, second_def } => { let primary_message = format!( @@ -133,18 +133,18 @@ impl From for Diagnostic { DefCollectorErrorKind::NonStructTypeInImpl { span } => Diagnostic::simple_error( "Non-struct type used in impl".into(), "Only struct types may have implementation methods".into(), - span, + *span, ), DefCollectorErrorKind::MutableReferenceInTraitImpl { span } => Diagnostic::simple_error( "Trait impls are not allowed on mutable reference types".into(), "Try using a struct type here instead".into(), - span, + *span, ), DefCollectorErrorKind::OverlappingImpl { span, typ } => { Diagnostic::simple_error( format!("Impl for type `{typ}` overlaps with existing impl"), "Overlapping impl".into(), - span, + *span, ) } DefCollectorErrorKind::OverlappingImplNote { span } => { @@ -154,13 +154,13 @@ impl From for Diagnostic { Diagnostic::simple_error( "Previous impl defined here".into(), "Previous impl defined here".into(), - span, + *span, ) } DefCollectorErrorKind::ForeignImpl { span, type_name } => Diagnostic::simple_error( "Cannot `impl` a type that was defined outside the current crate".into(), format!("{type_name} was defined outside the current crate"), - span, + *span, ), DefCollectorErrorKind::TraitNotFound { trait_path } => Diagnostic::simple_error( format!("Trait {trait_path} not found"), @@ -174,15 +174,15 @@ impl From for Diagnostic { origin, span, } => { - let plural = if expected_generic_count == 1 { "" } else { "s" }; + let plural = if *expected_generic_count == 1 { "" } else { "s" }; let primary_message = format!( "`{origin}` expects {expected_generic_count} generic{plural}, but {location} has {actual_generic_count}"); - Diagnostic::simple_error(primary_message, "".to_string(), span) + Diagnostic::simple_error(primary_message, "".to_string(), *span) } DefCollectorErrorKind::MethodNotInTrait { trait_name, impl_method } => { - let trait_name = trait_name.0.contents; + let trait_name = &trait_name.0.contents; let impl_method_span = impl_method.span(); - let impl_method_name = impl_method.0.contents; + let impl_method_name = &impl_method.0.contents; let primary_message = format!("Method with name `{impl_method_name}` is not part of trait `{trait_name}`, therefore it can't be implemented"); Diagnostic::simple_error(primary_message, "".to_owned(), impl_method_span) } @@ -191,15 +191,15 @@ impl From for Diagnostic { method_name, trait_impl_span, } => { - let trait_name = trait_name.0.contents; - let impl_method_name = method_name.0.contents; + let trait_name = &trait_name.0.contents; + let impl_method_name = &method_name.0.contents; let primary_message = format!( "Method `{impl_method_name}` from trait `{trait_name}` is not implemented" ); Diagnostic::simple_error( primary_message, format!("Please implement {impl_method_name} here"), - trait_impl_span, + *trait_impl_span, ) } DefCollectorErrorKind::NotATrait { not_a_trait_name } => { @@ -213,20 +213,20 @@ impl From for Diagnostic { DefCollectorErrorKind::ModuleAlreadyPartOfCrate { mod_name, span } => { let message = format!("Module '{mod_name}' is already part of the crate"); let secondary = String::new(); - Diagnostic::simple_error(message, secondary, span) + Diagnostic::simple_error(message, secondary, *span) } DefCollectorErrorKind::ModuleOriginallyDefined { mod_name, span } => { let message = format!("Note: {mod_name} was originally declared here"); let secondary = String::new(); - Diagnostic::simple_error(message, secondary, span) + Diagnostic::simple_error(message, secondary, *span) } DefCollectorErrorKind::TraitImplOrphaned { span } => Diagnostic::simple_error( "Orphaned trait implementation".into(), "Either the type or the trait must be from the same crate as the trait implementation".into(), - span, + *span, ), DefCollectorErrorKind::MacroError(macro_error) => { - Diagnostic::simple_error(macro_error.primary_message, macro_error.secondary_message.unwrap_or_default(), macro_error.span.unwrap_or_default()) + Diagnostic::simple_error(macro_error.primary_message.clone(), macro_error.secondary_message.clone().unwrap_or_default(), macro_error.span.unwrap_or_default()) }, } } diff --git a/compiler/noirc_frontend/src/hir/resolution/errors.rs b/compiler/noirc_frontend/src/hir/resolution/errors.rs index b6bcfe6972e..d542bd4802a 100644 --- a/compiler/noirc_frontend/src/hir/resolution/errors.rs +++ b/compiler/noirc_frontend/src/hir/resolution/errors.rs @@ -95,24 +95,24 @@ pub enum ResolverError { } impl ResolverError { - pub fn into_file_diagnostic(self, file: fm::FileId) -> FileDiagnostic { + pub fn into_file_diagnostic(&self, file: fm::FileId) -> FileDiagnostic { Diagnostic::from(self).in_file(file) } } -impl From for Diagnostic { +impl<'a> From<&'a ResolverError> for Diagnostic { /// Only user errors can be transformed into a Diagnostic /// ICEs will make the compiler panic, as they could affect the /// soundness of the generated program - fn from(error: ResolverError) -> Diagnostic { + fn from(error: &'a ResolverError) -> Diagnostic { match error { ResolverError::DuplicateDefinition { name, first_span, second_span } => { let mut diag = Diagnostic::simple_error( format!("duplicate definitions of {name} found"), "first definition found here".to_string(), - first_span, + *first_span, ); - diag.add_secondary("second definition found here".to_string(), second_span); + diag.add_secondary("second definition found here".to_string(), *second_span); diag } ResolverError::UnusedVariable { ident } => { @@ -127,18 +127,18 @@ impl From for Diagnostic { ResolverError::VariableNotDeclared { name, span } => Diagnostic::simple_error( format!("cannot find `{name}` in this scope "), "not found in this scope".to_string(), - span, + *span, ), ResolverError::PathIsNotIdent { span } => Diagnostic::simple_error( "cannot use path as an identifier".to_string(), String::new(), - span, + *span, ), ResolverError::PathResolutionError(error) => error.into(), ResolverError::Expected { span, expected, got } => Diagnostic::simple_error( format!("expected {expected} got {got}"), String::new(), - span, + *span, ), ResolverError::DuplicateField { field } => Diagnostic::simple_error( format!("duplicate field {field}"), @@ -152,7 +152,7 @@ impl From for Diagnostic { field.span(), ) } - ResolverError::MissingFields { span, mut missing_fields, struct_definition } => { + ResolverError::MissingFields { span, missing_fields, struct_definition } => { let plural = if missing_fields.len() != 1 { "s" } else { "" }; let remaining_fields_names = match &missing_fields[..] { [field1] => field1.clone(), @@ -163,7 +163,7 @@ impl From for Diagnostic { let len_plural = if len != 1 {"s"} else {""}; let truncated_fields = format!(" and {len} other field{len_plural}"); - missing_fields.truncate(3); + let missing_fields = &missing_fields[0..3]; format!("{}{truncated_fields}", missing_fields.join(", ")) } }; @@ -171,18 +171,18 @@ impl From for Diagnostic { Diagnostic::simple_error( format!("missing field{plural} {remaining_fields_names} in struct {struct_definition}"), String::new(), - span, + *span, ) } ResolverError::UnnecessaryMut { first_mut, second_mut } => { let mut error = Diagnostic::simple_error( "'mut' here is not necessary".to_owned(), "".to_owned(), - second_mut, + *second_mut, ); error.add_secondary( "Pattern was already made mutable from this 'mut'".to_owned(), - first_mut, + *first_mut, ); error } @@ -227,17 +227,17 @@ impl From for Diagnostic { "no expression specifying the value stored by the constant variable {name}" ), "expected expression to be stored for let statement".to_string(), - span, + *span, ), ResolverError::InvalidArrayLengthExpr { span } => Diagnostic::simple_error( "Expression invalid in an array-length context".into(), "Array-length expressions can only have simple integer operations and any variables used must be global constants".into(), - span, + *span, ), ResolverError::IntegerTooLarge { span } => Diagnostic::simple_error( "Integer too large to be evaluated to an array-length".into(), "Array-lengths may be a maximum size of usize::MAX, including intermediate calculations".into(), - span, + *span, ), ResolverError::NoSuchNumericTypeVariable { path } => Diagnostic::simple_error( format!("Cannot find a global or generic type parameter named `{path}`"), @@ -247,57 +247,57 @@ impl From for Diagnostic { ResolverError::CapturedMutableVariable { span } => Diagnostic::simple_error( "Closures cannot capture mutable variables".into(), "Mutable variable".into(), - span, + *span, ), ResolverError::TestFunctionHasParameters { span } => Diagnostic::simple_error( "Test functions cannot have any parameters".into(), "Try removing the parameters or moving the test into a wrapper function".into(), - span, + *span, ), ResolverError::NonStructUsedInConstructor { typ, span } => Diagnostic::simple_error( "Only struct types can be used in constructor expressions".into(), format!("{typ} has no fields to construct it with"), - span, + *span, ), ResolverError::NonStructWithGenerics { span } => Diagnostic::simple_error( "Only struct types can have generic arguments".into(), "Try removing the generic arguments".into(), - span, + *span, ), ResolverError::GenericsOnSelfType { span } => Diagnostic::simple_error( "Cannot apply generics to Self type".into(), "Use an explicit type name or apply the generics at the start of the impl instead".into(), - span, + *span, ), ResolverError::IncorrectGenericCount { span, item_name, actual, expected } => { - let expected_plural = if expected == 1 { "" } else { "s" }; - let actual_plural = if actual == 1 { "is" } else { "are" }; + let expected_plural = if *expected == 1 { "" } else { "s" }; + let actual_plural = if *actual == 1 { "is" } else { "are" }; Diagnostic::simple_error( format!("`{item_name}` has {expected} generic argument{expected_plural} but {actual} {actual_plural} given here"), "Incorrect number of generic arguments".into(), - span, + *span, ) } - ResolverError::ParserError(error) => (*error).into(), + ResolverError::ParserError(error) => error.as_ref().into(), ResolverError::MutableReferenceToImmutableVariable { variable, span } => { - Diagnostic::simple_error(format!("Cannot mutably reference the immutable variable {variable}"), format!("{variable} is immutable"), span) + Diagnostic::simple_error(format!("Cannot mutably reference the immutable variable {variable}"), format!("{variable} is immutable"), *span) }, ResolverError::MutableReferenceToArrayElement { span } => { - Diagnostic::simple_error("Mutable references to array elements are currently unsupported".into(), "Try storing the element in a fresh variable first".into(), span) + Diagnostic::simple_error("Mutable references to array elements are currently unsupported".into(), "Try storing the element in a fresh variable first".into(), *span) }, ResolverError::NumericConstantInFormatString { name, span } => Diagnostic::simple_error( format!("cannot find `{name}` in this scope "), "Numeric constants should be printed without formatting braces".to_string(), - span, + *span, ), ResolverError::InvalidClosureEnvironment { span, typ } => Diagnostic::simple_error( format!("{typ} is not a valid closure environment type"), - "Closure environment must be a tuple or unit type".to_string(), span), + "Closure environment must be a tuple or unit type".to_string(), *span), ResolverError::NestedSlices { span } => Diagnostic::simple_error( "Nested slices are not supported".into(), "Try to use a constant sized array instead".into(), - span, + *span, ), ResolverError::MisplacedRecursiveAttribute { ident } => { let name = &ident.0.contents; @@ -315,7 +315,7 @@ impl From for Diagnostic { Diagnostic::simple_error( "#[abi(tag)] attributes can only be used in contracts".to_string(), "misplaced #[abi(tag)] attribute".to_string(), - span, + *span, ) }, ResolverError::LowLevelFunctionOutsideOfStdlib { ident } => Diagnostic::simple_error( @@ -327,30 +327,30 @@ impl From for Diagnostic { Diagnostic::simple_error( "Dependency cycle found".into(), format!("'{item}' recursively depends on itself: {cycle}"), - span, + *span, ) }, ResolverError::JumpInConstrainedFn { is_break, span } => { - let item = if is_break { "break" } else { "continue" }; + let item = if *is_break { "break" } else { "continue" }; Diagnostic::simple_error( format!("{item} is only allowed in unconstrained functions"), "Constrained code must always have a known number of loop iterations".into(), - span, + *span, ) }, ResolverError::JumpOutsideLoop { is_break, span } => { - let item = if is_break { "break" } else { "continue" }; + let item = if *is_break { "break" } else { "continue" }; Diagnostic::simple_error( format!("{item} is only allowed within loops"), "".into(), - span, + *span, ) }, ResolverError::SelfReferentialStruct { span } => { Diagnostic::simple_error( "Self-referential structs are not supported".into(), "".into(), - span, + *span, ) }, ResolverError::InlineAttributeOnUnconstrained { ident } => { diff --git a/compiler/noirc_frontend/src/hir/resolution/import.rs b/compiler/noirc_frontend/src/hir/resolution/import.rs index 77e67567f8c..8850331f683 100644 --- a/compiler/noirc_frontend/src/hir/resolution/import.rs +++ b/compiler/noirc_frontend/src/hir/resolution/import.rs @@ -53,8 +53,8 @@ pub struct ResolvedImport { pub error: Option, } -impl From for CustomDiagnostic { - fn from(error: PathResolutionError) -> Self { +impl<'a> From<&'a PathResolutionError> for CustomDiagnostic { + fn from(error: &'a PathResolutionError) -> Self { match &error { PathResolutionError::Unresolved(ident) => { CustomDiagnostic::simple_error(error.to_string(), String::new(), ident.span()) diff --git a/compiler/noirc_frontend/src/hir/type_check/errors.rs b/compiler/noirc_frontend/src/hir/type_check/errors.rs index 9678bb8c9b9..b81727a27f5 100644 --- a/compiler/noirc_frontend/src/hir/type_check/errors.rs +++ b/compiler/noirc_frontend/src/hir/type_check/errors.rs @@ -147,36 +147,36 @@ impl TypeCheckError { } } -impl From for Diagnostic { - fn from(error: TypeCheckError) -> Diagnostic { +impl<'a> From<&'a TypeCheckError> for Diagnostic { + fn from(error: &'a TypeCheckError) -> Diagnostic { match error { TypeCheckError::TypeCannotBeUsed { typ, place, span } => Diagnostic::simple_error( format!("The type {} cannot be used in a {}", &typ, place), String::new(), - span, + *span, ), TypeCheckError::Context { err, ctx } => { - let mut diag = Diagnostic::from(*err); - diag.add_note(ctx.to_owned()); + let mut diag = Diagnostic::from(err.as_ref()); + diag.add_note(ctx.to_string()); diag } TypeCheckError::OpCannotBeUsed { op, place, span } => Diagnostic::simple_error( format!("The operator {op:?} cannot be used in a {place}"), String::new(), - span, + *span, ), TypeCheckError::TypeMismatch { expected_typ, expr_typ, expr_span } => { Diagnostic::simple_error( format!("Expected type {expected_typ}, found type {expr_typ}"), String::new(), - expr_span, + *expr_span, ) } TypeCheckError::TraitMethodParameterTypeMismatch { method_name, expected_typ, actual_typ, parameter_index, parameter_span } => { Diagnostic::simple_error( format!("Parameter #{parameter_index} of method `{method_name}` must be of type {expected_typ}, not {actual_typ}"), String::new(), - parameter_span, + *parameter_span, ) } TypeCheckError::NonHomogeneousArray { @@ -192,27 +192,27 @@ impl From for Diagnostic { "Non homogeneous array, different element types found at indices ({first_index},{second_index})" ), format!("Found type {first_type}"), - first_span, + *first_span, ); - diag.add_secondary(format!("but then found type {second_type}"), second_span); + diag.add_secondary(format!("but then found type {second_type}"), *second_span); diag } TypeCheckError::ArityMisMatch { expected, found, span } => { - let plural = if expected == 1 { "" } else { "s" }; + let plural = if *expected == 1 { "" } else { "s" }; let msg = format!("Expected {expected} argument{plural}, but found {found}"); - Diagnostic::simple_error(msg, String::new(), span) + Diagnostic::simple_error(msg, String::new(), *span) } TypeCheckError::ParameterCountMismatch { expected, found, span } => { - let empty_or_s = if expected == 1 { "" } else { "s" }; - let was_or_were = if found == 1 { "was" } else { "were" }; + let empty_or_s = if *expected == 1 { "" } else { "s" }; + let was_or_were = if *found == 1 { "was" } else { "were" }; let msg = format!("Function expects {expected} parameter{empty_or_s} but {found} {was_or_were} given"); - Diagnostic::simple_error(msg, String::new(), span) + Diagnostic::simple_error(msg, String::new(), *span) } TypeCheckError::GenericCountMismatch { item, expected, found, span } => { - let empty_or_s = if expected == 1 { "" } else { "s" }; - let was_or_were = if found == 1 { "was" } else { "were" }; + let empty_or_s = if *expected == 1 { "" } else { "s" }; + let was_or_were = if *found == 1 { "was" } else { "were" }; let msg = format!("{item} expects {expected} generic{empty_or_s} but {found} {was_or_were} given"); - Diagnostic::simple_error(msg, String::new(), span) + Diagnostic::simple_error(msg, String::new(), *span) } TypeCheckError::InvalidCast { span, .. } | TypeCheckError::ExpectedFunction { span, .. } @@ -238,17 +238,17 @@ impl From for Diagnostic { | TypeCheckError::NonConstantSliceLength { span } | TypeCheckError::StringIndexAssign { span } | TypeCheckError::InvalidShiftSize { span } => { - Diagnostic::simple_error(error.to_string(), String::new(), span) + Diagnostic::simple_error(error.to_string(), String::new(), *span) } TypeCheckError::PublicReturnType { typ, span } => Diagnostic::simple_error( "Functions cannot declare a public return type".to_string(), format!("return type is {typ}"), - span, + *span, ), TypeCheckError::TypeAnnotationsNeeded { span } => Diagnostic::simple_error( "Expression type is ambiguous".to_string(), "Type must be known at this point".to_string(), - span, + *span, ), TypeCheckError::ResolverError(error) => error.into(), TypeCheckError::TypeMismatchWithSource { expected, actual, span, source } => { @@ -274,30 +274,30 @@ impl From for Diagnostic { diagnostic.add_note(format!("help: try adding a return type: `-> {actual}`")); } - diagnostic.add_secondary(format!("{actual} returned here"), expr_span); + diagnostic.add_secondary(format!("{actual} returned here"), *expr_span); return diagnostic }, }; - Diagnostic::simple_error(message, String::new(), span) + Diagnostic::simple_error(message, String::new(), *span) } TypeCheckError::CallDeprecated { span, ref note, .. } => { let primary_message = error.to_string(); let secondary_message = note.clone().unwrap_or_default(); - Diagnostic::simple_warning(primary_message, secondary_message, span) + Diagnostic::simple_warning(primary_message, secondary_message, *span) } TypeCheckError::UnusedResultError { expr_type, expr_span } => { let msg = format!("Unused expression result of type {expr_type}"); - Diagnostic::simple_warning(msg, String::new(), expr_span) + Diagnostic::simple_warning(msg, String::new(), *expr_span) } TypeCheckError::NoMatchingImplFound { constraints, span } => { assert!(!constraints.is_empty()); let msg = format!("No matching impl found for `{}: {}`", constraints[0].0, constraints[0].1); let mut diagnostic = Diagnostic::from_message(&msg); - diagnostic.add_secondary(format!("No impl for `{}: {}`", constraints[0].0, constraints[0].1), span); + diagnostic.add_secondary(format!("No impl for `{}: {}`", constraints[0].0, constraints[0].1), *span); // These must be notes since secondaries are unordered for (typ, trait_name) in &constraints[1..] { @@ -308,11 +308,11 @@ impl From for Diagnostic { } TypeCheckError::UnneededTraitConstraint { trait_name, typ, span } => { let msg = format!("Constraint for `{typ}: {trait_name}` is not needed, another matching impl is already in scope"); - Diagnostic::simple_warning(msg, "Unnecessary trait constraint in where clause".into(), span) + Diagnostic::simple_warning(msg, "Unnecessary trait constraint in where clause".into(), *span) } TypeCheckError::InvalidTypeForEntryPoint { span } => Diagnostic::simple_error( "Only sized types may be used in the entry point to a program".to_string(), - "Slices, references, or any type containing them may not be used in main, contract functions, or foldable functions".to_string(), span), + "Slices, references, or any type containing them may not be used in main, contract functions, or foldable functions".to_string(), *span), TypeCheckError::MismatchTraitImplNumParameters { expected_num_parameters, actual_num_parameters, @@ -320,10 +320,10 @@ impl From for Diagnostic { method_name, span, } => { - let plural = if expected_num_parameters == 1 { "" } else { "s" }; + let plural = if *expected_num_parameters == 1 { "" } else { "s" }; let primary_message = format!( "`{trait_name}::{method_name}` expects {expected_num_parameters} parameter{plural}, but this method has {actual_num_parameters}"); - Diagnostic::simple_error(primary_message, "".to_string(), span) + Diagnostic::simple_error(primary_message, "".to_string(), *span) } } } diff --git a/compiler/noirc_frontend/src/lexer/errors.rs b/compiler/noirc_frontend/src/lexer/errors.rs index 35a07c11e0a..73c75af4cd7 100644 --- a/compiler/noirc_frontend/src/lexer/errors.rs +++ b/compiler/noirc_frontend/src/lexer/errors.rs @@ -96,8 +96,8 @@ impl LexerErrorKind { } } -impl From for Diagnostic { - fn from(error: LexerErrorKind) -> Diagnostic { +impl<'a> From<&'a LexerErrorKind> for Diagnostic { + fn from(error: &'a LexerErrorKind) -> Diagnostic { let (primary, secondary, span) = error.parts(); Diagnostic::simple_error(primary, secondary, span) } diff --git a/compiler/noirc_frontend/src/parser/errors.rs b/compiler/noirc_frontend/src/parser/errors.rs index 407b69c818b..af3d4caa145 100644 --- a/compiler/noirc_frontend/src/parser/errors.rs +++ b/compiler/noirc_frontend/src/parser/errors.rs @@ -112,7 +112,7 @@ impl std::fmt::Display for ParserError { let reason_str: String = if self.reason.is_none() { "".to_string() } else { - format!("\nreason: {}", Diagnostic::from(self.clone())) + format!("\nreason: {}", Diagnostic::from(self)) }; let mut expected = vecmap(&self.expected_tokens, ToString::to_string); expected.append(&mut vecmap(&self.expected_labels, |label| format!("{label}"))); @@ -138,9 +138,9 @@ impl std::fmt::Display for ParserError { } } -impl From for Diagnostic { - fn from(error: ParserError) -> Diagnostic { - match error.reason { +impl<'a> From<&'a ParserError> for Diagnostic { + fn from(error: &'a ParserError) -> Diagnostic { + match &error.reason { Some(reason) => { match reason { ParserErrorReason::ConstrainDeprecated => Diagnostic::simple_error( diff --git a/compiler/noirc_frontend/src/parser/parser.rs b/compiler/noirc_frontend/src/parser/parser.rs index 6e9f3969297..5cbab9bde3a 100644 --- a/compiler/noirc_frontend/src/parser/parser.rs +++ b/compiler/noirc_frontend/src/parser/parser.rs @@ -736,7 +736,7 @@ fn maybe_comp_time() -> impl NoirParser { keyword(Keyword::Comptime).or_not().validate(|opt, span, emit| { if opt.is_some() { emit(ParserError::with_reason( - ParserErrorReason::ExperimentalFeature("comptime"), + ParserErrorReason::ExperimentalFeature("Comptime values"), span, )); } diff --git a/compiler/noirc_frontend/src/parser/parser/test_helpers.rs b/compiler/noirc_frontend/src/parser/parser/test_helpers.rs index 6b8cb80a0a0..83c1e148f0e 100644 --- a/compiler/noirc_frontend/src/parser/parser/test_helpers.rs +++ b/compiler/noirc_frontend/src/parser/parser/test_helpers.rs @@ -15,9 +15,9 @@ where { let (tokens, lexer_errors) = Lexer::lex(program); if !lexer_errors.is_empty() { - return Err(vecmap(lexer_errors, Into::into)); + return Err(vecmap(&lexer_errors, Into::into)); } - parser.then_ignore(just(Token::EOF)).parse(tokens).map_err(|errors| vecmap(errors, Into::into)) + parser.then_ignore(just(Token::EOF)).parse(tokens).map_err(|errors| vecmap(&errors, Into::into)) } pub(crate) fn parse_recover(parser: P, program: &str) -> (Option, Vec) @@ -27,8 +27,8 @@ where let (tokens, lexer_errors) = Lexer::lex(program); let (opt, errs) = parser.then_ignore(force(just(Token::EOF))).parse_recovery(tokens); - let mut errors = vecmap(lexer_errors, Into::into); - errors.extend(errs.into_iter().map(Into::into)); + let mut errors = vecmap(&lexer_errors, Into::into); + errors.extend(errs.iter().map(Into::into)); (opt, errors) } diff --git a/tooling/nargo_cli/src/cli/fmt_cmd.rs b/tooling/nargo_cli/src/cli/fmt_cmd.rs index 2e0ca5632f1..8f66a0a328f 100644 --- a/tooling/nargo_cli/src/cli/fmt_cmd.rs +++ b/tooling/nargo_cli/src/cli/fmt_cmd.rs @@ -48,7 +48,7 @@ pub(crate) fn run(args: FormatCommand, config: NargoConfig) -> Result<(), CliErr let errors = errors .into_iter() .map(|error| { - let error: CustomDiagnostic = error.into(); + let error = CustomDiagnostic::from(&error); error.in_file(file_id) }) .collect();