From bbfbd127d7479fdc51246f6519ebeabab6d29b9c Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 28 Oct 2024 16:51:48 -0300 Subject: [PATCH 1/2] fix: allow globals in format strings --- .../src/elaborator/expressions.rs | 32 ++++++++++++------- .../fmtstr_with_global/Nargo.toml | 7 ++++ .../fmtstr_with_global/src/main.nr | 5 +++ 3 files changed, 32 insertions(+), 12 deletions(-) create mode 100644 test_programs/compile_success_empty/fmtstr_with_global/Nargo.toml create mode 100644 test_programs/compile_success_empty/fmtstr_with_global/src/main.nr 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/compile_success_empty/fmtstr_with_global/Nargo.toml b/test_programs/compile_success_empty/fmtstr_with_global/Nargo.toml new file mode 100644 index 00000000000..889683f7410 --- /dev/null +++ b/test_programs/compile_success_empty/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/compile_success_empty/fmtstr_with_global/src/main.nr b/test_programs/compile_success_empty/fmtstr_with_global/src/main.nr new file mode 100644 index 00000000000..8b9c9635015 --- /dev/null +++ b/test_programs/compile_success_empty/fmtstr_with_global/src/main.nr @@ -0,0 +1,5 @@ +global FOO = 1; + +fn main() { + println(f"foo = {FOO}"); +} From 7fd0212fe655fc0013c0a33a2079cd04d9392813 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Mon, 28 Oct 2024 17:05:37 -0300 Subject: [PATCH 2/2] Move test to somewhere else --- .../fmtstr_with_global/Nargo.toml | 0 .../fmtstr_with_global/src/main.nr | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename test_programs/{compile_success_empty => execution_success}/fmtstr_with_global/Nargo.toml (100%) rename test_programs/{compile_success_empty => execution_success}/fmtstr_with_global/src/main.nr (100%) diff --git a/test_programs/compile_success_empty/fmtstr_with_global/Nargo.toml b/test_programs/execution_success/fmtstr_with_global/Nargo.toml similarity index 100% rename from test_programs/compile_success_empty/fmtstr_with_global/Nargo.toml rename to test_programs/execution_success/fmtstr_with_global/Nargo.toml diff --git a/test_programs/compile_success_empty/fmtstr_with_global/src/main.nr b/test_programs/execution_success/fmtstr_with_global/src/main.nr similarity index 100% rename from test_programs/compile_success_empty/fmtstr_with_global/src/main.nr rename to test_programs/execution_success/fmtstr_with_global/src/main.nr