Skip to content

Support function args const generics - #162727

Open
bit-aloo wants to merge 12 commits into
rust-lang:mainfrom
bit-aloo:2026-09-06-function-argument-const-generics
Open

bit-aloo wants to merge 12 commits into
rust-lang:mainfrom
bit-aloo:2026-09-06-function-argument-const-generics

Conversation

@bit-aloo

@bit-aloo bit-aloo commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

View all comments

This PR adds initial support for argument-position const generics behind the function_arg_const_generics feature gate.

It allows const generics to be declared directly in function arguments. The PR adds the basic plumbing across AST, HIR, and ty, parses these parameters into the function's generics, lowers call arguments into consts. For now, we lower literals, negated literals, const parameters, const items, unit variants, and etc, This works for functions, methods and a couple of more cases.

r? @BoxyUwU
cc: @khyperia

@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-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue. labels Sep 13, 2026
@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU self-assigned this Sep 13, 2026
Comment thread compiler/rustc_parse/src/parser/function.rs Outdated
Comment thread compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs Outdated
@rust-log-analyzer

This comment has been minimized.

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-09-06-function-argument-const-generics branch from 069cb06 to 0816e30 Compare September 14, 2026 10:55
@rust-bors

This comment has been minimized.

@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-09-06-function-argument-const-generics branch from 0816e30 to 74ba4f6 Compare September 24, 2026 13:50
@rustbot rustbot added the T-clippy Relevant to the Clippy team. label Sep 24, 2026
@rustbot

rustbot commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

Requested reviewer is already assigned to this pull request.

Please choose another assignee.

@bit-aloo
bit-aloo marked this pull request as ready for review September 24, 2026 17:29
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 24, 2026
@rustbot

rustbot commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

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

cc @rust-lang/rustfmt

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

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

cc @fmease

Changes to the size of AST and/or HIR nodes.

cc @nnethercote

HIR ty lowering was modified

cc @fmease

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

cc @rust-lang/clippy

@rustbot rustbot removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Sep 24, 2026
Comment thread src/tools/rustfmt/src/types.rs Outdated
ref ty,
span,
default,
arg_pos: _,

@ytmimi ytmimi Sep 24, 2026 •

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.

What is arg_pos, and does rustfmt need to handle formatting it in some way?

Probably good to add a #![feature(function_arg_const_generics] test case to rustfmt to make sure things are getting formatted as expected.

View changes since the review

@rust-bors

This comment has been minimized.

@khyperia khyperia left a comment •

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.

I'm feeling a bit overwhelmed and struggling to review this, and haven't gotten through the parser changes yet. Still though, wanted to comment for what I've seen so far. I think I'd appreciate talking over how to deal with lowering here in zulip or something, I haven't fully thought through the theory space. If you have any direction/resources/etc. on lowering (e.g. whether we fundamentally must lower from hir instead of ast, what potential issues there are from doing so), I'd love to see.

View changes since this review

Comment thread compiler/rustc_ast/src/ast.rs Outdated
Comment thread compiler/rustc_feature/src/unstable.rs Outdated
Comment thread compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-09-06-function-argument-const-generics branch from 74ba4f6 to e0fc639 Compare October 7, 2026 10:41
@rustbot

rustbot commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in compiler/rustc_builtin_macros/src/autodiff.rs

cc @ZuseZ4

@khyperia khyperia left a comment •

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.

rawr, nice! looks good to me :3 just a couple nits!

... I am realizing now that you poking in Zulip maybe didn't imply you wanted a rereview but I only realized this just now after writing all this, so uuuh, get reviewed I guess

View changes since this review

Comment thread compiler/rustc_ast/src/ast.rs Outdated
Comment thread compiler/rustc_ast/src/ast.rs Outdated
Comment thread compiler/rustc_hir/src/hir.rs Outdated
@khyperia khyperia 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 Oct 8, 2026
@rust-cloud-vms
rust-cloud-vms Bot force-pushed the 2026-09-06-function-argument-const-generics branch 2 times, most recently from 9d3480a to 08a6a30 Compare October 9, 2026 09:08
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 9, 2026
…enerics, r=<try>

Support function args const generics

@khyperia khyperia left a comment •

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.

nice, looks good to me, thanks so much! ❤️ r=me if perf isn't red

View changes since this review

@rust-bors

rust-bors Bot commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 93791e6 (93791e636c2db4b72084ef8121f766d75d9a4971)
Base parent: 76c9095 (76c90957b7e422c4b9c45192b0197214d7de5a54)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (93791e6): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@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.2% [0.1%, 0.2%] 10
Regressions ❌
(secondary)
0.3% [0.1%, 0.9%] 22
Improvements ✅
(primary)
-0.3% [-0.5%, -0.1%] 5
Improvements ✅
(secondary)
-0.4% [-0.7%, -0.1%] 40
All ❌✅ (primary) 0.0% [-0.5%, 0.2%] 15

Max RSS (memory usage)

Results (primary 1.4%, secondary 2.3%)

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

mean range count
Regressions ❌
(primary)
1.4% [1.4%, 1.4%] 1
Regressions ❌
(secondary)
2.3% [1.6%, 3.0%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.4% [1.4%, 1.4%] 1

Cycles

Results (primary -1.7%, secondary 12.5%)

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)
12.5% [12.5%, 12.5%] 1
Improvements ✅
(primary)
-1.7% [-2.2%, -1.2%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.7% [-2.2%, -1.2%] 2

Binary size

This perf run didn't have relevant results for this metric.

Bootstrap: 490.84s -> 486.205s (-0.94%)
Artifact size: 406.48 MiB -> 406.67 MiB (0.05%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 9, 2026
let mut is_const = false;
if fn_parse_mode.allow_const
&& this.check_keyword(exp!(Const))
&& this.look_ahead(1, |token| token.is_ident())

@fmease fmease Oct 9, 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.

This lookahead is insufficient.

It makes Rust 2015 snippets like trait P { fn f(const dyn Trait); }, trait P { fn f(const impl Trait); } or trait P { fn f(const fn()); } syntactically legal modulo feature gating which is hardly intentional.

Moreover it alters the meaning of unstable Rust 2015 snippet trait P { fn f(const Fn()); }. On nightly, it means trait P { fn f(_: dyn const Fn()); } essentially but on your branch it now basically means trait P { fn f(_: dyn Fn()); } dropping the const trait bound modifier.

View changes since the review

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.

Damn! Thanks for mentioning this. I guess explicitly checking that the first token is const, followed by a non-reserved identifier and then a colon, should do the trick, unless I'm missing something. Added this: 624858f

Make arg_pos to Option<u16> instead of Option<u32>
And add a has_arg_pos_const in generics, so we can have shortcircuit
@bit-aloo

Copy link
Copy Markdown
Member Author

Lets try and check perf again:

@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 Oct 10, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 10, 2026
…enerics, r=<try>

Support function args const generics
@rust-bors

rust-bors Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: c48133e (c48133e73987fcf93fccd047b86595883c001ac0)
Base parent: 5ec56e3 (5ec56e3902656f2460259fc3ac0f6e2255f94417)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (c48133e): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@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.1% [0.1%, 0.1%] 3
Regressions ❌
(secondary)
0.3% [0.1%, 0.4%] 10
Improvements ✅
(primary)
-0.3% [-0.6%, -0.1%] 5
Improvements ✅
(secondary)
-0.4% [-0.7%, -0.1%] 39
All ❌✅ (primary) -0.1% [-0.6%, 0.1%] 8

Max RSS (memory usage)

Results (primary -1.1%, secondary -2.0%)

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

mean range count
Regressions ❌
(primary)
1.1% [1.1%, 1.1%] 1
Regressions ❌
(secondary)
4.6% [4.6%, 4.6%] 1
Improvements ✅
(primary)
-2.1% [-2.7%, -1.6%] 2
Improvements ✅
(secondary)
-8.7% [-8.7%, -8.7%] 1
All ❌✅ (primary) -1.1% [-2.7%, 1.1%] 3

Cycles

Results (primary 2.4%, secondary 1.8%)

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

mean range count
Regressions ❌
(primary)
2.4% [2.1%, 2.6%] 2
Regressions ❌
(secondary)
3.3% [2.1%, 4.8%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.5%] 1
All ❌✅ (primary) 2.4% [2.1%, 2.6%] 2

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
Regressions ❌
(secondary)
- - 0
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.0%] 4

Bootstrap: 487.044s -> 489.815s (0.57%)
Artifact size: 406.45 MiB -> 406.75 MiB (0.07%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 10, 2026
@bit-aloo

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 Oct 10, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Oct 10, 2026
…enerics, r=<try>

Support function args const generics
@rust-bors

rust-bors Bot commented Oct 10, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 5415718 (5415718efb0ab088d913bf332f4581c437f1cd84)
Base parent: 6e4cba3 (6e4cba3380a6f9e1e4d7672e11e40372bf04e873)

@rust-timer

This comment has been minimized.

@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (5415718): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

@bors rollup=never rustc-perf
@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.1% [0.1%, 0.1%] 1
Regressions ❌
(secondary)
0.3% [0.1%, 0.3%] 8
Improvements ✅
(primary)
-0.3% [-0.6%, -0.1%] 6
Improvements ✅
(secondary)
-0.4% [-0.7%, -0.1%] 39
All ❌✅ (primary) -0.2% [-0.6%, 0.1%] 7

Max RSS (memory usage)

Results (primary 1.3%, secondary 2.0%)

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

mean range count
Regressions ❌
(primary)
1.9% [0.6%, 2.8%] 8
Regressions ❌
(secondary)
3.8% [0.9%, 9.8%] 8
Improvements ✅
(primary)
-2.9% [-2.9%, -2.9%] 1
Improvements ✅
(secondary)
-2.8% [-3.2%, -2.2%] 3
All ❌✅ (primary) 1.3% [-2.9%, 2.8%] 9

Cycles

Results (secondary 4.4%)

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)
7.5% [2.8%, 11.5%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.3% [-3.8%, -2.8%] 2
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.1%, secondary 0.1%)

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.2%] 123
Regressions ❌
(secondary)
0.1% [0.0%, 0.2%] 105
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [0.0%, 0.2%] 123

Bootstrap: 488.311s -> 489.736s (0.29%)
Artifact size: 407.22 MiB -> 406.75 MiB (-0.12%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 10, 2026

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

perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-clippy Relevant to the Clippy team. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustfmt Relevant to the rustfmt team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants