Skip to content

Commit

Permalink
fix: Fix panic when returning a zeroed unit value (#4797)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #4791

## Summary\*

Zeroed used boolean values since we didn't have true unit values before.
These were meant to be filtered out by the type but as shown in the
issue, they'd still be used if they were directly returned.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
jfecher authored Apr 12, 2024
1 parent 1d96937 commit 2ea9292
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ impl<'a> FunctionContext<'a> {

Ok(Tree::Branch(vec![string, field_count.into(), fields]))
}
ast::Literal::Unit => Ok(Self::unit_value()),
}
}

Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/monomorphization/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub enum Literal {
Slice(ArrayLiteral),
Integer(FieldElement, Type, Location),
Bool(bool),
Unit,
Str(String),
FmtStr(String, u64, Box<Expression>),
}
Expand Down
4 changes: 1 addition & 3 deletions compiler/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1553,9 +1553,7 @@ impl<'interner> Monomorphizer<'interner> {
ast::Expression::Literal(ast::Literal::Integer(0_u128.into(), typ, location))
}
ast::Type::Bool => ast::Expression::Literal(ast::Literal::Bool(false)),
// There is no unit literal currently. Replace it with 'false' since it should be ignored
// anyway.
ast::Type::Unit => ast::Expression::Literal(ast::Literal::Bool(false)),
ast::Type::Unit => ast::Expression::Literal(ast::Literal::Unit),
ast::Type::Array(length, element_type) => {
let element = self.zeroed_value_of_type(element_type.as_ref(), location);
ast::Expression::Literal(ast::Literal::Array(ast::ArrayLiteral {
Expand Down
3 changes: 3 additions & 0 deletions compiler/noirc_frontend/src/monomorphization/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ impl AstPrinter {
s.fmt(f)?;
write!(f, "\"")
}
super::ast::Literal::Unit => {
write!(f, "()")
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/unit_value/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "short"
type = "bin"
authors = [""]
compiler_version = ">=0.23.0"

[dependencies]
7 changes: 7 additions & 0 deletions test_programs/execution_success/unit_value/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn get_transaction() {
dep::std::unsafe::zeroed()
}

fn main() {
get_transaction();
}

0 comments on commit 2ea9292

Please sign in to comment.