Skip to content

Commit 8677d64

Browse files
committed
Support weak alias types as self type of inherent impls
1 parent 0a5b998 commit 8677d64

9 files changed

+193
-1
lines changed

Diff for: compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl<'tcx> InherentCollect<'tcx> {
144144
let id = id.owner_id.def_id;
145145
let item_span = self.tcx.def_span(id);
146146
let self_ty = self.tcx.type_of(id).instantiate_identity();
147+
let self_ty = peel_off_weak_aliases(self.tcx, self_ty);
147148
match *self_ty.kind() {
148149
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
149150
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
@@ -166,14 +167,15 @@ impl<'tcx> InherentCollect<'tcx> {
166167
| ty::Never
167168
| ty::FnPtr(_)
168169
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
169-
ty::Alias(..) | ty::Param(_) => {
170+
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _) | ty::Param(_) => {
170171
Err(self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span }))
171172
}
172173
ty::FnDef(..)
173174
| ty::Closure(..)
174175
| ty::CoroutineClosure(..)
175176
| ty::Coroutine(..)
176177
| ty::CoroutineWitness(..)
178+
| ty::Alias(ty::Weak, _)
177179
| ty::Bound(..)
178180
| ty::Placeholder(_)
179181
| ty::Infer(_) => {
@@ -184,3 +186,30 @@ impl<'tcx> InherentCollect<'tcx> {
184186
}
185187
}
186188
}
189+
190+
/// Peel off all weak alias types in this type until there are none left.
191+
///
192+
/// <div class="warning">
193+
///
194+
/// This assumes that `ty` gets normalized later and that any overflows occurring
195+
/// during said normalization get reported.
196+
///
197+
/// </div>
198+
fn peel_off_weak_aliases<'tcx>(tcx: TyCtxt<'tcx>, mut ty: Ty<'tcx>) -> Ty<'tcx> {
199+
let ty::Alias(ty::Weak, _) = ty.kind() else { return ty };
200+
201+
let limit = tcx.recursion_limit();
202+
let mut depth = 0;
203+
204+
while let ty::Alias(ty::Weak, alias) = ty.kind() {
205+
if !limit.value_within_limit(depth) {
206+
let guar = tcx.dcx().delayed_bug("overflow expanding weak alias type");
207+
return Ty::new_error(tcx, guar);
208+
}
209+
210+
ty = tcx.type_of(alias.def_id).instantiate(tcx, alias.args);
211+
depth += 1;
212+
}
213+
214+
ty
215+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(lazy_type_alias)]
2+
#![allow(incomplete_features)]
3+
4+
type Alias = Local;
5+
struct Local;
6+
7+
impl Alias { fn method() {} } //~ ERROR duplicate definitions with name `method`
8+
impl Local { fn method() {} }
9+
10+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0592]: duplicate definitions with name `method`
2+
--> $DIR/inherent-impls-conflicting.rs:7:14
3+
|
4+
LL | impl Alias { fn method() {} }
5+
| ^^^^^^^^^^^ duplicate definitions for `method`
6+
LL | impl Local { fn method() {} }
7+
| ----------- other definition for `method`
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0592`.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(lazy_type_alias)]
2+
#![allow(incomplete_features)]
3+
4+
type Alias = <() as Trait>::Out;
5+
6+
trait Trait { type Out; }
7+
impl Trait for () { type Out = Local; }
8+
struct Local;
9+
10+
impl Alias {} //~ ERROR no nominal type found for inherent implementation
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0118]: no nominal type found for inherent implementation
2+
--> $DIR/inherent-impls-not-nominal.rs:10:1
3+
|
4+
LL | impl Alias {}
5+
| ^^^^^^^^^^ impl requires a nominal type
6+
|
7+
= note: either implement a trait on it or create a newtype to wrap it instead
8+
9+
error: aborting due to 1 previous error
10+
11+
For more information about this error, try `rustc --explain E0118`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error[E0275]: overflow evaluating the requirement `Loop`
2+
--> $DIR/inherent-impls-overflow.rs:7:13
3+
|
4+
LL | type Loop = Loop;
5+
| ^^^^
6+
|
7+
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
8+
9+
error[E0275]: overflow evaluating the requirement `Loop`
10+
--> $DIR/inherent-impls-overflow.rs:9:1
11+
|
12+
LL | impl Loop {}
13+
| ^^^^^^^^^^^^
14+
|
15+
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
16+
17+
error[E0275]: overflow evaluating the requirement `Poly0<((((((...,),),),),),)>`
18+
--> $DIR/inherent-impls-overflow.rs:11:17
19+
|
20+
LL | type Poly0<T> = Poly1<(T,)>;
21+
| ^^^^^^^^^^^
22+
|
23+
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
24+
25+
error[E0275]: overflow evaluating the requirement `Poly1<((((((...,),),),),),)>`
26+
--> $DIR/inherent-impls-overflow.rs:14:17
27+
|
28+
LL | type Poly1<T> = Poly0<(T,)>;
29+
| ^^^^^^^^^^^
30+
|
31+
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
32+
33+
error[E0275]: overflow evaluating the requirement `Poly1<((((((...,),),),),),)>`
34+
--> $DIR/inherent-impls-overflow.rs:18:1
35+
|
36+
LL | impl Poly0<()> {}
37+
| ^^^^^^^^^^^^^^^^^
38+
|
39+
= note: in case this is a recursive type alias, consider using a struct, enum, or union instead
40+
41+
error: aborting due to 5 previous errors
42+
43+
For more information about this error, try `rustc --explain E0275`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error[E0275]: overflow evaluating the requirement `Loop == _`
2+
--> $DIR/inherent-impls-overflow.rs:9:6
3+
|
4+
LL | impl Loop {}
5+
| ^^^^
6+
|
7+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
8+
9+
error[E0392]: type parameter `T` is never used
10+
--> $DIR/inherent-impls-overflow.rs:11:12
11+
|
12+
LL | type Poly0<T> = Poly1<(T,)>;
13+
| ^ unused type parameter
14+
|
15+
= help: consider removing `T` or referring to it in the body of the type alias
16+
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
17+
18+
error[E0392]: type parameter `T` is never used
19+
--> $DIR/inherent-impls-overflow.rs:14:12
20+
|
21+
LL | type Poly1<T> = Poly0<(T,)>;
22+
| ^ unused type parameter
23+
|
24+
= help: consider removing `T` or referring to it in the body of the type alias
25+
= help: if you intended `T` to be a const parameter, use `const T: /* Type */` instead
26+
27+
error[E0275]: overflow evaluating the requirement `Poly0<()> == _`
28+
--> $DIR/inherent-impls-overflow.rs:18:6
29+
|
30+
LL | impl Poly0<()> {}
31+
| ^^^^^^^^^
32+
|
33+
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`inherent_impls_overflow`)
34+
35+
error: aborting due to 4 previous errors
36+
37+
Some errors have detailed explanations: E0275, E0392.
38+
For more information about an error, try `rustc --explain E0275`.

Diff for: tests/ui/lazy-type-alias/inherent-impls-overflow.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//@ revisions: classic next
2+
//@[next] compile-flags: -Znext-solver
3+
4+
#![feature(lazy_type_alias)]
5+
#![allow(incomplete_features)]
6+
7+
type Loop = Loop; //[classic]~ ERROR overflow evaluating the requirement
8+
9+
impl Loop {} //~ ERROR overflow evaluating the requirement
10+
11+
type Poly0<T> = Poly1<(T,)>;
12+
//[classic]~^ ERROR overflow evaluating the requirement
13+
//[next]~^^ ERROR type parameter `T` is never used
14+
type Poly1<T> = Poly0<(T,)>;
15+
//[classic]~^ ERROR overflow evaluating the requirement
16+
//[next]~^^ ERROR type parameter `T` is never used
17+
18+
impl Poly0<()> {} //~ ERROR overflow evaluating the requirement
19+
20+
fn main() {}

Diff for: tests/ui/lazy-type-alias/inherent-impls.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@ check-pass
2+
3+
#![feature(lazy_type_alias)]
4+
#![allow(incomplete_features)]
5+
6+
type Alias = Local;
7+
struct Local;
8+
9+
impl Alias {
10+
fn method(self) {}
11+
}
12+
13+
fn main() {
14+
let _ = Local.method();
15+
let _ = Local::method;
16+
let _ = Alias {}.method();
17+
let _ = Alias::method;
18+
}

0 commit comments

Comments
 (0)