Skip to content

Commit d3df8ff

Browse files
committed
Auto merge of #120780 - fmease:lta-in-impls, r=oli-obk
Properly deal with weak alias types as self types of impls Fixes #114216. Fixes #116100. Not super happy about the two ad hoc “normalization” implementations for weak alias types: 1. In `inherent_impls`: The “peeling”, normalization to [“WHNF”][whnf]: Semantically that's exactly what we want (neither proper normalization nor shallow normalization would be correct here). Basically a weak alias type is “nominal” (well...^^) if the WHNF is nominal. [#97974](#97974) followed the same approach. 2. In `constrained_generic_params`: Generic parameters are constrained by a weak alias type if the corresp. “normalized” type constrains them (where we only normalize *weak* alias types not arbitrary ones). Weak alias types are injective if the corresp. “normalized” type is injective. Both have ad hoc overflow detection mechanisms. **Coherence** is handled in #117164. r? `@oli-obk` or types [whnf]: https://en.wikipedia.org/wiki/Lambda_calculus_definition#Weak_head_normal_form
2 parents 1f8e824 + fde4556 commit d3df8ff

18 files changed

+298
-18
lines changed

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

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
307307
tcx,
308308
&mut predicates,
309309
trait_ref,
310-
&mut cgp::parameters_for_impl(self_ty, trait_ref),
310+
&mut cgp::parameters_for_impl(tcx, self_ty, trait_ref),
311311
);
312312
}
313313

compiler/rustc_hir_analysis/src/constrained_generic_params.rs

+33-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_data_structures::fx::FxHashSet;
2+
use rustc_data_structures::stack::ensure_sufficient_stack;
23
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
34
use rustc_middle::ty::{self, Ty, TyCtxt};
45
use rustc_span::Span;
@@ -27,12 +28,13 @@ impl From<ty::ParamConst> for Parameter {
2728

2829
/// Returns the set of parameters constrained by the impl header.
2930
pub fn parameters_for_impl<'tcx>(
31+
tcx: TyCtxt<'tcx>,
3032
impl_self_ty: Ty<'tcx>,
3133
impl_trait_ref: Option<ty::TraitRef<'tcx>>,
3234
) -> FxHashSet<Parameter> {
3335
let vec = match impl_trait_ref {
34-
Some(tr) => parameters_for(&tr, false),
35-
None => parameters_for(&impl_self_ty, false),
36+
Some(tr) => parameters_for(tcx, &tr, false),
37+
None => parameters_for(tcx, &impl_self_ty, false),
3638
};
3739
vec.into_iter().collect()
3840
}
@@ -43,26 +45,47 @@ pub fn parameters_for_impl<'tcx>(
4345
/// of parameters whose values are needed in order to constrain `ty` - these
4446
/// differ, with the latter being a superset, in the presence of projections.
4547
pub fn parameters_for<'tcx>(
48+
tcx: TyCtxt<'tcx>,
4649
t: &impl TypeVisitable<TyCtxt<'tcx>>,
4750
include_nonconstraining: bool,
4851
) -> Vec<Parameter> {
49-
let mut collector = ParameterCollector { parameters: vec![], include_nonconstraining };
52+
let mut collector =
53+
ParameterCollector { tcx, parameters: vec![], include_nonconstraining, depth: 0 };
5054
t.visit_with(&mut collector);
5155
collector.parameters
5256
}
5357

54-
struct ParameterCollector {
58+
struct ParameterCollector<'tcx> {
59+
tcx: TyCtxt<'tcx>,
5560
parameters: Vec<Parameter>,
5661
include_nonconstraining: bool,
62+
depth: usize,
5763
}
5864

59-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
65+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector<'tcx> {
6066
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
6167
match *t.kind() {
62-
ty::Alias(..) if !self.include_nonconstraining => {
63-
// projections are not injective
68+
ty::Alias(ty::Projection | ty::Inherent | ty::Opaque, _)
69+
if !self.include_nonconstraining =>
70+
{
71+
// Projections are not injective in general.
6472
return ControlFlow::Continue(());
6573
}
74+
ty::Alias(ty::Weak, alias) if !self.include_nonconstraining => {
75+
if !self.tcx.recursion_limit().value_within_limit(self.depth) {
76+
// Other constituent types may still constrain some generic params, consider
77+
// `<T> (Overflow, T)` for example. Therefore we want to continue instead of
78+
// breaking. Only affects diagnostics.
79+
return ControlFlow::Continue(());
80+
}
81+
self.depth += 1;
82+
return ensure_sufficient_stack(|| {
83+
self.tcx
84+
.type_of(alias.def_id)
85+
.instantiate(self.tcx, alias.args)
86+
.visit_with(self)
87+
});
88+
}
6689
ty::Param(data) => {
6790
self.parameters.push(Parameter::from(data));
6891
}
@@ -82,7 +105,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
82105
fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
83106
match c.kind() {
84107
ty::ConstKind::Unevaluated(..) if !self.include_nonconstraining => {
85-
// Constant expressions are not injective
108+
// Constant expressions are not injective in general.
86109
return c.ty().visit_with(self);
87110
}
88111
ty::ConstKind::Param(data) => {
@@ -201,12 +224,12 @@ pub fn setup_constraining_predicates<'tcx>(
201224
// `<<T as Bar>::Baz as Iterator>::Output = <U as Iterator>::Output`
202225
// Then the projection only applies if `T` is known, but it still
203226
// does not determine `U`.
204-
let inputs = parameters_for(&projection.projection_ty, true);
227+
let inputs = parameters_for(tcx, &projection.projection_ty, true);
205228
let relies_only_on_inputs = inputs.iter().all(|p| input_parameters.contains(p));
206229
if !relies_only_on_inputs {
207230
continue;
208231
}
209-
input_parameters.extend(parameters_for(&projection.term, false));
232+
input_parameters.extend(parameters_for(tcx, &projection.term, false));
210233
} else {
211234
continue;
212235
}

compiler/rustc_hir_analysis/src/impl_wf_check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ fn enforce_impl_params_are_constrained(
9494
let impl_predicates = tcx.predicates_of(impl_def_id);
9595
let impl_trait_ref = tcx.impl_trait_ref(impl_def_id).map(ty::EarlyBinder::instantiate_identity);
9696

97-
let mut input_parameters = cgp::parameters_for_impl(impl_self_ty, impl_trait_ref);
97+
let mut input_parameters = cgp::parameters_for_impl(tcx, impl_self_ty, impl_trait_ref);
9898
cgp::identify_constrained_generic_params(
9999
tcx,
100100
impl_predicates,
@@ -111,7 +111,7 @@ fn enforce_impl_params_are_constrained(
111111
match item.kind {
112112
ty::AssocKind::Type => {
113113
if item.defaultness(tcx).has_value() {
114-
cgp::parameters_for(&tcx.type_of(def_id).instantiate_identity(), true)
114+
cgp::parameters_for(tcx, &tcx.type_of(def_id).instantiate_identity(), true)
115115
} else {
116116
vec![]
117117
}

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -266,15 +266,15 @@ fn unconstrained_parent_impl_args<'tcx>(
266266
continue;
267267
}
268268

269-
unconstrained_parameters.extend(cgp::parameters_for(&projection_ty, true));
269+
unconstrained_parameters.extend(cgp::parameters_for(tcx, &projection_ty, true));
270270

271-
for param in cgp::parameters_for(&projected_ty, false) {
271+
for param in cgp::parameters_for(tcx, &projected_ty, false) {
272272
if !unconstrained_parameters.contains(&param) {
273273
constrained_params.insert(param.0);
274274
}
275275
}
276276

277-
unconstrained_parameters.extend(cgp::parameters_for(&projected_ty, true));
277+
unconstrained_parameters.extend(cgp::parameters_for(tcx, &projected_ty, true));
278278
}
279279
}
280280

@@ -312,7 +312,7 @@ fn check_duplicate_params<'tcx>(
312312
parent_args: &Vec<GenericArg<'tcx>>,
313313
span: Span,
314314
) -> Result<(), ErrorGuaranteed> {
315-
let mut base_params = cgp::parameters_for(parent_args, true);
315+
let mut base_params = cgp::parameters_for(tcx, parent_args, true);
316316
base_params.sort_by_key(|param| param.0);
317317
if let (_, [duplicate, ..]) = base_params.partition_dedup() {
318318
let param = impl1_args[duplicate.0 as usize];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ check-pass
2+
3+
#![feature(lazy_type_alias)]
4+
#![allow(incomplete_features)]
5+
6+
type Injective<T> = Local<T>;
7+
struct Local<T>(T);
8+
9+
impl<T> Injective<T> {
10+
fn take(_: T) {}
11+
}
12+
13+
trait Trait {
14+
type Out;
15+
fn produce() -> Self::Out;
16+
}
17+
18+
impl<T: Default> Trait for Injective<T> {
19+
type Out = T;
20+
fn produce() -> Self::Out { T::default() }
21+
}
22+
23+
fn main() {
24+
Injective::take(0);
25+
let _: String = Injective::produce();
26+
let _: bool = Local::produce();
27+
}
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`.
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`.
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() {}

0 commit comments

Comments
 (0)