Commit c02a6f6
authored
fix: let trait calls work in globals (#5602)
# Description
## Problem
Resolves #5029
## Summary
Trait constraints weren't checked after elaborating a global.
## Additional Context
I couldn't just call `check_and_pop_function_context()` because that
also defaults type variables... and if we do that the usage of a global
will not have an effect on its type. I guess this is one scenario:
```rust
global x = 1; // What's the type of x?
```
We don't know. I guess it ends up being `Field` later on (where?) But if
we have this:
```rust
global x = 1;
fn main() {
let y: i32 = 0;
assert_eq(x, y);
}
```
now x's type will be `i32` because of that comparison. So if we eagerly
bind `1` to its default type, this will break.
All of this to say: in Rust a global's type (well, for a `const`) must
be explicitly specified. I guess that if we also did that in Noir then
this wouldn't be a problem. And given that globals are, well, global, it
might be strange to have `global x = 1` expecting that to be a Field,
used as a Field in many places, but you accidentally compare it to an
`i32` and the type changes under the hood. But that's a bigger
discussion, so maybe the fix in this PR is good for now?
## 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.1 parent fd7002c commit c02a6f6
3 files changed
Lines changed: 12 additions & 0 deletions
File tree
- compiler/noirc_frontend/src/elaborator
- test_programs/compile_success_empty/trait_call_in_global
- src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
248 | 248 | | |
249 | 249 | | |
250 | 250 | | |
| 251 | + | |
251 | 252 | | |
252 | 253 | | |
253 | 254 | | |
| |||
Lines changed: 6 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
Lines changed: 5 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
0 commit comments