Skip to content

Commit 0692db1

Browse files
committed
Auto merge of rust-lang#115865 - RalfJung:mir-mod, r=oli-obk
move things out of mir/mod.rs This moves a bunch of things out of `mir/mod.rs`: - all const-related stuff to a new file consts.rs - all statement/place/operand-related stuff to a new file statement.rs - all pretty-printing related stuff to pretty.rs `mod.rs` started out with 3100 lines and ends up with 1600. :) Also there was some pretty-printing stuff in terminator.rs, that also got moved to pretty.rs, and I reordered things in pretty.rs so that it can be grouped by functionality. Only the commit "use pretty_print_const_value from MIR constant 'extra' printing" has any behavior changes; it resolves the issue of having a fancy and a very crude pretty-printer for `ConstValue`. r? `@oli-obk`
2 parents 8769c26 + d14e601 commit 0692db1

File tree

41 files changed

+2269
-2214
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+2269
-2214
lines changed

compiler/rustc_codegen_cranelift/src/constant.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
33
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
5-
use rustc_middle::mir::interpret::{read_target_uint, AllocId, ConstValue, GlobalAlloc, Scalar};
5+
use rustc_middle::mir::interpret::{read_target_uint, AllocId, GlobalAlloc, Scalar};
6+
use rustc_middle::mir::ConstValue;
67

78
use cranelift_module::*;
89

compiler/rustc_codegen_ssa/src/common.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(non_camel_case_types)]
22

33
use rustc_hir::LangItem;
4-
use rustc_middle::mir::interpret::ConstValue;
4+
use rustc_middle::mir;
55
use rustc_middle::ty::{self, layout::TyAndLayout, Ty, TyCtxt};
66
use rustc_span::Span;
77

@@ -194,10 +194,10 @@ pub fn shift_mask_val<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
194194
pub fn asm_const_to_str<'tcx>(
195195
tcx: TyCtxt<'tcx>,
196196
sp: Span,
197-
const_value: ConstValue<'tcx>,
197+
const_value: mir::ConstValue<'tcx>,
198198
ty_and_layout: TyAndLayout<'tcx>,
199199
) -> String {
200-
let ConstValue::Scalar(scalar) = const_value else {
200+
let mir::ConstValue::Scalar(scalar) = const_value else {
201201
span_bug!(sp, "expected Scalar for promoted asm const, but got {:#?}", const_value)
202202
};
203203
let value = scalar.assert_bits(ty_and_layout.size);

compiler/rustc_codegen_ssa/src/mir/constant.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::errors;
22
use crate::mir::operand::OperandRef;
33
use crate::traits::*;
44
use rustc_middle::mir;
5-
use rustc_middle::mir::interpret::{ConstValue, ErrorHandled};
5+
use rustc_middle::mir::interpret::ErrorHandled;
66
use rustc_middle::ty::layout::HasTyCtxt;
77
use rustc_middle::ty::{self, Ty};
88
use rustc_target::abi::Abi;
@@ -20,7 +20,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2020
OperandRef::from_const(bx, val, ty)
2121
}
2222

23-
pub fn eval_mir_constant(&self, constant: &mir::Constant<'tcx>) -> ConstValue<'tcx> {
23+
pub fn eval_mir_constant(&self, constant: &mir::Constant<'tcx>) -> mir::ConstValue<'tcx> {
2424
self.monomorphize(constant.literal)
2525
.eval(self.cx.tcx(), ty::ParamEnv::reveal_all(), Some(constant.span))
2626
.expect("erroneous constant not captured by required_consts")

compiler/rustc_codegen_ssa/src/mir/operand.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::glue;
66
use crate::traits::*;
77
use crate::MemFlags;
88

9-
use rustc_middle::mir;
10-
use rustc_middle::mir::interpret::{alloc_range, ConstValue, Pointer, Scalar};
9+
use rustc_middle::mir::interpret::{alloc_range, Pointer, Scalar};
10+
use rustc_middle::mir::{self, ConstValue};
1111
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1212
use rustc_middle::ty::Ty;
1313
use rustc_target::abi::{self, Abi, Align, Size};
@@ -86,7 +86,7 @@ impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
8686

8787
pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
8888
bx: &mut Bx,
89-
val: ConstValue<'tcx>,
89+
val: mir::ConstValue<'tcx>,
9090
ty: Ty<'tcx>,
9191
) -> Self {
9292
let layout = bx.layout_of(ty);

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::errors::ConstEvalError;
44
use either::{Left, Right};
55

66
use rustc_hir::def::DefKind;
7-
use rustc_middle::mir;
87
use rustc_middle::mir::interpret::{ErrorHandled, InterpErrorInfo};
98
use rustc_middle::mir::pretty::write_allocation_bytes;
9+
use rustc_middle::mir::{self, ConstAlloc, ConstValue};
1010
use rustc_middle::traits::Reveal;
1111
use rustc_middle::ty::layout::LayoutOf;
1212
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -18,9 +18,8 @@ use super::{CanAccessStatics, CompileTimeEvalContext, CompileTimeInterpreter};
1818
use crate::errors;
1919
use crate::interpret::eval_nullary_intrinsic;
2020
use crate::interpret::{
21-
intern_const_alloc_recursive, ConstAlloc, ConstValue, CtfeValidationMode, GlobalId, Immediate,
22-
InternKind, InterpCx, InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking,
23-
StackPopCleanup,
21+
intern_const_alloc_recursive, CtfeValidationMode, GlobalId, Immediate, InternKind, InterpCx,
22+
InterpError, InterpResult, MPlaceTy, MemoryKind, OpTy, RefTracking, StackPopCleanup,
2423
};
2524

2625
// Returns a pointer to where the result lives

compiler/rustc_const_eval/src/const_eval/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Not in interpret to make sure we do not use private implementation details
22

33
use crate::errors::MaxNumNodesInConstErr;
4-
use crate::interpret::{intern_const_alloc_recursive, ConstValue, InternKind, InterpCx, Scalar};
4+
use crate::interpret::{intern_const_alloc_recursive, InternKind, InterpCx, Scalar};
55
use rustc_middle::mir;
66
use rustc_middle::mir::interpret::{EvalToValTreeResult, GlobalId};
77
use rustc_middle::ty::{self, Ty, TyCtxt};
@@ -22,15 +22,15 @@ pub(crate) use valtrees::{const_to_valtree_inner, valtree_to_const_value};
2222
pub(crate) fn const_caller_location(
2323
tcx: TyCtxt<'_>,
2424
(file, line, col): (Symbol, u32, u32),
25-
) -> ConstValue<'_> {
25+
) -> mir::ConstValue<'_> {
2626
trace!("const_caller_location: {}:{}:{}", file, line, col);
2727
let mut ecx = mk_eval_cx(tcx, DUMMY_SP, ty::ParamEnv::reveal_all(), CanAccessStatics::No);
2828

2929
let loc_place = ecx.alloc_caller_location(file, line, col);
3030
if intern_const_alloc_recursive(&mut ecx, InternKind::Constant, &loc_place).is_err() {
3131
bug!("intern_const_alloc_recursive should not error in this case")
3232
}
33-
ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
33+
mir::ConstValue::Scalar(Scalar::from_maybe_pointer(loc_place.ptr(), &tcx))
3434
}
3535

3636
// We forbid type-level constants that contain more than `VALTREE_MAX_NODES` nodes.
@@ -87,7 +87,7 @@ pub(crate) fn eval_to_valtree<'tcx>(
8787
#[instrument(skip(tcx), level = "debug")]
8888
pub(crate) fn try_destructure_mir_constant_for_diagnostics<'tcx>(
8989
tcx: TyCtxt<'tcx>,
90-
val: ConstValue<'tcx>,
90+
val: mir::ConstValue<'tcx>,
9191
ty: Ty<'tcx>,
9292
) -> Option<mir::DestructuredConstant<'tcx>> {
9393
let param_env = ty::ParamEnv::reveal_all();

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use super::{ValTreeCreationError, ValTreeCreationResult, VALTREE_MAX_NODES};
44
use crate::const_eval::CanAccessStatics;
55
use crate::interpret::MPlaceTy;
66
use crate::interpret::{
7-
intern_const_alloc_recursive, ConstValue, ImmTy, Immediate, InternKind, MemPlaceMeta,
8-
MemoryKind, PlaceTy, Projectable, Scalar,
7+
intern_const_alloc_recursive, ImmTy, Immediate, InternKind, MemPlaceMeta, MemoryKind, PlaceTy,
8+
Projectable, Scalar,
99
};
10+
use rustc_middle::mir;
1011
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
1112
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
1213
use rustc_span::source_map::DUMMY_SP;
@@ -206,7 +207,7 @@ pub fn valtree_to_const_value<'tcx>(
206207
tcx: TyCtxt<'tcx>,
207208
param_env_ty: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
208209
valtree: ty::ValTree<'tcx>,
209-
) -> ConstValue<'tcx> {
210+
) -> mir::ConstValue<'tcx> {
210211
// Basic idea: We directly construct `Scalar` values from trivial `ValTree`s
211212
// (those for constants with type bool, int, uint, float or char).
212213
// For all other types we create an `MPlace` and fill that by walking
@@ -219,10 +220,10 @@ pub fn valtree_to_const_value<'tcx>(
219220
match ty.kind() {
220221
ty::FnDef(..) => {
221222
assert!(valtree.unwrap_branch().is_empty());
222-
ConstValue::ZeroSized
223+
mir::ConstValue::ZeroSized
223224
}
224225
ty::Bool | ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::Char => match valtree {
225-
ty::ValTree::Leaf(scalar_int) => ConstValue::Scalar(Scalar::Int(scalar_int)),
226+
ty::ValTree::Leaf(scalar_int) => mir::ConstValue::Scalar(Scalar::Int(scalar_int)),
226227
ty::ValTree::Branch(_) => bug!(
227228
"ValTrees for Bool, Int, Uint, Float or Char should have the form ValTree::Leaf"
228229
),
@@ -237,7 +238,7 @@ pub fn valtree_to_const_value<'tcx>(
237238
let layout = tcx.layout_of(param_env_ty).unwrap();
238239
if layout.is_zst() {
239240
// Fast path to avoid some allocations.
240-
return ConstValue::ZeroSized;
241+
return mir::ConstValue::ZeroSized;
241242
}
242243
if layout.abi.is_scalar()
243244
&& (matches!(ty.kind(), ty::Tuple(_))

compiler/rustc_const_eval/src/interpret/intrinsics.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
use rustc_hir::def_id::DefId;
66
use rustc_middle::mir::{
77
self,
8-
interpret::{
9-
Allocation, ConstAllocation, ConstValue, GlobalId, InterpResult, PointerArithmetic, Scalar,
10-
},
11-
BinOp, NonDivergingIntrinsic,
8+
interpret::{Allocation, ConstAllocation, GlobalId, InterpResult, PointerArithmetic, Scalar},
9+
BinOp, ConstValue, NonDivergingIntrinsic,
1210
};
1311
use rustc_middle::ty;
1412
use rustc_middle::ty::layout::{LayoutOf as _, ValidityRequirement};

compiler/rustc_const_eval/src/interpret/operand.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ use rustc_middle::{mir, ty};
1313
use rustc_target::abi::{self, Abi, Align, HasDataLayout, Size};
1414

1515
use super::{
16-
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, ConstValue, Frame, InterpCx,
17-
InterpResult, MPlaceTy, Machine, MemPlace, MemPlaceMeta, PlaceTy, Pointer, Projectable,
18-
Provenance, Scalar,
16+
alloc_range, from_known_layout, mir_assign_valid_types, AllocId, Frame, InterpCx, InterpResult,
17+
MPlaceTy, Machine, MemPlace, MemPlaceMeta, PlaceTy, Pointer, Projectable, Provenance, Scalar,
1918
};
2019

2120
/// An `Immediate` represents a single immediate self-contained Rust value.
@@ -702,7 +701,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
702701

703702
pub(crate) fn const_val_to_op(
704703
&self,
705-
val_val: ConstValue<'tcx>,
704+
val_val: mir::ConstValue<'tcx>,
706705
ty: Ty<'tcx>,
707706
layout: Option<TyAndLayout<'tcx>>,
708707
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
@@ -715,15 +714,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
715714
};
716715
let layout = from_known_layout(self.tcx, self.param_env, layout, || self.layout_of(ty))?;
717716
let op = match val_val {
718-
ConstValue::Indirect { alloc_id, offset } => {
717+
mir::ConstValue::Indirect { alloc_id, offset } => {
719718
// We rely on mutability being set correctly in that allocation to prevent writes
720719
// where none should happen.
721720
let ptr = self.global_base_pointer(Pointer::new(alloc_id, offset))?;
722721
Operand::Indirect(MemPlace::from_ptr(ptr.into()))
723722
}
724-
ConstValue::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),
725-
ConstValue::ZeroSized => Operand::Immediate(Immediate::Uninit),
726-
ConstValue::Slice { data, start, end } => {
723+
mir::ConstValue::Scalar(x) => Operand::Immediate(adjust_scalar(x)?.into()),
724+
mir::ConstValue::ZeroSized => Operand::Immediate(Immediate::Uninit),
725+
mir::ConstValue::Slice { data, start, end } => {
727726
// We rely on mutability being set correctly in `data` to prevent writes
728727
// where none should happen.
729728
let ptr = Pointer::new(

compiler/rustc_const_eval/src/interpret/place.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ use either::{Either, Left, Right};
99
use rustc_ast::Mutability;
1010
use rustc_index::IndexSlice;
1111
use rustc_middle::mir;
12-
use rustc_middle::mir::interpret::PointerArithmetic;
1312
use rustc_middle::ty;
1413
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1514
use rustc_middle::ty::Ty;
1615
use rustc_target::abi::{Abi, Align, FieldIdx, HasDataLayout, Size, FIRST_VARIANT};
1716

1817
use super::{
19-
alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, CheckInAllocMsg,
20-
ConstAlloc, ImmTy, Immediate, InterpCx, InterpResult, Machine, MemoryKind, OpTy, Operand,
21-
Pointer, Projectable, Provenance, Readable, Scalar,
18+
alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, CheckInAllocMsg, ImmTy,
19+
Immediate, InterpCx, InterpResult, Machine, MemoryKind, OpTy, Operand, Pointer,
20+
PointerArithmetic, Projectable, Provenance, Readable, Scalar,
2221
};
2322

2423
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
@@ -1037,7 +1036,7 @@ where
10371036

10381037
pub fn raw_const_to_mplace(
10391038
&self,
1040-
raw: ConstAlloc<'tcx>,
1039+
raw: mir::ConstAlloc<'tcx>,
10411040
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::Provenance>> {
10421041
// This must be an allocation in `tcx`
10431042
let _ = self.tcx.global_alloc(raw.alloc_id);

compiler/rustc_data_structures/src/lib.rs

+19
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ extern crate cfg_if;
4747
#[macro_use]
4848
extern crate rustc_macros;
4949

50+
use std::fmt;
51+
5052
pub use rustc_index::static_assert_size;
5153

5254
#[inline(never)]
@@ -126,6 +128,23 @@ impl<F: FnOnce()> Drop for OnDrop<F> {
126128
}
127129
}
128130

131+
/// Turns a closure that takes an `&mut Formatter` into something that can be display-formatted.
132+
pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
133+
struct Printer<F> {
134+
f: F,
135+
}
136+
impl<F> fmt::Display for Printer<F>
137+
where
138+
F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
139+
{
140+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
141+
(self.f)(fmt)
142+
}
143+
}
144+
145+
Printer { f }
146+
}
147+
129148
// See comments in src/librustc_middle/lib.rs
130149
#[doc(hidden)]
131150
pub fn __noop_fix_for_27438() {}

0 commit comments

Comments
 (0)