Skip to content

Commit 049d780

Browse files
committed
Auto merge of #135569 - matthiaskrgr:rollup-8vs4lv6, r=matthiaskrgr
Rollup of 4 pull requests Successful merges: - #135249 (Fix overflows in the implementation of `overflowing_literals` lint's help) - #135534 (use indirect return for `i128` and `f128` on wasm32) - #135556 (Clarify note in `std::sync::LazyLock` example) - #135560 (Update `compiler-builtins` to 0.1.144) r? `@ghost` `@rustbot` modify labels: rollup
2 parents d61f55d + c0b543b commit 049d780

File tree

11 files changed

+211
-23
lines changed

11 files changed

+211
-23
lines changed

compiler/rustc_codegen_cranelift/patches/0029-stdlib-Disable-f16-and-f128-in-compiler-builtins.patch

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ index 7165c3e48af..968552ad435 100644
1616

1717
[dependencies]
1818
core = { path = "../core" }
19-
-compiler_builtins = { version = "=0.1.143", features = ['rustc-dep-of-std'] }
20-
+compiler_builtins = { version = "=0.1.143", features = ['rustc-dep-of-std', 'no-f16-f128'] }
19+
-compiler_builtins = { version = "=0.1.144", features = ['rustc-dep-of-std'] }
20+
+compiler_builtins = { version = "=0.1.144", features = ['rustc-dep-of-std', 'no-f16-f128'] }
2121

2222
[dev-dependencies]
2323
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }

compiler/rustc_lint/src/types/literal.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,35 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
204204
match t.kind() {
205205
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
206206
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
207-
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
208-
ty::Int(int) => {
209-
let signed = Integer::fit_signed(val as i128);
210-
let unsigned = Integer::fit_unsigned(val);
211-
Some(if Some(unsigned.size().bits()) == int.bit_width() {
212-
unsigned.uint_ty_str()
207+
ty::Int(_) => {
208+
let signed = literal_to_i128(val, negative).map(Integer::fit_signed);
209+
if negative {
210+
signed.map(Integer::int_ty_str)
213211
} else {
214-
signed.int_ty_str()
215-
})
212+
let unsigned = Integer::fit_unsigned(val);
213+
Some(if let Some(signed) = signed {
214+
if unsigned.size() < signed.size() {
215+
unsigned.uint_ty_str()
216+
} else {
217+
signed.int_ty_str()
218+
}
219+
} else {
220+
unsigned.uint_ty_str()
221+
})
222+
}
216223
}
217224
_ => None,
218225
}
219226
}
220227

228+
fn literal_to_i128(val: u128, negative: bool) -> Option<i128> {
229+
if negative {
230+
(val <= i128::MAX as u128 + 1).then(|| val.wrapping_neg() as i128)
231+
} else {
232+
val.try_into().ok()
233+
}
234+
}
235+
221236
fn lint_int_literal<'tcx>(
222237
cx: &LateContext<'tcx>,
223238
type_limits: &TypeLimits,

compiler/rustc_target/src/callconv/wasm.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use rustc_abi::{BackendRepr, Float, Integer, Primitive};
2+
13
use crate::abi::call::{ArgAbi, FnAbi};
24
use crate::abi::{HasDataLayout, TyAbiInterface};
35

@@ -27,6 +29,16 @@ where
2729
if ret.layout.is_aggregate() && !unwrap_trivial_aggregate(cx, ret) {
2830
ret.make_indirect();
2931
}
32+
33+
// `long double`, `__int128_t` and `__uint128_t` use an indirect return
34+
if let BackendRepr::Scalar(scalar) = ret.layout.backend_repr {
35+
match scalar.primitive() {
36+
Primitive::Int(Integer::I128, _) | Primitive::Float(Float::F128) => {
37+
ret.make_indirect();
38+
}
39+
_ => {}
40+
}
41+
}
3042
}
3143

3244
fn classify_arg<'a, Ty, C>(cx: &C, arg: &mut ArgAbi<'a, Ty>)

library/Cargo.lock

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ dependencies = [
6161

6262
[[package]]
6363
name = "compiler_builtins"
64-
version = "0.1.143"
64+
version = "0.1.144"
6565
source = "registry+https://github.com./rust-lang/crates.io-index"
66-
checksum = "c85ba2077e3eab3dd81be4ece6b7fb2ad0887c1fb813e9a45400baf75c6c7c29"
66+
checksum = "d18a7b7b5a56aa131e62314b4d862c9f6aa2860f615f3770094ec9064d7ec572"
6767
dependencies = [
6868
"cc",
6969
"rustc-std-workspace-core",

library/alloc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ edition = "2021"
1010

1111
[dependencies]
1212
core = { path = "../core" }
13-
compiler_builtins = { version = "=0.1.143", features = ['rustc-dep-of-std'] }
13+
compiler_builtins = { version = "=0.1.144", features = ['rustc-dep-of-std'] }
1414

1515
[dev-dependencies]
1616
rand = { version = "0.8.5", default-features = false, features = ["alloc"] }

library/std/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ cfg-if = { version = "1.0", features = ['rustc-dep-of-std'] }
1717
panic_unwind = { path = "../panic_unwind", optional = true }
1818
panic_abort = { path = "../panic_abort" }
1919
core = { path = "../core", public = true }
20-
compiler_builtins = { version = "=0.1.143" }
20+
compiler_builtins = { version = "=0.1.144" }
2121
unwind = { path = "../unwind" }
2222
hashbrown = { version = "0.15", default-features = false, features = [
2323
'rustc-dep-of-std',

library/std/src/sync/lazy_lock.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ union Data<T, F> {
3131
/// ```
3232
/// use std::sync::LazyLock;
3333
///
34-
/// // n.b. static items do not call [`Drop`] on program termination, so this won't be deallocated.
34+
/// // Note: static items do not call [`Drop`] on program termination, so this won't be deallocated.
3535
/// // this is fine, as the OS can deallocate the terminated program faster than we can free memory
3636
/// // but tools like valgrind might report "memory leaks" as it isn't obvious this is intentional.
3737
/// static DEEP_THOUGHT: LazyLock<String> = LazyLock::new(|| {

tests/codegen/f128-wasm32-callconv.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Verify that Rust implements the expected calling convention for `f128`
2+
3+
//@ add-core-stubs
4+
//@ compile-flags: --target wasm32-wasip1
5+
//@ needs-llvm-components: webassembly
6+
7+
#![crate_type = "lib"]
8+
#![no_std]
9+
#![no_core]
10+
#![feature(no_core, lang_items, f128)]
11+
12+
extern crate minicore;
13+
14+
extern "C" {
15+
fn extern_call(arg0: f128);
16+
fn extern_ret() -> f128;
17+
}
18+
19+
#[no_mangle]
20+
pub extern "C" fn pass(_arg0: u32, arg1: f128) {
21+
// CHECK-LABEL: @pass(
22+
// an f128 is passed via registers
23+
// CHECK-SAME: fp128 noundef %arg1
24+
// CHECK: call void @extern_call
25+
unsafe { extern_call(arg1) };
26+
}
27+
28+
// Check that we produce the correct return ABI
29+
#[no_mangle]
30+
pub extern "C" fn ret(_arg0: u32, arg1: f128) -> f128 {
31+
// CHECK-LABEL: @ret(
32+
// but an f128 is returned via the stack
33+
// CHECK-SAME: sret
34+
// CHECK: store fp128 %arg1
35+
// CHECK-NEXT: ret void
36+
arg1
37+
}
38+
39+
// Check that we consume the correct return ABI
40+
#[no_mangle]
41+
pub extern "C" fn forward(dst: *mut f128) {
42+
// CHECK-LABEL: @forward
43+
// CHECK-SAME: ptr{{.*}} %dst)
44+
// without optimizatons, an intermediate alloca is used
45+
// CHECK: call void @extern_ret
46+
// CHECK: store fp128
47+
// CHECK: ret void
48+
unsafe { *dst = extern_ret() };
49+
}

tests/codegen/i128-wasm32-callconv.rs

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//! Verify that Rust implements the expected calling convention for `i128`/`u128`.
2+
3+
//@ add-core-stubs
4+
//@ compile-flags: --target wasm32-wasip1
5+
//@ needs-llvm-components: webassembly
6+
7+
#![crate_type = "lib"]
8+
#![no_std]
9+
#![no_core]
10+
#![feature(no_core, lang_items)]
11+
12+
extern crate minicore;
13+
14+
extern "C" {
15+
fn extern_call(arg0: i128);
16+
fn extern_ret() -> i128;
17+
}
18+
19+
#[no_mangle]
20+
pub extern "C" fn pass(_arg0: u32, arg1: i128) {
21+
// CHECK-LABEL: @pass(
22+
// an i128 is passed via registers
23+
// CHECK-SAME: i128 noundef %arg1
24+
// CHECK: call void @extern_call
25+
unsafe { extern_call(arg1) };
26+
}
27+
28+
// Check that we produce the correct return ABI
29+
#[no_mangle]
30+
pub extern "C" fn ret(_arg0: u32, arg1: i128) -> i128 {
31+
// CHECK-LABEL: @ret(
32+
// but an i128 is returned via the stack
33+
// CHECK-SAME: sret
34+
// CHECK: store i128 %arg1
35+
// CHECK-NEXT: ret void
36+
arg1
37+
}
38+
39+
// Check that we consume the correct return ABI
40+
#[no_mangle]
41+
pub extern "C" fn forward(dst: *mut i128) {
42+
// CHECK-LABEL: @forward
43+
// CHECK-SAME: ptr{{.*}} %dst)
44+
// without optimizatons, an intermediate alloca is used
45+
// CHECK: call void @extern_ret
46+
// CHECK: store i128
47+
// CHECK: ret void
48+
unsafe { *dst = extern_ret() };
49+
}

tests/ui/lint/type-overflow.rs

+26
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,46 @@
33

44
fn main() {
55
let error = 255i8; //~WARNING literal out of range for `i8`
6+
//~^ HELP consider using the type `u8` instead
67

78
let ok = 0b1000_0001; // should be ok -> i32
89
let ok = 0b0111_1111i8; // should be ok -> 127i8
910

1011
let fail = 0b1000_0001i8; //~WARNING literal out of range for `i8`
12+
//~^ HELP consider using the type `u8` instead
13+
//~| HELP consider using the type `u8` for the literal and cast it to `i8`
1114

1215
let fail = 0x8000_0000_0000_0000i64; //~WARNING literal out of range for `i64`
16+
//~^ HELP consider using the type `u64` instead
17+
//~| HELP consider using the type `u64` for the literal and cast it to `i64`
1318

1419
let fail = 0x1_FFFF_FFFFu32; //~WARNING literal out of range for `u32`
20+
//~^ HELP consider using the type `u64` instead
1521

1622
let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
1723
//~^ WARNING literal out of range for `i128`
24+
//~| HELP consider using the type `u128` instead
25+
//~| HELP consider using the type `u128` for the literal and cast it to `i128`
26+
27+
let fail = 0x8000_0000_0000_0000_0000_0000_0000_0000;
28+
//~^ WARNING literal out of range for `i32`
29+
//~| HELP consider using the type `u128` instead
30+
31+
let fail = -0x8000_0000_0000_0000_0000_0000_0000_0000; // issue #131849
32+
//~^ WARNING literal out of range for `i32`
33+
//~| HELP consider using the type `i128` instead
34+
35+
let fail = -0x8000_0000_0000_0000_0000_0000_0000_0001i128;
36+
//~^ WARNING literal out of range for `i128`
37+
38+
let fail = 340282366920938463463374607431768211455i8;
39+
//~^ WARNING literal out of range for `i8`
40+
//~| HELP consider using the type `u128` instead
1841

1942
let fail = 0x8FFF_FFFF_FFFF_FFFE; //~WARNING literal out of range for `i32`
43+
//~| HELP consider using the type `u64` instead
44+
//~| HELP
2045

2146
let fail = -0b1111_1111i8; //~WARNING literal out of range for `i8`
47+
//~| HELP consider using the type `i16` instead
2248
}

tests/ui/lint/type-overflow.stderr

+45-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | #![warn(overflowing_literals)]
1313
| ^^^^^^^^^^^^^^^^^^^^
1414

1515
warning: literal out of range for `i8`
16-
--> $DIR/type-overflow.rs:10:16
16+
--> $DIR/type-overflow.rs:11:16
1717
|
1818
LL | let fail = 0b1000_0001i8;
1919
| ^^^^^^^^^^^^^
@@ -29,7 +29,7 @@ LL | let fail = 0b1000_0001u8 as i8;
2929
| ~~~~~~~~~~~~~~~~~~~
3030

3131
warning: literal out of range for `i64`
32-
--> $DIR/type-overflow.rs:12:16
32+
--> $DIR/type-overflow.rs:15:16
3333
|
3434
LL | let fail = 0x8000_0000_0000_0000i64;
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^
@@ -45,15 +45,15 @@ LL | let fail = 0x8000_0000_0000_0000u64 as i64;
4545
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4646

4747
warning: literal out of range for `u32`
48-
--> $DIR/type-overflow.rs:14:16
48+
--> $DIR/type-overflow.rs:19:16
4949
|
5050
LL | let fail = 0x1_FFFF_FFFFu32;
5151
| ^^^^^^^^^^^^^^^^ help: consider using the type `u64` instead: `0x1_FFFF_FFFFu64`
5252
|
5353
= note: the literal `0x1_FFFF_FFFFu32` (decimal `8589934591`) does not fit into the type `u32` and will become `4294967295u32`
5454

5555
warning: literal out of range for `i128`
56-
--> $DIR/type-overflow.rs:16:22
56+
--> $DIR/type-overflow.rs:22:22
5757
|
5858
LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000;
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -66,26 +66,63 @@ LL | let fail: i128 = 0x8000_0000_0000_0000_0000_0000_0000_0000u128 as i128;
6666
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6767

6868
warning: literal out of range for `i32`
69-
--> $DIR/type-overflow.rs:19:16
69+
--> $DIR/type-overflow.rs:27:16
70+
|
71+
LL | let fail = 0x8000_0000_0000_0000_0000_0000_0000_0000;
72+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
73+
|
74+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32` and will become `0i32`
75+
= help: consider using the type `u128` instead
76+
77+
warning: literal out of range for `i32`
78+
--> $DIR/type-overflow.rs:31:17
79+
|
80+
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0000; // issue #131849
81+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
82+
|
83+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0000` (decimal `170141183460469231731687303715884105728`) does not fit into the type `i32`
84+
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0000` will become `0i32`
85+
= help: consider using the type `i128` instead
86+
87+
warning: literal out of range for `i128`
88+
--> $DIR/type-overflow.rs:35:17
89+
|
90+
LL | let fail = -0x8000_0000_0000_0000_0000_0000_0000_0001i128;
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
92+
|
93+
= note: the literal `0x8000_0000_0000_0000_0000_0000_0000_0001i128` (decimal `170141183460469231731687303715884105729`) does not fit into the type `i128`
94+
= note: and the value `-0x8000_0000_0000_0000_0000_0000_0000_0001i128` will become `170141183460469231731687303715884105727i128`
95+
96+
warning: literal out of range for `i8`
97+
--> $DIR/type-overflow.rs:38:16
98+
|
99+
LL | let fail = 340282366920938463463374607431768211455i8;
100+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
101+
|
102+
= note: the literal `340282366920938463463374607431768211455i8` does not fit into the type `i8` whose range is `-128..=127`
103+
= help: consider using the type `u128` instead
104+
105+
warning: literal out of range for `i32`
106+
--> $DIR/type-overflow.rs:42:16
70107
|
71108
LL | let fail = 0x8FFF_FFFF_FFFF_FFFE;
72109
| ^^^^^^^^^^^^^^^^^^^^^
73110
|
74111
= note: the literal `0x8FFF_FFFF_FFFF_FFFE` (decimal `10376293541461622782`) does not fit into the type `i32` and will become `-2i32`
75-
= help: consider using the type `i128` instead
112+
= help: consider using the type `u64` instead
76113
help: to use as a negative number (decimal `-2`), consider using the type `u32` for the literal and cast it to `i32`
77114
|
78115
LL | let fail = 0x8FFF_FFFF_FFFF_FFFEu32 as i32;
79116
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80117

81118
warning: literal out of range for `i8`
82-
--> $DIR/type-overflow.rs:21:17
119+
--> $DIR/type-overflow.rs:46:17
83120
|
84121
LL | let fail = -0b1111_1111i8;
85122
| ^^^^^^^^^^^^^ help: consider using the type `i16` instead: `0b1111_1111i16`
86123
|
87124
= note: the literal `0b1111_1111i8` (decimal `255`) does not fit into the type `i8`
88125
= note: and the value `-0b1111_1111i8` will become `1i8`
89126

90-
warning: 7 warnings emitted
127+
warning: 11 warnings emitted
91128

0 commit comments

Comments
 (0)