-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
[macro_metavar_expr_concat
] Add support for literals
#126841
Conversation
rustbot has assigned @petrochenkov. Use |
1cf8398
to
1a52f42
Compare
However, restricting literals to simple string literals |
Also, is there any validation for the concatenation result, like in https://doc.rust-lang.org/stable/proc_macro/struct.Ident.html#method.new ? When concatenating arbitrary strings you can get something that is
Both lexer and proc macro interface prevent creation of such ill-formed identifiers. |
Yeah, this is bad :| The current code allows the creation of invalid identifiers so I need investigate how to perform such verification. |
Thanks for the review @rustbot review |
Nice! @rustbot review |
macro_rules! starting_invalid_unicode { | ||
($ident:ident) => {{ | ||
let ${concat("\u{999999}", $ident)}: () = (); | ||
//~^ ERROR invalid unicode character escape |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this error coming from?
I don't see any additional checking in transcribe.rs that would result in this.
Also, will concat!("\x41")
produce a
or an invalid identifier \x41
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this error coming from?
rust/compiler/rustc_parse/src/lexer/unescape_error_reporting.rs
Lines 36 to 41 in 8a8ad34
EscapeError::LoneSurrogateUnicodeEscape => { | |
dcx.emit_err(UnescapeError::InvalidUnicodeEscape { span: err_span, surrogate: true }) | |
} | |
EscapeError::OutOfRangeUnicodeEscape => { | |
dcx.emit_err(UnescapeError::InvalidUnicodeEscape { span: err_span, surrogate: false }) | |
} |
Looks like the parse eagerly halts when an invalid unicode literal is found. Changed to "\u{00BD}" (½).
Also, will concat!("\x41") produce a or an invalid identifier \x41?
At the current time it is not possible to accept literal parameters but I expect to see a ${concat(..)} is not generating a valid identifier
error like in https://github.com/rust-lang/rust/pull/126841/files#diff-c527d06b3a89d2cf1181cf0b3122091572d2992346b42217dc290a18d2e2366cR75
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, the current behavior looks acceptable to me - either we have a literal that doesn't have \
in it so its escaped and unescaped values are the same, or it has \
and then it's not a valid identifier and we produce an error.
@rustbot review |
As usual, literals turned out more complex than it may seem. |
[`macro_metavar_expr_concat`] Add support for literals Adds support for things like `${concat($variable, 123)}` or `${concat("hello", "_world")}` . cc rust-lang#124225
Rollup of 12 pull requests Successful merges: - rust-lang#113128 (Support tail calls in mir via `TerminatorKind::TailCall`) - rust-lang#126841 ([`macro_metavar_expr_concat`] Add support for literals) - rust-lang#126881 (Make `NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE` a deny-by-default lint in edition 2024) - rust-lang#126921 (Give VaList its own home) - rust-lang#127276 (rustdoc: Remove OpaqueTy) - rust-lang#127367 (Run alloc sync tests) - rust-lang#127431 (Use field ident spans directly instead of the full field span in diagnostics on local fields) - rust-lang#127437 (Uplift trait ref is knowable into `rustc_next_trait_solver`) - rust-lang#127439 (Uplift elaboration into `rustc_type_ir`) - rust-lang#127451 (Improve `run-make/output-type-permutations` code and improve `filename_not_in_denylist` API) - rust-lang#127452 (Fix intrinsic const parameter counting with `effects`) - rust-lang#127459 (rustdoc-json: add type/trait alias tests) r? `@ghost` `@rustbot` modify labels: rollup
Rollup of 10 pull requests Successful merges: - rust-lang#126841 ([`macro_metavar_expr_concat`] Add support for literals) - rust-lang#126881 (Make `NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE` a deny-by-default lint in edition 2024) - rust-lang#126921 (Give VaList its own home) - rust-lang#127367 (Run alloc sync tests) - rust-lang#127431 (Use field ident spans directly instead of the full field span in diagnostics on local fields) - rust-lang#127437 (Uplift trait ref is knowable into `rustc_next_trait_solver`) - rust-lang#127439 (Uplift elaboration into `rustc_type_ir`) - rust-lang#127451 (Improve `run-make/output-type-permutations` code and improve `filename_not_in_denylist` API) - rust-lang#127452 (Fix intrinsic const parameter counting with `effects`) - rust-lang#127459 (rustdoc-json: add type/trait alias tests) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#126841 - c410-f3r:concat-again, r=petrochenkov [`macro_metavar_expr_concat`] Add support for literals Adds support for things like `${concat($variable, 123)}` or `${concat("hello", "_world")}` . cc rust-lang#124225
Rollup of 10 pull requests Successful merges: - rust-lang#126841 ([`macro_metavar_expr_concat`] Add support for literals) - rust-lang#126881 (Make `NEVER_TYPE_FALLBACK_FLOWING_INTO_UNSAFE` a deny-by-default lint in edition 2024) - rust-lang#126921 (Give VaList its own home) - rust-lang#127367 (Run alloc sync tests) - rust-lang#127431 (Use field ident spans directly instead of the full field span in diagnostics on local fields) - rust-lang#127437 (Uplift trait ref is knowable into `rustc_next_trait_solver`) - rust-lang#127439 (Uplift elaboration into `rustc_type_ir`) - rust-lang#127451 (Improve `run-make/output-type-permutations` code and improve `filename_not_in_denylist` API) - rust-lang#127452 (Fix intrinsic const parameter counting with `effects`) - rust-lang#127459 (rustdoc-json: add type/trait alias tests) r? `@ghost` `@rustbot` modify labels: rollup
Adds support for things like
${concat($variable, 123)}
or${concat("hello", "_world")}
.cc #124225