Skip to content

Commit 3be987e

Browse files
committed
dont make lit_to_mir_constant a query
1 parent c349137 commit 3be987e

File tree

4 files changed

+56
-57
lines changed

4 files changed

+56
-57
lines changed

compiler/rustc_middle/src/query/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -960,10 +960,6 @@ rustc_queries! {
960960
desc { "converting literal to const" }
961961
}
962962

963-
query lit_to_mir_constant(key: LitToConstInput<'tcx>) -> Result<mir::ConstantKind<'tcx>, LitToConstError> {
964-
desc { "converting literal to mir constant" }
965-
}
966-
967963
query check_match(key: DefId) {
968964
desc { |tcx| "match-checking `{}`", tcx.def_path_str(key) }
969965
cache_on_disk_if { key.is_local() }

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+56-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
//! See docs in build/expr/mod.rs
22
33
use crate::build::Builder;
4+
use crate::thir::constant::parse_float;
5+
use rustc_ast as ast;
46
use rustc_hir::def_id::DefId;
7+
use rustc_middle::mir::interpret::Allocation;
58
use rustc_middle::mir::interpret::{ConstValue, LitToConstError, LitToConstInput, Scalar};
69
use rustc_middle::mir::*;
710
use rustc_middle::thir::*;
811
use rustc_middle::ty::subst::SubstsRef;
912
use rustc_middle::ty::{self, CanonicalUserTypeAnnotation, Ty, TyCtxt};
13+
use rustc_target::abi::Size;
1014

1115
impl<'a, 'tcx> Builder<'a, 'tcx> {
1216
/// Compile `expr`, yielding a compile-time constant. Assumes that
@@ -27,7 +31,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
2731
}
2832
ExprKind::Literal { lit, neg } => {
2933
let literal =
30-
match tcx.lit_to_mir_constant(LitToConstInput { lit: &lit.node, ty, neg }) {
34+
match lit_to_mir_constant(tcx, LitToConstInput { lit: &lit.node, ty, neg }) {
3135
Ok(c) => c,
3236
Err(LitToConstError::Reported) => ConstantKind::Ty(tcx.const_error(ty)),
3337
Err(LitToConstError::TypeError) => {
@@ -84,3 +88,54 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8488
}
8589
}
8690
}
91+
92+
#[instrument(skip(tcx, lit_input))]
93+
fn lit_to_mir_constant<'tcx>(
94+
tcx: TyCtxt<'tcx>,
95+
lit_input: LitToConstInput<'tcx>,
96+
) -> Result<ConstantKind<'tcx>, LitToConstError> {
97+
let LitToConstInput { lit, ty, neg } = lit_input;
98+
let trunc = |n| {
99+
let param_ty = ty::ParamEnv::reveal_all().and(ty);
100+
let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
101+
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
102+
let result = width.truncate(n);
103+
trace!("trunc result: {}", result);
104+
Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
105+
};
106+
107+
let value = match (lit, &ty.kind()) {
108+
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
109+
let s = s.as_str();
110+
let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());
111+
let allocation = tcx.intern_const_alloc(allocation);
112+
ConstValue::Slice { data: allocation, start: 0, end: s.len() }
113+
}
114+
(ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _))
115+
if matches!(inner_ty.kind(), ty::Slice(_)) =>
116+
{
117+
let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
118+
let allocation = tcx.intern_const_alloc(allocation);
119+
ConstValue::Slice { data: allocation, start: 0, end: data.len() }
120+
}
121+
(ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
122+
let id = tcx.allocate_bytes(data);
123+
ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
124+
}
125+
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
126+
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
127+
}
128+
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
129+
trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
130+
}
131+
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
132+
parse_float(*n, *fty, neg).ok_or(LitToConstError::Reported)?
133+
}
134+
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
135+
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
136+
(ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
137+
_ => return Err(LitToConstError::TypeError),
138+
};
139+
140+
Ok(ConstantKind::Val(value, ty))
141+
}

compiler/rustc_mir_build/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use rustc_middle::ty::query::Providers;
2727
pub fn provide(providers: &mut Providers) {
2828
providers.check_match = thir::pattern::check_match;
2929
providers.lit_to_const = thir::constant::lit_to_const;
30-
providers.lit_to_mir_constant = thir::constant::lit_to_mir_constant;
3130
providers.mir_built = build::mir_built;
3231
providers.thir_check_unsafety = check_unsafety::thir_check_unsafety;
3332
providers.thir_check_unsafety_for_const_arg = check_unsafety::thir_check_unsafety_for_const_arg;

compiler/rustc_mir_build/src/thir/constant.rs

-51
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use rustc_ast as ast;
33
use rustc_middle::mir::interpret::{
44
Allocation, ConstValue, LitToConstError, LitToConstInput, Scalar,
55
};
6-
use rustc_middle::mir::ConstantKind;
76
use rustc_middle::ty::{self, ParamEnv, TyCtxt};
87
use rustc_span::symbol::Symbol;
98
use rustc_target::abi::Size;
@@ -59,56 +58,6 @@ crate fn lit_to_const<'tcx>(
5958
Ok(ty::Const::from_value(tcx, lit, ty))
6059
}
6160

62-
crate fn lit_to_mir_constant<'tcx>(
63-
tcx: TyCtxt<'tcx>,
64-
lit_input: LitToConstInput<'tcx>,
65-
) -> Result<ConstantKind<'tcx>, LitToConstError> {
66-
let LitToConstInput { lit, ty, neg } = lit_input;
67-
let trunc = |n| {
68-
let param_ty = ty::ParamEnv::reveal_all().and(ty);
69-
let width = tcx.layout_of(param_ty).map_err(|_| LitToConstError::Reported)?.size;
70-
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
71-
let result = width.truncate(n);
72-
trace!("trunc result: {}", result);
73-
Ok(ConstValue::Scalar(Scalar::from_uint(result, width)))
74-
};
75-
76-
let value = match (lit, &ty.kind()) {
77-
(ast::LitKind::Str(s, _), ty::Ref(_, inner_ty, _)) if inner_ty.is_str() => {
78-
let s = s.as_str();
79-
let allocation = Allocation::from_bytes_byte_aligned_immutable(s.as_bytes());
80-
let allocation = tcx.intern_const_alloc(allocation);
81-
ConstValue::Slice { data: allocation, start: 0, end: s.len() }
82-
}
83-
(ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _))
84-
if matches!(inner_ty.kind(), ty::Slice(_)) =>
85-
{
86-
let allocation = Allocation::from_bytes_byte_aligned_immutable(data as &[u8]);
87-
let allocation = tcx.intern_const_alloc(allocation);
88-
ConstValue::Slice { data: allocation, start: 0, end: data.len() }
89-
}
90-
(ast::LitKind::ByteStr(data), ty::Ref(_, inner_ty, _)) if inner_ty.is_array() => {
91-
let id = tcx.allocate_bytes(data);
92-
ConstValue::Scalar(Scalar::from_pointer(id.into(), &tcx))
93-
}
94-
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
95-
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
96-
}
97-
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
98-
trunc(if neg { (*n as i128).overflowing_neg().0 as u128 } else { *n })?
99-
}
100-
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
101-
parse_float(*n, *fty, neg).ok_or(LitToConstError::Reported)?
102-
}
103-
(ast::LitKind::Bool(b), ty::Bool) => ConstValue::Scalar(Scalar::from_bool(*b)),
104-
(ast::LitKind::Char(c), ty::Char) => ConstValue::Scalar(Scalar::from_char(*c)),
105-
(ast::LitKind::Err(_), _) => return Err(LitToConstError::Reported),
106-
_ => return Err(LitToConstError::TypeError),
107-
};
108-
109-
Ok(ConstantKind::Val(value, ty))
110-
}
111-
11261
// FIXME move this to rustc_mir_build::build
11362
pub(crate) fn parse_float<'tcx>(
11463
num: Symbol,

0 commit comments

Comments
 (0)