Skip to content

Commit 6718116

Browse files
committed
Pack the u128 in LitKind::Int
1 parent 7d2f022 commit 6718116

File tree

38 files changed

+89
-71
lines changed

38 files changed

+89
-71
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub use UnsafeSource::*;
2727
use crate::ptr::P;
2828
use crate::token::{self, CommentKind, Delimiter};
2929
use crate::tokenstream::{DelimSpan, LazyAttrTokenStream, TokenStream};
30+
use rustc_data_structures::packed::Pu128;
3031
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3132
use rustc_data_structures::stack::ensure_sufficient_stack;
3233
use rustc_data_structures::sync::Lrc;
@@ -1829,7 +1830,7 @@ pub enum LitKind {
18291830
/// A character literal (`'a'`).
18301831
Char(char),
18311832
/// An integer literal (`1`).
1832-
Int(u128, LitIntType),
1833+
Int(Pu128, LitIntType),
18331834
/// A float literal (`1.0`, `1f64` or `1E10f64`). The pre-suffix part is
18341835
/// stored as a symbol rather than `f64` so that `LitKind` can impl `Eq`
18351836
/// and `Hash`.
@@ -3300,13 +3301,9 @@ mod size_asserts {
33003301
static_assert_size!(Impl, 136);
33013302
static_assert_size!(Item, 136);
33023303
static_assert_size!(ItemKind, 64);
3303-
// This can be removed after i128:128 is in the bootstrap compiler's target.
3304-
#[cfg(not(bootstrap))]
3305-
static_assert_size!(LitKind, 32);
3304+
static_assert_size!(LitKind, 24);
33063305
static_assert_size!(Local, 72);
3307-
// This can be removed after i128:128 is in the bootstrap compiler's target.
3308-
#[cfg(not(bootstrap))]
3309-
static_assert_size!(MetaItemLit, 48);
3306+
static_assert_size!(MetaItemLit, 40);
33103307
static_assert_size!(Param, 40);
33113308
static_assert_size!(Pat, 72);
33123309
static_assert_size!(Path, 24);

compiler/rustc_ast/src/util/literal.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitErr
374374
};
375375

376376
let s = &s[if base != 10 { 2 } else { 0 }..];
377-
u128::from_str_radix(s, base).map(|i| LitKind::Int(i, ty)).map_err(|_| {
377+
u128::from_str_radix(s, base).map(|i| LitKind::Int(i.into(), ty)).map_err(|_| {
378378
// Small bases are lexed as if they were base 10, e.g, the string
379379
// might be `0b10201`. This will cause the conversion above to fail,
380380
// but these kinds of errors are already reported by the lexer.

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1908,15 +1908,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
19081908
pub(super) fn expr_usize(&mut self, sp: Span, value: usize) -> hir::Expr<'hir> {
19091909
let lit = self.arena.alloc(hir::Lit {
19101910
span: sp,
1911-
node: ast::LitKind::Int(value as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)),
1911+
node: ast::LitKind::Int(
1912+
(value as u128).into(),
1913+
ast::LitIntType::Unsigned(ast::UintTy::Usize),
1914+
),
19121915
});
19131916
self.expr(sp, hir::ExprKind::Lit(lit))
19141917
}
19151918

19161919
pub(super) fn expr_u32(&mut self, sp: Span, value: u32) -> hir::Expr<'hir> {
19171920
let lit = self.arena.alloc(hir::Lit {
19181921
span: sp,
1919-
node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ast::UintTy::U32)),
1922+
node: ast::LitKind::Int(
1923+
u128::from(value).into(),
1924+
ast::LitIntType::Unsigned(ast::UintTy::U32),
1925+
),
19201926
});
19211927
self.expr(sp, hir::ExprKind::Lit(lit))
19221928
}

compiler/rustc_attr/src/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,9 +1185,9 @@ fn allow_unstable<'a>(
11851185

11861186
pub fn parse_alignment(node: &ast::LitKind) -> Result<u32, &'static str> {
11871187
if let ast::LitKind::Int(literal, ast::LitIntType::Unsuffixed) = node {
1188-
if literal.is_power_of_two() {
1188+
if literal.get().is_power_of_two() {
11891189
// rustc_middle::ty::layout::Align restricts align to <= 2^29
1190-
if *literal <= 1 << 29 { Ok(*literal as u32) } else { Err("larger than 2^29") }
1190+
if *literal <= 1 << 29 { Ok(literal.get() as u32) } else { Err("larger than 2^29") }
11911191
} else {
11921192
Err("not a power of two")
11931193
}

compiler/rustc_attr/src/session_diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'a> IncorrectReprFormatGenericCause<'a> {
296296
pub fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
297297
match kind {
298298
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
299-
Some(Self::Int { span, name, int: *int })
299+
Some(Self::Int { span, name, int: int.get() })
300300
}
301301
ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
302302
_ => None,

compiler/rustc_builtin_macros/src/concat_bytes.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn invalid_type_err(
5454
val,
5555
ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
5656
)) => {
57-
assert!(val > u8::MAX.into()); // must be an error
57+
assert!(val.get() > u8::MAX.into()); // must be an error
5858
dcx.emit_err(ConcatBytesOob { span });
5959
}
6060
Ok(ast::LitKind::Int(_, _)) => {
@@ -86,7 +86,7 @@ fn handle_array_element(
8686
Ok(ast::LitKind::Int(
8787
val,
8888
ast::LitIntType::Unsuffixed | ast::LitIntType::Unsigned(ast::UintTy::U8),
89-
)) if val <= u8::MAX.into() => Some(val as u8),
89+
)) if val.get() <= u8::MAX.into() => Some(val.get() as u8),
9090

9191
Ok(ast::LitKind::Byte(val)) => Some(val),
9292
Ok(ast::LitKind::ByteStr(..)) => {
@@ -148,7 +148,7 @@ pub fn expand_concat_bytes(
148148
if let Some(elem) =
149149
handle_array_element(cx, &mut has_errors, &mut missing_literals, expr)
150150
{
151-
for _ in 0..count_val {
151+
for _ in 0..count_val.get() {
152152
accumulator.push(elem);
153153
}
154154
}

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ fn check_link_ordinal(tcx: TyCtxt<'_>, attr: &ast::Attribute) -> Option<u16> {
658658
// if the resulting EXE runs, as I haven't yet built the necessary DLL -- see earlier comment
659659
// about LINK.EXE failing.)
660660
if *ordinal <= u16::MAX as u128 {
661-
Some(*ordinal as u16)
661+
Some(ordinal.get() as u16)
662662
} else {
663663
let msg = format!("ordinal value in `link_ordinal` is too large: `{}`", &ordinal);
664664
tcx.dcx()

compiler/rustc_expand/src/mbe/metavar_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn parse_depth<'sess>(
124124
};
125125
if let Ok(lit_kind) = LitKind::from_token_lit(*lit)
126126
&& let LitKind::Int(n_u128, LitIntType::Unsuffixed) = lit_kind
127-
&& let Ok(n_usize) = usize::try_from(n_u128)
127+
&& let Ok(n_usize) = usize::try_from(n_u128.get())
128128
{
129129
Ok(n_usize)
130130
} else {

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2982,10 +2982,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
29822982
// fixed expression:
29832983
if let ExprKind::Lit(lit) = idx.kind
29842984
&& let ast::LitKind::Int(i, ast::LitIntType::Unsuffixed) = lit.node
2985-
&& i < types
2986-
.len()
2987-
.try_into()
2988-
.expect("expected tuple index to be < usize length")
2985+
&& i.get()
2986+
< types
2987+
.len()
2988+
.try_into()
2989+
.expect("expected tuple index to be < usize length")
29892990
{
29902991
err.span_suggestion(
29912992
brackets_span,

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::ty::TypeAndMut;
1111
use core::cmp::min;
1212
use core::iter;
1313
use rustc_ast::util::parser::{ExprPrecedence, PREC_POSTFIX};
14+
use rustc_data_structures::packed::Pu128;
1415
use rustc_errors::{Applicability, Diagnostic, MultiSpan};
1516
use rustc_hir as hir;
1617
use rustc_hir::def::Res;
@@ -1409,8 +1410,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14091410
}
14101411
let (_, suffix) = snippet.split_at(snippet.len() - 3);
14111412
let value = match suffix {
1412-
"f32" => (lit - 0xf32) / (16 * 16 * 16),
1413-
"f64" => (lit - 0xf64) / (16 * 16 * 16),
1413+
"f32" => (lit.get() - 0xf32) / (16 * 16 * 16),
1414+
"f64" => (lit.get() - 0xf64) / (16 * 16 * 16),
14141415
_ => return false,
14151416
};
14161417
err.span_suggestions(
@@ -1440,7 +1441,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14401441
};
14411442

14421443
// Provided expression needs to be a literal `0`.
1443-
let ExprKind::Lit(Spanned { node: rustc_ast::LitKind::Int(0, _), span }) = expr.kind else {
1444+
let ExprKind::Lit(Spanned { node: rustc_ast::LitKind::Int(Pu128(0), _), span }) = expr.kind
1445+
else {
14441446
return false;
14451447
};
14461448

0 commit comments

Comments
 (0)