Skip to content

Implement forced keywords (k#) - #161775

Draft
fmease wants to merge 4 commits into
rust-lang:mainfrom
fmease:kay-hash
Draft

Implement forced keywords (k#)#161775
fmease wants to merge 4 commits into
rust-lang:mainfrom
fmease:kay-hash

Conversation

@fmease

@fmease fmease commented Aug 25, 2026

Copy link
Copy Markdown
Member

Part of #153839. CC @dianne

Re. draft status: Needs one final self-review pass.


What won't be done in this PR:

  1. implementing forced keyword lifetimes ('k#static)
  2. migrating away from built-in syntax builtin # $ident($($tt)*) / introducing k#-exclusive (weak) keywords
  3. allowing the user to force edition-dependent keywords to be keywords even in editions where they're usually not a (strong) keyword (today, that only affects gen anyway IINM)
  4. extending the proc_macro::Ident API to allow users to programmatically create forced keywords (might never be added)

(No LLM was or will be used by me during the entire creation process of this PR)

@rustbot rustbot added 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-rust-analyzer Relevant to the rust-analyzer 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. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output. labels Aug 25, 2026
@fmease

fmease commented Aug 25, 2026

Copy link
Copy Markdown
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 25, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Aug 25, 2026
Implement forced keywords (`k#`)
@rust-log-analyzer

This comment has been minimized.

match pnr {
ParseNtResult::Ident(nt_ident, is_raw) => {
if let IdentIsRaw::Yes = is_raw {
if let IdentKind::Raw = is_raw {

@fmease fmease Aug 25, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

sussy, needs re-audit

View changes since the review

_,
)) => {
if let IdentIsRaw::Yes = is_raw {
if let IdentKind::Raw = is_raw {

@fmease fmease Aug 25, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

/// Traits for function pointers and items
(unstable, fn_static, "CURRENT_RUSTC_VERSION", Some(148768)),
/// Allows using forced keywords `k#fn`.
(unstable, forced_keywords, "CURRENT_RUSTC_VERSION", Some(153839)),

@fmease fmease Aug 25, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I've intentionally not marked it internal even though this feature is only backed by a T-compiler MCP and not by a T-lang RFC or in-tree experiment:

I would find it a bit weird if we (in a hypothetical future) told the users to "please try out" "k#only bounds" or "&k#own" types & exprs where the corresponding feature will obviously be non-internal.

However, I'm fine with switching it to internal (or incomplete).

View changes since the review

@@ -103,6 +104,26 @@ impl ToInternal<tk::LitKind> for LitKind {
}
}

@fmease fmease Aug 25, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We can't test the roundtripping in the current form of this PR because I haven't added any k#-exclusive weak keywords where it'd be observable if we accidentally dropped k# during encoding&decoding.

I have however tested locally that it works by introducing a new dummy keyword. A hypothetical UI test would have a setup like

use proc_macro::TokenStream;

#[proc_macro]
pub fn perform(_: TokenStream) -> TokenStream {
    let stream: TokenStream = "const _: Option<k#never> = None;").parse().unwrap(); // positive
    //let stream: TokenStream = "fn k#branded() {}").parse().unwrap(); // negative

    stream.into_iter().collect() // forces encoding+decoding
}

View changes since the review

@rust-log-analyzer

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 4eb8eac (4eb8eac3e01e4dbc9d58546df979f51a2ac2e5d5)
Base parent: b751e7a (b751e7a48501be14dcd57b4a430b73a0e51a0c52)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (4eb8eac): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking means the PR may be perf-sensitive. Consider adding rollup=never if this change is not fit for rolling up.

@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.3%, -0.2%] 2
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -3.0%, secondary 1.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.8% [2.5%, 3.1%] 2
Improvements ✅
(primary)
-3.0% [-4.1%, -1.9%] 3
Improvements ✅
(secondary)
-2.2% [-2.2%, -2.2%] 1
All ❌✅ (primary) -3.0% [-4.1%, -1.9%] 3

Cycles

Results (primary 2.1%, secondary -1.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.1% [2.1%, 2.1%] 1
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.2% [-4.1%, -2.3%] 2
All ❌✅ (primary) 2.1% [2.1%, 2.1%] 1

Binary size

Results (primary 0.0%, secondary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.1%] 4
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.0% [-0.1%, -0.0%] 4
Improvements ✅
(secondary)
-0.0% [-0.1%, -0.0%] 2
All ❌✅ (primary) 0.0% [-0.1%, 0.1%] 8

Bootstrap: 477.576s -> 469.017s (-1.79%)
Artifact size: 400.35 MiB -> 400.38 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Aug 25, 2026
@rust-bors

rust-bors Bot commented Aug 26, 2026

Copy link
Copy Markdown
Contributor

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

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-rust-analyzer Relevant to the rust-analyzer 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. T-rustdoc-frontend Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants