Skip to content
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

Merged
merged 1 commit into from
Jul 8, 2024

Conversation

c410-f3r
Copy link
Contributor

@c410-f3r c410-f3r commented Jun 22, 2024

Adds support for things like ${concat($variable, 123)} or ${concat("hello", "_world")} .

cc #124225

@rustbot
Copy link
Collaborator

rustbot commented Jun 22, 2024

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 22, 2024
@c410-f3r c410-f3r force-pushed the concat-again branch 2 times, most recently from 1cf8398 to 1a52f42 Compare June 22, 2024 21:18
@petrochenkov
Copy link
Contributor

concat_idents! doesn't currently support literal, but the motivation here is pretty clear, you want to combine ident and 0 into ident0, but 0 is not an identifier lexically.

However, restricting literals to simple string literals "abc" (no raws, no suffixes) would be enough for that.
All possible identifier parts should fit into that.

@petrochenkov
Copy link
Contributor

petrochenkov commented Jun 24, 2024

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.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 24, 2024
@c410-f3r
Copy link
Contributor Author

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

* not lexically an identifier

* not NFC normalized (https://doc.rust-lang.org/reference/identifiers.html?highlight=nfc#normalization)

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.

@c410-f3r
Copy link
Contributor Author

Thanks for the review

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 29, 2024
@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 1, 2024
@c410-f3r
Copy link
Contributor Author

c410-f3r commented Jul 1, 2024

Nice!

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 1, 2024
macro_rules! starting_invalid_unicode {
($ident:ident) => {{
let ${concat("\u{999999}", $ident)}: () = ();
//~^ ERROR invalid unicode character escape
Copy link
Contributor

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?

Copy link
Contributor Author

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?

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

cc #126841 (comment)

Copy link
Contributor

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.

@petrochenkov petrochenkov added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 1, 2024
@c410-f3r
Copy link
Contributor Author

c410-f3r commented Jul 6, 2024

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 6, 2024
@petrochenkov
Copy link
Contributor

As usual, literals turned out more complex than it may seem.
@bors r+

@bors
Copy link
Contributor

bors commented Jul 7, 2024

📌 Commit c990e00 has been approved by petrochenkov

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 7, 2024
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Jul 8, 2024
[`macro_metavar_expr_concat`] Add support for literals

Adds support for things like `${concat($variable, 123)}` or `${concat("hello", "_world")}` .

cc rust-lang#124225
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 8, 2024
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
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 8, 2024
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
@bors bors merged commit 2c16d65 into rust-lang:master Jul 8, 2024
6 checks passed
@rustbot rustbot added this to the 1.81.0 milestone Jul 8, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Jul 8, 2024
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
flip1995 pushed a commit to flip1995/rust that referenced this pull request Jul 11, 2024
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
@tgross35 tgross35 added the F-macro_metavar_expr_concat `#![feature(macro_metavar_expr_concat)]` label Aug 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-macro_metavar_expr_concat `#![feature(macro_metavar_expr_concat)]` S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants