Skip to content

Commit

Permalink
Fixes local const shadowed by global const with same name. (FuelLabs#…
Browse files Browse the repository at this point in the history
…3402)

With PR FuelLabs#3233 `compile_const_decl` is now able to check for local
variables. To fix the issue described in FuelLabs#3237 we need to pass the
`FnCompiler` to `compile_constant_expression` so it can check the local
variables in use.

Closes FuelLabs#3237
  • Loading branch information
esdrubal authored Nov 22, 2022
1 parent dd4a521 commit de22d2b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sway-core/src/ir_generation/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ impl FnCompiler {
// globals like other const decls.
let ty::TyConstantDeclaration { name, value, .. } = ast_const_decl;
let const_expr_val =
compile_constant_expression(context, md_mgr, self.module, None, None, &value)?;
compile_constant_expression(context, md_mgr, self.module, None, Some(self), &value)?;
let local_name = self.lexical_map.insert(name.as_str().to_owned());
let return_type = convert_resolved_typeid(context, &value.return_type, &value.span)?;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = 'const_global_shadow'
source = 'member'
dependencies = ['core']

[[package]]
name = 'core'
source = 'path+from-root-37F288FECEE71F89'
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "const_global_shadow"
entry = "main.sw"

[dependencies]
core = { path = "../../../../../../../sway-lib-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"functions": [
{
"inputs": [],
"name": "main",
"output": {
"name": "",
"type": 0,
"typeArguments": null
}
}
],
"loggedTypes": [],
"types": [
{
"components": null,
"type": "u64",
"typeId": 0,
"typeParameters": null
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
script;

const GLOBAL_VAL: u64 = 1;

fn main() -> u64 {
const GLOBAL_VAL = 100;
const LOCAL_VAL = GLOBAL_VAL;
LOCAL_VAL
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 100 }
validate_abi = true

0 comments on commit de22d2b

Please sign in to comment.