From e67809165c277423e25110c3f1f8eff6e8daa0e4 Mon Sep 17 00:00:00 2001 From: jfecher Date: Wed, 18 Sep 2024 09:04:01 -0500 Subject: [PATCH] fix: Fix comptime type formatting (#6079) # Description ## Problem\* Resolves https://github.com/noir-lang/noir/issues/6077 ## Summary\* The change made to `impl Debug for Type` to display kinds differently was breaking the use case of formatting types into a format string then using `.quoted_contents()` to quote the contents of the string. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- .../noirc_frontend/src/hir/comptime/value.rs | 2 +- .../regression_6077/Nargo.toml | 7 +++++ .../regression_6077/Prover.toml | 1 + .../regression_6077/src/main.nr | 28 +++++++++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 test_programs/compile_success_empty/regression_6077/Nargo.toml create mode 100644 test_programs/compile_success_empty/regression_6077/Prover.toml create mode 100644 test_programs/compile_success_empty/regression_6077/src/main.nr diff --git a/compiler/noirc_frontend/src/hir/comptime/value.rs b/compiler/noirc_frontend/src/hir/comptime/value.rs index f2cb714deb4..b027d069536 100644 --- a/compiler/noirc_frontend/src/hir/comptime/value.rs +++ b/compiler/noirc_frontend/src/hir/comptime/value.rs @@ -682,7 +682,7 @@ impl<'value, 'interner> Display for ValuePrinter<'value, 'interner> { } } Value::Zeroed(typ) => write!(f, "(zeroed {typ})"), - Value::Type(typ) => write!(f, "{:?}", typ), + Value::Type(typ) => write!(f, "{}", typ), Value::Expr(ExprValue::Expression(expr)) => { write!(f, "{}", remove_interned_in_expression_kind(self.interner, expr.clone())) } diff --git a/test_programs/compile_success_empty/regression_6077/Nargo.toml b/test_programs/compile_success_empty/regression_6077/Nargo.toml new file mode 100644 index 00000000000..0c1118245f4 --- /dev/null +++ b/test_programs/compile_success_empty/regression_6077/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "regression_6077" +type = "bin" +authors = [""] +compiler_version = ">=0.30.0" + +[dependencies] diff --git a/test_programs/compile_success_empty/regression_6077/Prover.toml b/test_programs/compile_success_empty/regression_6077/Prover.toml new file mode 100644 index 00000000000..ba3522cab01 --- /dev/null +++ b/test_programs/compile_success_empty/regression_6077/Prover.toml @@ -0,0 +1 @@ +a = 0 diff --git a/test_programs/compile_success_empty/regression_6077/src/main.nr b/test_programs/compile_success_empty/regression_6077/src/main.nr new file mode 100644 index 00000000000..fe067177e77 --- /dev/null +++ b/test_programs/compile_success_empty/regression_6077/src/main.nr @@ -0,0 +1,28 @@ +struct WeirdStruct { + a: T, + b: U, +} + +#[mangle_fn] +pub fn my_fn() -> [u8; 3] { + [0; 3] +} + +comptime fn mangle_fn(f: FunctionDefinition) { + let return_type = f.return_type(); + + // This relies on how types are displayed + let generics = f"Field,{return_type}".quoted_contents(); + let new_return_type = quote { WeirdStruct<$generics>}.as_type(); + + let new_body = quote { + { + WeirdStruct { a: 1, b: [0;3] } + } + }.as_expr().unwrap(); + + f.set_return_type(new_return_type); + f.set_body(new_body); +} + +fn main() {}