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

fix: Slice coercions #4640

Merged
merged 4 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 10 additions & 5 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1510,7 +1510,7 @@
Type::Tuple(fields)
}
Type::Forall(typevars, typ) => {
// Trying to substitute_helper a variable de, substitute_bound_typevarsfined within a nested Forall

Check warning on line 1513 in compiler/noirc_frontend/src/hir_def/types.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (typevarsfined)
// is usually impossible and indicative of an error in the type checker somewhere.
for var in typevars {
assert!(!type_bindings.contains_key(&var.id()));
Expand Down Expand Up @@ -1662,14 +1662,19 @@
let as_slice = HirExpression::Ident(HirIdent::non_trait_method(as_slice_id, location));
let func = interner.push_expr(as_slice);

let arguments = vec![expression];
// Copy the expression and give it a new ExprId. The old one
// will be mutated in place into a Call expression.
let argument = interner.expression(&expression);
let argument = interner.push_expr(argument);
interner.push_expr_type(argument, array_type.clone());
interner.push_expr_location(argument, location.span, location.file);

let arguments = vec![argument];
let call = HirExpression::Call(HirCallExpression { func, arguments, location });
let call = interner.push_expr(call);
interner.replace_expr(&expression, call);

interner.push_expr_location(call, location.span, location.file);
interner.push_expr_location(func, location.span, location.file);

interner.push_expr_type(call, target_type.clone());
interner.push_expr_type(expression, target_type.clone());

let func_type = Type::Function(vec![array_type], Box::new(target_type), Box::new(Type::Unit));
interner.push_expr_type(func, func_type);
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/slice_coercion/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "slice_coercion"
type = "bin"
authors = [""]
compiler_version = ">=0.25.0"

[dependencies]
2 changes: 2 additions & 0 deletions test_programs/execution_success/slice_coercion/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
first = 3
expected = 3
19 changes: 19 additions & 0 deletions test_programs/execution_success/slice_coercion/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
struct Hasher {
fields: [Field],
}

impl Hasher {
pub fn new() -> Self {
Self { fields: [] }
}

pub fn add(&mut self, field: Field) {
self.fields = self.fields.push_back(field);
}
}

fn main(expected: pub Field, first: Field) {
let mut hasher = Hasher::new();
hasher.add(first);
assert(hasher.fields[0] == expected);
}
Loading