Skip to content

Commit

Permalink
Merge 437a967 into 8d7f1f9
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm authored Jan 9, 2025
2 parents 8d7f1f9 + 437a967 commit 193d910
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
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 @@ use crate::{
},
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 @@ -363,8 +363,35 @@ impl Value {
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())));
interner.push_expr_location(placeholder, location.span, location.file);

let dummy_captures = match fmt_str_type {
Type::FmtString(_, item_types) => {
dbg!(item_types.clone());
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

0 comments on commit 193d910

Please sign in to comment.