Skip to content

Commit 14e3d03

Browse files
committed
rebase and remove dead code
1 parent 9b28d3b commit 14e3d03

File tree

7 files changed

+4
-234
lines changed

7 files changed

+4
-234
lines changed

compiler/rustc_const_eval/src/const_eval/mod.rs

-70
Original file line numberDiff line numberDiff line change
@@ -172,40 +172,6 @@ pub(crate) fn try_destructure_const<'tcx>(
172172
Ok(mir::DestructuredConst { variant, fields })
173173
}
174174

175-
pub(crate) fn destructure_mir_constant<'tcx>(
176-
tcx: TyCtxt<'tcx>,
177-
param_env: ty::ParamEnv<'tcx>,
178-
val: mir::ConstantKind<'tcx>,
179-
) -> mir::DestructuredMirConstant<'tcx> {
180-
trace!("destructure_const: {:?}", val);
181-
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
182-
let op = ecx.mir_const_to_op(&val, None).unwrap();
183-
184-
// We go to `usize` as we cannot allocate anything bigger anyway.
185-
let (field_count, variant, down) = match val.ty().kind() {
186-
ty::Array(_, len) => (usize::try_from(len.eval_usize(tcx, param_env)).unwrap(), None, op),
187-
ty::Adt(def, _) if def.variants().is_empty() => {
188-
return mir::DestructuredMirConstant { variant: None, fields: &[] };
189-
}
190-
ty::Adt(def, _) => {
191-
let variant = ecx.read_discriminant(&op).unwrap().1;
192-
let down = ecx.operand_downcast(&op, variant).unwrap();
193-
(def.variants()[variant].fields.len(), Some(variant), down)
194-
}
195-
ty::Tuple(substs) => (substs.len(), None, op),
196-
_ => bug!("cannot destructure constant {:?}", val),
197-
};
198-
199-
let fields_iter = (0..field_count).map(|i| {
200-
let field_op = ecx.operand_field(&down, i).unwrap();
201-
let val = op_to_const(&ecx, &field_op);
202-
mir::ConstantKind::Val(val, field_op.layout.ty)
203-
});
204-
let fields = tcx.arena.alloc_from_iter(fields_iter);
205-
206-
mir::DestructuredMirConstant { variant, fields }
207-
}
208-
209175
pub(crate) fn deref_const<'tcx>(
210176
tcx: TyCtxt<'tcx>,
211177
param_env: ty::ParamEnv<'tcx>,
@@ -241,39 +207,3 @@ pub(crate) fn deref_const<'tcx>(
241207

242208
tcx.mk_const(ty::ConstS { val: ty::ConstKind::Value(op_to_const(&ecx, &mplace.into())), ty })
243209
}
244-
245-
#[instrument(skip(tcx), level = "debug")]
246-
pub(crate) fn deref_mir_constant<'tcx>(
247-
tcx: TyCtxt<'tcx>,
248-
param_env: ty::ParamEnv<'tcx>,
249-
val: mir::ConstantKind<'tcx>,
250-
) -> mir::ConstantKind<'tcx> {
251-
let ecx = mk_eval_cx(tcx, DUMMY_SP, param_env, false);
252-
let op = ecx.mir_const_to_op(&val, None).unwrap();
253-
let mplace = ecx.deref_operand(&op).unwrap();
254-
if let Some(alloc_id) = mplace.ptr.provenance {
255-
assert_eq!(
256-
tcx.get_global_alloc(alloc_id).unwrap().unwrap_memory().0.0.mutability,
257-
Mutability::Not,
258-
"deref_const cannot be used with mutable allocations as \
259-
that could allow pattern matching to observe mutable statics",
260-
);
261-
}
262-
263-
let ty = match mplace.meta {
264-
MemPlaceMeta::None => mplace.layout.ty,
265-
MemPlaceMeta::Poison => bug!("poison metadata in `deref_const`: {:#?}", mplace),
266-
// In case of unsized types, figure out the real type behind.
267-
MemPlaceMeta::Meta(scalar) => match mplace.layout.ty.kind() {
268-
ty::Str => bug!("there's no sized equivalent of a `str`"),
269-
ty::Slice(elem_ty) => tcx.mk_array(*elem_ty, scalar.to_machine_usize(&tcx).unwrap()),
270-
_ => bug!(
271-
"type {} should not have metadata, but had {:?}",
272-
mplace.layout.ty,
273-
mplace.meta
274-
),
275-
},
276-
};
277-
278-
mir::ConstantKind::Val(op_to_const(&ecx, &mplace.into()), ty)
279-
}

compiler/rustc_const_eval/src/lib.rs

-8
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ pub fn provide(providers: &mut Providers) {
4545
let (param_env, value) = param_env_and_value.into_parts();
4646
const_eval::try_destructure_const(tcx, param_env, value).ok()
4747
};
48-
providers.destructure_mir_constant = |tcx, param_env_and_value| {
49-
let (param_env, value) = param_env_and_value.into_parts();
50-
const_eval::destructure_mir_constant(tcx, param_env, value)
51-
};
5248
providers.const_to_valtree = |tcx, param_env_and_value| {
5349
let (param_env, raw) = param_env_and_value.into_parts();
5450
const_eval::const_to_valtree(tcx, param_env, raw)
@@ -57,8 +53,4 @@ pub fn provide(providers: &mut Providers) {
5753
let (param_env, value) = param_env_and_value.into_parts();
5854
const_eval::deref_const(tcx, param_env, value)
5955
};
60-
providers.deref_mir_constant = |tcx, param_env_and_value| {
61-
let (param_env, value) = param_env_and_value.into_parts();
62-
const_eval::deref_mir_constant(tcx, param_env, value)
63-
}
6456
}

compiler/rustc_middle/src/mir/mod.rs

+1-107
Original file line numberDiff line numberDiff line change
@@ -2721,8 +2721,8 @@ impl<'tcx> ConstantKind<'tcx> {
27212721
}
27222722
}
27232723

2724-
#[inline]
27252724
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
2725+
#[inline]
27262726
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
27272727
self.try_eval_bits(tcx, param_env, ty)
27282728
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
@@ -2793,112 +2793,6 @@ impl<'tcx> ConstantKind<'tcx> {
27932793
Self::from_bits(tcx, n as u128, ty::ParamEnv::empty().and(ty))
27942794
}
27952795

2796-
#[instrument(skip(tcx), level = "debug")]
2797-
pub fn try_eval_lit_or_param(
2798-
tcx: TyCtxt<'tcx>,
2799-
ty: Ty<'tcx>,
2800-
expr: &'tcx hir::Expr<'tcx>,
2801-
) -> Option<Self> {
2802-
// Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2803-
// currently have to be wrapped in curly brackets, so it's necessary to special-case.
2804-
let expr = match &expr.kind {
2805-
hir::ExprKind::Block(block, _) if block.stmts.is_empty() && block.expr.is_some() => {
2806-
block.expr.as_ref().unwrap()
2807-
}
2808-
_ => expr,
2809-
};
2810-
2811-
let lit_input = match expr.kind {
2812-
hir::ExprKind::Lit(ref lit) => {
2813-
Some(interpret::LitToConstInput { lit: &lit.node, ty, neg: false })
2814-
}
2815-
hir::ExprKind::Unary(hir::UnOp::Neg, ref expr) => match expr.kind {
2816-
hir::ExprKind::Lit(ref lit) => {
2817-
Some(interpret::LitToConstInput { lit: &lit.node, ty, neg: true })
2818-
}
2819-
_ => None,
2820-
},
2821-
_ => None,
2822-
};
2823-
2824-
if let Some(lit_input) = lit_input {
2825-
// If an error occurred, ignore that it's a literal and leave reporting the error up to
2826-
// mir.
2827-
match tcx.at(expr.span).lit_to_mir_constant(lit_input) {
2828-
Ok(c) => return Some(c),
2829-
Err(e) => {
2830-
tcx.sess.delay_span_bug(
2831-
expr.span,
2832-
&format!("Const::from_anon_const: couldn't lit_to_const {:?}", e),
2833-
);
2834-
}
2835-
}
2836-
}
2837-
use hir::{def::DefKind::ConstParam, def::Res, ExprKind, Path, QPath};
2838-
match expr.kind {
2839-
ExprKind::Path(QPath::Resolved(_, &Path { res: Res::Def(ConstParam, def_id), .. })) => {
2840-
// Find the name and index of the const parameter by indexing the generics of
2841-
// the parent item and construct a `ParamConst`.
2842-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
2843-
let item_id = tcx.hir().get_parent_node(hir_id);
2844-
let item_def_id = tcx.hir().local_def_id(item_id);
2845-
let generics = tcx.generics_of(item_def_id.to_def_id());
2846-
let index = generics.param_def_id_to_index[&def_id];
2847-
let name = tcx.hir().name(hir_id);
2848-
let ty_const = tcx.mk_const(ty::ConstS {
2849-
val: ty::ConstKind::Param(ty::ParamConst::new(index, name)),
2850-
ty,
2851-
});
2852-
2853-
Some(Self::Ty(ty_const))
2854-
}
2855-
_ => None,
2856-
}
2857-
}
2858-
2859-
#[instrument(skip(tcx), level = "debug")]
2860-
pub fn from_inline_const(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Self {
2861-
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
2862-
2863-
let body_id = match tcx.hir().get(hir_id) {
2864-
hir::Node::AnonConst(ac) => ac.body,
2865-
_ => span_bug!(
2866-
tcx.def_span(def_id.to_def_id()),
2867-
"from_inline_const can only process anonymous constants"
2868-
),
2869-
};
2870-
2871-
let expr = &tcx.hir().body(body_id).value;
2872-
2873-
let ty = tcx.typeck(def_id).node_type(hir_id);
2874-
2875-
let ret = match Self::try_eval_lit_or_param(tcx, ty, expr) {
2876-
Some(v) => v,
2877-
None => {
2878-
let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id());
2879-
let parent_substs =
2880-
tcx.erase_regions(InternalSubsts::identity_for_item(tcx, typeck_root_def_id));
2881-
let substs = ty::InlineConstSubsts::new(
2882-
tcx,
2883-
ty::InlineConstSubstsParts { parent_substs, ty },
2884-
)
2885-
.substs;
2886-
let ty_const = tcx.mk_const(ty::ConstS {
2887-
val: ty::ConstKind::Unevaluated(ty::Unevaluated {
2888-
def: ty::WithOptConstParam::unknown(def_id).to_global(),
2889-
substs,
2890-
promoted: None,
2891-
}),
2892-
ty,
2893-
});
2894-
2895-
Self::Ty(ty_const)
2896-
}
2897-
};
2898-
debug_assert!(!ret.has_free_regions());
2899-
ret
2900-
}
2901-
29022796
/// Literals are converted to `ConstantKindVal`, const generic parameters are eagerly
29032797
/// converted to a constant, everything else becomes `Unevaluated`.
29042798
pub fn from_anon_const(

compiler/rustc_middle/src/mir/query.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Values computed by queries that use MIR.
22
3-
use crate::mir::{self, Body, Promoted};
3+
use crate::mir::{Body, Promoted};
44
use crate::ty::{self, OpaqueHiddenType, Ty, TyCtxt};
55
use rustc_data_structures::stable_map::FxHashMap;
66
use rustc_data_structures::vec_map::VecMap;
@@ -421,13 +421,6 @@ pub struct DestructuredConst<'tcx> {
421421
pub fields: &'tcx [ty::Const<'tcx>],
422422
}
423423

424-
/// The constituent parts of an ADT or array.
425-
#[derive(Copy, Clone, Debug, HashStable)]
426-
pub struct DestructuredMirConstant<'tcx> {
427-
pub variant: Option<VariantIdx>,
428-
pub fields: &'tcx [mir::ConstantKind<'tcx>],
429-
}
430-
431424
/// Coverage information summarized from a MIR if instrumented for source code coverage (see
432425
/// compiler option `-Cinstrument-coverage`). This information is generated by the
433426
/// `InstrumentCoverage` MIR pass and can be retrieved via the `coverageinfo` query.

compiler/rustc_middle/src/query/mod.rs

-16
Original file line numberDiff line numberDiff line change
@@ -940,13 +940,6 @@ rustc_queries! {
940940
remap_env_constness
941941
}
942942

943-
/// Destructure an `mir::ConstantKind` ADT or array into its variant index
944-
/// and its field values.
945-
query destructure_mir_constant(key: ty::ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>) -> mir::DestructuredMirConstant<'tcx> {
946-
desc { "destructure mir constant"}
947-
remap_env_constness
948-
}
949-
950943
/// Dereference a constant reference or raw pointer and turn the result into a constant
951944
/// again.
952945
query deref_const(
@@ -956,15 +949,6 @@ rustc_queries! {
956949
remap_env_constness
957950
}
958951

959-
/// Dereference a constant reference or raw pointer and turn the result into a constant
960-
/// again.
961-
query deref_mir_constant(
962-
key: ty::ParamEnvAnd<'tcx, mir::ConstantKind<'tcx>>
963-
) -> mir::ConstantKind<'tcx> {
964-
desc { "deref constant" }
965-
remap_env_constness
966-
}
967-
968952
query const_caller_location(key: (rustc_span::Symbol, u32, u32)) -> ConstValue<'tcx> {
969953
desc { "get a &core::panic::Location referring to a span" }
970954
}

compiler/rustc_mir_build/src/thir/cx/mod.rs

+1-22
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
use crate::thir::pattern::pat_from_hir;
66
use crate::thir::util::UserAnnotatedTyHelpers;
77

8-
use rustc_ast::ast;
98
use rustc_data_structures::steal::Steal;
109
use rustc_errors::ErrorGuaranteed;
1110
use rustc_hir as hir;
1211
use rustc_hir::def_id::{DefId, LocalDefId};
1312
use rustc_hir::HirId;
1413
use rustc_hir::Node;
1514
use rustc_middle::middle::region;
16-
use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
17-
use rustc_middle::mir::ConstantKind;
1815
use rustc_middle::thir::*;
19-
use rustc_middle::ty::{self, Ty, TyCtxt};
16+
use rustc_middle::ty::{self, TyCtxt};
2017
use rustc_span::Span;
2118

2219
crate fn thir_body<'tcx>(
@@ -78,24 +75,6 @@ impl<'tcx> Cx<'tcx> {
7875
}
7976
}
8077

81-
#[instrument(skip(self), level = "debug")]
82-
crate fn const_eval_literal(
83-
&mut self,
84-
lit: &'tcx ast::LitKind,
85-
ty: Ty<'tcx>,
86-
sp: Span,
87-
neg: bool,
88-
) -> ConstantKind<'tcx> {
89-
match self.tcx.at(sp).lit_to_mir_constant(LitToConstInput { lit, ty, neg }) {
90-
Ok(c) => c,
91-
Err(LitToConstError::Reported) => {
92-
// create a dummy value and continue compiling
93-
ConstantKind::Ty(self.tcx.const_error(ty))
94-
}
95-
Err(LitToConstError::TypeError) => bug!("const_eval_literal: had type error"),
96-
}
97-
}
98-
9978
crate fn pattern_from_hir(&mut self, p: &hir::Pat<'_>) -> Pat<'tcx> {
10079
let p = match self.tcx.hir().get(p.hir_id) {
10180
Node::Pat(p) | Node::Binding(p) => p,

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,6 @@ impl<'tcx> PatternFoldable<'tcx> for PatKind<'tcx> {
739739
}
740740
}
741741

742-
// FIXME: Get rid of this function once valtrees land
743742
#[instrument(skip(tcx), level = "debug")]
744743
crate fn compare_const_vals<'tcx>(
745744
tcx: TyCtxt<'tcx>,
@@ -759,8 +758,7 @@ crate fn compare_const_vals<'tcx>(
759758

760759
// Early return for equal constants (so e.g. references to ZSTs can be compared, even if they
761760
// are just integer addresses).
762-
// FIXME This might be wrong
763-
if a == b {
761+
if a.val() == b.val() {
764762
return from_bool(true);
765763
}
766764

0 commit comments

Comments
 (0)