Repository navigation
Tracking Issue for inline const patterns (RFC 2920) #76001
Description
This is a tracking issue for the RFC "Inline const expressions and patterns" (rust-lang/rfcs#2920).
Const expressions have been stabilized, but patterns have not
The feature gate for the issue is #![feature(inline_const_pat)].
This was originally a tracking issue for const blocks in both expression and pattern position. Inline const expressions have been stabilized in #104087 while patterns are still unstable.
About tracking issues
Tracking issues are used to record the overall progress of implementation.
They are also uses as hubs connecting to other relevant issues, e.g., bugs or open design questions.
A tracking issue is however not meant for large scale discussion, questions, or bug reports about a feature.
Instead, open a dedicated issue for the specific matter and add the relevant feature gate label.
Steps
- Implement the RFC (Implement const expressions and patterns (RFC 2920) #77124 does most of it, modulo some bugs around range patterns that should be fixed in Make inline const work in range patterns #78116)
- Adjust documentation (see instructions on rustc-dev-guide) [Unstable Book docs in Document inline-const #78250]
- Handle inner attributes. Parse inner attributes on inline const block #94985
- Decide whether Non-terminals, e.g. expr, have diverged between parser and macro matcher #86730 needs to be resolved.
- Lang decided in 2024-04-03 triage that this is not a blocker.
- Parsing bug inside let-else: Let-else not checking for curly brace of inline const before
else#118859 - Resolve Const-eval errors in dead functions are optimization-dependent #107503
- Decide whether Which functions are "reachable", and therefore subject to monomorphization-time checks, is optimization-dependent #122814 needs to be resolved
- Lang decided in 2024-04-03 triage that this is not a blocker.
- Decide whether Evaluating constants in MIR optimizations introduces const eval errors #122828 needs to be resolved
- Lang decided in 2024-04-03 triage that this is not a blocker.
- Stabilization PR: expressions Stabilise inline_const #104087
- Reference PR: Document inline const/const block expression reference#1295
- Stabilization PR: patterns
Unresolved Questions
- Naming: "inline const", "const block", or "anonymous const"?
- Lint about
&const { 4 }vsconst { &4 }? - How should this work for the special has-block syntax rules? https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/const.20blocks.20differ.20from.20normal.20and.20from.20unsafe.20blocks/near/290229453
Implementation history
- Primary implementation PR: Implement const expressions and patterns (RFC 2920) #77124
- Unstable Book documentation: Document inline-const #78250
FnDeftype is disallowed in patterns: DenyFnDefin patterns #114668
Activity
jhpratt commentedon Aug 28, 2020
With regard to the lint, is there any semantic difference between the two? Intuitively I would expect them to behave identically (static lifetimes).
RalfJung commentedon Aug 29, 2020
They behave identically. The point of the lint is to have a single "idiomatic" form.
The issue title calls it "const expressons" so maybe that should also be on the list.
I prefer "inline const" because it emphasizes that this is a totally separate body of code that is just written inline. That's much more like a closure than normal blocks.
Also, is anyone up for implementing this? (I can't even mentor this I am afraid, this affects surface-level syntax and MIR building which is way outside what I know.^^)
Lokathor commentedon Aug 29, 2020
"const block" is clearly correct.
it matches "unsafe block" that way.
RalfJung commentedon Aug 29, 2020
That false parallel with "unsafe block" is exactly why it is not correct. An unsafe block inherits scope, execution environment, everything from its parent block. An inline const does not.
ecstatic-morse commentedon Aug 29, 2020
I'm happy to mentor, although by no means am I an expert on all the parts of the compiler you'll need to touch. There's already an
AnonConsttype in the AST and the HIR that should for the most part have the same rules as an inlineconst(e.g. no outside generic parameters), which should make the lowering part of this pretty easy.If no one volunteers in the next few weeks, I'll try to set aside a day or two to implement this. However, I'm mostly in maintenance/review mode as far as Rust is concerned.
workingjubilee commentedon Sep 1, 2020
Does
const { }differ from normal block expressions, however?I think either "inline const" or "const expression" should be favored. Everyone's going to call it a constexpr anyways because of C++ user bleedover, and I don't think it's worth offering that much pushback this time. :^)
Hm... I need to fix my computer still. Guess I have a good reason today.
memoryruins commentedon Sep 1, 2020
const {}does not inherit the same scopes as block expressions do.It can be a useful comparison when introducing C++ users to Rust's
constandconst fn, yet I have not seen most users conflate them. Additionally, C++ does not haveconstexpr { ... }.There are no mentions of
constexpror C++ on this issue before or the RFC. I find it unproductive to label it as pushback from that. The concerns about naming are mentioned in the RFC https://rust-lang.github.io/rfcs/2920-inline-const.html and the comments.workingjubilee commentedon Sep 1, 2020
memoryruins commentedon Sep 1, 2020
138 remaining items
Rollup merge of rust-lang#138492 - lcnr:rm-inline_const_pat, r=oli-obk
Rollup merge of rust-lang#138492 - lcnr:rm-inline_const_pat, r=oli-obk
Auto merge of rust-lang#138492 - lcnr:rm-inline_const_pat, r=oli-obk
zeroflaw commentedon Apr 2, 2025
Anyone that is refactoring today, another way to get a const inline without the inline_const_pat feature is:
becomes:
The downside of the above is you can't "~const" PartialEq on a non-primitive type, like you could before. So you might need to implement a const_eq function so you can do:
Hope this helps someone.
Qix- commentedon Apr 3, 2025
The downside to that is that you cannot generalize that to an expression form.
There's no way to write
key!()in a way where that's usable in all expression contexts without this feature :|RalfJung commentedon Apr 3, 2025
The LHS of
=>is not an expression, it's a pattern. So what you are asking is for the same term to work both as a pattern and as an expression.Yeah that does not work without
constpatterns, true.Auto merge of rust-lang#138492 - lcnr:rm-inline_const_pat, r=oli-obk
Auto merge of rust-lang#138492 - lcnr:rm-inline_const_pat, r=oli-obk
fmease commentedon Dec 6, 2025
@Qix-, there is, thanks to adt const params. Well, the example below actually requires unsized const params for the
&strparameter whose future is unclear but in most other cases you wouldn't need it. I'm using generic const items (GCI, #113521) for convenience but they're not strictly necessary.It's more restricted, of course, but at least there is a way to express basic forms of this.
Without GCI:
sys: Fix build error on nightly-2024-04-19
lcnr commentedon Feb 14, 2026
Inline const patterns have been entirely removed from the compiler due to significant technical limitations. readding them should require a separate proposal. Closing
Auto merge of #138492 - lcnr:rm-inline_const_pat, r=oli-obk