You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 8e84d83
Browse filesBrowse the repository at this point in the historyBrowse files
Move function-local records in global scope (#375)
```cpp
template <typename T> int get(T t) { return t.x; }
int main() {
struct Local {
int x;
};
Local l{7};
assert(get(l) == 7);
return 0;
}
```
The `get` function template lives in the global scope while `Local`
(used to instantiate `get`) lives at function scope. In the AST
instantiations live in the global scope as well, so it gets translated
as:
```rs
// Wrong: the global scope has no Local_1. It only exists inside main
pub unsafe fn get_0(mut t: Local_1) -> i32 {
return (t.x as i32);
}
unsafe fn main_0() -> i32 {
pub struct Local_1 {
pub x: i32,
};
let mut l: Local_1 = Local_1 { x: 7 };
assert!(((unsafe { get_0(l,) }) == (7)));
return 0;
}
```
This PR fixes this by moving the definition of `Local_1` at a global
scope:
```rs
// Correct: now Local_1 is visible for the instantiation
pub unsafe fn get_0(mut t: Local_1) -> i32 {
return (t.x as i32);
}
unsafe fn main_0() -> i32 {
let mut l: Local_1 = Local_1 { x: 7 };
assert!(((unsafe { get_0(l,) }) == (7)));
return 0;
}
pub struct Local_1 {
pub x: i32,
};
```
0 commit comments