Skip to content

Commit 6aa611a

Browse files
committed
mv utility methods into separate module
1 parent 45f441a commit 6aa611a

File tree

6 files changed

+72
-65
lines changed

6 files changed

+72
-65
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::{
2323
MemPlaceMeta, Memory, MemoryKind, Operand, Place, PlaceTy, PointerArithmetic, Provenance,
2424
Scalar, StackPopJump,
2525
};
26-
use crate::transform::validate;
26+
use crate::util;
2727

2828
pub struct InterpCx<'mir, 'tcx, M: Machine<'mir, 'tcx>> {
2929
/// Stores the `Machine` instance.
@@ -355,7 +355,7 @@ pub(super) fn mir_assign_valid_types<'tcx>(
355355
// all normal lifetimes are erased, higher-ranked types with their
356356
// late-bound lifetimes are still around and can lead to type
357357
// differences.
358-
if validate::is_subtype(tcx, param_env, src.ty, dest.ty) {
358+
if util::is_subtype(tcx, param_env, src.ty, dest.ty) {
359359
// Make sure the layout is equal, too -- just to be safe. Miri really
360360
// needs layout equality. For performance reason we skip this check when
361361
// the types are equal. Equal types *can* have different layouts when

compiler/rustc_const_eval/src/transform/validate.rs

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
use rustc_data_structures::fx::FxHashSet;
44
use rustc_index::bit_set::BitSet;
5-
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
6-
use rustc_infer::traits::ObligationCause;
75
use rustc_middle::mir::interpret::Scalar;
86
use rustc_middle::mir::visit::NonUseContext::VarDebugInfo;
97
use rustc_middle::mir::visit::{PlaceContext, Visitor};
@@ -18,7 +16,6 @@ use rustc_mir_dataflow::impls::MaybeStorageLive;
1816
use rustc_mir_dataflow::storage::always_storage_live_locals;
1917
use rustc_mir_dataflow::{Analysis, ResultsCursor};
2018
use rustc_target::abi::{Size, VariantIdx};
21-
use rustc_trait_selection::traits::ObligationCtxt;
2219

2320
#[derive(Copy, Clone, Debug)]
2421
enum EdgeKind {
@@ -71,55 +68,6 @@ impl<'tcx> MirPass<'tcx> for Validator {
7168
}
7269
}
7370

74-
/// Returns whether the two types are equal up to subtyping.
75-
///
76-
/// This is used in case we don't know the expected subtyping direction
77-
/// and still want to check whether anything is broken.
78-
pub fn is_equal_up_to_subtyping<'tcx>(
79-
tcx: TyCtxt<'tcx>,
80-
param_env: ParamEnv<'tcx>,
81-
src: Ty<'tcx>,
82-
dest: Ty<'tcx>,
83-
) -> bool {
84-
// Fast path.
85-
if src == dest {
86-
return true;
87-
}
88-
89-
// Check for subtyping in either direction.
90-
is_subtype(tcx, param_env, src, dest) || is_subtype(tcx, param_env, dest, src)
91-
}
92-
93-
pub fn is_subtype<'tcx>(
94-
tcx: TyCtxt<'tcx>,
95-
param_env: ParamEnv<'tcx>,
96-
src: Ty<'tcx>,
97-
dest: Ty<'tcx>,
98-
) -> bool {
99-
if src == dest {
100-
return true;
101-
}
102-
103-
let mut builder =
104-
tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(DefiningAnchor::Bubble);
105-
let infcx = builder.build();
106-
let ocx = ObligationCtxt::new(&infcx);
107-
let cause = ObligationCause::dummy();
108-
let src = ocx.normalize(cause.clone(), param_env, src);
109-
let dest = ocx.normalize(cause.clone(), param_env, dest);
110-
let Ok(infer_ok) = infcx.at(&cause, param_env).sub(src, dest) else {
111-
return false;
112-
};
113-
let () = ocx.register_infer_ok_obligations(infer_ok);
114-
let errors = ocx.select_all_or_error();
115-
// With `Reveal::All`, opaque types get normalized away, with `Reveal::UserFacing`
116-
// we would get unification errors because we're unable to look into opaque types,
117-
// even if they're constrained in our current function.
118-
//
119-
// It seems very unlikely that this hides any bugs.
120-
let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
121-
errors.is_empty()
122-
}
12371
struct TypeChecker<'a, 'tcx> {
12472
when: &'a str,
12573
body: &'a Body<'tcx>,
@@ -195,7 +143,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
195143
return true;
196144
}
197145

198-
is_subtype(self.tcx, self.param_env, src, dest)
146+
crate::util::is_subtype(self.tcx, self.param_env, src, dest)
199147
}
200148
}
201149

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//! Routines to check for relations between fully inferred types.
2+
//!
3+
//! FIXME: Move this to a more general place. The utility of this extends to
4+
//! other areas of the compiler as well.
5+
6+
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
7+
use rustc_infer::traits::ObligationCause;
8+
use rustc_middle::ty::{ParamEnv, Ty, TyCtxt};
9+
use rustc_trait_selection::traits::ObligationCtxt;
10+
11+
/// Returns whether the two types are equal up to subtyping.
12+
///
13+
/// This is used in case we don't know the expected subtyping direction
14+
/// and still want to check whether anything is broken.
15+
pub fn is_equal_up_to_subtyping<'tcx>(
16+
tcx: TyCtxt<'tcx>,
17+
param_env: ParamEnv<'tcx>,
18+
src: Ty<'tcx>,
19+
dest: Ty<'tcx>,
20+
) -> bool {
21+
// Fast path.
22+
if src == dest {
23+
return true;
24+
}
25+
26+
// Check for subtyping in either direction.
27+
is_subtype(tcx, param_env, src, dest) || is_subtype(tcx, param_env, dest, src)
28+
}
29+
30+
/// Returns whether `src` is a subtype of `dest`, i.e. `src <: dest`.
31+
pub fn is_subtype<'tcx>(
32+
tcx: TyCtxt<'tcx>,
33+
param_env: ParamEnv<'tcx>,
34+
src: Ty<'tcx>,
35+
dest: Ty<'tcx>,
36+
) -> bool {
37+
if src == dest {
38+
return true;
39+
}
40+
41+
let mut builder =
42+
tcx.infer_ctxt().ignoring_regions().with_opaque_type_inference(DefiningAnchor::Bubble);
43+
let infcx = builder.build();
44+
let ocx = ObligationCtxt::new(&infcx);
45+
let cause = ObligationCause::dummy();
46+
let src = ocx.normalize(cause.clone(), param_env, src);
47+
let dest = ocx.normalize(cause.clone(), param_env, dest);
48+
let Ok(infer_ok) = infcx.at(&cause, param_env).sub(src, dest) else {
49+
return false;
50+
};
51+
let () = ocx.register_infer_ok_obligations(infer_ok);
52+
let errors = ocx.select_all_or_error();
53+
// With `Reveal::All`, opaque types get normalized away, with `Reveal::UserFacing`
54+
// we would get unification errors because we're unable to look into opaque types,
55+
// even if they're constrained in our current function.
56+
//
57+
// It seems very unlikely that this hides any bugs.
58+
let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
59+
errors.is_empty()
60+
}

compiler/rustc_const_eval/src/util/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ pub mod aggregate;
22
mod alignment;
33
mod call_kind;
44
pub mod collect_writes;
5+
mod compare_types;
56
mod find_self_call;
67
mod might_permit_raw_init;
78
mod type_name;
89

910
pub use self::aggregate::expand_aggregate;
1011
pub use self::alignment::is_disaligned;
1112
pub use self::call_kind::{call_kind, CallDesugaringKind, CallKind};
13+
pub use self::compare_types::{is_equal_up_to_subtyping, is_subtype};
1214
pub use self::find_self_call::find_self_call;
1315
pub use self::might_permit_raw_init::might_permit_raw_init;
1416
pub use self::type_name::type_name;

compiler/rustc_infer/src/infer/nll_relate/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,11 +557,8 @@ where
557557

558558
debug!(?self.ambient_variance);
559559
// In a bivariant context this always succeeds.
560-
let r = if self.ambient_variance == ty::Variance::Bivariant {
561-
a
562-
} else {
563-
self.relate(a, b)?
564-
};
560+
let r =
561+
if self.ambient_variance == ty::Variance::Bivariant { a } else { self.relate(a, b)? };
565562

566563
self.ambient_variance = old_ambient_variance;
567564

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_target::abi::VariantIdx;
1414
use rustc_target::spec::abi::Abi;
1515

1616
use crate::simplify::{remove_dead_blocks, CfgSimplifier};
17-
use crate::validate;
17+
use crate::util;
1818
use crate::MirPass;
1919
use std::iter;
2020
use std::ops::{Range, RangeFrom};
@@ -180,7 +180,7 @@ impl<'tcx> Inliner<'tcx> {
180180
let TerminatorKind::Call { args, destination, .. } = &terminator.kind else { bug!() };
181181
let destination_ty = destination.ty(&caller_body.local_decls, self.tcx).ty;
182182
let output_type = callee_body.return_ty();
183-
if !validate::is_subtype(self.tcx, self.param_env, output_type, destination_ty) {
183+
if !util::is_subtype(self.tcx, self.param_env, output_type, destination_ty) {
184184
trace!(?output_type, ?destination_ty);
185185
return Err("failed to normalize return type");
186186
}
@@ -200,7 +200,7 @@ impl<'tcx> Inliner<'tcx> {
200200
arg_tuple_tys.iter().zip(callee_body.args_iter().skip(skipped_args))
201201
{
202202
let input_type = callee_body.local_decls[input].ty;
203-
if !validate::is_subtype(self.tcx, self.param_env, input_type, arg_ty) {
203+
if !util::is_subtype(self.tcx, self.param_env, input_type, arg_ty) {
204204
trace!(?arg_ty, ?input_type);
205205
return Err("failed to normalize tuple argument type");
206206
}
@@ -209,7 +209,7 @@ impl<'tcx> Inliner<'tcx> {
209209
for (arg, input) in args.iter().zip(callee_body.args_iter()) {
210210
let input_type = callee_body.local_decls[input].ty;
211211
let arg_ty = arg.ty(&caller_body.local_decls, self.tcx);
212-
if !validate::is_subtype(self.tcx, self.param_env, input_type, arg_ty) {
212+
if !util::is_subtype(self.tcx, self.param_env, input_type, arg_ty) {
213213
trace!(?arg_ty, ?input_type);
214214
return Err("failed to normalize argument type");
215215
}
@@ -847,7 +847,7 @@ impl<'tcx> Visitor<'tcx> for CostChecker<'_, 'tcx> {
847847
let parent = Place { local, projection: self.tcx.intern_place_elems(proj_base) };
848848
let parent_ty = parent.ty(&self.callee_body.local_decls, self.tcx);
849849
let check_equal = |this: &mut Self, f_ty| {
850-
if !validate::is_equal_up_to_subtyping(this.tcx, this.param_env, ty, f_ty) {
850+
if !util::is_equal_up_to_subtyping(this.tcx, this.param_env, ty, f_ty) {
851851
trace!(?ty, ?f_ty);
852852
this.validation = Err("failed to normalize projection type");
853853
return;

0 commit comments

Comments
 (0)