From 6a662d3e4555fce94755d854ff4c9347d5488ada Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 12 Sep 2024 12:27:42 -0300 Subject: [PATCH 1/5] feat: add `fmtstr::quoted` --- .../src/hir/comptime/interpreter/builtin.rs | 13 +++++++++++++ docs/docs/noir/standard_library/fmtstr.md | 6 ++++++ noir_stdlib/src/meta/format_string.nr | 5 +++++ .../comptime_fmt_strings/src/main.nr | 11 +++++++++++ 4 files changed, 35 insertions(+) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 797b8f3828a..988a847fc3f 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -100,6 +100,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "expr_is_continue" => expr_is_continue(interner, arguments, location), "expr_resolve" => expr_resolve(self, arguments, location), "is_unconstrained" => Ok(Value::Bool(true)), + "fmtstr_quoted" => fmtstr_quoted(interner, arguments, location), "fmtstr_quoted_contents" => fmtstr_quoted_contents(interner, arguments, location), "fresh_type_variable" => fresh_type_variable(interner), "function_def_add_attribute" => function_def_add_attribute(self, arguments, location), @@ -1897,6 +1898,18 @@ fn unwrap_expr_value(interner: &NodeInterner, mut expr_value: ExprValue) -> Expr expr_value } +// fn quoted(self) -> Quoted +fn fmtstr_quoted( + interner: &NodeInterner, + arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult { + let self_argument = check_one_argument(arguments, location)?; + let (string, _) = get_format_string(interner, self_argument)?; + let token = Token::Str((*string).clone()); + Ok(Value::Quoted(Rc::new(vec![token]))) +} + // fn quoted_contents(self) -> Quoted fn fmtstr_quoted_contents( interner: &NodeInterner, diff --git a/docs/docs/noir/standard_library/fmtstr.md b/docs/docs/noir/standard_library/fmtstr.md index 293793e23ff..a4eb5829931 100644 --- a/docs/docs/noir/standard_library/fmtstr.md +++ b/docs/docs/noir/standard_library/fmtstr.md @@ -6,6 +6,12 @@ title: fmtstr ## Methods +### quoted + +#include_code quoted noir_stdlib/src/meta/format_string.nr rust + +Returns the string literal as a `Quoted` value (this includes the leading and trailing double quoted). + ### quoted_contents #include_code quoted_contents noir_stdlib/src/meta/format_string.nr rust diff --git a/noir_stdlib/src/meta/format_string.nr b/noir_stdlib/src/meta/format_string.nr index 075a69fdafb..14089d97a80 100644 --- a/noir_stdlib/src/meta/format_string.nr +++ b/noir_stdlib/src/meta/format_string.nr @@ -1,4 +1,9 @@ impl fmtstr { + #[builtin(fmtstr_quoted)] + // docs:start:quoted + comptime fn quoted(self) -> Quoted {} + // docs:end:quoted + #[builtin(fmtstr_quoted_contents)] // docs:start:quoted_contents comptime fn quoted_contents(self) -> Quoted {} diff --git a/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr b/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr index ca337c822d8..dfda104be29 100644 --- a/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr +++ b/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr @@ -15,6 +15,14 @@ fn main() { // Mainly test fmtstr::quoted_contents call!(glue(quote { hello }, quote { world })); + + comptime + { + let x = "world"; + let s = f"Hello {x}".quoted(); + let code = quote { check_hello_world($s) }; + std::meta::unquote!(code); + } } comptime fn glue(x: Quoted, y: Quoted) -> Quoted { @@ -26,4 +34,7 @@ fn hello_world() {} comptime fn call(x: Quoted) -> Quoted { quote { $x() } } +fn check_hello_world(s: str) { + assert_eq(s.as_bytes().len(), "Hello world".as_bytes().len()); +} From c8f293a03e9d070530087544c03f30bb2d949748 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 12 Sep 2024 12:33:18 -0300 Subject: [PATCH 2/5] Correctly print string tokens --- compiler/noirc_frontend/src/lexer/token.rs | 6 +++--- .../compile_success_empty/comptime_fmt_strings/src/main.nr | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/noirc_frontend/src/lexer/token.rs b/compiler/noirc_frontend/src/lexer/token.rs index f5f7f0458d7..3b615e91f8f 100644 --- a/compiler/noirc_frontend/src/lexer/token.rs +++ b/compiler/noirc_frontend/src/lexer/token.rs @@ -362,11 +362,11 @@ impl fmt::Display for Token { Token::Ident(ref s) => write!(f, "{s}"), Token::Int(n) => write!(f, "{}", n.to_u128()), Token::Bool(b) => write!(f, "{b}"), - Token::Str(ref b) => write!(f, "{b}"), - Token::FmtStr(ref b) => write!(f, "f{b}"), + Token::Str(ref b) => write!(f, "{b:?}"), + Token::FmtStr(ref b) => write!(f, "f{b:?}"), Token::RawStr(ref b, hashes) => { let h: String = std::iter::once('#').cycle().take(hashes as usize).collect(); - write!(f, "r{h}\"{b}\"{h}") + write!(f, "r{h}{b:?}{h}") } Token::Keyword(k) => write!(f, "{k}"), Token::Attribute(ref a) => write!(f, "{a}"), diff --git a/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr b/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr index dfda104be29..f98d21cbe7a 100644 --- a/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr +++ b/test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr @@ -18,7 +18,7 @@ fn main() { comptime { - let x = "world"; + let x = "world\n\t\""; let s = f"Hello {x}".quoted(); let code = quote { check_hello_world($s) }; std::meta::unquote!(code); @@ -35,6 +35,6 @@ comptime fn call(x: Quoted) -> Quoted { quote { $x() } } fn check_hello_world(s: str) { - assert_eq(s.as_bytes().len(), "Hello world".as_bytes().len()); + assert_eq(s.as_bytes().len(), "Hello world\n\t\"".as_bytes().len()); } From bc17a138ca7bbd85b2642c433496146e2b368b23 Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 12 Sep 2024 12:38:44 -0300 Subject: [PATCH 3/5] Update docs/docs/noir/standard_library/fmtstr.md --- docs/docs/noir/standard_library/fmtstr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/noir/standard_library/fmtstr.md b/docs/docs/noir/standard_library/fmtstr.md index a4eb5829931..dc08eb595fe 100644 --- a/docs/docs/noir/standard_library/fmtstr.md +++ b/docs/docs/noir/standard_library/fmtstr.md @@ -10,7 +10,7 @@ title: fmtstr #include_code quoted noir_stdlib/src/meta/format_string.nr rust -Returns the string literal as a `Quoted` value (this includes the leading and trailing double quoted). +Returns the string literal as a `Quoted` value (this includes the leading and trailing double quotes). ### quoted_contents From 0994b468757fe33ceae6dc1328b60999e2fb05fb Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 12 Sep 2024 13:28:52 -0300 Subject: [PATCH 4/5] Implement `fmtstr::quoted` in Noir --- .../src/hir/comptime/interpreter/builtin.rs | 13 ------------- noir_stdlib/src/meta/format_string.nr | 5 +++-- 2 files changed, 3 insertions(+), 15 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 988a847fc3f..797b8f3828a 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -100,7 +100,6 @@ impl<'local, 'context> Interpreter<'local, 'context> { "expr_is_continue" => expr_is_continue(interner, arguments, location), "expr_resolve" => expr_resolve(self, arguments, location), "is_unconstrained" => Ok(Value::Bool(true)), - "fmtstr_quoted" => fmtstr_quoted(interner, arguments, location), "fmtstr_quoted_contents" => fmtstr_quoted_contents(interner, arguments, location), "fresh_type_variable" => fresh_type_variable(interner), "function_def_add_attribute" => function_def_add_attribute(self, arguments, location), @@ -1898,18 +1897,6 @@ fn unwrap_expr_value(interner: &NodeInterner, mut expr_value: ExprValue) -> Expr expr_value } -// fn quoted(self) -> Quoted -fn fmtstr_quoted( - interner: &NodeInterner, - arguments: Vec<(Value, Location)>, - location: Location, -) -> IResult { - let self_argument = check_one_argument(arguments, location)?; - let (string, _) = get_format_string(interner, self_argument)?; - let token = Token::Str((*string).clone()); - Ok(Value::Quoted(Rc::new(vec![token]))) -} - // fn quoted_contents(self) -> Quoted fn fmtstr_quoted_contents( interner: &NodeInterner, diff --git a/noir_stdlib/src/meta/format_string.nr b/noir_stdlib/src/meta/format_string.nr index 14089d97a80..912a8a7cd46 100644 --- a/noir_stdlib/src/meta/format_string.nr +++ b/noir_stdlib/src/meta/format_string.nr @@ -1,7 +1,8 @@ impl fmtstr { - #[builtin(fmtstr_quoted)] // docs:start:quoted - comptime fn quoted(self) -> Quoted {} + comptime fn quoted(self) -> Quoted { + crate::meta::unquote!(quote { $self }) + } // docs:end:quoted #[builtin(fmtstr_quoted_contents)] From 380b3bd62a9c00154f93dc2d1797997d9776bc8c Mon Sep 17 00:00:00 2001 From: Ary Borenszweig Date: Thu, 12 Sep 2024 13:30:49 -0300 Subject: [PATCH 5/5] Revert "Implement `fmtstr::quoted` in Noir" This reverts commit 0994b468757fe33ceae6dc1328b60999e2fb05fb. --- .../src/hir/comptime/interpreter/builtin.rs | 13 +++++++++++++ noir_stdlib/src/meta/format_string.nr | 5 ++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs index 797b8f3828a..988a847fc3f 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter/builtin.rs @@ -100,6 +100,7 @@ impl<'local, 'context> Interpreter<'local, 'context> { "expr_is_continue" => expr_is_continue(interner, arguments, location), "expr_resolve" => expr_resolve(self, arguments, location), "is_unconstrained" => Ok(Value::Bool(true)), + "fmtstr_quoted" => fmtstr_quoted(interner, arguments, location), "fmtstr_quoted_contents" => fmtstr_quoted_contents(interner, arguments, location), "fresh_type_variable" => fresh_type_variable(interner), "function_def_add_attribute" => function_def_add_attribute(self, arguments, location), @@ -1897,6 +1898,18 @@ fn unwrap_expr_value(interner: &NodeInterner, mut expr_value: ExprValue) -> Expr expr_value } +// fn quoted(self) -> Quoted +fn fmtstr_quoted( + interner: &NodeInterner, + arguments: Vec<(Value, Location)>, + location: Location, +) -> IResult { + let self_argument = check_one_argument(arguments, location)?; + let (string, _) = get_format_string(interner, self_argument)?; + let token = Token::Str((*string).clone()); + Ok(Value::Quoted(Rc::new(vec![token]))) +} + // fn quoted_contents(self) -> Quoted fn fmtstr_quoted_contents( interner: &NodeInterner, diff --git a/noir_stdlib/src/meta/format_string.nr b/noir_stdlib/src/meta/format_string.nr index 912a8a7cd46..14089d97a80 100644 --- a/noir_stdlib/src/meta/format_string.nr +++ b/noir_stdlib/src/meta/format_string.nr @@ -1,8 +1,7 @@ impl fmtstr { + #[builtin(fmtstr_quoted)] // docs:start:quoted - comptime fn quoted(self) -> Quoted { - crate::meta::unquote!(quote { $self }) - } + comptime fn quoted(self) -> Quoted {} // docs:end:quoted #[builtin(fmtstr_quoted_contents)]