Skip to content

Commit c02a6f6

Browse files
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

compiler/noirc_frontend/src/elaborator/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ impl<'context> Elaborator<'context> {
248248
let (comptime_items, runtime_items) = Self::filter_comptime_items(items);
249249
this.elaborate_items(comptime_items);
250250
this.elaborate_items(runtime_items);
251+
this.check_and_pop_function_context();
251252
this
252253
}
253254

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "trait_call_in_global"
3+
type = "bin"
4+
authors = [""]
5+
6+
[dependencies]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
global s: BoundedVec<Field, 2> = From::from([0]);
2+
3+
fn main() {
4+
let _ = s;
5+
}

0 commit comments

Comments
 (0)