Skip to content

Support function args const generics - #162727

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

bit-aloo wants to merge 8 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

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
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

rust-bors Bot commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

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

@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

/// Optional default value for the const generic param.
default: Option<AnonConst>,
#[visitable(ignore)]
arg_pos: Option<u32>,

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.

Potentially a better name, but I think especially a doc comment, would go a long way here. It's rather opaque/confusing what this is right now.


/// Some features require one or more other features to be enabled.
pub const DEPENDENT_FEATURES: &[(Symbol, &[Symbol])] = &[
(sym::function_arg_const_generics, &[sym::min_generic_const_args]),

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, this is not strictly necessary - it is possible to use direct args right now on stable, plain simple paths to generic parameters are direct args on stable. Maaaybe still want to include it though, probably as being an Or(gca_min_const_items, gca_adts), unsure.

self.check_param_uses_if_mcg(ct, tcx.hir_span(path_hir_id), false)
}

pub fn lower_const_arg_expr(&self, expr: &hir::Expr<'_>, ty: Ty<'tcx>) -> Const<'tcx> {

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 would strongly prefer this to not be duplicated with the lower_expr_to_const_arg_direct machinery. I'm unsure of how to do so, and is my main bit of feedback in this PR... perhaps it's best to chat about it in Zulip or something. I haven't fully thought this through yet.

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

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.

7 participants