Skip to content

Commit

Permalink
Rollup merge of #112493 - fmease:iat-select-complete-bound-var-erasur…
Browse files Browse the repository at this point in the history
…e, r=compiler-errors

iat selection: normalize self ty & completely erase bound vars

Erase bound vars (most notably late-bound regions) irrespective of their binding level instead of just at the innermost one.
Fixes #111404.
  • Loading branch information
matthiaskrgr committed Jun 10, 2023
2 parents fcf621e + a995255 commit e9666d4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 19 deletions.
76 changes: 57 additions & 19 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@ use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::intravisit::{walk_generics, Visitor as _};
use rustc_hir::{GenericArg, GenericArgs, OpaqueTyOrigin};
use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
use rustc_infer::infer::{InferCtxt, InferOk, TyCtxtInferExt};
use rustc_infer::traits::ObligationCause;
use rustc_middle::middle::stability::AllowUnstable;
use rustc_middle::ty::fold::FnMutDelegate;
use rustc_middle::ty::subst::{self, GenericArgKind, InternalSubsts, SubstsRef};
use rustc_middle::ty::GenericParamDefKind;
use rustc_middle::ty::{self, Const, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
Expand All @@ -43,7 +42,10 @@ use rustc_trait_selection::traits::error_reporting::{
report_object_safety_error, suggestions::NextTypeParamName,
};
use rustc_trait_selection::traits::wf::object_region_bounds;
use rustc_trait_selection::traits::{self, astconv_object_safety_violations, ObligationCtxt};
use rustc_trait_selection::traits::{
self, astconv_object_safety_violations, NormalizeExt, ObligationCtxt,
};
use rustc_type_ir::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};

use smallvec::{smallvec, SmallVec};
use std::collections::BTreeSet;
Expand Down Expand Up @@ -2442,6 +2444,11 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
return Ok(None);
}

if !tcx.features().inherent_associated_types {
tcx.sess
.delay_span_bug(span, "found inherent assoc type without the feature being gated");
}

//
// Select applicable inherent associated type candidates modulo regions.
//
Expand All @@ -2465,30 +2472,61 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {

let mut fulfillment_errors = Vec::new();
let mut applicable_candidates: Vec<_> = infcx.probe(|_| {
let universe = infcx.create_next_universe();

// Regions are not considered during selection.
// FIXME(non_lifetime_binders): Here we are "truncating" or "flattening" the universes
// of type and const binders. Is that correct in the selection phase? See also #109505.
let self_ty = tcx.replace_escaping_bound_vars_uncached(
self_ty,
FnMutDelegate {
regions: &mut |_| tcx.lifetimes.re_erased,
types: &mut |bv| {
tcx.mk_placeholder(ty::PlaceholderType { universe, bound: bv })
},
consts: &mut |bv, ty| {
tcx.mk_const(ty::PlaceholderConst { universe, bound: bv }, ty)
},
},
);
let self_ty = self_ty
.fold_with(&mut BoundVarEraser { tcx, universe: infcx.create_next_universe() });

struct BoundVarEraser<'tcx> {
tcx: TyCtxt<'tcx>,
universe: ty::UniverseIndex,
}

// FIXME(non_lifetime_binders): Don't assign the same universe to each placeholder.
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for BoundVarEraser<'tcx> {
fn interner(&self) -> TyCtxt<'tcx> {
self.tcx
}

fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> {
if r.is_late_bound() { self.tcx.lifetimes.re_erased } else { r }
}

fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
match *ty.kind() {
ty::Bound(_, bv) => self.tcx.mk_placeholder(ty::PlaceholderType {
universe: self.universe,
bound: bv,
}),
_ => ty.super_fold_with(self),
}
}

fn fold_const(
&mut self,
ct: ty::Const<'tcx>,
) -> <TyCtxt<'tcx> as rustc_type_ir::Interner>::Const {
assert!(!ct.ty().has_escaping_bound_vars());

match ct.kind() {
ty::ConstKind::Bound(_, bv) => self.tcx.mk_const(
ty::PlaceholderConst { universe: self.universe, bound: bv },
ct.ty(),
),
_ => ct.super_fold_with(self),
}
}
}

let InferOk { value: self_ty, obligations } =
infcx.at(&cause, param_env).normalize(self_ty);

candidates
.iter()
.copied()
.filter(|&(impl_, _)| {
infcx.probe(|_| {
let ocx = ObligationCtxt::new_in_snapshot(&infcx);
ocx.register_obligations(obligations.clone());

let impl_substs = infcx.fresh_substs_for_item(span, impl_);
let impl_ty = tcx.type_of(impl_).subst(tcx, impl_substs);
Expand Down
14 changes: 14 additions & 0 deletions tests/ui/associated-inherent-types/issue-111404-0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass

#![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(&'a ())>::Assoc)>::Assoc)) {}

fn main() {}
13 changes: 13 additions & 0 deletions tests/ui/associated-inherent-types/issue-111404-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![feature(inherent_associated_types)]
#![allow(incomplete_features)]

struct Foo<T>(T);

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

fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
//~^ ERROR higher-ranked subtype error

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/associated-inherent-types/issue-111404-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: higher-ranked subtype error
--> $DIR/issue-111404-1.rs:10:1
|
LL | fn bar(_: fn(Foo<for<'b> fn(Foo<fn(&'b ())>::Assoc)>::Assoc)) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

0 comments on commit e9666d4

Please sign in to comment.