-
Notifications
You must be signed in to change notification settings - Fork 213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix formatting of Quoted
values
#5899
Comments
We don't need to allow unquoting inside string literals - you can use format strings to format them which the example shows. The error is only because of the |
Quoted
values
Ah, great! I didn't know if the issue was only about converting Quoted values to string. It seems #5899 fixes this, then. |
# Description ## Problem Resolves #5899 For debugging purposes, when you `println` a quoted value, some token values aren't expanded, making it a bit harder to understand what's going on. ## Summary Now interned expressions are shown in `Quoted` values. This program: ```rust fn main() { comptime { let n = quote { [1, 2, 3] }.as_expr().unwrap(); let q = quote { $n }; println(q); } } ``` Used to print: ``` quote { (expr) } ``` Now it prints: ``` quote { [1, 2, 3] } ``` ## Additional Context None. ## 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.
@jfecher I'm not sure this is solved. For example this program: fn main() {
comptime
{
let name = quote { bar };
assert_param(name);
}
}
comptime fn assert_param(name: Quoted) {
assert(false, f"Param {name} failed assertion");
} Maybe I'd expect the output to be:
but right now this is the error:
That is, Mainly thinking about the original use case that triggered this: assert(false, "Param $a_quote of type $a_type_quote is not supported as an argument") As a side note, I'm also wondering if format strings wouldn't be useful to constructor identifiers. Something like this: let hello = quote { hello };
let world = quote { world };
let name = f"{hello}_{world}";
let identifier: Quoted = name.as_identifier().unwrap();
println(identifier); // Prints "quote { hello_world }"; That way we wouldn't need to "glue" tokens because format strings kind of do that now. I'm also thinking about Elixir here, I guess in their macros you can operate with strings and eventually call |
I like the thought of using format strings for name formatting as well, then re-converting them into Quoted values. As far as the |
# Description ## Problem Resolves #5899 Resolves #5914 ## Summary Two things here: 1. When interpolating quotes values inside a format string, we do that without producing the `quote {` and `}` parts, which is likely what a user would expect (similar to unquoting those values). 2. In order to create identifiers (or any piece of code in general) by joining severa quoted values you can use format strings together with the new `fmtstr::contents` method, which returns a `Quoted` value with the string contents (that is, without the leading and trailing double quotes). ## Additional Context I originally thought about a method like `fmtstr::as_identifier` that would try to parse the string contents as an identifier (maybe an `Ident`, or maybe a `Path`), returning `Option<Quoted>` and `None` in case it couldn't be parsed to that. But I think in general it could be more useful to just get the string contents as a `Quoted` value. After all, if it isn't an identifier you'll learn it later on once the value is unquoted or interpolated. ## 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.
Problem
Unquoting inside string literals doesn't work
Happy Case
This should work:
Right now it gives this error:
Workaround
None
Workaround Description
No response
Additional Context
No response
Project Impact
None
Blocker Context
No response
Would you like to submit a PR for this Issue?
None
Support Needs
No response
The text was updated successfully, but these errors were encountered: