Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# 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 <jake@aztecprotocol.com>
- Loading branch information