Commit a297ec6
feat: Add
# Description
## Problem\*
While `fmtstr.quoted_contents` is great for 99% of use cases, sometimes
the raw str (including double quotes) is needed. Consider this case (fn
signature generation in `aztec.nr`)
```noir
pub(crate) comptime fn compute_fn_selector(f: FunctionDefinition) -> Field {
let fn_name = f.name();
let args_signatures = f.parameters().map(
| (_, typ): (Quoted, Type) | {
signature_of_type(typ)
}
).join(quote {,});
let signature = f"{fn_name}({args_signatures})".quoted_contents();
// from_signature takes a str, quoted_contents is no good here!
let computation_quote = quote {
protocol_types::abis::function_selector::FunctionSelector::from_signature($signature).to_field()
};
unquote!(computation_quote)
}
```
We have a bunch of `Quoted` values obtained during comptime that we have
to string (ha) together. The result is then needed to compute a hash.
## Summary\*
~~This just adds a builtin that "resolves" the `fmtstr` and then turns
it into a `Token::Str`~~
It's possible to create a string from quoted values by doing:
```noir
let signature = f"{name}({args})";
let signature_as_str = unquote!(quote { $signature });
```
But this includes whitespaces around the tokens. To get rid of them,
ended up going with the approach suggested by @jfecher, which certainly
complicates user code, but avoids the "eating whitespaces" problem:
> Going forward I think we'll need to instead control the formatting of
Quoted values more. The compiler itself can never guess what exact
formatting users will want of them since spaces are already not present.
So I'm leaning towards a Quoted::tokens(self) -> [Quoted] method to
return a slice of each individual token in the Quoted value. Then users
could iterate over it and apply (or not apply) spacing in between each
token as they see fit.
Also, the returned values are `str<_>`, since the compiler has no idea
of the returned length after formatting. This means values can only be
used by unquoting them and not directly.
## Additional Context
My first intuition was to do
```noir
let signature = f"\"{quotedvalues}\""
```
But it doesn't work =/
## Documentation\*
Check one:
- [ ] No documentation needed.
- [x] 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.
---------
Co-authored-by: jfecher <[email protected]>Quoted::tokens (#5942)1 parent b84009c commit a297ec6
File tree
3 files changed
+27
-4
lines changed- compiler/noirc_frontend/src/hir/comptime/interpreter
- docs/docs/noir/standard_library/meta
- noir_stdlib/src/meta
3 files changed
+27
-4
lines changedLines changed: 12 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| 126 | + | |
126 | 127 | | |
127 | 128 | | |
128 | 129 | | |
| |||
535 | 536 | | |
536 | 537 | | |
537 | 538 | | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
538 | 550 | | |
539 | 551 | | |
540 | 552 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
53 | 59 | | |
54 | 60 | | |
55 | 61 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
11 | | - | |
| 11 | + | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
24 | 29 | | |
25 | 30 | | |
26 | 31 | | |
| |||
0 commit comments