Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow globals in format strings #6382

Merged
merged 2 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions compiler/noirc_frontend/src/elaborator/expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -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,
},
Expand Down Expand Up @@ -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::<usize>().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());
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/fmtstr_with_global/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "fmtstr_with_global"
type = "bin"
authors = [""]
compiler_version = ">=0.32.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
global FOO = 1;

fn main() {
println(f"foo = {FOO}");
}
Loading