Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 36 additions & 3 deletions compiler/noirc_frontend/src/ast/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use iter_extended::vecmap;
use noirc_errors::{Span, Spanned};

use super::{
BlockExpression, Expression, ExpressionKind, GenericTypeArgs, IndexExpression, ItemVisibility,
MemberAccessExpression, MethodCallExpression, UnresolvedType,
BlockExpression, ConstructorExpression, Expression, ExpressionKind, GenericTypeArgs,
IndexExpression, ItemVisibility, MemberAccessExpression, MethodCallExpression, UnresolvedType,
};
use crate::elaborator::types::SELF_TYPE_NAME;
use crate::lexer::token::SpannedToken;
use crate::macros_api::{SecondaryAttribute, UnresolvedTypeData};
use crate::macros_api::{NodeInterner, SecondaryAttribute, UnresolvedTypeData};
use crate::node_interner::{InternedExpressionKind, InternedPattern, InternedStatementKind};
use crate::parser::{ParserError, ParserErrorReason};
use crate::token::Token;
Expand Down Expand Up @@ -597,6 +597,39 @@ impl Pattern {
other => panic!("Pattern::into_ident called on {other} pattern with no identifier"),
}
}

pub(crate) fn try_as_expression(&self, interner: &NodeInterner) -> Option<Expression> {
match self {
Pattern::Identifier(ident) => Some(Expression {
kind: ExpressionKind::Variable(Path::from_ident(ident.clone())),
span: ident.span(),
}),
Pattern::Mutable(_, _, _) => None,
Pattern::Tuple(patterns, span) => {
let mut expressions = Vec::new();
for pattern in patterns {
expressions.push(pattern.try_as_expression(interner)?);
}
Some(Expression { kind: ExpressionKind::Tuple(expressions), span: *span })
}
Pattern::Struct(path, patterns, span) => {
let mut fields = Vec::new();
for (field, pattern) in patterns {
let expression = pattern.try_as_expression(interner)?;
fields.push((field.clone(), expression));
}
Some(Expression {
kind: ExpressionKind::Constructor(Box::new(ConstructorExpression {
type_name: path.clone(),
fields,
struct_type: None,
})),
span: *span,
})
}
Pattern::Interned(id, _) => interner.get_pattern(*id).try_as_expression(interner),
}
}
}

impl Recoverable for Pattern {
Expand Down
20 changes: 19 additions & 1 deletion compiler/noirc_frontend/src/hir/comptime/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,14 @@ pub enum InterpreterError {
duplicate_location: Location,
existing_location: Location,
},
CannotResolveExpression {
location: Location,
expression: String,
},
CannotSetFunctionBody {
location: Location,
expression: String,
},

// These cases are not errors, they are just used to prevent us from running more code
// until the loop can be resumed properly. These cases will never be displayed to users.
Expand Down Expand Up @@ -291,7 +299,9 @@ impl InterpreterError {
| InterpreterError::InvalidAttribute { location, .. }
| InterpreterError::GenericNameShouldBeAnIdent { location, .. }
| InterpreterError::DuplicateGeneric { duplicate_location: location, .. }
| InterpreterError::TypeAnnotationsNeededForMethodCall { location } => *location,
| InterpreterError::TypeAnnotationsNeededForMethodCall { location }
| InterpreterError::CannotResolveExpression { location, .. }
| InterpreterError::CannotSetFunctionBody { location, .. } => *location,

InterpreterError::FailedToParseMacro { error, file, .. } => {
Location::new(error.span(), *file)
Expand Down Expand Up @@ -626,6 +636,14 @@ impl<'a> From<&'a InterpreterError> for CustomDiagnostic {
);
error
}
InterpreterError::CannotResolveExpression { location, expression } => {
let msg = format!("Cannot resolve expression `{expression}`");
CustomDiagnostic::simple_error(msg, String::new(), location.span)
}
InterpreterError::CannotSetFunctionBody { location, expression } => {
let msg = format!("`{expression}` is not a valid function body");
CustomDiagnostic::simple_error(msg, String::new(), location.span)
}
}
}
}
59 changes: 34 additions & 25 deletions compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1807,30 +1807,32 @@ fn expr_resolve(
interpreter.current_function
};

let value =
interpreter.elaborate_in_function(function_to_resolve_in, |elaborator| match expr_value {
ExprValue::Expression(expression_kind) => {
let expr = Expression { kind: expression_kind, span: self_argument_location.span };
let (expr_id, _) = elaborator.elaborate_expression(expr);
Value::TypedExpr(TypedExpr::ExprId(expr_id))
}
ExprValue::Statement(statement_kind) => {
let statement =
Statement { kind: statement_kind, span: self_argument_location.span };
let (stmt_id, _) = elaborator.elaborate_statement(statement);
Value::TypedExpr(TypedExpr::StmtId(stmt_id))
}
ExprValue::LValue(lvalue) => {
let expr = lvalue.as_expression();
let (expr_id, _) = elaborator.elaborate_expression(expr);
Value::TypedExpr(TypedExpr::ExprId(expr_id))
}
ExprValue::Pattern(_) => {
todo!("Can't resolve a pattern");
interpreter.elaborate_in_function(function_to_resolve_in, |elaborator| match expr_value {
ExprValue::Expression(expression_kind) => {
let expr = Expression { kind: expression_kind, span: self_argument_location.span };
let (expr_id, _) = elaborator.elaborate_expression(expr);
Ok(Value::TypedExpr(TypedExpr::ExprId(expr_id)))
}
ExprValue::Statement(statement_kind) => {
let statement = Statement { kind: statement_kind, span: self_argument_location.span };
let (stmt_id, _) = elaborator.elaborate_statement(statement);
Ok(Value::TypedExpr(TypedExpr::StmtId(stmt_id)))
}
ExprValue::LValue(lvalue) => {
let expr = lvalue.as_expression();
let (expr_id, _) = elaborator.elaborate_expression(expr);
Ok(Value::TypedExpr(TypedExpr::ExprId(expr_id)))
}
ExprValue::Pattern(pattern) => {
if let Some(expression) = pattern.try_as_expression(elaborator.interner) {
Ok(Value::expression(expression.kind))
} else {
let expression = Value::pattern(pattern).display(elaborator.interner).to_string();
let location = self_argument_location;
Err(InterpreterError::CannotResolveExpression { location, expression })
}
});

Ok(value)
}
})
}

fn unwrap_expr_value(interner: &NodeInterner, mut expr_value: ExprValue) -> ExprValue {
Expand Down Expand Up @@ -2055,8 +2057,15 @@ fn function_def_set_body(
}),
ExprValue::Statement(statement_kind) => statement_kind,
ExprValue::LValue(lvalue) => StatementKind::Expression(lvalue.as_expression()),
ExprValue::Pattern(_) => {
todo!("Can't set function body to a pattern");
ExprValue::Pattern(pattern) => {
if let Some(expression) = pattern.try_as_expression(interpreter.elaborator.interner) {
StatementKind::Expression(expression)
} else {
let expression =
Value::pattern(pattern).display(interpreter.elaborator.interner).to_string();
let location = body_location;
return Err(InterpreterError::CannotSetFunctionBody { location, expression });
}
}
};

Expand Down