diff --git a/compiler/noirc_frontend/src/elaborator/expressions.rs b/compiler/noirc_frontend/src/elaborator/expressions.rs index 31a518ca97f..c0a91cca4d7 100644 --- a/compiler/noirc_frontend/src/elaborator/expressions.rs +++ b/compiler/noirc_frontend/src/elaborator/expressions.rs @@ -8,7 +8,7 @@ use crate::{ ast::{ ArrayLiteral, BlockExpression, CallExpression, CastExpression, ConstructorExpression, Expression, ExpressionKind, Ident, IfExpression, IndexExpression, InfixExpression, - ItemVisibility, Lambda, Literal, MemberAccessExpression, MethodCallExpression, + ItemVisibility, Lambda, Literal, MemberAccessExpression, MethodCallExpression, Path, PrefixExpression, StatementKind, UnaryOp, UnresolvedTypeData, UnresolvedTypeExpression, }, hir::{ @@ -21,7 +21,7 @@ use crate::{ hir_def::{ expr::{ HirArrayLiteral, HirBinaryOp, HirBlockExpression, HirCallExpression, HirCastExpression, - HirConstructorExpression, HirExpression, HirIfExpression, HirIndexExpression, + HirConstructorExpression, HirExpression, HirIdent, HirIfExpression, HirIndexExpression, HirInfixExpression, HirLambda, HirLiteral, HirMemberAccess, HirMethodCallExpression, HirPrefixExpression, }, @@ -247,27 +247,35 @@ impl<'context> Elaborator<'context> { let scope_tree = self.scopes.current_scope_tree(); let variable = scope_tree.find(ident_name); - if let Some((old_value, _)) = variable { + + let hir_ident = if let Some((old_value, _)) = variable { old_value.num_times_used += 1; - let ident = HirExpression::Ident(old_value.ident.clone(), None); - let expr_id = self.interner.push_expr(ident); - self.interner.push_expr_location(expr_id, call_expr_span, self.file); - let ident = old_value.ident.clone(); - let typ = self.type_check_variable(ident, expr_id, None); - self.interner.push_expr_type(expr_id, typ.clone()); - capture_types.push(typ); - fmt_str_idents.push(expr_id); + old_value.ident.clone() + } else if let Ok(definition_id) = + self.lookup_global(Path::from_single(ident_name.to_string(), call_expr_span)) + { + HirIdent::non_trait_method(definition_id, Location::new(call_expr_span, self.file)) } else if ident_name.parse::().is_ok() { self.push_err(ResolverError::NumericConstantInFormatString { name: ident_name.to_owned(), span: call_expr_span, }); + continue; } else { self.push_err(ResolverError::VariableNotDeclared { name: ident_name.to_owned(), span: call_expr_span, }); - } + continue; + }; + + let hir_expr = HirExpression::Ident(hir_ident.clone(), None); + let expr_id = self.interner.push_expr(hir_expr); + self.interner.push_expr_location(expr_id, call_expr_span, self.file); + let typ = self.type_check_variable(hir_ident, expr_id, None); + self.interner.push_expr_type(expr_id, typ.clone()); + capture_types.push(typ); + fmt_str_idents.push(expr_id); } let len = Type::Constant(str.len().into(), Kind::u32()); diff --git a/test_programs/execution_success/fmtstr_with_global/Nargo.toml b/test_programs/execution_success/fmtstr_with_global/Nargo.toml new file mode 100644 index 00000000000..889683f7410 --- /dev/null +++ b/test_programs/execution_success/fmtstr_with_global/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "fmtstr_with_global" +type = "bin" +authors = [""] +compiler_version = ">=0.32.0" + +[dependencies] diff --git a/test_programs/execution_success/fmtstr_with_global/src/main.nr b/test_programs/execution_success/fmtstr_with_global/src/main.nr new file mode 100644 index 00000000000..8b9c9635015 --- /dev/null +++ b/test_programs/execution_success/fmtstr_with_global/src/main.nr @@ -0,0 +1,5 @@ +global FOO = 1; + +fn main() { + println(f"foo = {FOO}"); +}