Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Jul 31, 2024
1 parent 2b262bb commit 797f41f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
42 changes: 26 additions & 16 deletions compiler/noirc_frontend/src/elaborator/comptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(());
};
Expand Down Expand Up @@ -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<Expression>)> {
let (tokens, lexing_errors) = Lexer::lex(annotation);
fn parse_attribute(
annotation: &str,
file: FileId,
) -> Result<Option<(Expression, Vec<Expression>)>, (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(
Expand All @@ -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());

Expand Down
8 changes: 4 additions & 4 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
4 changes: 2 additions & 2 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down
7 changes: 7 additions & 0 deletions compiler/noirc_frontend/src/lexer/errors.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::hir::def_collector::dc_crate::CompilationError;
use crate::parser::ParserError;
use crate::parser::ParserErrorReason;
use crate::token::SpannedToken;
Expand Down Expand Up @@ -42,6 +43,12 @@ impl From<LexerErrorKind> for ParserError {
}
}

impl From<LexerErrorKind> for CompilationError {
fn from(error: LexerErrorKind) -> Self {
ParserError::from(error).into()
}
}

impl LexerErrorKind {
pub fn span(&self) -> Span {
match self {
Expand Down

0 comments on commit 797f41f

Please sign in to comment.