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: Keep format string literal when converting comptime value to HIR expression #6994

Closed
wants to merge 2 commits into from
Closed
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
33 changes: 30 additions & 3 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
},
node_interner::{ExprId, FuncId, NodeInterner, StmtId, StructId, TraitId, TraitImplId},
parser::{Item, Parser},
token::{SpannedToken, Token, Tokens},
token::{FmtStrFragment, SpannedToken, Token, Tokens},
Kind, QuotedType, Shared, Type, TypeBindings,
};
use rustc_hash::FxHashMap as HashMap;
Expand Down Expand Up @@ -98,8 +98,8 @@
Value::Expr(ExprValue::Statement(statement))
}

pub(crate) fn lvalue(lvaue: LValue) -> Self {

Check warning on line 101 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
Value::Expr(ExprValue::LValue(lvaue))

Check warning on line 102 in compiler/noirc_frontend/src/hir/comptime/value.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lvaue)
}

pub(crate) fn pattern(pattern: Pattern) -> Self {
Expand Down Expand Up @@ -363,8 +363,35 @@
HirExpression::Literal(HirLiteral::Str(unwrap_rc(value)))
}
// Format strings are lowered as normal strings since they are already interpolated.
Value::FormatString(value, _) => {
HirExpression::Literal(HirLiteral::Str(unwrap_rc(value)))
// We need to mock a FmtStr literal that matches the type expected elsewhere in the program.
// If we do not do this, we may end up with mismatched types
Value::FormatString(value, fmt_str_type) => {
// We include a space here as otherwise the ABI decoder will expect a value that does not exist.
let placeholder =
interner.push_expr(HirExpression::Literal(HirLiteral::Str(" ".to_owned())));
vezenovm marked this conversation as resolved.
Show resolved Hide resolved
interner.push_expr_location(placeholder, location.span, location.file);

let dummy_captures = match fmt_str_type {
Type::FmtString(_, item_types) => {
dbg!(item_types.clone());
vezenovm marked this conversation as resolved.
Show resolved Hide resolved
let Type::Tuple(fields) = *item_types else {
return Err(InterpreterError::Unimplemented {
item: "Only Type::Tuple is supported for format strings".to_owned(),
location,
});
};
std::iter::repeat(placeholder).take(fields.len()).collect()
}
_ => unreachable!("Expected a Type::FmtString"),
};

let raw_str = unwrap_rc(value);
let str_len = raw_str.len();
HirExpression::Literal(HirLiteral::FmtStr(
vec![FmtStrFragment::String(raw_str)],
dummy_captures,
str_len as u32,
))
}
Value::Function(id, typ, bindings) => {
let id = interner.function_definition_id(id);
Expand Down
13 changes: 12 additions & 1 deletion test_programs/execution_success/debug_logs/src/main.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
global A: Field = 50;
global B: Field = 100;
global GLOBAL_FMT_STR: fmtstr<14, (Field, Field)> = f"i: {A}, j: {B}";
global GLOBAL_FMT_STR_BIG: fmtstr<22, (Field, Field, Field)> = f"i: {A}, j: {B}, k: {A}";

fn main(x: Field, y: pub Field) {
let string = "i: {i}, j: {j}";
println(string);

// TODO: fmtstr cannot be printed
println(GLOBAL_FMT_STR);
println(GLOBAL_FMT_STR_BIG);

let LOCAL_FMT_STR: fmtstr<14, (Field, Field)> = f"i: {A}, j: {B}";
println(LOCAL_FMT_STR);

// TODO: fmtstr cannot be printed inside another fmtstr
// let fmt_str: fmtstr<14, (Field, Field)> = f"i: {x}, j: {y}";
// let fmt_fmt_str = f"fmtstr: {fmt_str}, i: {x}";
// println(fmt_fmt_str);
Expand Down
Loading