Skip to content

Commit 93955e0

Browse files
committed
Auto merge of rust-lang#120080 - cuviper:128-align-packed, r=nikic
Pack u128 in the compiler to mitigate new alignment This is based on rust-lang#116672, adding a new `#[repr(packed(8))]` wrapper on `u128` to avoid changing any of the compiler's size assertions. This is needed in two places: * `SwitchTargets`, otherwise its `SmallVec<[u128; 1]>` gets padded up to 32 bytes. * `LitKind::Int`, so that entire `enum` can stay 24 bytes. * This change definitely has far-reaching effects though, since it's public.
2 parents a417366 + eb42f3e commit 93955e0

20 files changed

+46
-35
lines changed

clippy_lints/src/casts/unnecessary_cast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub(super) fn check<'tcx>(
107107
&& let Some(src) = snippet_opt(cx, cast_expr.span)
108108
&& cast_to.is_floating_point()
109109
&& let Some(num_lit) = NumericLiteral::from_lit_kind(&src, &lit.node)
110-
&& let from_nbits = 128 - n.leading_zeros()
110+
&& let from_nbits = 128 - n.get().leading_zeros()
111111
&& let to_nbits = fp_ty_mantissa_nbits(cast_to)
112112
&& from_nbits != 0
113113
&& to_nbits != 0

clippy_lints/src/implicit_saturating_add.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::get_parent_expr;
44
use clippy_utils::source::snippet_with_context;
55
use rustc_ast::ast::{LitIntType, LitKind};
6+
use rustc_data_structures::packed::Pu128;
67
use rustc_errors::Applicability;
78
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, Stmt, StmtKind};
89
use rustc_lint::{LateContext, LateLintPass};
@@ -69,7 +70,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingAdd {
6970
&& clippy_utils::SpanlessEq::new(cx).eq_expr(l, target)
7071
&& BinOpKind::Add == op1.node
7172
&& let ExprKind::Lit(lit) = value.kind
72-
&& let LitKind::Int(1, LitIntType::Unsuffixed) = lit.node
73+
&& let LitKind::Int(Pu128(1), LitIntType::Unsuffixed) = lit.node
7374
&& block.expr.is_none()
7475
{
7576
let mut app = Applicability::MachineApplicable;

clippy_lints/src/implicit_saturating_sub.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::{higher, is_integer_literal, peel_blocks_with_stmt, SpanlessEq};
33
use rustc_ast::ast::LitKind;
4+
use rustc_data_structures::packed::Pu128;
45
use rustc_errors::Applicability;
56
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath};
67
use rustc_lint::{LateContext, LateLintPass};
@@ -86,7 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
8687
match cond_num_val.kind {
8788
ExprKind::Lit(cond_lit) => {
8889
// Check if the constant is zero
89-
if let LitKind::Int(0, _) = cond_lit.node {
90+
if let LitKind::Int(Pu128(0), _) = cond_lit.node {
9091
if cx.typeck_results().expr_ty(cond_left).is_signed() {
9192
} else {
9293
print_lint_and_sugg(cx, var_name, expr);

clippy_lints/src/loops/manual_memcpy.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ fn is_array_length_equal_to_range(cx: &LateContext<'_>, start: &Expr<'_>, end: &
461461
if let ExprKind::Lit(lit) = expr.kind
462462
&& let ast::LitKind::Int(value, _) = lit.node
463463
{
464-
Some(value)
464+
Some(value.get())
465465
} else {
466466
None
467467
}

clippy_lints/src/loops/needless_range_loop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ fn is_end_eq_array_len<'tcx>(
209209
&& let Some(arr_len) = arr_len_const.try_eval_target_usize(cx.tcx, cx.param_env)
210210
{
211211
return match limits {
212-
ast::RangeLimits::Closed => end_int + 1 >= arr_len.into(),
213-
ast::RangeLimits::HalfOpen => end_int >= arr_len.into(),
212+
ast::RangeLimits::Closed => end_int.get() + 1 >= arr_len.into(),
213+
ast::RangeLimits::HalfOpen => end_int.get() >= arr_len.into(),
214214
};
215215
}
216216

clippy_lints/src/manual_bits.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::get_parent_expr;
44
use clippy_utils::source::snippet_with_context;
55
use rustc_ast::ast::LitKind;
6+
use rustc_data_structures::packed::Pu128;
67
use rustc_errors::Applicability;
78
use rustc_hir::{BinOpKind, Expr, ExprKind, GenericArg, QPath};
89
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -62,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualBits {
6263
&& let Some((real_ty, resolved_ty, other_expr)) = get_one_size_of_ty(cx, left_expr, right_expr)
6364
&& matches!(resolved_ty.kind(), ty::Int(_) | ty::Uint(_))
6465
&& let ExprKind::Lit(lit) = &other_expr.kind
65-
&& let LitKind::Int(8, _) = lit.node
66+
&& let LitKind::Int(Pu128(8), _) = lit.node
6667
{
6768
let mut app = Applicability::MachineApplicable;
6869
let ty_snip = snippet_with_context(cx, real_ty.span, ctxt, "..", &mut app).0;

clippy_lints/src/manual_range_patterns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn expr_as_i128(expr: &Expr<'_>) -> Option<i128> {
4545
&& let LitKind::Int(num, _) = lit.node
4646
{
4747
// Intentionally not handling numbers greater than i128::MAX (for u128 literals) for now.
48-
num.try_into().ok()
48+
num.get().try_into().ok()
4949
} else {
5050
None
5151
}

clippy_lints/src/manual_strip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ fn eq_pattern_length<'tcx>(cx: &LateContext<'tcx>, pattern: &Expr<'_>, expr: &'t
161161
..
162162
}) = expr.kind
163163
{
164-
constant_length(cx, pattern).map_or(false, |length| length == *n)
164+
constant_length(cx, pattern).map_or(false, |length| *n == length)
165165
} else {
166166
len_arg(cx, expr).map_or(false, |arg| eq_expr_value(cx, pattern, arg))
167167
}

clippy_lints/src/matches/match_same_arms.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ impl<'a> NormalizedPat<'a> {
293293
LitKind::ByteStr(ref bytes, _) | LitKind::CStr(ref bytes, _) => Self::LitBytes(bytes),
294294
LitKind::Byte(val) => Self::LitInt(val.into()),
295295
LitKind::Char(val) => Self::LitInt(val.into()),
296-
LitKind::Int(val, _) => Self::LitInt(val),
296+
LitKind::Int(val, _) => Self::LitInt(val.get()),
297297
LitKind::Bool(val) => Self::LitBool(val),
298298
LitKind::Float(..) | LitKind::Err => Self::Wild,
299299
},
@@ -305,7 +305,7 @@ impl<'a> NormalizedPat<'a> {
305305
None => 0,
306306
Some(e) => match &e.kind {
307307
ExprKind::Lit(lit) => match lit.node {
308-
LitKind::Int(val, _) => val,
308+
LitKind::Int(val, _) => val.get(),
309309
LitKind::Char(val) => val.into(),
310310
LitKind::Byte(val) => val.into(),
311311
_ => return Self::Wild,
@@ -317,7 +317,7 @@ impl<'a> NormalizedPat<'a> {
317317
None => (u128::MAX, RangeEnd::Included),
318318
Some(e) => match &e.kind {
319319
ExprKind::Lit(lit) => match lit.node {
320-
LitKind::Int(val, _) => (val, bounds),
320+
LitKind::Int(val, _) => (val.get(), bounds),
321321
LitKind::Char(val) => (val.into(), bounds),
322322
LitKind::Byte(val) => (val.into(), bounds),
323323
_ => return Self::Wild,

clippy_lints/src/methods/get_first.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::ty::is_type_diagnostic_item;
44
use rustc_ast::LitKind;
5+
use rustc_data_structures::packed::Pu128;
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
78
use rustc_lint::LateContext;
@@ -20,7 +21,7 @@ pub(super) fn check<'tcx>(
2021
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
2122
&& let identity = cx.tcx.type_of(impl_id).instantiate_identity()
2223
&& let hir::ExprKind::Lit(Spanned {
23-
node: LitKind::Int(0, _),
24+
node: LitKind::Int(Pu128(0), _),
2425
..
2526
}) = arg.kind
2627
{

clippy_lints/src/methods/iter_out_of_bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn expr_as_u128(cx: &LateContext<'_>, e: &Expr<'_>) -> Option<u128> {
1313
if let ExprKind::Lit(lit) = expr_or_init(cx, e).kind
1414
&& let LitKind::Int(n, _) = lit.node
1515
{
16-
Some(n)
16+
Some(n.get())
1717
} else {
1818
None
1919
}

clippy_lints/src/methods/seek_from_current.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_ast::ast::{LitIntType, LitKind};
2+
use rustc_data_structures::packed::Pu128;
23
use rustc_errors::Applicability;
34
use rustc_hir::{Expr, ExprKind};
45
use rustc_lint::LateContext;
@@ -41,7 +42,7 @@ fn arg_is_seek_from_current<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>)
4142
// check if argument of `SeekFrom::Current` is `0`
4243
if args.len() == 1
4344
&& let ExprKind::Lit(lit) = args[0].kind
44-
&& let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node
45+
&& let LitKind::Int(Pu128(0), LitIntType::Unsuffixed) = lit.node
4546
{
4647
return true;
4748
}

clippy_lints/src/methods/seek_to_start_instead_of_rewind.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::implements_trait;
33
use clippy_utils::{is_expr_used_or_unified, match_def_path, paths};
44
use rustc_ast::ast::{LitIntType, LitKind};
5+
use rustc_data_structures::packed::Pu128;
56
use rustc_errors::Applicability;
67
use rustc_hir::{Expr, ExprKind};
78
use rustc_lint::LateContext;
@@ -31,7 +32,7 @@ pub(super) fn check<'tcx>(
3132
&& match_def_path(cx, def_id, &paths::STD_IO_SEEKFROM_START)
3233
&& args1.len() == 1
3334
&& let ExprKind::Lit(lit) = args1[0].kind
34-
&& let LitKind::Int(0, LitIntType::Unsuffixed) = lit.node
35+
&& let LitKind::Int(Pu128(0), LitIntType::Unsuffixed) = lit.node
3536
{
3637
let method_call_span = expr.span.with_lo(name_span.lo());
3738
span_lint_and_then(

clippy_lints/src/methods/unnecessary_fold.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::{is_trait_method, path_to_local_id, peel_blocks, strip_pat_refs};
44
use rustc_ast::ast;
5+
use rustc_data_structures::packed::Pu128;
56
use rustc_errors::Applicability;
67
use rustc_hir as hir;
78
use rustc_hir::PatKind;
@@ -150,7 +151,7 @@ pub(super) fn check(
150151
},
151152
);
152153
},
153-
ast::LitKind::Int(0, _) => check_fold_with_op(
154+
ast::LitKind::Int(Pu128(0), _) => check_fold_with_op(
154155
cx,
155156
expr,
156157
acc,
@@ -162,7 +163,7 @@ pub(super) fn check(
162163
method_name: "sum",
163164
},
164165
),
165-
ast::LitKind::Int(1, _) => {
166+
ast::LitKind::Int(Pu128(1), _) => {
166167
check_fold_with_op(
167168
cx,
168169
expr,

clippy_lints/src/methods/vec_resize_to_zero.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
33
use rustc_ast::LitKind;
4+
use rustc_data_structures::packed::Pu128;
45
use rustc_errors::Applicability;
56
use rustc_hir::{Expr, ExprKind};
67
use rustc_lint::LateContext;
@@ -20,7 +21,7 @@ pub(super) fn check<'tcx>(
2021
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
2122
&& is_type_diagnostic_item(cx, cx.tcx.type_of(impl_id).instantiate_identity(), sym::Vec)
2223
&& let ExprKind::Lit(Spanned {
23-
node: LitKind::Int(0, _),
24+
node: LitKind::Int(Pu128(0), _),
2425
..
2526
}) = count_arg.kind
2627
&& let ExprKind::Lit(Spanned {

clippy_lints/src/missing_asserts_for_indexing.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clippy_utils::source::snippet;
77
use clippy_utils::visitors::for_each_expr;
88
use clippy_utils::{eq_expr_value, hash_expr, higher};
99
use rustc_ast::{LitKind, RangeLimits};
10+
use rustc_data_structures::packed::Pu128;
1011
use rustc_data_structures::unhash::UnhashMap;
1112
use rustc_errors::{Applicability, Diagnostic};
1213
use rustc_hir::{BinOp, Block, Body, Expr, ExprKind, UnOp};
@@ -102,8 +103,8 @@ fn len_comparison<'hir>(
102103
) -> Option<(LengthComparison, usize, &'hir Expr<'hir>)> {
103104
macro_rules! int_lit_pat {
104105
($id:ident) => {
105-
ExprKind::Lit(Spanned {
106-
node: LitKind::Int($id, _),
106+
ExprKind::Lit(&Spanned {
107+
node: LitKind::Int(Pu128($id), _),
107108
..
108109
})
109110
};
@@ -112,13 +113,13 @@ fn len_comparison<'hir>(
112113
// normalize comparison, `v.len() > 4` becomes `4 < v.len()`
113114
// this simplifies the logic a bit
114115
let (op, left, right) = normalize_comparison(bin_op.node, left, right)?;
115-
match (op, &left.kind, &right.kind) {
116-
(Rel::Lt, int_lit_pat!(left), _) => Some((LengthComparison::IntLessThanLength, *left as usize, right)),
117-
(Rel::Lt, _, int_lit_pat!(right)) => Some((LengthComparison::LengthLessThanInt, *right as usize, left)),
118-
(Rel::Le, int_lit_pat!(left), _) => Some((LengthComparison::IntLessThanOrEqualLength, *left as usize, right)),
119-
(Rel::Le, _, int_lit_pat!(right)) => Some((LengthComparison::LengthLessThanOrEqualInt, *right as usize, left)),
120-
(Rel::Eq, int_lit_pat!(left), _) => Some((LengthComparison::LengthEqualInt, *left as usize, right)),
121-
(Rel::Eq, _, int_lit_pat!(right)) => Some((LengthComparison::LengthEqualInt, *right as usize, left)),
116+
match (op, left.kind, right.kind) {
117+
(Rel::Lt, int_lit_pat!(left), _) => Some((LengthComparison::IntLessThanLength, left as usize, right)),
118+
(Rel::Lt, _, int_lit_pat!(right)) => Some((LengthComparison::LengthLessThanInt, right as usize, left)),
119+
(Rel::Le, int_lit_pat!(left), _) => Some((LengthComparison::IntLessThanOrEqualLength, left as usize, right)),
120+
(Rel::Le, _, int_lit_pat!(right)) => Some((LengthComparison::LengthLessThanOrEqualInt, right as usize, left)),
121+
(Rel::Eq, int_lit_pat!(left), _) => Some((LengthComparison::LengthEqualInt, left as usize, right)),
122+
(Rel::Eq, _, int_lit_pat!(right)) => Some((LengthComparison::LengthEqualInt, right as usize, left)),
122123
_ => None,
123124
}
124125
}
@@ -206,14 +207,14 @@ impl<'hir> IndexEntry<'hir> {
206207
/// for `..=5` this returns `Some(5)`
207208
fn upper_index_expr(expr: &Expr<'_>) -> Option<usize> {
208209
if let ExprKind::Lit(lit) = &expr.kind
209-
&& let LitKind::Int(index, _) = lit.node
210+
&& let LitKind::Int(Pu128(index), _) = lit.node
210211
{
211212
Some(index as usize)
212213
} else if let Some(higher::Range {
213214
end: Some(end), limits, ..
214215
}) = higher::Range::hir(expr)
215216
&& let ExprKind::Lit(lit) = &end.kind
216-
&& let LitKind::Int(index @ 1.., _) = lit.node
217+
&& let LitKind::Int(Pu128(index @ 1..), _) = lit.node
217218
{
218219
match limits {
219220
RangeLimits::HalfOpen => Some(index as usize - 1),

clippy_lints/src/operators/arithmetic_side_effects.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl ArithmeticSideEffects {
151151
if let hir::ExprKind::Lit(lit) = actual.kind
152152
&& let ast::LitKind::Int(n, _) = lit.node
153153
{
154-
return Some(n);
154+
return Some(n.get());
155155
}
156156
if let Some(Constant::Int(n)) = constant(cx, cx.typeck_results(), expr) {
157157
return Some(n);

clippy_lints/src/operators/verbose_bit_mask.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::sugg::Sugg;
33
use rustc_ast::ast::LitKind;
4+
use rustc_data_structures::packed::Pu128;
45
use rustc_errors::Applicability;
56
use rustc_hir::{BinOpKind, Expr, ExprKind};
67
use rustc_lint::LateContext;
@@ -19,9 +20,9 @@ pub(super) fn check<'tcx>(
1920
&& let ExprKind::Binary(op1, left1, right1) = &left.kind
2021
&& BinOpKind::BitAnd == op1.node
2122
&& let ExprKind::Lit(lit) = &right1.kind
22-
&& let LitKind::Int(n, _) = lit.node
23+
&& let LitKind::Int(Pu128(n), _) = lit.node
2324
&& let ExprKind::Lit(lit1) = &right.kind
24-
&& let LitKind::Int(0, _) = lit1.node
25+
&& let LitKind::Int(Pu128(0), _) = lit1.node
2526
&& n.leading_zeros() == n.count_zeros()
2627
&& n > u128::from(threshold)
2728
{

clippy_utils/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ pub fn lit_to_mir_constant<'tcx>(lit: &LitKind, ty: Option<Ty<'tcx>>) -> Constan
275275
LitKind::Byte(b) => Constant::Int(u128::from(b)),
276276
LitKind::ByteStr(ref s, _) | LitKind::CStr(ref s, _) => Constant::Binary(Lrc::clone(s)),
277277
LitKind::Char(c) => Constant::Char(c),
278-
LitKind::Int(n, _) => Constant::Int(n),
278+
LitKind::Int(n, _) => Constant::Int(n.get()),
279279
LitKind::Float(ref is, LitFloatType::Suffixed(fty)) => match fty {
280280
ast::FloatTy::F32 => Constant::F32(is.as_str().parse().unwrap()),
281281
ast::FloatTy::F64 => Constant::F64(is.as_str().parse().unwrap()),

clippy_utils/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ use std::sync::{Mutex, MutexGuard, OnceLock};
8080
use itertools::Itertools;
8181
use rustc_ast::ast::{self, LitKind, RangeLimits};
8282
use rustc_data_structures::fx::FxHashMap;
83+
use rustc_data_structures::packed::Pu128;
8384
use rustc_data_structures::unhash::UnhashMap;
8485
use rustc_hir::def::{DefKind, Res};
8586
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId, LOCAL_CRATE};
@@ -838,7 +839,7 @@ pub fn is_default_equivalent_call(cx: &LateContext<'_>, repl_func: &Expr<'_>) ->
838839
pub fn is_default_equivalent(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
839840
match &e.kind {
840841
ExprKind::Lit(lit) => match lit.node {
841-
LitKind::Bool(false) | LitKind::Int(0, _) => true,
842+
LitKind::Bool(false) | LitKind::Int(Pu128(0), _) => true,
842843
LitKind::Str(s, _) => s.is_empty(),
843844
_ => false,
844845
},

0 commit comments

Comments
 (0)