Skip to content

Commit

Permalink
Auto merge of #118261 - spastorino:fix-placeholder-replacer, r=compil…
Browse files Browse the repository at this point in the history
…er-errors

Make PlaceholderReplacer shallow_resolver and recur when infer vars

This makes resolve type and const infer vars resolve.

Given:

```rust
#![feature(inherent_associated_types)]
#![allow(incomplete_features)]

struct Foo<T>(T);

impl<'a> Foo<fn(&'a ())> {
    type Assoc = &'a ();
}

fn bar(_: for<'a> fn(Foo<fn(Foo<fn(&'static ())>::Assoc)>::Assoc)) {}

fn main() {}
```

We should normalize `for<'a> fn(Foo<fn(Foo<fn(&'static ())>::Assoc)>::Assoc)` to `for<'0> fn(&'1 ())` with `'1 == '0` and `'0 == 'static` constraints. We have to resolve `'1` to `'static` in the infcx associated to `PlaceholderReplacer`.

This is part of #118118 but unrelated to that PR.

r? `@compiler-errors` `@lcnr`
  • Loading branch information
bors committed Nov 25, 2023
2 parents 34c5ab9 + f57e184 commit b2e73e9
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
&mut self,
t: ty::Binder<'tcx, T>,
) -> ty::Binder<'tcx, T> {
if !t.has_placeholders() && !t.has_infer_regions() {
if !t.has_placeholders() && !t.has_infer() {
return t;
}
self.current_index.shift_in(1);
Expand Down Expand Up @@ -1048,6 +1048,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
}

fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
let ty = self.infcx.shallow_resolve(ty);
match *ty.kind() {
ty::Placeholder(p) => {
let replace_var = self.mapped_types.get(&p);
Expand All @@ -1063,16 +1064,23 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
);
Ty::new_bound(self.infcx.tcx, db, *replace_var)
}
None => ty,
None => {
if ty.has_infer() {
ty.super_fold_with(self)
} else {
ty
}
}
}
}

_ if ty.has_placeholders() || ty.has_infer_regions() => ty.super_fold_with(self),
_ if ty.has_placeholders() || ty.has_infer() => ty.super_fold_with(self),
_ => ty,
}
}

fn fold_const(&mut self, ct: ty::Const<'tcx>) -> ty::Const<'tcx> {
let ct = self.infcx.shallow_resolve(ct);
if let ty::ConstKind::Placeholder(p) = ct.kind() {
let replace_var = self.mapped_consts.get(&p);
match replace_var {
Expand All @@ -1087,7 +1095,13 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
);
ty::Const::new_bound(self.infcx.tcx, db, *replace_var, ct.ty())
}
None => ct,
None => {
if ct.has_infer() {
ct.super_fold_with(self)
} else {
ct
}
}
}
} else {
ct.super_fold_with(self)
Expand Down

0 comments on commit b2e73e9

Please sign in to comment.