Skip to content

Commit e8139df

Browse files
committed
IAT: Introduce AliasKind::Inherent
1 parent 6f8c055 commit e8139df

File tree

82 files changed

+1008
-167
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1008
-167
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2378,6 +2378,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23782378
return Ok(None);
23792379
}
23802380

2381+
//
2382+
// Select applicable inherent associated type candidates modulo regions.
2383+
//
2384+
23812385
// In contexts that have no inference context, just make a new one.
23822386
// We do need a local variable to store it, though.
23832387
let infcx_;
@@ -2390,14 +2394,18 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
23902394
}
23912395
};
23922396

2393-
let param_env = tcx.param_env(block.owner.to_def_id());
2397+
// FIXME(inherent_associated_types): Acquiring the ParamEnv this early leads to cycle errors
2398+
// when inside of an ADT (#108491) or where clause.
2399+
let param_env = tcx.param_env(block.owner);
23942400
let cause = ObligationCause::misc(span, block.owner.def_id);
23952401

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

24002406
// Regions are not considered during selection.
2407+
// FIXME(non_lifetime_binders): Here we are "truncating" or "flattening" the universes
2408+
// of type and const binders. Is that correct in the selection phase? See also #109505.
24012409
let self_ty = tcx.replace_escaping_bound_vars_uncached(
24022410
self_ty,
24032411
FnMutDelegate {
@@ -2413,41 +2421,40 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24132421

24142422
candidates
24152423
.iter()
2416-
.filter_map(|&(impl_, (assoc_item, def_scope))| {
2424+
.copied()
2425+
.filter(|&(impl_, _)| {
24172426
infcx.probe(|_| {
24182427
let ocx = ObligationCtxt::new_in_snapshot(&infcx);
24192428

2420-
let impl_ty = tcx.type_of(impl_);
24212429
let impl_substs = infcx.fresh_item_substs(impl_);
2422-
let impl_ty = impl_ty.subst(tcx, impl_substs);
2430+
let impl_ty = tcx.type_of(impl_).subst(tcx, impl_substs);
24232431
let impl_ty = ocx.normalize(&cause, param_env, impl_ty);
24242432

2425-
// Check that the Self-types can be related.
2426-
// FIXME(fmease): Should we use `eq` here?
2427-
ocx.sup(&ObligationCause::dummy(), param_env, impl_ty, self_ty).ok()?;
2433+
// Check that the self types can be related.
2434+
// FIXME(inherent_associated_types): Should we use `eq` here? Method probing uses
2435+
// `sup` for this situtation, too. What for? To constrain inference variables?
2436+
if ocx.sup(&ObligationCause::dummy(), param_env, impl_ty, self_ty).is_err()
2437+
{
2438+
return false;
2439+
}
24282440

24292441
// Check whether the impl imposes obligations we have to worry about.
2430-
let impl_bounds = tcx.predicates_of(impl_);
2431-
let impl_bounds = impl_bounds.instantiate(tcx, impl_substs);
2432-
2442+
let impl_bounds = tcx.predicates_of(impl_).instantiate(tcx, impl_substs);
24332443
let impl_bounds = ocx.normalize(&cause, param_env, impl_bounds);
2434-
24352444
let impl_obligations = traits::predicates_for_generics(
24362445
|_, _| cause.clone(),
24372446
param_env,
24382447
impl_bounds,
24392448
);
2440-
24412449
ocx.register_obligations(impl_obligations);
24422450

24432451
let mut errors = ocx.select_where_possible();
24442452
if !errors.is_empty() {
24452453
fulfillment_errors.append(&mut errors);
2446-
return None;
2454+
return false;
24472455
}
24482456

2449-
// FIXME(fmease): Unsolved vars can escape this InferCtxt snapshot.
2450-
Some((assoc_item, def_scope, infcx.resolve_vars_if_possible(impl_substs)))
2457+
true
24512458
})
24522459
})
24532460
.collect()
@@ -2456,24 +2463,26 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
24562463
if applicable_candidates.len() > 1 {
24572464
return Err(self.complain_about_ambiguous_inherent_assoc_type(
24582465
name,
2459-
applicable_candidates.into_iter().map(|(candidate, ..)| candidate).collect(),
2466+
applicable_candidates.into_iter().map(|(_, (candidate, _))| candidate).collect(),
24602467
span,
24612468
));
24622469
}
24632470

2464-
if let Some((assoc_item, def_scope, impl_substs)) = applicable_candidates.pop() {
2471+
if let Some((impl_, (assoc_item, def_scope))) = applicable_candidates.pop() {
24652472
self.check_assoc_ty(assoc_item, name, def_scope, block, span);
24662473

2467-
// FIXME(inherent_associated_types): To fully *confirm* the *probed* candidate, we still
2468-
// need to relate the Self-type with fresh item substs & register region obligations for
2469-
// regionck to prove/disprove.
2470-
2471-
let item_substs =
2472-
self.create_substs_for_associated_item(span, assoc_item, segment, impl_substs);
2474+
// FIXME(fmease): Currently creating throwaway `parent_substs` to please
2475+
// `create_substs_for_associated_item`. Modify the latter instead (or sth. similar) to
2476+
// not require the parent substs logic.
2477+
let parent_substs = InternalSubsts::identity_for_item(tcx, impl_);
2478+
let substs =
2479+
self.create_substs_for_associated_item(span, assoc_item, segment, parent_substs);
2480+
let substs = tcx.mk_substs_from_iter(
2481+
std::iter::once(ty::GenericArg::from(self_ty))
2482+
.chain(substs.into_iter().skip(parent_substs.len())),
2483+
);
24732484

2474-
// FIXME(fmease, #106722): Check if the bounds on the parameters of the
2475-
// associated type hold, if any.
2476-
let ty = tcx.type_of(assoc_item).subst(tcx, item_substs);
2485+
let ty = tcx.mk_alias(ty::Inherent, tcx.mk_alias_ty(assoc_item, substs));
24772486

24782487
return Ok(Some((ty, assoc_item)));
24792488
}

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ fn do_orphan_check_impl<'tcx>(
210210
NonlocalImpl::DisallowOther,
211211
),
212212

213+
// ```
214+
// struct S<T>(T);
215+
// impl<T: ?Sized> S<T> {
216+
// type This = T;
217+
// }
218+
// impl<T: ?Sized> AutoTrait for S<T>::This {}
219+
// ```
220+
// FIXME(inherent_associated_types): The example code above currently leads to a cycle
221+
ty::Alias(AliasKind::Inherent, _) => (
222+
LocalImpl::Disallow { problematic_kind: "associated type" },
223+
NonlocalImpl::DisallowOther,
224+
),
225+
213226
// type Opaque = impl Trait;
214227
// impl AutoTrait for Opaque {}
215228
ty::Alias(AliasKind::Opaque, _) => (

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1938,7 +1938,7 @@ fn is_late_bound_map(
19381938
ty::Param(param_ty) => {
19391939
self.arg_is_constrained[param_ty.index as usize] = true;
19401940
}
1941-
ty::Alias(ty::Projection, _) => return ControlFlow::Continue(()),
1941+
ty::Alias(ty::Projection | ty::Inherent, _) => return ControlFlow::Continue(()),
19421942
_ => (),
19431943
}
19441944
t.super_visit_with(self)

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
127127
// the def_id that this query was called with. We filter to only type and const args here
128128
// as a precaution for if it's ever allowed to elide lifetimes in GAT's. It currently isn't
129129
// but it can't hurt to be safe ^^
130-
if let ty::Alias(ty::Projection, projection) = ty.kind() {
130+
if let ty::Alias(ty::Projection | ty::Inherent, projection) = ty.kind() {
131131
let generics = tcx.generics_of(projection.def_id);
132132

133133
let arg_index = segment

compiler/rustc_hir_analysis/src/constrained_generic_params.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct ParameterCollector {
5959
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParameterCollector {
6060
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
6161
match *t.kind() {
62-
ty::Alias(ty::Projection, ..) if !self.include_nonconstraining => {
62+
ty::Alias(ty::Projection | ty::Inherent, ..) if !self.include_nonconstraining => {
6363
// projections are not injective
6464
return ControlFlow::Continue(());
6565
}

compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ fn insert_required_predicates_to_be_wf<'tcx>(
210210
);
211211
}
212212

213+
// FIXME(inherent_associated_types): Handle this case properly.
214+
ty::Alias(ty::Inherent, _) => {}
215+
213216
_ => {}
214217
}
215218
}

compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -843,7 +843,7 @@ fn find_param_in_ty<'tcx>(
843843
return true;
844844
}
845845
if let ty::GenericArgKind::Type(ty) = arg.unpack()
846-
&& let ty::Alias(ty::Projection, ..) = ty.kind()
846+
&& let ty::Alias(ty::Projection | ty::Inherent, ..) = ty.kind()
847847
{
848848
// This logic may seem a bit strange, but typically when
849849
// we have a projection type in a function signature, the

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ impl<'a, 'tcx> AstConv<'tcx> for FnCtxt<'a, 'tcx> {
300300
match ty.kind() {
301301
ty::Adt(adt_def, _) => Some(*adt_def),
302302
// FIXME(#104767): Should we handle bound regions here?
303-
ty::Alias(ty::Projection, _) if !ty.has_escaping_bound_vars() => {
303+
ty::Alias(ty::Projection | ty::Inherent, _) if !ty.has_escaping_bound_vars() => {
304304
self.normalize(span, ty).ty_adt_def()
305305
}
306306
_ => None,

compiler/rustc_hir_typeck/src/method/suggest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2211,7 +2211,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
22112211
| ty::Float(_)
22122212
| ty::Adt(_, _)
22132213
| ty::Str
2214-
| ty::Alias(ty::Projection, _)
2214+
| ty::Alias(ty::Projection | ty::Inherent, _)
22152215
| ty::Param(_) => format!("{deref_ty}"),
22162216
// we need to test something like <&[_]>::len or <(&[u32])>::len
22172217
// and Vec::function();

compiler/rustc_infer/src/infer/combine.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ impl<'tcx> InferCtxt<'tcx> {
125125
bug!()
126126
}
127127

128-
(_, ty::Alias(AliasKind::Projection, _)) | (ty::Alias(AliasKind::Projection, _), _)
128+
(_, ty::Alias(AliasKind::Projection | AliasKind::Inherent, _))
129+
| (ty::Alias(AliasKind::Projection | AliasKind::Inherent, _), _)
129130
if self.tcx.trait_solver_next() =>
130131
{
131132
relation.register_type_relate_obligation(a, b);

0 commit comments

Comments
 (0)