Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't Fold Self-Referential ADTs in check_inferred_predicates() #123866

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions compiler/rustc_hir_analysis/src/outlives/implicit_infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ pub(super) fn infer_predicates(
// requirements for adt_def.
let field_ty = tcx.type_of(field_def.did).instantiate_identity();
let field_span = tcx.def_span(field_def.did);
let is_self_referential = is_self_referential(field_ty, adt_def);
insert_required_predicates_to_be_wf(
tcx,
field_ty,
field_span,
&global_inferred_outlives,
&mut item_required_predicates,
&mut explicit_map,
is_self_referential,
);
}
}
Expand All @@ -67,6 +69,7 @@ pub(super) fn infer_predicates(
&global_inferred_outlives,
&mut item_required_predicates,
&mut explicit_map,
IsSelfReferential::No,
);
}

Expand Down Expand Up @@ -104,6 +107,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
global_inferred_outlives: &FxIndexMap<DefId, ty::EarlyBinder<RequiredPredicates<'tcx>>>,
required_predicates: &mut RequiredPredicates<'tcx>,
explicit_map: &mut ExplicitPredicatesMap<'tcx>,
is_self_referential: IsSelfReferential,
) {
for arg in ty.walk() {
let leaf_ty = match arg.unpack() {
Expand Down Expand Up @@ -133,6 +137,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
args,
global_inferred_outlives,
required_predicates,
is_self_referential,
);
check_explicit_predicates(
tcx,
Expand All @@ -154,6 +159,7 @@ fn insert_required_predicates_to_be_wf<'tcx>(
alias.args,
global_inferred_outlives,
required_predicates,
is_self_referential,
);
check_explicit_predicates(
tcx,
Expand Down Expand Up @@ -324,6 +330,7 @@ fn check_inferred_predicates<'tcx>(
args: ty::GenericArgsRef<'tcx>,
global_inferred_outlives: &FxIndexMap<DefId, ty::EarlyBinder<RequiredPredicates<'tcx>>>,
required_predicates: &mut RequiredPredicates<'tcx>,
is_self_referential: IsSelfReferential,
) {
// Load the current set of inferred and explicit predicates from `global_inferred_outlives`
// and filter the ones that are `TypeOutlives`.
Expand All @@ -335,8 +342,13 @@ fn check_inferred_predicates<'tcx>(
for (&predicate, &span) in predicates.as_ref().skip_binder() {
// `predicate` is `U: 'b` in the example above.
// So apply the instantiation to get `T: 'a`.
let ty::OutlivesPredicate(arg, region) =
predicates.rebind(predicate).instantiate(tcx, args);
let ty::OutlivesPredicate(arg, region) = if is_self_referential.into() {
// However, for self referential ADTs, we don't instantiate
// to avoid issues like #118163
predicate
} else {
predicates.rebind(predicate).instantiate(tcx, args)
};
insert_outlives_predicate(tcx, arg, region, span, required_predicates);
}
}
40 changes: 39 additions & 1 deletion compiler/rustc_hir_analysis/src/outlives/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_data_structures::fx::FxIndexMap;
use rustc_infer::infer::outlives::components::{push_outlives_components, Component};
use rustc_middle::ty::{self, Region, Ty, TyCtxt};
use rustc_middle::ty::{self, Adt, AdtDef, Ref, Region, Ty, TyCtxt};
use rustc_middle::ty::{GenericArg, GenericArgKind};
use rustc_span::Span;
use smallvec::smallvec;
Expand Down Expand Up @@ -182,3 +182,41 @@ fn is_free_region(region: Region<'_>) -> bool {
}
}
}

#[derive(Copy, Clone)]
pub(crate) enum IsSelfReferential {
Yes,
No,
}

impl From<bool> for IsSelfReferential {
fn from(value: bool) -> Self {
match value {
true => IsSelfReferential::Yes,
false => IsSelfReferential::No,
}
}
}

impl From<IsSelfReferential> for bool {
fn from(value: IsSelfReferential) -> Self {
match value {
IsSelfReferential::Yes => true,
IsSelfReferential::No => false,
}
}
}

// Check if an ADT's field is a reference to the ADT itself.
// Example: `struct Recurse<'a, T>(T, &'a Recurse<'a, T>)`
// will return true
pub(crate) fn is_self_referential(field_ty: Ty<'_>, adt_def: AdtDef<'_>) -> IsSelfReferential {
if let Ref(_, ref_ty, _) = field_ty.kind()
&& let Adt(field_ty_adt, _) = ref_ty.kind()
{
field_ty_adt.did() == adt_def.did()
} else {
false
}
.into()
}
14 changes: 14 additions & 0 deletions tests/ui/regions/regions-outlives-for-recursive-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ check-pass

#![allow(dead_code)]

trait Bound {
type Assoc: Bound;
}

struct Recurse<'a, T: Bound> {
first: &'a T,
value: &'a Recurse<'a, T::Assoc>,
}

fn main() {}
Loading