Skip to content

Remove DefiningAnchor::Bubble from MIR validation #116803

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

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 9 additions & 1 deletion compiler/rustc_const_eval/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use rustc_index::IndexVec;
use rustc_middle::mir;
use rustc_middle::mir::interpret::{ErrorHandled, InvalidMetaKind, ReportedErrorInfo};
use rustc_middle::query::TyCtxtAt;
use rustc_middle::traits::DefiningAnchor;
use rustc_middle::ty::layout::{
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOf, LayoutOfHelpers,
TyAndLayout,
Expand Down Expand Up @@ -384,7 +385,14 @@ pub(super) fn mir_assign_valid_types<'tcx>(
// all normal lifetimes are erased, higher-ranked types with their
// late-bound lifetimes are still around and can lead to type
// differences.
if util::relate_types(tcx, param_env, Variance::Covariant, src.ty, dest.ty) {
if util::relate_types(
tcx,
param_env,
DefiningAnchor::Error,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CTFE is always performed with a Reveal::All param-env (or it should, as far as I'm aware?) so it makes sense to use DefiningAnchor::Error here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that's true? We run CTFE for const generics as well and the param-env is different there.
Cc @oli-obk @lcnr

Copy link
Member Author

@compiler-errors compiler-errors Oct 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My justification is wrong here. We're not using a Reveal::All param-env, but we have passed this body through the RevealAll mir-pass.

Not totally clear if that's sufficient, but if it isn't, then we don't have a test for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also approximate the defining anchor from the current stack frame. See newest commit for approach.

Copy link
Contributor

@lcnr lcnr Oct 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My justification is wrong here. We're not using a Reveal::All param-env, but we have passed this body through the RevealAll mir-pass.

Not totally clear if that's sufficient, but if it isn't, then we don't have a test for it.

This is a bit of a mess. We re-use the result from running with Reveal::UserFacing when running with Reveal::All in CTFE. However, afaik there is (and well... should be) no call site actually using Reveal::UserFacing. I think we should switch to always use Reveal::All:

  • using Reveal::UserFacing after revealing opaque types can result in weird errors and failures, as we end up trying to relate an opaque and its revealed (but normally hidden) type
  • when people call a const-function returning impl Iterator<Item = u32> they actually want to be able to use the returned value as an iterator. The check that we only treat the type opaquely happened in typeck but during codegen this behavior is undesirable. I haven't thought about specialization and default associated types yet, but also, idc. cc Stop using Reveal::All before borrowck #101478 where I tried to change const generics CTFE to use Reveal::UserFacing.

Variance::Covariant,
src.ty,
dest.ty,
) {
// Make sure the layout is equal, too -- just to be safe. Miri really
// needs layout equality. For performance reason we skip this check when
// the types are equal. Equal types *can* have different layouts when
Expand Down
17 changes: 16 additions & 1 deletion compiler/rustc_const_eval/src/transform/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_infer::traits::Reveal;
use rustc_middle::mir::interpret::Scalar;
use rustc_middle::mir::visit::{NonUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::traits::DefiningAnchor;
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeVisitableExt, Variance};
use rustc_mir_dataflow::impls::MaybeStorageLive;
use rustc_mir_dataflow::storage::always_storage_live_locals;
Expand Down Expand Up @@ -612,7 +613,17 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Variance::Covariant
};

crate::util::relate_types(self.tcx, self.param_env, variance, src, dest)
crate::util::relate_types(
self.tcx,
self.param_env,
DefiningAnchor::from_def_id_and_reveal(
self.body.source.def_id(),
self.param_env.reveal(),
),
variance,
src,
dest,
)
}
}

Expand Down Expand Up @@ -767,6 +778,10 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
if !relate_types(
self.tcx,
self.param_env,
DefiningAnchor::from_def_id_and_reveal(
self.body.source.def_id(),
self.param_env.reveal(),
),
Variance::Covariant,
ty,
place_ref.ty(&self.body.local_decls, self.tcx).ty,
Expand Down
37 changes: 21 additions & 16 deletions compiler/rustc_const_eval/src/util/compare_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rustc_trait_selection::traits::ObligationCtxt;
pub fn is_equal_up_to_subtyping<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
anchor: DefiningAnchor,
src: Ty<'tcx>,
dest: Ty<'tcx>,
) -> bool {
Expand All @@ -24,8 +25,8 @@ pub fn is_equal_up_to_subtyping<'tcx>(
}

// Check for subtyping in either direction.
relate_types(tcx, param_env, Variance::Covariant, src, dest)
|| relate_types(tcx, param_env, Variance::Covariant, dest, src)
relate_types(tcx, param_env, anchor, Variance::Covariant, src, dest)
|| relate_types(tcx, param_env, anchor, Variance::Covariant, dest, src)
}

/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
Expand All @@ -39,6 +40,7 @@ pub fn is_equal_up_to_subtyping<'tcx>(
pub fn relate_types<'tcx>(
tcx: TyCtxt<'tcx>,
param_env: ParamEnv<'tcx>,
anchor: DefiningAnchor,
variance: Variance,
src: Ty<'tcx>,
dest: Ty<'tcx>,
Expand All @@ -47,8 +49,7 @@ pub fn relate_types<'tcx>(
return true;
}

let mut builder =
tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(DefiningAnchor::Bubble);
let mut builder = tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(anchor);
let infcx = builder.build();
let ocx = ObligationCtxt::new(&infcx);
let cause = ObligationCause::dummy();
Expand All @@ -59,19 +60,23 @@ pub fn relate_types<'tcx>(
Err(_) => return false,
};
let errors = ocx.select_all_or_error();
// With `Reveal::All`, opaque types get normalized away, with `Reveal::UserFacing`
// we would get unification errors because we're unable to look into opaque types,
// even if they're constrained in our current function.
for (key, ty) in infcx.take_opaque_types() {
let hidden_ty = tcx.type_of(key.def_id).instantiate(tcx, key.args);
if hidden_ty != ty.hidden_type.ty {
span_bug!(
ty.hidden_type.span,
"{}, {}",
tcx.type_of(key.def_id).instantiate(tcx, key.args),
ty.hidden_type.ty
);

if anchor != DefiningAnchor::Error {
// With `Reveal::All`, opaque types get normalized away, with `Reveal::UserFacing`
Comment on lines +64 to +65
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The relationship between what the if is checking (DefiningAnchor, whatever that is) and what the comment talks about (Reveal::UserFacing/Reveal::All) is completely unclear.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I didn't touch that comment, but I'll rework it I guess.

// we would get unification errors because we're unable to look into opaque types,
// even if they're constrained in our current function.
for (key, ty) in infcx.take_opaque_types() {
let hidden_ty = tcx.type_of(key.def_id).instantiate(tcx, key.args);
if hidden_ty != ty.hidden_type.ty {
span_bug!(
ty.hidden_type.span,
"{}, {}",
tcx.type_of(key.def_id).instantiate(tcx, key.args),
ty.hidden_type.ty
);
}
}
}

errors.is_empty()
}
11 changes: 11 additions & 0 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,14 @@ pub enum DefiningAnchor {
/// Used to catch type mismatch errors when handling opaque types.
Error,
}

impl DefiningAnchor {
/// Mostly for use in the MIR validator, which only needs to pass a `Bind`
/// anchor if the reveal is not yet `All` and we're validating a local body.
pub fn from_def_id_and_reveal(body_def_id: DefId, reveal: Reveal) -> DefiningAnchor {
body_def_id
.as_local()
.filter(|_| reveal == Reveal::UserFacing)
.map_or(DefiningAnchor::Error, |def_id| DefiningAnchor::Bind(def_id))
}
}
4 changes: 4 additions & 0 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_index::Idx;
use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs};
use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::traits::DefiningAnchor;
use rustc_middle::ty::TypeVisitableExt;
use rustc_middle::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
use rustc_session::config::OptLevel;
Expand Down Expand Up @@ -221,6 +222,7 @@ impl<'tcx> Inliner<'tcx> {
if !util::relate_types(
self.tcx,
self.param_env,
DefiningAnchor::Error,
ty::Variance::Covariant,
output_type,
destination_ty,
Expand Down Expand Up @@ -257,6 +259,7 @@ impl<'tcx> Inliner<'tcx> {
if !util::relate_types(
self.tcx,
self.param_env,
DefiningAnchor::Error,
ty::Variance::Covariant,
input_type,
arg_ty,
Expand All @@ -272,6 +275,7 @@ impl<'tcx> Inliner<'tcx> {
if !util::relate_types(
self.tcx,
self.param_env,
DefiningAnchor::Error,
ty::Variance::Covariant,
input_type,
arg_ty,
Expand Down