From 797f41f4b24ddf0d3d6d1e64b1c1928a488c8794 Mon Sep 17 00:00:00 2001 From: Jake Fecher Date: Wed, 31 Jul 2024 15:09:39 -0500 Subject: [PATCH] Feedback --- .../noirc_frontend/src/elaborator/comptime.rs | 42 ++++++++++++------- compiler/noirc_frontend/src/elaborator/mod.rs | 8 ++-- .../src/hir/comptime/interpreter.rs | 4 +- compiler/noirc_frontend/src/lexer/errors.rs | 7 ++++ 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/compiler/noirc_frontend/src/elaborator/comptime.rs b/compiler/noirc_frontend/src/elaborator/comptime.rs index c5c39196c3c..1ee84f363e9 100644 --- a/compiler/noirc_frontend/src/elaborator/comptime.rs +++ b/compiler/noirc_frontend/src/elaborator/comptime.rs @@ -8,14 +8,23 @@ use noirc_errors::{Location, Span}; use crate::{ hir::{ comptime::{Interpreter, InterpreterError, Value}, - def_collector::{dc_crate::{CollectedItems, CompilationError, UnresolvedFunctions, UnresolvedStruct, UnresolvedTrait, UnresolvedTraitImpl}, dc_mod}, + def_collector::{ + dc_crate::{ + CollectedItems, CompilationError, UnresolvedFunctions, UnresolvedStruct, + UnresolvedTrait, UnresolvedTraitImpl, + }, + dc_mod, + }, resolution::errors::ResolverError, }, hir_def::expr::HirIdent, lexer::Lexer, - macros_api::{Expression, ExpressionKind, HirExpression, SecondaryAttribute, StructId, NodeInterner}, + macros_api::{ + Expression, ExpressionKind, HirExpression, NodeInterner, SecondaryAttribute, StructId, + }, node_interner::{DefinitionKind, DependencyId, FuncId, TraitId}, - parser::{self, TopLevelStatement}, Type, TypeBindings, + parser::{self, TopLevelStatement}, + Type, TypeBindings, }; use super::{Elaborator, FunctionContext, ResolverMeta}; @@ -105,7 +114,7 @@ impl<'context> Elaborator<'context> { generated_items: &mut CollectedItems, ) -> Result<(), (CompilationError, FileId)> { let location = Location::new(span, self.file); - let Some((function, arguments)) = Self::parse_attribute(attribute) else { + let Some((function, arguments)) = Self::parse_attribute(attribute, self.file)? else { // Do not issue an error if the attribute is unknown return Ok(()); }; @@ -153,19 +162,24 @@ impl<'context> Elaborator<'context> { /// Parses an attribute in the form of a function call (e.g. `#[foo(a b, c d)]`) into /// the function and quoted arguments called (e.g. `("foo", vec![(a b, location), (c d, location)])`) - fn parse_attribute(annotation: &str) -> Option<(Expression, Vec)> { - let (tokens, lexing_errors) = Lexer::lex(annotation); + fn parse_attribute( + annotation: &str, + file: FileId, + ) -> Result)>, (CompilationError, FileId)> { + let (tokens, mut lexing_errors) = Lexer::lex(annotation); if !lexing_errors.is_empty() { - return None; + return Err((lexing_errors.swap_remove(0).into(), file)); } - let expression = parser::expression().parse(tokens).ok()?; + let expression = parser::expression() + .parse(tokens) + .map_err(|mut errors| (errors.swap_remove(0).into(), file))?; - match expression.kind { + Ok(match expression.kind { ExpressionKind::Call(call) => Some((*call.func, call.arguments)), ExpressionKind::Variable(_) => Some((expression, Vec::new())), - _ => return None, - } + _ => None, + }) } fn handle_attribute_arguments( @@ -185,11 +199,7 @@ impl<'context> Elaborator<'context> { // to account for N extra arguments. let modifiers = interpreter.elaborator.interner.function_modifiers(&function); let is_varargs = modifiers.attributes.is_varargs(); - let varargs_type = if is_varargs { - parameters.pop() - } else { - None - }; + let varargs_type = if is_varargs { parameters.pop() } else { None }; let varargs_elem_type = varargs_type.as_ref().and_then(|t| t.slice_element_type()); diff --git a/compiler/noirc_frontend/src/elaborator/mod.rs b/compiler/noirc_frontend/src/elaborator/mod.rs index 7649d8d3a32..eadc4169882 100644 --- a/compiler/noirc_frontend/src/elaborator/mod.rs +++ b/compiler/noirc_frontend/src/elaborator/mod.rs @@ -8,12 +8,12 @@ use crate::{ hir::{ comptime::Value, def_collector::dc_crate::{ - filter_literal_globals, CompilationError, ImplMap, UnresolvedGlobal, - UnresolvedStruct, UnresolvedTypeAlias, - }, + filter_literal_globals, CompilationError, ImplMap, UnresolvedGlobal, UnresolvedStruct, + UnresolvedTypeAlias, + }, def_map::DefMaps, - scope::ScopeForest as GenericScopeForest, resolution::{errors::ResolverError, path_resolver::PathResolver}, + scope::ScopeForest as GenericScopeForest, type_check::TypeCheckError, }, hir_def::{ diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index ce05e2ef23e..8f3f1295cac 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -1247,8 +1247,8 @@ impl<'local, 'interner> Interpreter<'local, 'interner> { let mut result = self.call_function(function_id, arguments, bindings, location)?; if call.is_macro_call { let expr = result.into_expression(self.elaborator.interner, location)?; - let expr = self.elaborate_item(self.current_function, |elab| { - elab.elaborate_expression(expr).0 + let expr = self.elaborate_item(self.current_function, |elaborator| { + elaborator.elaborate_expression(expr).0 }); result = self.evaluate(expr)?; } diff --git a/compiler/noirc_frontend/src/lexer/errors.rs b/compiler/noirc_frontend/src/lexer/errors.rs index 387ced05258..be5180a777b 100644 --- a/compiler/noirc_frontend/src/lexer/errors.rs +++ b/compiler/noirc_frontend/src/lexer/errors.rs @@ -1,3 +1,4 @@ +use crate::hir::def_collector::dc_crate::CompilationError; use crate::parser::ParserError; use crate::parser::ParserErrorReason; use crate::token::SpannedToken; @@ -42,6 +43,12 @@ impl From for ParserError { } } +impl From for CompilationError { + fn from(error: LexerErrorKind) -> Self { + ParserError::from(error).into() + } +} + impl LexerErrorKind { pub fn span(&self) -> Span { match self {