Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d4bdd1e

Browse files
committed
Auto merge of #136302 - oli-obk:push-vvqmwzunxsrk, r=compiler-errors
Avoid calling the layout_of query in lit_to_const We got all the information available locally
2 parents 820bfff + 0cd5186 commit d4bdd1e

File tree

2 files changed

+28
-21
lines changed

2 files changed

+28
-21
lines changed

‎compiler/rustc_mir_build/src/builder/expr/as_constant.rs‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,12 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
105105
return Const::Ty(Ty::new_error(tcx, guar), ty::Const::new_error(tcx, guar));
106106
}
107107

108-
let trunc = |n| {
109-
let width = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) {
110-
Ok(layout) => layout.size,
111-
Err(_) => {
112-
tcx.dcx().bug(format!("couldn't compute width of literal: {:?}", lit_input.lit))
113-
}
114-
};
108+
let trunc = |n, width: ty::UintTy| {
109+
let width = width
110+
.normalize(tcx.data_layout.pointer_size.bits().try_into().unwrap())
111+
.bit_width()
112+
.unwrap();
113+
let width = Size::from_bits(width);
115114
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
116115
let result = width.truncate(n);
117116
trace!("trunc result: {}", result);
@@ -145,9 +144,11 @@ fn lit_to_mir_constant<'tcx>(tcx: TyCtxt<'tcx>, lit_input: LitToConstInput<'tcx>
145144
(ast::LitKind::Byte(n), ty::Uint(ty::UintTy::U8)) => {
146145
ConstValue::Scalar(Scalar::from_uint(*n, Size::from_bytes(1)))
147146
}
148-
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
149-
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() })
150-
}
147+
(ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => trunc(n.get(), *ui),
148+
(ast::LitKind::Int(n, _), ty::Int(i)) => trunc(
149+
if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() },
150+
i.to_unsigned(),
151+
),
151152
(ast::LitKind::Float(n, _), ty::Float(fty)) => {
152153
parse_float_into_constval(*n, *fty, neg).unwrap()
153154
}

‎compiler/rustc_mir_build/src/thir/constant.rs‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use rustc_ast as ast;
1+
use rustc_abi::Size;
2+
use rustc_ast::{self as ast};
23
use rustc_hir::LangItem;
34
use rustc_middle::bug;
45
use rustc_middle::mir::interpret::LitToConstInput;
@@ -17,13 +18,12 @@ pub(crate) fn lit_to_const<'tcx>(
1718
return ty::Const::new_error(tcx, guar);
1819
}
1920

20-
let trunc = |n| {
21-
let width = match tcx.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(ty)) {
22-
Ok(layout) => layout.size,
23-
Err(_) => {
24-
tcx.dcx().bug(format!("couldn't compute width of literal: {:?}", lit_input.lit))
25-
}
26-
};
21+
let trunc = |n, width: ty::UintTy| {
22+
let width = width
23+
.normalize(tcx.data_layout.pointer_size.bits().try_into().unwrap())
24+
.bit_width()
25+
.unwrap();
26+
let width = Size::from_bits(width);
2727
trace!("trunc {} with size {} and shift {}", n, width.bits(), 128 - width.bits());
2828
let result = width.truncate(n);
2929
trace!("trunc result: {}", result);
@@ -55,9 +55,15 @@ pub(crate) fn lit_to_const<'tcx>(
5555
let bytes = data as &[u8];
5656
ty::ValTree::from_raw_bytes(tcx, bytes)
5757
}
58-
(ast::LitKind::Int(n, _), ty::Uint(_)) | (ast::LitKind::Int(n, _), ty::Int(_)) => {
59-
let scalar_int =
60-
trunc(if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() });
58+
(ast::LitKind::Int(n, _), ty::Uint(ui)) if !neg => {
59+
let scalar_int = trunc(n.get(), *ui);
60+
ty::ValTree::from_scalar_int(scalar_int)
61+
}
62+
(ast::LitKind::Int(n, _), ty::Int(i)) => {
63+
let scalar_int = trunc(
64+
if neg { (n.get() as i128).overflowing_neg().0 as u128 } else { n.get() },
65+
i.to_unsigned(),
66+
);
6167
ty::ValTree::from_scalar_int(scalar_int)
6268
}
6369
(ast::LitKind::Bool(b), ty::Bool) => ty::ValTree::from_scalar_int((*b).into()),

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /