Skip to content

Commit f817a32

Browse files
committed
cleanup and comments
1 parent 3f53f55 commit f817a32

File tree

9 files changed

+185
-27
lines changed

9 files changed

+185
-27
lines changed

compiler/rustc_infer/src/infer/combine.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,16 @@ impl<'tcx> InferCtxt<'tcx> {
326326
) -> RelateResult<'tcx, ty::Const<'tcx>> {
327327
let span =
328328
self.inner.borrow_mut().const_unification_table().probe_value(target_vid).origin.span;
329-
let Generalization { value, needs_wf: _ } = generalize::generalize(
329+
// FIXME(generic_const_exprs): Occurs check failures for unevaluated
330+
// constants and generic expressions are not yet handled correctly.
331+
let Generalization { value_may_be_infer: value, needs_wf: _ } = generalize::generalize(
330332
self,
331333
&mut CombineDelegate { infcx: self, span, param_env },
332334
ct,
333335
target_vid,
334336
ty::Variance::Invariant,
335337
)?;
336338

337-
// FIXME(generic_const_exprs): Occurs check failures for unevaluated
338-
// constants and generic expressions are not yet handled correctly.
339-
let value = value.may_be_infer();
340-
341339
self.inner.borrow_mut().const_unification_table().union_value(
342340
target_vid,
343341
ConstVarValue {
@@ -449,7 +447,7 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
449447
// `'?2` and `?3` are fresh region/type inference
450448
// variables. (Down below, we will relate `a_ty <: b_ty`,
451449
// adding constraints like `'x: '?2` and `?1 <: ?3`.)
452-
let Generalization { value, needs_wf } = generalize::generalize(
450+
let Generalization { value_may_be_infer: b_ty, needs_wf } = generalize::generalize(
453451
self.infcx,
454452
&mut CombineDelegate {
455453
infcx: self.infcx,
@@ -461,7 +459,6 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
461459
ambient_variance,
462460
)?;
463461

464-
let b_ty = value.may_be_infer(); // we handle this further down.
465462
self.infcx.inner.borrow_mut().type_variables().instantiate(b_vid, b_ty);
466463

467464
if needs_wf {
@@ -501,6 +498,11 @@ impl<'infcx, 'tcx> CombineFields<'infcx, 'tcx> {
501498
}
502499
}
503500
}
501+
502+
// FIXME: This does not handle subtyping correctly, we should switch to
503+
// alias-relate in the new solver and could instead create a new inference
504+
// variable for `a_ty`, emitting `Projection(a_ty, a_infer)` and
505+
// `a_infer <: b_ty`.
504506
self.obligations.push(Obligation::new(
505507
self.tcx(),
506508
self.trace.cause.clone(),

compiler/rustc_infer/src/infer/generalize.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ pub(super) fn generalize<'tcx, D: GeneralizerDelegate<'tcx>, T: Into<Term<'tcx>>
4747
};
4848

4949
assert!(!term.has_escaping_bound_vars());
50-
let value = generalizer.relate(term, term)?;
50+
let value_may_be_infer = generalizer.relate(term, term)?;
5151
let needs_wf = generalizer.needs_wf;
52-
Ok(Generalization { value: HandleProjection(value), needs_wf })
52+
Ok(Generalization { value_may_be_infer, needs_wf })
5353
}
5454

5555
/// Abstracts the handling of region vars between HIR and MIR/NLL typechecking
@@ -153,10 +153,11 @@ struct Generalizer<'me, 'tcx, D> {
153153

154154
cache: SsoHashMap<Ty<'tcx>, Ty<'tcx>>,
155155

156-
/// This is set once we're generalizing the arguments of an alias. In case
157-
/// we encounter an occurs check failure we generalize the alias to an
158-
/// inference variable instead of erroring. This is necessary to avoid
159-
/// incorrect errors when relating `?0` with `<?0 as Trait>::Assoc`.
156+
/// This is set once we're generalizing the arguments of an alias.
157+
///
158+
/// This is necessary to correctly handle
159+
/// `<T as Bar<<?0 as Foo>::Assoc>::Assoc == ?0`. This equality can
160+
/// hold by either normalizing the outer or the inner associated type.
160161
in_alias: bool,
161162

162163
/// See the field `needs_wf` in `Generalization`.
@@ -330,6 +331,12 @@ where
330331
}
331332

332333
ty::Alias(kind, data) => {
334+
// An occurs check failure inside of an alias does not mean
335+
// that the types definitely don't unify. We may be able
336+
// to normalize the alias after all.
337+
//
338+
// We handle this by lazily equating the alias and generalizing
339+
// it to an inference variable.
333340
let is_nested_alias = mem::replace(&mut self.in_alias, true);
334341
let result = match self.relate(data, data) {
335342
Ok(data) => Ok(Ty::new_alias(self.tcx(), kind, data)),
@@ -343,7 +350,7 @@ where
343350
self.for_universe.can_name(visitor.max_universe())
344351
&& !t.has_escaping_bound_vars();
345352
if !infer_replacement_is_complete {
346-
warn!("incomplete generalization of an alias type: {t:?}");
353+
warn!("may incompletely handle alias type: {t:?}");
347354
}
348355

349356
debug!("generalization failure in alias");
@@ -500,20 +507,20 @@ where
500507
}
501508
}
502509

503-
#[derive(Debug)]
504-
pub(super) struct HandleProjection<T>(T);
505-
impl<T> HandleProjection<T> {
506-
pub(super) fn may_be_infer(self) -> T {
507-
self.0
508-
}
509-
}
510-
511510
/// Result from a generalization operation. This includes
512511
/// not only the generalized type, but also a bool flag
513512
/// indicating whether further WF checks are needed.
514513
#[derive(Debug)]
515514
pub(super) struct Generalization<T> {
516-
pub(super) value: HandleProjection<T>,
515+
/// When generalizing `<?0 as Trait>::Assoc` or
516+
/// `<T as Bar<<?0 as Foo>::Assoc>>::Assoc`
517+
/// for `?0` generalization returns an inference
518+
/// variable.
519+
///
520+
/// This has to be handled wotj care as it can
521+
/// otherwise very easily result in infinite
522+
/// recursion.
523+
pub(super) value_may_be_infer: T,
517524

518525
/// If true, then the generalized type may not be well-formed,
519526
/// even if the source type is well-formed, so we should add an

compiler/rustc_infer/src/infer/nll_relate/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,16 @@ where
217217
}
218218

219219
fn generalize(&mut self, ty: Ty<'tcx>, for_vid: ty::TyVid) -> RelateResult<'tcx, Ty<'tcx>> {
220-
let Generalization { value, needs_wf: _ } = generalize::generalize(
220+
let Generalization { value_may_be_infer: ty, needs_wf: _ } = generalize::generalize(
221221
self.infcx,
222222
&mut self.delegate,
223223
ty,
224224
for_vid,
225225
self.ambient_variance,
226226
)?;
227227

228-
let ty = value.may_be_infer();
229228
if ty.is_ty_var() {
230-
warn!("occurs check failure in MIR typeck");
229+
span_bug!(self.delegate.span(), "occurs check failure in MIR typeck");
231230
}
232231
Ok(ty)
233232
}

compiler/rustc_trait_selection/src/traits/coherence.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,13 @@ impl<'a, 'tcx> ProofTreeVisitor<'tcx> for AmbiguityCausesVisitor<'a> {
930930
let trait_ref = match predicate.kind().no_bound_vars() {
931931
Some(ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr))) => tr.trait_ref,
932932
Some(ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj))) => {
933-
proj.projection_ty.trait_ref(infcx.tcx)
933+
match proj.projection_ty.kind(infcx.tcx) {
934+
ty::AliasKind::Projection => proj.projection_ty.trait_ref(infcx.tcx),
935+
ty::AliasKind::Inherent | ty::AliasKind::Opaque | ty::AliasKind::Weak => {
936+
// No intercrate ambiguity causes here.
937+
return ControlFlow::Continue(());
938+
}
939+
}
934940
}
935941
_ => return ControlFlow::Continue(()),
936942
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
2+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!2_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
3+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
4+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!2_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
5+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
6+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!2_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
7+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
8+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!2_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
9+
error[E0119]: conflicting implementations of trait `Overlap<for<'a> fn(&'a (), ())>` for type `for<'a> fn(&'a (), ())`
10+
--> $DIR/associated-type.rs:31:1
11+
|
12+
LL | impl<T> Overlap<T> for T {
13+
| ------------------------ first implementation here
14+
...
15+
LL | / impl<T> Overlap<for<'a> fn(&'a (), Assoc<'a, T>)> for T
16+
LL | |
17+
LL | | where
18+
LL | | for<'a> *const T: ToUnit<'a>,
19+
| |_________________________________^ conflicting implementation for `for<'a> fn(&'a (), ())`
20+
|
21+
= note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details
22+
23+
error: aborting due to previous error
24+
25+
For more information about this error, try `rustc --explain E0119`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
2+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!3_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
3+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
4+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!3_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
5+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
6+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!3_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
7+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, ReLateBound(DebruijnIndex(0), BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
8+
WARN rustc_infer::infer::generalize may incompletely handle alias type: Alias(Projection, AliasTy { args: [*const ?1t, RePlaceholder(!3_BoundRegion { var: 0, kind: BrNamed(DefId(0:27 ~ associated_type[f554]::{impl#3}::'a#1), 'a) })], def_id: DefId(0:5 ~ associated_type[f554]::ToUnit::Unit) })
9+
error[E0119]: conflicting implementations of trait `Overlap<for<'a> fn(&'a (), _)>` for type `for<'a> fn(&'a (), _)`
10+
--> $DIR/associated-type.rs:31:1
11+
|
12+
LL | impl<T> Overlap<T> for T {
13+
| ------------------------ first implementation here
14+
...
15+
LL | / impl<T> Overlap<for<'a> fn(&'a (), Assoc<'a, T>)> for T
16+
LL | |
17+
LL | | where
18+
LL | | for<'a> *const T: ToUnit<'a>,
19+
| |_________________________________^ conflicting implementation for `for<'a> fn(&'a (), _)`
20+
|
21+
= note: this behavior recently changed as a result of a bug fix; see rust-lang/rust#56105 for details
22+
23+
error: aborting due to previous error
24+
25+
For more information about this error, try `rustc --explain E0119`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// revisions: old next
2+
//[next] compile-flags: -Ztrait-solver=next
3+
4+
// A regression test for #105787
5+
6+
// Using the higher ranked projection hack to prevent us from replacing the projection
7+
// with an inference variable.
8+
trait ToUnit<'a> {
9+
type Unit;
10+
}
11+
12+
struct LocalTy;
13+
impl<'a> ToUnit<'a> for *const LocalTy {
14+
type Unit = ();
15+
}
16+
17+
impl<'a, T: Copy + ?Sized> ToUnit<'a> for *const T {
18+
type Unit = ();
19+
}
20+
21+
trait Overlap<T> {
22+
type Assoc;
23+
}
24+
25+
type Assoc<'a, T> = <*const T as ToUnit<'a>>::Unit;
26+
27+
impl<T> Overlap<T> for T {
28+
type Assoc = usize;
29+
}
30+
31+
impl<T> Overlap<for<'a> fn(&'a (), Assoc<'a, T>)> for T
32+
//~^ ERROR conflicting implementations of trait
33+
where
34+
for<'a> *const T: ToUnit<'a>,
35+
{
36+
type Assoc = Box<usize>;
37+
}
38+
39+
fn foo<T: Overlap<U>, U>(x: T::Assoc) -> T::Assoc {
40+
x
41+
}
42+
43+
fn main() {
44+
foo::<for<'a> fn(&'a (), ()), for<'a> fn(&'a (), ())>(3usize);
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0119]: conflicting implementations of trait `Trait<Alias<_>>` for type `Alias<_>`
2+
--> $DIR/opaques.rs:29:1
3+
|
4+
LL | impl<T> Trait<T> for T {
5+
| ---------------------- first implementation here
6+
...
7+
LL | impl<T> Trait<T> for defining_scope::Alias<T> {
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Alias<_>`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0119`.
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//revisions: old next
2+
//[next] compile-flags: -Ztrait-solver=next
3+
4+
// A regression test for #105787
5+
6+
//[old] known-bug: #105787
7+
//[old] check-pass
8+
#![feature(type_alias_impl_trait)]
9+
mod defining_scope {
10+
use super::*;
11+
pub type Alias<T> = impl Sized;
12+
13+
pub fn cast<T>(x: Container<Alias<T>, T>) -> Container<T, T> {
14+
x
15+
}
16+
}
17+
18+
struct Container<T: Trait<U>, U> {
19+
x: <T as Trait<U>>::Assoc,
20+
}
21+
22+
trait Trait<T> {
23+
type Assoc;
24+
}
25+
26+
impl<T> Trait<T> for T {
27+
type Assoc = Box<u32>;
28+
}
29+
impl<T> Trait<T> for defining_scope::Alias<T> {
30+
//[next]~^ ERROR conflicting implementations of trait
31+
type Assoc = usize;
32+
}
33+
34+
fn main() {
35+
let x: Box<u32> = defining_scope::cast::<()>(Container { x: 0 }).x;
36+
println!("{}", *x);
37+
}

0 commit comments

Comments
 (0)