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

Treat k#ident keywords as valid tokens #119351

Closed
wants to merge 3 commits into from

Conversation

fee1-dead
Copy link
Member

We treat k#ident as a valid token, since it could be useful in implementing experimental features without introducing too much frontend changes (for example, with deref patterns). However, this PR as is would also make the following code compile:

macro_rules! a {
    (k#test) => {}
}

fn main() {
    a!(k#test);
}

We'd probably want to prevent that from happening, so should we feature gate this specific token?

@rustbot
Copy link
Collaborator

rustbot commented Dec 27, 2023

r? @WaffleLapkin

(rustbot has picked a reviewer for you, use r? to override)

@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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Dec 27, 2023
@rustbot
Copy link
Collaborator

rustbot commented Dec 27, 2023

rust-analyzer is developed in its own repository. If possible, consider making this change to rust-lang/rust-analyzer instead.

cc @rust-lang/rust-analyzer

@rust-log-analyzer

This comment has been minimized.

@@ -299,6 +299,10 @@ pub enum TokenKind {
/// It's recommended to use `Token::(ident,uninterpolate,uninterpolated_span)` to
/// treat regular and interpolated identifiers in the same way.
Ident(Symbol, /* is_raw */ bool),

/// A `k#ident` keyword
Keyword(Symbol),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actual keywords are Ident, so it seems confusing for a "forced keyword" to be special here.

Copy link
Member

@WaffleLapkin WaffleLapkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'd probably want to prevent that from happening, so should we feature gate this specific token?

Yes, I think we should feature gate k# and stabilize it separately and only if some stable feature would make use of it.

Comment on lines 303 to 304
/// A `k#ident` keyword
Keyword(Symbol),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be merged with Ident, which, instead of /* is_raw */ bool would need

enum IdentKind {
    /// The usual identifiers (or, depending on the context, keywords): `v`, `union`, `await`, `loop`.
    Default,
    /// Raw identifiers: `r#just_an_ident`, `r#loop`.
    Raw,
    /// Forced keywords: `k#break`, `k#await`, `k#some_new_experimental_keyword`.
    Keyword,
}

@@ -185,6 +185,18 @@ impl<'a> StringReader<'a> {
self.sess.raw_identifier_spans.push(span);
token::Ident(sym, true)
}
// Treat `k#ident` as a normal identifier token before 2021.
rustc_lexer::TokenKind::KeywordIdent if !self.mk_sp(start, self.pos).edition().at_least_rust_2021() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not is a bit hard to see here, would something like this work?

Suggested change
rustc_lexer::TokenKind::KeywordIdent if !self.mk_sp(start, self.pos).edition().at_least_rust_2021() => {
rustc_lexer::TokenKind::KeywordIdent if self.mk_sp(start, self.pos).edition() < Edition2021 => {

@rustbot rustbot 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 Dec 27, 2023
@fee1-dead
Copy link
Member Author

just converted token::Ident to use IdentKind - feature gating and tests to follow.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Dec 28, 2023

☔ The latest upstream changes (presumably #119384) made this pull request unmergeable. Please resolve the merge conflicts.

@Veykril
Copy link
Member

Veykril commented Dec 29, 2023

The CI failure is due to #118861

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-16 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
Built container sha256:9c3c93a371e5aed5c18185b24f130d95d5140dbd72a9b325e7b6b49e521a4faa
Looks like docker image is the same as before, not uploading
https://ci-caches.rust-lang.org/docker/7ebc15c01a233894034d277c8cce4e949f4e7791f66b4727c8fb6e058a0b8171d6152e1441d677cef0653843ceeee469c097b8699b2bb74249e674f6aa1a8813
sha256:9c3c93a371e5aed5c18185b24f130d95d5140dbd72a9b325e7b6b49e521a4faa
Setting extra environment values for docker:  --env ENABLE_GCC_CODEGEN=1 --env GCC_EXEC_PREFIX=/usr/lib/gcc/
[CI_JOB_NAME=x86_64-gnu-llvm-16]
##[group]Clock drift check
  local time: Sat Dec 30 06:53:38 UTC 2023
  network time: Sat, 30 Dec 2023 06:53:38 GMT
  network time: Sat, 30 Dec 2023 06:53:38 GMT
##[endgroup]
sccache: Starting the server...
##[group]Configure the build
configure: processing command line
configure: 
configure: build.configure-args := ['--build=x86_64-unknown-linux-gnu', '--llvm-root=/usr/lib/llvm-16', '--enable-llvm-link-shared', '--set', 'rust.thin-lto-import-instr-limit=10', '--enable-verbose-configure', '--enable-sccache', '--disable-manage-submodules', '--enable-locked-deps', '--enable-cargo-native-static', '--set', 'rust.codegen-units-std=1', '--set', 'dist.compression-profile=balanced', '--dist-compression-formats=xz', '--disable-dist-src', '--release-channel=nightly', '--enable-debug-assertions', '--enable-overflow-checks', '--enable-llvm-assertions', '--set', 'rust.verify-llvm-ir', '--set', 'rust.codegen-backends=llvm,cranelift,gcc', '--set', 'llvm.static-libstdcpp', '--enable-new-symbol-mangling']
configure: target.x86_64-unknown-linux-gnu.llvm-config := /usr/lib/llvm-16/bin/llvm-config
configure: llvm.link-shared     := True
configure: rust.thin-lto-import-instr-limit := 10
configure: rust.codegen-units-std := 1
---
    |
503 |     macro_rules! gate_all {
    |     --------------------- when calling this macro
...
519 |     gate_all!(forced_keywords, "`k#ident` syntax is experimental",);
    |                                                                   ^ missing tokens in macro arguments
    |
note: while trying to match meta-variable `$help:literal`
    |
    |
511 |         ($gate:ident, $msg:literal, $help:literal) => {

error: could not compile `rustc_ast_passes` (lib) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
Build completed unsuccessfully in 0:01:19

@fee1-dead
Copy link
Member Author

closed due to design concerns, there is an alternative - using builtin # (with a space) and this isn't really useful.

@fee1-dead fee1-dead closed this Jan 2, 2024
@WaffleLapkin
Copy link
Member

@fee1-dead what are the design concerns? builtin # looks very annoying to use.

@fee1-dead fee1-dead reopened this Jan 8, 2024
@fee1-dead
Copy link
Member Author

No energy to revive this PR, so closing

@fee1-dead fee1-dead closed this Jun 25, 2024
@fee1-dead fee1-dead deleted the myth branch August 8, 2024 11:06
@fee1-dead fee1-dead restored the myth branch August 8, 2024 11:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants