Allow self in const generics#157949
Conversation
| @@ -1535,7 +1535,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { | |||
| } | |||
|
|
|||
| RibKind::ConstParamTy => { | |||
There was a problem hiding this comment.
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);There was a problem hiding this comment.
Is there a way to check if Self depends on generics?
There was a problem hiding this comment.
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?)?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
f087424 to
4b4635a
Compare
This comment has been minimized.
This comment has been minimized.
1e306d8 to
d655af4
Compare
This comment has been minimized.
This comment has been minimized.
d655af4 to
c17a818
Compare
|
@rustbot ready |
|
sry for the long wait, thanks for being patient :3 @rustbot author |
|
Reminder, once the PR becomes ready for a review, use |
Fixes #149203