Skip to content

Commit

Permalink
fix: use UnresolvedType::span for a nicer error
Browse files Browse the repository at this point in the history
  • Loading branch information
alexvitkov committed Aug 23, 2023
1 parent 90a50e3 commit 33e2701
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,6 @@ pub struct Resolver<'a> {
/// that are captured. We do this in order to create the hidden environment
/// parameter for the lambda function.
lambda_stack: Vec<LambdaContext>,

/// UnresolvedTypes currently don't have Spans, so if we hit an error while resolving a type
/// the best we can do is show the expression where the type is used
current_expr_span: Option<Span>,
}

/// ResolverMetas are tagged onto each definition to track how many times they are used
Expand Down Expand Up @@ -124,7 +120,6 @@ impl<'a> Resolver<'a> {
errors: Vec::new(),
lambda_stack: Vec::new(),
file,
current_expr_span: None,
}
}

Expand Down Expand Up @@ -372,6 +367,12 @@ impl<'a> Resolver<'a> {
Function(args, ret, env) => {
let args = vecmap(args, |arg| self.resolve_type_inner(arg, new_variables));
let ret = Box::new(self.resolve_type_inner(*ret, new_variables));

// unwrap() here is valid, because the only places we don't have a span are omitted types
// e.g. a function without return type implicitly has a spanless UnresolvedType::Unit return type
// To get an invalid env type, the user must explicitly specify the type, which will have a span
let env_span = env.span.unwrap();

let env = Box::new(self.resolve_type_inner(*env, new_variables));

match *env {
Expand All @@ -381,7 +382,7 @@ impl<'a> Resolver<'a> {
_ => {
self.push_err(ResolverError::InvalidClosureEnvironment {
typ: *env,
span: self.current_expr_span.unwrap(),
span: env_span,
});
Type::Error
}
Expand Down Expand Up @@ -677,7 +678,6 @@ impl<'a> Resolver<'a> {
}
});

self.current_expr_span = Some(func.span());
let mut parameters = vec![];
let mut parameter_types = vec![];

Expand Down Expand Up @@ -995,8 +995,6 @@ impl<'a> Resolver<'a> {
}

pub fn resolve_expression(&mut self, expr: Expression) -> ExprId {
self.current_expr_span = Some(expr.span);

let hir_expr = match expr.kind {
ExpressionKind::Literal(literal) => HirExpression::Literal(match literal {
Literal::Bool(b) => HirLiteral::Bool(b),
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ fn self_parameter() -> impl NoirParser<(Pattern, UnresolvedType, Visibility)> {
match pattern_keyword {
Some((Token::Ampersand, _)) => {
self_type =
UnresolvedTypeData::MutableReference(Box::new(self_type)).with_span(span)
UnresolvedTypeData::MutableReference(Box::new(self_type)).with_span(span);
}
Some((Token::Keyword(_), span)) => {
pattern = Pattern::Mutable(Box::new(pattern), span);
Expand Down

0 comments on commit 33e2701

Please sign in to comment.