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
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_index::bit_set::DenseBitSet;
use rustc_index::interval::IntervalSet;
use rustc_infer::infer::canonical::QueryRegionConstraints;
use rustc_infer::infer::outlives::for_liveness;
use rustc_middle::mir::{BasicBlock, Body, ConstraintCategory, HasLocalDecls, Local, Location};
use rustc_middle::traits::query::DropckOutlivesResult;
use rustc_middle::ty::relate::Relate;
Expand All @@ -14,6 +13,7 @@ use rustc_mir_dataflow::{Analysis, ResultsCursor};
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::traits::ObligationCtxt;
use rustc_trait_selection::traits::outlives_for_liveness::FreeRegionsVisitor;
use rustc_trait_selection::traits::query::dropck_outlives;
use rustc_trait_selection::traits::query::type_op::{DropckOutlives, TypeOp, TypeOpOutput};
use tracing::debug;
Expand Down Expand Up @@ -611,7 +611,7 @@ impl<'tcx> LivenessContext<'_, '_, 'tcx> {
values::pretty_print_points(location_map, live_at.iter()),
);

value.visit_with(&mut for_liveness::FreeRegionsVisitor {
value.visit_with(&mut FreeRegionsVisitor {
tcx: typeck.tcx(),
param_env: typeck.infcx.param_env,
op: |r| {
Expand Down
70 changes: 4 additions & 66 deletions compiler/rustc_hir_analysis/src/check/wfcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{DefId, LocalDefId};
use rustc_hir::lang_items::LangItem;
use rustc_hir::{AmbigArg, ItemKind, find_attr};
use rustc_infer::infer::TyCtxtInferExt;
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{self, InferCtxt, SubregionOrigin, TyCtxtInferExt};
use rustc_infer::traits::PredicateObligations;
use rustc_lint_defs::builtin::SHADOWING_SUPERTRAIT_ITEMS;
use rustc_macros::Diagnostic;
Expand All @@ -30,7 +30,9 @@ use rustc_middle::{bug, span_bug};
use rustc_session::errors::feature_err;
use rustc_span::{DUMMY_SP, Span, sym};
use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
use rustc_trait_selection::regions::{InferCtxtRegionExt, OutlivesEnvironmentBuildExt};
use rustc_trait_selection::regions::{
InferCtxtRegionExt, OutlivesEnvironmentBuildExt, region_known_to_outlive, ty_known_to_outlive,
};
use rustc_trait_selection::traits::misc::{
ConstParamTyImplementationError, type_allowed_to_implement_const_param_ty,
};
Expand Down Expand Up @@ -695,70 +697,6 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
Some(bounds)
}

/// Given a known `param_env` and a set of well formed types, can we prove that
/// `ty` outlives `region`.
fn ty_known_to_outlive<'tcx>(
tcx: TyCtxt<'tcx>,
id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>,
ty: Ty<'tcx>,
region: ty::Region<'tcx>,
) -> bool {
test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
infcx.register_type_outlives_constraint_inner(infer::TypeOutlivesConstraint {
sub_region: region,
sup_type: ty,
origin: SubregionOrigin::RelateParamBound(DUMMY_SP, ty, None),
});
})
}

/// Given a known `param_env` and a set of well formed types, can we prove that
/// `region_a` outlives `region_b`
fn region_known_to_outlive<'tcx>(
tcx: TyCtxt<'tcx>,
id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>,
region_a: ty::Region<'tcx>,
region_b: ty::Region<'tcx>,
) -> bool {
test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
infcx.sub_regions(
SubregionOrigin::RelateRegionParamBound(DUMMY_SP, None),
region_b,
region_a,
ty::VisibleForLeakCheck::Unreachable,
);
})
}

/// Given a known `param_env` and a set of well formed types, set up an
/// `InferCtxt`, call the passed function (to e.g. set up region constraints
/// to be tested), then resolve region and return errors
fn test_region_obligations<'tcx>(
tcx: TyCtxt<'tcx>,
id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>,
add_constraints: impl FnOnce(&InferCtxt<'tcx>),
) -> bool {
// Unfortunately, we have to use a new `InferCtxt` each call, because
// region constraints get added and solved there and we need to test each
// call individually.
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());

add_constraints(&infcx);

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

// If we were able to prove that the type outlives the region without
// an error, it must be because of the implied or explicit bounds...
errors.is_empty()
}

/// TypeVisitor that looks for uses of GATs like
/// `<P0 as Trait<P1..Pn>>::GAT<Pn..Pm>` and adds the arguments `P0..Pm` into
/// the two vectors, `regions` and `types` (depending on their kind). For each
Expand Down
111 changes: 0 additions & 111 deletions compiler/rustc_infer/src/infer/outlives/for_liveness.rs

This file was deleted.

1 change: 0 additions & 1 deletion compiler/rustc_infer/src/infer/outlives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use crate::infer::lexical_region_resolve;
use crate::infer::region_constraints::ConstraintKind;

pub mod env;
pub mod for_liveness;
pub mod obligations;
pub mod test_type_match;
pub(crate) mod verify;
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ provide! { tcx, def_id, other, cdata,
}
anon_const_kind => { table }
const_of_item => { table }
live_regions_for_opaque_from_outlives_bounds => { table }
}

pub(in crate::rmeta) fn provide(providers: &mut Providers) {
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
record_defaulted_array!(self.tables.explicit_implied_const_bounds[def_id]
<- tcx.explicit_implied_const_bounds(def_id).skip_binder());
}
record!(self.tables.live_regions_for_opaque_from_outlives_bounds[def_id] <- tcx.live_regions_for_opaque_from_outlives_bounds(def_id));
}
if let DefKind::AnonConst = def_kind {
record!(self.tables.anon_const_kind[def_id] <- self.tcx.anon_const_kind(def_id));
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ define_tables! {
anon_const_kind: Table<DefIndex, LazyValue<ty::AnonConstKind>>,
const_of_item: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::Const<'static>>>>,
associated_types_for_impl_traits_in_trait_or_impl: Table<DefIndex, LazyValue<DefIdMap<Vec<DefId>>>>,
live_regions_for_opaque_from_outlives_bounds: Table<DefIndex, LazyValue<Option<ty::EarlyBinder<'static, Vec<ty::Region<'static>>>>>>,
}

#[derive(TyEncodable, TyDecodable)]
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_metadata/src/rmeta/parameterized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::Hash;
use rustc_data_structures::unord::UnordMap;
use rustc_hir::def_id::DefIndex;
use rustc_index::{Idx, IndexVec};
use rustc_middle::ty::{Binder, EarlyBinder};
use rustc_middle::ty::{Binder, EarlyBinder, Region};
use rustc_span::Symbol;

use crate::rmeta::{LazyArray, LazyValue};
Expand Down Expand Up @@ -48,6 +48,10 @@ impl<T: ParameterizedOverTcx> ParameterizedOverTcx for LazyArray<T> {
type Value<'tcx> = LazyArray<T::Value<'tcx>>;
}

impl ParameterizedOverTcx for Region<'static> {
type Value<'tcx> = Region<'tcx>;
}

macro_rules! trivially_parameterized_over_tcx {
($($ty:ty),+ $(,)?) => {
$(
Expand Down
18 changes: 18 additions & 0 deletions compiler/rustc_middle/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2118,6 +2118,24 @@ rustc_queries! {
desc { "listing captured lifetimes for opaque `{}`", tcx.def_path_str(def_id) }
}

/// For an opaque type, return the list of potentially live args from the set of outlives bounds on that opaque.
/// ```ignore (illustrative)
/// // Edition 2024: all args are captured
/// fn foo<'a, 'b, T: 'static>(&'a &'b T) -> impl Sized + 'a {}
/// fn bar<'a, 'b, T: 'static>(&'a &'b T) -> impl Sized + 'static {}
/// fn baz<'a, 'b, T: 'static>(&'a &'b T) -> impl Sized {}
/// ```
///
/// In the above:
/// - `foo` outlives `'a`, but we know that `'b: 'a` holds, so `'b` is *also* potentially live
/// - `bar` outlives `'static`, so we know that all lifetimes are potentially live and we can return an empty set
/// - `baz` has not outlives bound, so return `None` and let the caller decide what to do
query live_regions_for_opaque_from_outlives_bounds(def_id: DefId) -> &'tcx Option<ty::EarlyBinder<'tcx, Vec<ty::Region<'tcx>>>> {
arena_cache
desc { "identifying live args for opaque `{}`", tcx.def_path_str(def_id) }
separate_provide_extern
}

/// Computes the visibility of the provided `def_id`.
///
/// If the item from the `def_id` doesn't have a visibility, it will panic. For example
Expand Down
73 changes: 70 additions & 3 deletions compiler/rustc_trait_selection/src/regions.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use rustc_data_structures::fx::FxIndexSet;
use rustc_hir::def_id::LocalDefId;
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
use rustc_infer::infer::{InferCtxt, RegionResolutionError};
use rustc_infer::infer::{
InferCtxt, RegionResolutionError, SubregionOrigin, TyCtxtInferExt, TypeOutlivesConstraint,
};
use rustc_macros::extension;
use rustc_middle::traits::ObligationCause;
use rustc_middle::traits::query::NoSolution;
use rustc_middle::ty::{self, Ty, Unnormalized, elaborate};
use rustc_span::Span;
use rustc_middle::ty::{self, Ty, TyCtxt, TypingMode, Unnormalized, elaborate};
use rustc_span::{DUMMY_SP, Span};

use crate::traits::ScrubbedTraitError;
use crate::traits::outlives_bounds::InferCtxtExt;
Expand Down Expand Up @@ -120,3 +123,67 @@ impl<'tcx> InferCtxt<'tcx> {
)
}
}

/// Given a known `param_env` and a set of well formed types, can we prove that
/// `ty` outlives `region`.
pub fn ty_known_to_outlive<'tcx>(
tcx: TyCtxt<'tcx>,
id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>,
ty: 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),
});
})
}

/// Given a known `param_env` and a set of well formed types, can we prove that
/// `region_a` outlives `region_b`
pub fn region_known_to_outlive<'tcx>(
tcx: TyCtxt<'tcx>,
id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>,
region_a: ty::Region<'tcx>,
region_b: ty::Region<'tcx>,
) -> bool {
test_region_obligations(tcx, id, param_env, wf_tys, |infcx| {
infcx.sub_regions(
SubregionOrigin::RelateRegionParamBound(DUMMY_SP, None),
region_b,
region_a,
ty::VisibleForLeakCheck::Unreachable,
);
})
}

/// Given a known `param_env` and a set of well formed types, set up an
/// `InferCtxt`, call the passed function (to e.g. set up region constraints
/// to be tested), then resolve region and return errors
pub fn test_region_obligations<'tcx>(
tcx: TyCtxt<'tcx>,
id: LocalDefId,
param_env: ty::ParamEnv<'tcx>,
wf_tys: &FxIndexSet<Ty<'tcx>>,
add_constraints: impl FnOnce(&InferCtxt<'tcx>),
) -> bool {
// Unfortunately, we have to use a new `InferCtxt` each call, because
// region constraints get added and solved there and we need to test each
// call individually.
let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());

add_constraints(&infcx);

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

// If we were able to prove that the type outlives the region without
// an error, it must be because of the implied or explicit bounds...
errors.is_empty()
}
Loading
Loading