Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: don't eagerly error on cast expressions #5635

Merged
merged 9 commits into from
Jul 31, 2024
19 changes: 12 additions & 7 deletions compiler/noirc_driver/src/abi_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use noirc_frontend::{
macros_api::{HirExpression, HirLiteral},
node_interner::{FuncId, NodeInterner},
};
use noirc_frontend::{TypeBinding, TypeVariableKind};
use noirc_frontend::{PolymorphicKind, TypeBinding, TypeVariableKind};

/// Arranges a function signature and a generated circuit's return witnesses into a
/// `noirc_abi::Abi`.
Expand Down Expand Up @@ -68,13 +68,18 @@ pub(super) fn abi_type_from_hir_type(context: &Context, typ: &Type) -> AbiType {

AbiType::Integer { sign, width: (*bit_width).into() }
}
Type::TypeVariable(binding, TypeVariableKind::IntegerOrField)
| Type::TypeVariable(binding, TypeVariableKind::Integer) => match &*binding.borrow() {
TypeBinding::Bound(typ) => abi_type_from_hir_type(context, typ),
TypeBinding::Unbound(_) => {
abi_type_from_hir_type(context, &Type::default_int_or_field_type())
Type::TypeVariable(
binding,
TypeVariableKind::Polymorphic(PolymorphicKind::IntegerOrField),
)
| Type::TypeVariable(binding, TypeVariableKind::Polymorphic(PolymorphicKind::Integer)) => {
match &*binding.borrow() {
TypeBinding::Bound(typ) => abi_type_from_hir_type(context, typ),
TypeBinding::Unbound(_) => {
abi_type_from_hir_type(context, &Type::default_int_or_field_type())
}
}
},
}
Type::Bool => AbiType::Boolean,
Type::String(size) => {
let size = size
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ impl<'context> Elaborator<'context> {
fn elaborate_cast(&mut self, cast: CastExpression, span: Span) -> (HirExpression, Type) {
let (lhs, lhs_type) = self.elaborate_expression(cast.lhs);
let r#type = self.resolve_type(cast.r#type);
let result = self.check_cast(lhs_type, &r#type, span);
let result = self.check_cast(&lhs_type, &r#type, span);
let expr = HirExpression::Cast(HirCastExpression { lhs, r#type });
(expr, result)
}
Expand Down
17 changes: 11 additions & 6 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -779,17 +779,22 @@ impl<'context> Elaborator<'context> {
}
}

pub(super) fn check_cast(&mut self, from: Type, to: &Type, span: Span) -> Type {
pub(super) fn check_cast(&mut self, from: &Type, to: &Type, span: Span) -> Type {
match from.follow_bindings() {
Type::Integer(..)
| Type::FieldElement
| Type::TypeVariable(_, TypeVariableKind::IntegerOrField)
| Type::TypeVariable(_, TypeVariableKind::Integer)
| Type::TypeVariable(_, TypeVariableKind::Polymorphic(..))
| Type::Bool => (),

Type::TypeVariable(_, _) => {
self.push_err(TypeCheckError::TypeAnnotationsNeeded { span });
return Type::Error;
Type::TypeVariable(binding, TypeVariableKind::Normal) => {
if let TypeBinding::Bound(typ) = &*binding.borrow() {
return self.check_cast(from, typ, span);
}
asterite marked this conversation as resolved.
Show resolved Hide resolved
let expected = &&Type::polymorphic_integer_or_field_or_bool(self.interner);
self.unify(from, expected, || TypeCheckError::InvalidCast {
from: from.clone(),
span,
});
}
Type::Error => return Type::Error,
from => {
Expand Down
14 changes: 11 additions & 3 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
undo_instantiation_bindings,
};
use crate::token::Tokens;
use crate::TypeVariable;
use crate::{
hir_def::{
expr::{
Expand All @@ -36,6 +35,7 @@
node_interner::{DefinitionId, DefinitionKind, ExprId, FuncId, StmtId},
Shared, Type, TypeBinding, TypeBindings, TypeVariableKind,
};
use crate::{PolymorphicKind, TypeVariable};

use super::errors::{IResult, InterpreterError};
use super::value::{unwrap_rc, Value};
Expand Down Expand Up @@ -208,7 +208,7 @@
}
} else {
let name = self.elaborator.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 211 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

Expand Down Expand Up @@ -661,9 +661,17 @@
Ok(Value::I64(value))
}
}
} else if let Type::TypeVariable(variable, TypeVariableKind::IntegerOrField) = &typ {
} else if let Type::TypeVariable(
variable,
TypeVariableKind::Polymorphic(PolymorphicKind::IntegerOrField),
) = &typ
{
Ok(Value::Field(value))
} else if let Type::TypeVariable(variable, TypeVariableKind::Integer) = &typ {
} else if let Type::TypeVariable(
variable,
TypeVariableKind::Polymorphic(PolymorphicKind::Integer),
) = &typ
{
let value: u64 = value
.try_to_u64()
.ok_or(InterpreterError::IntegerOutOfRangeForType { value, typ, location })?;
Expand Down Expand Up @@ -1238,8 +1246,8 @@
let expr = result.into_expression(self.elaborator.interner, location)?;
let expr = self
.elaborator
.elaborate_item_from_comptime(self.current_function, |elab| {

Check warning on line 1249 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)
elab.elaborate_expression(expr).0

Check warning on line 1250 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elab)
});
result = self.evaluate(expr)?;
}
Expand Down
Loading
Loading