Skip to content

Allow self in const generics#157949

Open
bb1yd wants to merge 1 commit into
rust-lang:mainfrom
bb1yd:allow-self-in-const-generics
Open

Allow self in const generics#157949
bb1yd wants to merge 1 commit into
rust-lang:mainfrom
bb1yd:allow-self-in-const-generics

Conversation

@bb1yd

@bb1yd bb1yd commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Fixes #149203

@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. labels Jun 16, 2026
@@ -1535,7 +1535,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}

RibKind::ConstParamTy => {

@fmease fmease Jun 16, 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.

IINM you're now accepting this code despite N depending on T which shouldn't be allowed w/o enabling GCPT.

impl<T> Wrap<T> {
    fn f<const N: Self>() {}
}

struct Wrap<T>(T);

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to check if Self depends on generics?

@fmease fmease Jun 18, 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.

I'm not super well-versed in name resolution, so I don't know if there's a good way to do this in rustc_resolve that still nicely leverages the existing rib structure. Boxy probably has an idea.

Calling type_of here on the LocalDefId found in the SelfTyAlias is probably too prone to cycles or hangs; looking at hir_node corresp. to the LocalDefId might be an option but that might not integrate with the visitor.

Naively speaking, I would do it in HIR ty lowering by inspecting the type as returned by type_of and by reusing+generalizing check_assoc_const_binding_type but that's probably also not super ideal. I haven't read a lot of the new mGCA code in HIR ty lowering, maybe there's already a function for that (that's similar to check_assoc_const_binding_type?)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Is it okay to check if Self has param in check_param_wf? I saw that we do other checks on const params here, but I think it doesn't belong to the HIR ty lowering process.

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.

we used to have a nice way of handling this and no longer do 😅 previously you could set a flag on the Self to tell hir ty lowering to not support generic parameters on it 🤔

one problem with checking this in HIR ty lowering is we'd not error on this code (probably) due to the fact that the lowered ty wouldn't have the type alias Foo<N> it'd just have Bar... 🤔

#![feature(adt_const_params)]

type Foo<const N: usize> = Bar;

#[derive(Eq, PartialEq, core::marker::ConstParamTy)]
struct Bar;

trait Trait<const N: usize> {
    fn bar<const C: Bar>();
}

impl<const N: usize> Trait<N> for Foo<N> {
    fn bar<const C: Self>() {}
}

i think that's probably just fine... such code will be accepted long term once generic_const_parameter_types is stabilized anyway 🤷‍♀️ though- can you feature gate support for Self under min_adt_const_params/adt_const_params, I don't want to immediately stabilize this but it is something we should just support when stabilizing type aliases here (which we are doing with adt const params)

Is it okay to check if Self has param in check_param_wf?

I would either do this, or do it in fn type_of when lowering the type of const parameters. I think I prefer type_of as you can then return TyKind::Error for the type of the const parameter which should avoid the rest of the compiler needing to handle types of const parameters having generics with GCPT enabled

@rust-bors

This comment has been minimized.

@bb1yd bb1yd force-pushed the allow-self-in-const-generics branch from f087424 to 4b4635a Compare June 25, 2026 16:00
@rust-log-analyzer

This comment has been minimized.

@bb1yd bb1yd force-pushed the allow-self-in-const-generics branch 2 times, most recently from 1e306d8 to d655af4 Compare June 26, 2026 07:29
@rust-log-analyzer

This comment has been minimized.

@bb1yd bb1yd force-pushed the allow-self-in-const-generics branch from d655af4 to c17a818 Compare June 26, 2026 09:50
@bb1yd

bb1yd commented Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 26, 2026
@bb1yd bb1yd marked this pull request as ready for review June 26, 2026 11:02
@BoxyUwU

BoxyUwU commented Jul 8, 2026

Copy link
Copy Markdown
Member

sry for the long wait, thanks for being patient :3

@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 Jul 8, 2026
@rustbot

rustbot commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

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

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Self not allowed in type of const generics even when it's concrete

5 participants