Support weak alias types as self type of inherent impls

This commit is contained in:
León Orell Valerian Liehr 2024-02-08 12:54:07 +01:00
parent 0a5b998c57
commit 8677d64c72
No known key found for this signature in database
GPG key ID: D17A07215F68E713
9 changed files with 193 additions and 1 deletions

View file

@ -144,6 +144,7 @@ impl<'tcx> InherentCollect<'tcx> {
let id = id.owner_id.def_id; let id = id.owner_id.def_id;
let item_span = self.tcx.def_span(id); let item_span = self.tcx.def_span(id);
let self_ty = self.tcx.type_of(id).instantiate_identity(); let self_ty = self.tcx.type_of(id).instantiate_identity();
let self_ty = peel_off_weak_aliases(self.tcx, self_ty);
match *self_ty.kind() { match *self_ty.kind() {
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()), ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
ty::Foreign(did) => self.check_def_id(id, self_ty, did), ty::Foreign(did) => self.check_def_id(id, self_ty, did),
@ -166,7 +167,7 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::Never | ty::Never
| ty::FnPtr(_) | ty::FnPtr(_)
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty), | ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
ty::Alias(..) | ty::Param(_) => { ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _) | ty::Param(_) => {
Err(self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span })) Err(self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span }))
} }
ty::FnDef(..) ty::FnDef(..)
@ -174,6 +175,7 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::CoroutineClosure(..) | ty::CoroutineClosure(..)
| ty::Coroutine(..) | ty::Coroutine(..)
| ty::CoroutineWitness(..) | ty::CoroutineWitness(..)
| ty::Alias(ty::Weak, _)
| ty::Bound(..) | ty::Bound(..)
| ty::Placeholder(_) | ty::Placeholder(_)
| ty::Infer(_) => { | ty::Infer(_) => {
@ -184,3 +186,30 @@ impl<'tcx> InherentCollect<'tcx> {
} }
} }
} }
/// Peel off all weak alias types in this type until there are none left.
///
/// <div class="warning">
///
/// This assumes that `ty` gets normalized later and that any overflows occurring
/// during said normalization get reported.
///
/// </div>
fn peel_off_weak_aliases<'tcx>(tcx: TyCtxt<'tcx>, mut ty: Ty<'tcx>) -> Ty<'tcx> {
let ty::Alias(ty::Weak, _) = ty.kind() else { return ty };
let limit = tcx.recursion_limit();
let mut depth = 0;
while let ty::Alias(ty::Weak, alias) = ty.kind() {
if !limit.value_within_limit(depth) {
let guar = tcx.dcx().delayed_bug("overflow expanding weak alias type");
return Ty::new_error(tcx, guar);
}
ty = tcx.type_of(alias.def_id).instantiate(tcx, alias.args);
depth += 1;
}
ty
}

View file

@ -0,0 +1,10 @@
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]
type Alias = Local;
struct Local;
impl Alias { fn method() {} } //~ ERROR duplicate definitions with name `method`
impl Local { fn method() {} }
fn main() {}

View file

@ -0,0 +1,11 @@
error[E0592]: duplicate definitions with name `method`
--> $DIR/inherent-impls-conflicting.rs:7:14
|
LL | impl Alias { fn method() {} }
| ^^^^^^^^^^^ duplicate definitions for `method`
LL | impl Local { fn method() {} }
| ----------- other definition for `method`
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0592`.

View file

@ -0,0 +1,12 @@
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]
type Alias = <() as Trait>::Out;
trait Trait { type Out; }
impl Trait for () { type Out = Local; }
struct Local;
impl Alias {} //~ ERROR no nominal type found for inherent implementation
fn main() {}

View file

@ -0,0 +1,11 @@
error[E0118]: no nominal type found for inherent implementation
--> $DIR/inherent-impls-not-nominal.rs:10:1
|
LL | impl Alias {}
| ^^^^^^^^^^ impl requires a nominal type
|
= note: either implement a trait on it or create a newtype to wrap it instead
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0118`.

View file

@ -0,0 +1,43 @@
error[E0275]: overflow evaluating the requirement `Loop`
--> $DIR/inherent-impls-overflow.rs:7:13
|
LL | type Loop = Loop;
| ^^^^
|
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
error[E0275]: overflow evaluating the requirement `Loop`
--> $DIR/inherent-impls-overflow.rs:9:1
|
LL | impl Loop {}
| ^^^^^^^^^^^^
|
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
error[E0275]: overflow evaluating the requirement `Poly0<((((((...,),),),),),)>`
--> $DIR/inherent-impls-overflow.rs:11:17
|
LL | type Poly0<T> = Poly1<(T,)>;
| ^^^^^^^^^^^
|
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
error[E0275]: overflow evaluating the requirement `Poly1<((((((...,),),),),),)>`
--> $DIR/inherent-impls-overflow.rs:14:17
|
LL | type Poly1<T> = Poly0<(T,)>;
| ^^^^^^^^^^^
|
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
error[E0275]: overflow evaluating the requirement `Poly1<((((((...,),),),),),)>`
--> $DIR/inherent-impls-overflow.rs:18:1
|
LL | impl Poly0<()> {}
| ^^^^^^^^^^^^^^^^^
|
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0275`.

View file

@ -0,0 +1,38 @@
error[E0275]: overflow evaluating the requirement `Loop == _`
--> $DIR/inherent-impls-overflow.rs:9:6
|
LL | impl Loop {}
| ^^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
error[E0392]: type parameter `T` is never used
--> $DIR/inherent-impls-overflow.rs:11:12
|
LL | type Poly0<T> = Poly1<(T,)>;
| ^ unused type parameter
|
= help: consider removing `T` or referring to it in the body of the type alias
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
error[E0392]: type parameter `T` is never used
--> $DIR/inherent-impls-overflow.rs:14:12
|
LL | type Poly1<T> = Poly0<(T,)>;
| ^ unused type parameter
|
= help: consider removing `T` or referring to it in the body of the type alias
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
error[E0275]: overflow evaluating the requirement `Poly0<()> == _`
--> $DIR/inherent-impls-overflow.rs:18:6
|
LL | impl Poly0<()> {}
| ^^^^^^^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
error: aborting due to 4 previous errors
Some errors have detailed explanations: E0275, E0392.
For more information about an error, try `rustc --explain E0275`.

View file

@ -0,0 +1,20 @@
//@ revisions: classic next
//@[next] compile-flags: -Znext-solver
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]
type Loop = Loop; //[classic]~ ERROR overflow evaluating the requirement
impl Loop {} //~ ERROR overflow evaluating the requirement
type Poly0<T> = Poly1<(T,)>;
//[classic]~^ ERROR overflow evaluating the requirement
//[next]~^^ ERROR type parameter `T` is never used
type Poly1<T> = Poly0<(T,)>;
//[classic]~^ ERROR overflow evaluating the requirement
//[next]~^^ ERROR type parameter `T` is never used
impl Poly0<()> {} //~ ERROR overflow evaluating the requirement
fn main() {}

View file

@ -0,0 +1,18 @@
//@ check-pass
#![feature(lazy_type_alias)]
#![allow(incomplete_features)]
type Alias = Local;
struct Local;
impl Alias {
fn method(self) {}
}
fn main() {
let _ = Local.method();
let _ = Local::method;
let _ = Alias {}.method();
let _ = Alias::method;
}