Skip to content

Allow : after pat_param - #162264

Open
coolreader18 wants to merge 2 commits into
rust-lang:mainfrom
coolreader18:pat_param-colon-follow
Open

coolreader18 wants to merge 2 commits into
rust-lang:mainfrom
coolreader18:pat_param-colon-follow

Conversation

@coolreader18

@coolreader18 coolreader18 commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

View all comments

Reference PR:

As on the tin. Perhaps this requires a more formal decision process, but in my view this is something of a bug. Originally, pat_param was just the bikeshodden name of pat2015, c.f. #83386 (comment):

  • pat_no_or would be used for closure arguments; can we have something that talks about that (e.g., pat_arg, pat_param -- pattern for a closure argument)
  • pat_param is preferred because it is a parameter, not the argument passed to a call

But if it truly is a matcher for "a pattern you can use for a parameter", there shouldn't be a problem allowing it to be followed by a colon, given that that's kind of the definitional constraint for that grammar. And if, in the distant future, the language decides to separate the name and type of a parameter with something other than a colon, well, that'll need an edition change anyway.

In my opinion, this would resolve #87724: I'd imagine that most-if-not-all use cases for a pat followed by a colon are for matching function signatures or let statements, which don't let you use | patterns anyway. But, we get to have our cake and eat it too by still reserving : for future extensions to match expression (pat) patterns. If it ever is desired to make pat match the whole $pat : $ty syntax, as mentioned in #87724 (comment), that's something that can be done over an edition boundary and/or just for pat matchers.

@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 Sep 3, 2026
@rustbot

rustbot commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

r? @nnethercote

rustbot has assigned @nnethercote.
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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 20 candidates

@nnethercote nnethercote added T-lang Relevant to the language team I-lang-nominated Nominated for discussion during a lang team meeting. labels Sep 3, 2026
@nnethercote

Copy link
Copy Markdown
Contributor

This definitely would need approval from the lang team.

cc @rust-lang/lang @rust-lang/lang-advisors
cc @petrochenkov

@traviscross traviscross added I-lang-radar Items that are on lang's radar and will need eventual work or consideration. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Sep 4, 2026
@traviscross

Copy link
Copy Markdown
Contributor

Makes sense to me. Thanks @coolreader18, @nnethercote.

@rfcbot fcp merge lang

@rust-rfcbot

rust-rfcbot commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

@traviscross has proposed to merge this. The next step is review by the rest of the tagged team members:

Concerns:

Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up!

cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns.
See this document for info about what commands tagged team members can give me.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Sep 16, 2026
@tmandry

tmandry commented Sep 16, 2026

Copy link
Copy Markdown
Member

It's surprising we don't already allow this. Thanks for the PR.

@rfcbot reviewed

@rust-bors

This comment has been minimized.

@traviscross traviscross added the needs-reference-pr This language change needs an approved Reference PR to proceed. label Sep 23, 2026
@scottmcm

scottmcm commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

This absolutely makes sense to me. And if for some reason there's an edge case, we have the option later to introduce a new matcher that doesn't allow this.

@rfcbot reviewed


There's potentially some surprises that this could cause if people are writing new macros in e2015 (where param means pat_param), but surprises for people doing that doesn't really bother me.

If future if we do want colons for things like Some(x: u32) that wouldn't be impacted by this either because that inner pattern is a full pat, not a pat param.

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Sep 23, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@traviscross

Copy link
Copy Markdown
Contributor

@rfcbot concern extend-migration-lint

Do we need to extend rust_2021_incompatible_or_patterns to handle pat followed by :? Let's please add a test for the migration here.

@rust-rfcbot rust-rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 23, 2026
@traviscross

Copy link
Copy Markdown
Contributor

@rustbot author

@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 Sep 23, 2026
@rustbot

rustbot commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@coolreader18

Copy link
Copy Markdown
Contributor Author

There's potentially some surprises that this could cause if people are writing new macros in e2015 (where param means pat_param), but surprises for people doing that doesn't really bother me.

Yeah I considered making it such that only explicit pat_param allows this, but figured it didn't matter very much.

@coolreader18

Copy link
Copy Markdown
Contributor Author

Actually maybe I will make it such that only explicit pat_param allows this? That way we don't need to edit a years-old migration lint to be now about "old versions of or-patterns OR param-followed-by-colon"

Comment thread tests/ui/macros/macro-follow.stderr Resolved
Comment thread tests/ui/macros/macro-follow.rs Outdated
($p:pat $i:item) => {}; //~ERROR `$p:pat` is followed by `$i:item`
($p:pat $m:meta) => {}; //~ERROR `$p:pat` is followed by `$m:meta`
}
// FOLLOW(pat_param) = {FOLLOW(pat), Colon}

@steffahn steffahn Oct 7, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Technically FOLLOW(pat_param) = {FOLLOW(pat), Colon} is not accurate for all editions. I'd say it's more like this:

Suggested change
// FOLLOW(pat_param) = {FOLLOW(pat), Colon}
// FOLLOW(pat_param) = {FOLLOW(pat), Colon[, Or]}
// optional inclusion of `Or` depends on the edition; this is tested separately,
// in `macro-pat-pattern-followed-by-or.rs` / `macro-pat-pattern-followed-by-or-in-2021.rs`

View changes since the review

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@coolreader18 wait... I got that suggestion completely wrong. The optional inclusions need to be on // FOLLOW(pat) not // FOLLOW(pat_param) 😅

@steffahn steffahn Oct 8, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

So hmm... that means this ↓ might be what's actually correct?

// FOLLOW(pat) = {FatArrow, Comma, Eq,[ Or,] Ident(if), MetaVarDecl(Guard), Ident(in)}
// the added inclusion of `Or` is only on editions 2021 or later; this is tested separately,
// in `macro-pat-pattern-followed-by-or.rs` / `macro-pat-pattern-followed-by-or-in-2021.rs`
macro_rules! follow_pat {
    …
}

// FOLLOW(pat_param) = FOLLOW(pat) ∪ {Colon, Or}
macro_rules! follow_pat {
    …
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@coolreader18 oh.. just noticing also you used follow_pat twice, probably a new name like follow_pat_param is better ^^

@traviscross

Copy link
Copy Markdown
Contributor

To summarize, this PR proposes that pat_param should live up to its name by being useful for matching parameters. To do that, we need to allow : after the fragment in a matcher.

But then what should we do about pat prior to Rust 2021? In those editions, pat is an alias for pat_param. The edition migrations don't handle : following pat and so wouldn't migrate $p:pat : to $p:pat_param : (resulting in a broken migration). Should we fix the migrations? The author proposes that we not do that. Instead, the proposal is to allow : only after pat_param and not after pat, even in the editions where pat is otherwise an alias for pat_param.

That makes logical sense given our motivations: the name pat doesn't imply usefulness for matching parameters, so there's no reason to retroactively support : after pat in the earlier editions. It creates some tension, since the behavior of pat_param and that of older-edition pat now differ. But this is less bad than it sounds, as they both continue to match the same grammar — only what's allowed to follow the fragment changes.

We talked about this in the lang meeting today. The solution makes sense to me. Thanks to @coolreader18 for this work.

@rfcbot resolve extend-migration-lint

@rust-rfcbot rust-rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Oct 7, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

🔔 This is now entering its final comment period, as per the review above. 🔔

@coolreader18
coolreader18 force-pushed the pat_param-colon-follow branch from 3b451a8 to 8175c33 Compare October 8, 2026 20:39
@rustbot

This comment has been minimized.

@rustbot

rustbot commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

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

cc @rust-lang/rustfmt

@rustbot rustbot added the T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. label Oct 8, 2026
@coolreader18

Copy link
Copy Markdown
Contributor Author

I've added a commit that splits NtPatKind::PatParam { inferred: bool } into two variants, NtPatKind::PatParam and NtPatKind::Pat2018, since they're now more distinct. If it'd be better to do that in a separate PR or to not do it at all, let me know.

@rust-log-analyzer

This comment has been minimized.

@coolreader18
coolreader18 force-pushed the pat_param-colon-follow branch from 71f159d to 87fe68c Compare October 8, 2026 20:55
@traviscross traviscross removed I-lang-nominated Nominated for discussion during a lang team meeting. P-lang-drag-1 Lang team prioritization drag level 1. https://rust-lang.zulipchat.com/#narrow/channel/410516-t-lang labels Oct 14, 2026
@rust-rfcbot rust-rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Oct 17, 2026
@rust-rfcbot

Copy link
Copy Markdown
Collaborator

The final comment period, with a disposition to merge, as per the review above, is now complete.

As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed.

@nnethercote nnethercote left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There is a test tests/ui/macros/macro-follow-rpass.rs that is a partner of macro-follow.rs. Should it be updated?

View changes since this review

Comment thread tests/ui/macros/macro-follow.rs Resolved
Comment thread tests/ui/macros/macro-follow.rs Resolved
@nnethercote nnethercote 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. S-waiting-on-t-lang Status: Awaiting decision from T-lang labels Oct 21, 2026
@coolreader18
coolreader18 force-pushed the pat_param-colon-follow branch from 87fe68c to 9cea0e1 Compare November 2, 2026 18:08
@rustbot

rustbot commented Nov 2, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@coolreader18
coolreader18 force-pushed the pat_param-colon-follow branch from 9cea0e1 to c33afe9 Compare November 2, 2026 18:35

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. I-lang-radar Items that are on lang's radar and will need eventual work or consideration. needs-reference-pr This language change needs an approved Reference PR to proceed. 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-lang Relevant to the language team T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. to-announce Announce this issue on triage meeting

Projects

None yet

Development

Successfully merging this pull request may close these issues.

$pat followed by a : is used in the language, but isn't allowed for MBE

10 participants