1
Fork 0

use TypingEnv when no infcx is available

the behavior of the type system not only depends on the current
assumptions, but also the currentnphase of the compiler. This is
mostly necessary as we need to decide whether and how to reveal
opaque types. We track this via the `TypingMode`.
This commit is contained in:
lcnr 2024-11-15 13:53:31 +01:00
parent bf6adec108
commit 9cba14b95b
240 changed files with 1739 additions and 1340 deletions

View file

@ -528,7 +528,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
// Find the method being called.
let Ok(Some(instance)) = ty::Instance::try_resolve(
tcx,
ctxt.param_env,
self.cx.typing_env(ctxt.param_env),
ctxt.assoc_item.def_id,
self.cx.resolve_vars_if_possible(ctxt.args),
) else {

View file

@ -2079,7 +2079,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
cand.trait_ref = self
.tcx
.try_normalize_erasing_regions(
self.tcx.param_env(cand.impl_def_id),
ty::TypingEnv::non_body_analysis(self.tcx, cand.impl_def_id),
cand.trait_ref,
)
.unwrap_or(cand.trait_ref);

View file

@ -513,7 +513,7 @@ fn check_receiver_correct<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, method:
let method_def_id = method.def_id;
let sig = tcx.fn_sig(method_def_id).instantiate_identity();
let param_env = tcx.param_env(method_def_id);
let typing_env = ty::TypingEnv::non_body_analysis(tcx, method_def_id);
let receiver_ty = tcx.liberate_late_bound_regions(method_def_id, sig.input(0));
if receiver_ty == tcx.types.self_param {
@ -523,7 +523,7 @@ fn check_receiver_correct<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, method:
// e.g., `Rc<()>`
let unit_receiver_ty = receiver_for_self_ty(tcx, receiver_ty, tcx.types.unit, method_def_id);
match tcx.layout_of(param_env.and(unit_receiver_ty)).map(|l| l.backend_repr) {
match tcx.layout_of(typing_env.as_query_input(unit_receiver_ty)).map(|l| l.backend_repr) {
Ok(BackendRepr::Scalar(..)) => (),
abi => {
tcx.dcx().span_delayed_bug(
@ -538,7 +538,7 @@ fn check_receiver_correct<'tcx>(tcx: TyCtxt<'tcx>, trait_def_id: DefId, method:
// e.g., `Rc<dyn Trait>`
let trait_object_receiver =
receiver_for_self_ty(tcx, receiver_ty, trait_object_ty, method_def_id);
match tcx.layout_of(param_env.and(trait_object_receiver)).map(|l| l.backend_repr) {
match tcx.layout_of(typing_env.as_query_input(trait_object_receiver)).map(|l| l.backend_repr) {
Ok(BackendRepr::ScalarPair(..)) => (),
abi => {
tcx.dcx().span_delayed_bug(

View file

@ -611,11 +611,12 @@ pub fn try_evaluate_const<'tcx>(
//
// FIXME: `const_eval_resolve_for_typeck` should probably just set the env to `Reveal::All`
// instead of having this logic here
let env = tcx.erase_regions(param_env).with_reveal_all_normalized(tcx);
let typing_env =
tcx.erase_regions(infcx.typing_env(param_env)).with_reveal_all_normalized(tcx);
let erased_uv = tcx.erase_regions(uv);
use rustc_middle::mir::interpret::ErrorHandled;
match tcx.const_eval_resolve_for_typeck(env, erased_uv, DUMMY_SP) {
match tcx.const_eval_resolve_for_typeck(typing_env, erased_uv, DUMMY_SP) {
Ok(Ok(val)) => Ok(ty::Const::new_value(
tcx,
val,

View file

@ -144,7 +144,14 @@ pub fn compute_dropck_outlives_inner<'tcx>(
result.overflows.len(),
ty_stack.len()
);
dtorck_constraint_for_ty_inner(tcx, param_env, DUMMY_SP, depth, ty, &mut constraints)?;
dtorck_constraint_for_ty_inner(
tcx,
ocx.infcx.typing_env(param_env),
DUMMY_SP,
depth,
ty,
&mut constraints,
)?;
// "outlives" represent types/regions that may be touched
// by a destructor.
@ -196,10 +203,10 @@ pub fn compute_dropck_outlives_inner<'tcx>(
/// Returns a set of constraints that needs to be satisfied in
/// order for `ty` to be valid for destruction.
#[instrument(level = "debug", skip(tcx, param_env, span, constraints))]
#[instrument(level = "debug", skip(tcx, typing_env, span, constraints))]
pub fn dtorck_constraint_for_ty_inner<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
typing_env: ty::TypingEnv<'tcx>,
span: Span,
depth: usize,
ty: Ty<'tcx>,
@ -234,20 +241,20 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
ty::Pat(ety, _) | ty::Array(ety, _) | ty::Slice(ety) => {
// single-element containers, behave like their element
rustc_data_structures::stack::ensure_sufficient_stack(|| {
dtorck_constraint_for_ty_inner(tcx, param_env, span, depth + 1, *ety, constraints)
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, *ety, constraints)
})?;
}
ty::Tuple(tys) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in tys.iter() {
dtorck_constraint_for_ty_inner(tcx, param_env, span, depth + 1, ty, constraints)?;
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints)?;
}
Ok::<_, NoSolution>(())
})?,
ty::Closure(_, args) => rustc_data_structures::stack::ensure_sufficient_stack(|| {
for ty in args.as_closure().upvar_tys() {
dtorck_constraint_for_ty_inner(tcx, param_env, span, depth + 1, ty, constraints)?;
dtorck_constraint_for_ty_inner(tcx, typing_env, span, depth + 1, ty, constraints)?;
}
Ok::<_, NoSolution>(())
})?,
@ -257,7 +264,7 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
for ty in args.as_coroutine_closure().upvar_tys() {
dtorck_constraint_for_ty_inner(
tcx,
param_env,
typing_env,
span,
depth + 1,
ty,
@ -296,7 +303,7 @@ pub fn dtorck_constraint_for_ty_inner<'tcx>(
// While we conservatively assume that all coroutines require drop
// to avoid query cycles during MIR building, we can check the actual
// witness during borrowck to avoid unnecessary liveness constraints.
if args.witness().needs_drop(tcx, tcx.erase_regions(param_env)) {
if args.witness().needs_drop(tcx, tcx.erase_regions(typing_env)) {
constraints.outlives.extend(args.upvar_tys().iter().map(ty::GenericArg::from));
constraints.outlives.push(args.resume_ty().into());
}

View file

@ -1224,16 +1224,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// The regions of a type don't affect the size of the type
let tcx = self.tcx();
let self_ty = tcx.instantiate_bound_regions_with_erased(obligation.predicate.self_ty());
// We should erase regions from both the param-env and type, since both
// may have infer regions. Specifically, after canonicalizing and instantiating,
// early bound regions turn into region vars in both the new and old solver.
let key = tcx.erase_regions(obligation.param_env.and(self_ty));
// But if there are inference variables, we have to wait until it's resolved.
if key.has_non_region_infer() {
if (obligation.param_env, self_ty).has_non_region_infer() {
candidates.ambiguous = true;
return;
}
// We should erase regions from both the param-env and type, since both
// may have infer regions. Specifically, after canonicalizing and instantiating,
// early bound regions turn into region vars in both the new and old solver.
let key = self.infcx.pseudo_canonicalize_query(
tcx.erase_regions(obligation.param_env),
tcx.erase_regions(self_ty),
);
if let Ok(layout) = tcx.layout_of(key)
&& layout.layout.is_pointer_like(&tcx.data_layout)
{

View file

@ -276,8 +276,10 @@ fn vtable_entries<'tcx>(
// The trait type may have higher-ranked lifetimes in it;
// erase them if they appear, so that we get the type
// at some particular call site.
let args =
tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), args);
let args = tcx.normalize_erasing_late_bound_regions(
ty::TypingEnv::fully_monomorphized(),
args,
);
// It's possible that the method relies on where-clauses that
// do not hold for this particular set of type parameters.
@ -294,7 +296,7 @@ fn vtable_entries<'tcx>(
let instance = ty::Instance::expect_resolve_for_vtable(
tcx,
ty::ParamEnv::reveal_all(),
ty::TypingEnv::fully_monomorphized(),
def_id,
args,
DUMMY_SP,