Skip to content
Open
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
20 changes: 16 additions & 4 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,9 +520,14 @@ pub(crate) fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId)
b,
)
}
ty::ClauseKind::TypeOutlives(ty::OutlivesClause(a, b)) => {
!ty_known_to_outlive(tcx, gat_def_id, param_env, &FxIndexSet::default(), a, b)
}
ty::ClauseKind::TypeOutlives(ty::OutlivesClause(a, b)) => !ty_known_to_outlive(
tcx,
gat_def_id,
param_env,
&FxIndexSet::default(),
Unnormalized::new_wip(a),
b,
),
_ => bug!("Unexpected ClauseKind"),
})
.map(|clause| clause.to_string())
Expand Down Expand Up @@ -627,7 +632,14 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
// reflected in a where clause on the GAT itself.
for (ty, ty_idx) in &types {
// In our example, requires that `Self: 'a`
if ty_known_to_outlive(tcx, item_def_id, param_env, wf_tys, *ty, *region_a) {
if ty_known_to_outlive(
tcx,
item_def_id,
param_env,
wf_tys,
Unnormalized::new_wip(*ty),
*region_a,
) {
debug!(?ty_idx, ?region_a_idx);
debug!("required clause: {ty} must outlive {region_a}");
// Translate into the generic parameters of the GAT. In
Expand Down
42 changes: 33 additions & 9 deletions compiler/rustc_trait_selection/src/regions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ use rustc_infer::infer::{
InferCtxt, RegionResolutionError, SubregionOrigin, TyCtxtInferExt, TypeOutlivesConstraint,
};
use rustc_macros::extension;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypingMode, elaborate};
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt, TypingMode, Unnormalized, elaborate};
use rustc_span::DUMMY_SP;

use crate::traits::ScrubbedTraitError;
use crate::traits::outlives_bounds::InferCtxtExt;

#[extension(pub trait OutlivesEnvironmentBuildExt<'tcx>)]
Expand Down Expand Up @@ -88,16 +90,38 @@ pub fn ty_known_to_outlive<'tcx>(
id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>,
ty: Ty<'tcx>,
ty: Unnormalized<'tcx, Ty<'tcx>>,
region: ty::Region<'tcx>,
) -> bool {
test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
infcx.register_type_outlives_constraint_inner(TypeOutlivesConstraint {
sub_region: region,
sup_type: ty,
origin: SubregionOrigin::RelateParamBound(DUMMY_SP, ty, None),
});
})
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());

let ty = ty.skip_norm_wip();

// Unlike ordinary `TypeOutlives` goals, this helper directly registers a
// region obligation. Normalize non-rigid aliases before lexical region
// solving, while avoiding trait solving for inputs without non-rigid aliases.
let ty = if infcx.next_trait_solver() && ty.has_non_rigid_aliases() {
let Ok(ty) = crate::solve::deeply_normalize::<_, ScrubbedTraitError<'tcx>>(
infcx.at(&ObligationCause::dummy_with_span(DUMMY_SP), param_env),
Unnormalized::new_wip(ty),
) else {
return false;
};
ty
} else {
ty
};

infcx.register_type_outlives_constraint_inner(TypeOutlivesConstraint {
sub_region: region,
sup_type: ty,
origin: SubregionOrigin::RelateParamBound(DUMMY_SP, ty, None),
});

let errors = infcx.resolve_regions(id, param_env, wf_tys.iter().copied());
tracing::debug!(?errors, "errors");

errors.is_empty()
}

/// Given a known `param_env` and a set of well formed types, can we prove that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub(crate) fn args_known_to_outlive_opaque_params<'tcx>(
def_id,
parent_param_env,
&wf_tys,
t,
ty::Unnormalized::new_wip(t),
*parent_outlived_region,
),
};
Expand Down Expand Up @@ -295,9 +295,14 @@ pub(crate) fn args_known_to_outlive_non_opaque_params<'tcx>(
ty::GenericArgKind::Lifetime(r) => {
region_known_to_outlive(tcx, def_id, param_env, &wf_tys, r, outlived_region)
}
ty::GenericArgKind::Type(t) => {
ty_known_to_outlive(tcx, def_id, param_env, &wf_tys, t, outlived_region)
}
ty::GenericArgKind::Type(t) => ty_known_to_outlive(
tcx,
def_id,
param_env,
&wf_tys,
ty::Unnormalized::new_wip(t),
outlived_region,
),
ty::GenericArgKind::Const(_) => false,
})
.collect();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//@ compile-flags: -Zassumptions-on-binders
//@ needs-rustc-debug-assertions
//@ normalize-stderr: "(\n)\n$" -> "$1"

// Regression test for #161067. A nested non-rigid alias must be normalized
// before it reaches lexical region solving through `ty_known_to_outlive`.

struct D;

trait Des {
type Out<'x, T>;
//~^ ERROR missing required bound on `Out`

fn des<'z>() -> Self::Out<'z, Self::Out<'z, D>>;
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: missing required bound on `Out`
--> $DIR/nested-gat-outlives-issue-161067.rs:11:5
|
LL | type Out<'x, T>;
| ^^^^^^^^^^^^^^^-
| |
| help: add the required where clause: `where T: 'x`
|
= note: this bound is currently required to ensure that impls have maximum flexibility
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information

error: aborting due to 1 previous error
Loading