Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit eda9973

Browse files
committedMar 20, 2025
Add #[repr(u128)]/#[repr(i128)] enums to improper_ctypes_definitions
1 parent 2947be7 commit eda9973

File tree

3 files changed

+86
-32
lines changed

3 files changed

+86
-32
lines changed
 

‎compiler/rustc_lint/src/types.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use std::iter;
22
use std::ops::ControlFlow;
33

4-
use rustc_abi::{BackendRepr, TagEncoding, VariantIdx, Variants, WrappingRange};
4+
use rustc_abi::{
5+
BackendRepr, Integer, IntegerType, TagEncoding, VariantIdx, Variants, WrappingRange,
6+
};
57
use rustc_data_structures::fx::FxHashSet;
68
use rustc_errors::DiagMessage;
79
use rustc_hir::intravisit::VisitorExt;
@@ -1246,6 +1248,14 @@ impl<'a, 'tcx> ImproperCTypesVisitor<'a, 'tcx> {
12461248
};
12471249
}
12481250

1251+
if let Some(IntegerType::Fixed(Integer::I128, _)) = def.repr().int {
1252+
return FfiUnsafe {
1253+
ty,
1254+
reason: fluent::lint_improper_ctypes_128bit,
1255+
help: None,
1256+
};
1257+
}
1258+
12491259
use improper_ctypes::check_non_exhaustive_variant;
12501260

12511261
let non_exhaustive = def.variant_list_has_applicable_non_exhaustive();

‎tests/ui/lint/lint-ctypes-enum.rs

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#![deny(improper_ctypes)]
33
#![feature(ptr_internals)]
44
#![feature(transparent_unions)]
5+
#![feature(repr128)]
6+
#![allow(incomplete_features)]
57

68
use std::num;
79

@@ -40,6 +42,20 @@ enum Isize {
4042
C,
4143
}
4244

45+
#[repr(u128)]
46+
enum U128 {
47+
A,
48+
B,
49+
C,
50+
}
51+
52+
#[repr(i128)]
53+
enum I128 {
54+
A,
55+
B,
56+
C,
57+
}
58+
4359
#[repr(transparent)]
4460
struct TransparentStruct<T>(T, std::marker::PhantomData<Z>);
4561

@@ -71,6 +87,8 @@ extern "C" {
7187
fn repr_c(x: ReprC);
7288
fn repr_u8(x: U8);
7389
fn repr_isize(x: Isize);
90+
fn repr_u128(x: U128); //~ ERROR `extern` block uses type `U128`
91+
fn repr_i128(x: I128); //~ ERROR `extern` block uses type `I128`
7492
fn option_ref(x: Option<&'static u8>);
7593
fn option_fn(x: Option<extern "C" fn()>);
7694
fn option_nonnull(x: Option<std::ptr::NonNull<u8>>);

‎tests/ui/lint/lint-ctypes-enum.stderr

+57-31
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
error: `extern` block uses type `U`, which is not FFI-safe
2-
--> $DIR/lint-ctypes-enum.rs:68:14
2+
--> $DIR/lint-ctypes-enum.rs:84:14
33
|
44
LL | fn uf(x: U);
55
| ^ not FFI-safe
66
|
77
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
88
= note: enum has no representation hint
99
note: the type is defined here
10-
--> $DIR/lint-ctypes-enum.rs:9:1
10+
--> $DIR/lint-ctypes-enum.rs:11:1
1111
|
1212
LL | enum U {
1313
| ^^^^^^
@@ -18,51 +18,77 @@ LL | #![deny(improper_ctypes)]
1818
| ^^^^^^^^^^^^^^^
1919

2020
error: `extern` block uses type `B`, which is not FFI-safe
21-
--> $DIR/lint-ctypes-enum.rs:69:14
21+
--> $DIR/lint-ctypes-enum.rs:85:14
2222
|
2323
LL | fn bf(x: B);
2424
| ^ not FFI-safe
2525
|
2626
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
2727
= note: enum has no representation hint
2828
note: the type is defined here
29-
--> $DIR/lint-ctypes-enum.rs:12:1
29+
--> $DIR/lint-ctypes-enum.rs:14:1
3030
|
3131
LL | enum B {
3232
| ^^^^^^
3333

3434
error: `extern` block uses type `T`, which is not FFI-safe
35-
--> $DIR/lint-ctypes-enum.rs:70:14
35+
--> $DIR/lint-ctypes-enum.rs:86:14
3636
|
3737
LL | fn tf(x: T);
3838
| ^ not FFI-safe
3939
|
4040
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
4141
= note: enum has no representation hint
4242
note: the type is defined here
43-
--> $DIR/lint-ctypes-enum.rs:16:1
43+
--> $DIR/lint-ctypes-enum.rs:18:1
4444
|
4545
LL | enum T {
4646
| ^^^^^^
4747

48+
error: `extern` block uses type `U128`, which is not FFI-safe
49+
--> $DIR/lint-ctypes-enum.rs:90:21
50+
|
51+
LL | fn repr_u128(x: U128);
52+
| ^^^^ not FFI-safe
53+
|
54+
= note: 128-bit integers don't currently have a known stable ABI
55+
note: the type is defined here
56+
--> $DIR/lint-ctypes-enum.rs:46:1
57+
|
58+
LL | enum U128 {
59+
| ^^^^^^^^^
60+
61+
error: `extern` block uses type `I128`, which is not FFI-safe
62+
--> $DIR/lint-ctypes-enum.rs:91:21
63+
|
64+
LL | fn repr_i128(x: I128);
65+
| ^^^^ not FFI-safe
66+
|
67+
= note: 128-bit integers don't currently have a known stable ABI
68+
note: the type is defined here
69+
--> $DIR/lint-ctypes-enum.rs:53:1
70+
|
71+
LL | enum I128 {
72+
| ^^^^^^^^^
73+
4874
error: `extern` block uses type `u128`, which is not FFI-safe
49-
--> $DIR/lint-ctypes-enum.rs:82:31
75+
--> $DIR/lint-ctypes-enum.rs:100:31
5076
|
5177
LL | fn option_nonzero_u128(x: Option<num::NonZero<u128>>);
5278
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
5379
|
5480
= note: 128-bit integers don't currently have a known stable ABI
5581

5682
error: `extern` block uses type `i128`, which is not FFI-safe
57-
--> $DIR/lint-ctypes-enum.rs:89:31
83+
--> $DIR/lint-ctypes-enum.rs:107:31
5884
|
5985
LL | fn option_nonzero_i128(x: Option<num::NonZero<i128>>);
6086
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
6187
|
6288
= note: 128-bit integers don't currently have a known stable ABI
6389

6490
error: `extern` block uses type `Option<TransparentUnion<NonZero<u8>>>`, which is not FFI-safe
65-
--> $DIR/lint-ctypes-enum.rs:94:36
91+
--> $DIR/lint-ctypes-enum.rs:112:36
6692
|
6793
LL | fn option_transparent_union(x: Option<TransparentUnion<num::NonZero<u8>>>);
6894
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -71,7 +97,7 @@ LL | fn option_transparent_union(x: Option<TransparentUnion<num::NonZero<u8>
7197
= note: enum has no representation hint
7298

7399
error: `extern` block uses type `Option<Rust<NonZero<u8>>>`, which is not FFI-safe
74-
--> $DIR/lint-ctypes-enum.rs:96:28
100+
--> $DIR/lint-ctypes-enum.rs:114:28
75101
|
76102
LL | fn option_repr_rust(x: Option<Rust<num::NonZero<u8>>>);
77103
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -80,7 +106,7 @@ LL | fn option_repr_rust(x: Option<Rust<num::NonZero<u8>>>);
80106
= note: enum has no representation hint
81107

82108
error: `extern` block uses type `Option<u8>`, which is not FFI-safe
83-
--> $DIR/lint-ctypes-enum.rs:97:21
109+
--> $DIR/lint-ctypes-enum.rs:115:21
84110
|
85111
LL | fn option_u8(x: Option<u8>);
86112
| ^^^^^^^^^^ not FFI-safe
@@ -89,23 +115,23 @@ LL | fn option_u8(x: Option<u8>);
89115
= note: enum has no representation hint
90116

91117
error: `extern` block uses type `u128`, which is not FFI-safe
92-
--> $DIR/lint-ctypes-enum.rs:107:33
118+
--> $DIR/lint-ctypes-enum.rs:125:33
93119
|
94120
LL | fn result_nonzero_u128_t(x: Result<num::NonZero<u128>, ()>);
95121
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
96122
|
97123
= note: 128-bit integers don't currently have a known stable ABI
98124

99125
error: `extern` block uses type `i128`, which is not FFI-safe
100-
--> $DIR/lint-ctypes-enum.rs:114:33
126+
--> $DIR/lint-ctypes-enum.rs:132:33
101127
|
102128
LL | fn result_nonzero_i128_t(x: Result<num::NonZero<i128>, ()>);
103129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
104130
|
105131
= note: 128-bit integers don't currently have a known stable ABI
106132

107133
error: `extern` block uses type `Result<TransparentUnion<NonZero<u8>>, ()>`, which is not FFI-safe
108-
--> $DIR/lint-ctypes-enum.rs:119:38
134+
--> $DIR/lint-ctypes-enum.rs:137:38
109135
|
110136
LL | fn result_transparent_union_t(x: Result<TransparentUnion<num::NonZero<u8>>, ()>);
111137
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -114,7 +140,7 @@ LL | fn result_transparent_union_t(x: Result<TransparentUnion<num::NonZero<u
114140
= note: enum has no representation hint
115141

116142
error: `extern` block uses type `Result<Rust<NonZero<u8>>, ()>`, which is not FFI-safe
117-
--> $DIR/lint-ctypes-enum.rs:121:30
143+
--> $DIR/lint-ctypes-enum.rs:139:30
118144
|
119145
LL | fn result_repr_rust_t(x: Result<Rust<num::NonZero<u8>>, ()>);
120146
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -123,7 +149,7 @@ LL | fn result_repr_rust_t(x: Result<Rust<num::NonZero<u8>>, ()>);
123149
= note: enum has no representation hint
124150

125151
error: `extern` block uses type `Result<NonZero<u8>, U>`, which is not FFI-safe
126-
--> $DIR/lint-ctypes-enum.rs:125:51
152+
--> $DIR/lint-ctypes-enum.rs:143:51
127153
|
128154
LL | fn result_1zst_exhaustive_single_variant_t(x: Result<num::NonZero<u8>, U>);
129155
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -132,7 +158,7 @@ LL | fn result_1zst_exhaustive_single_variant_t(x: Result<num::NonZero<u8>,
132158
= note: enum has no representation hint
133159

134160
error: `extern` block uses type `Result<NonZero<u8>, B>`, which is not FFI-safe
135-
--> $DIR/lint-ctypes-enum.rs:127:53
161+
--> $DIR/lint-ctypes-enum.rs:145:53
136162
|
137163
LL | fn result_1zst_exhaustive_multiple_variant_t(x: Result<num::NonZero<u8>, B>);
138164
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -141,7 +167,7 @@ LL | fn result_1zst_exhaustive_multiple_variant_t(x: Result<num::NonZero<u8>
141167
= note: enum has no representation hint
142168

143169
error: `extern` block uses type `Result<NonZero<u8>, NonExhaustive>`, which is not FFI-safe
144-
--> $DIR/lint-ctypes-enum.rs:129:51
170+
--> $DIR/lint-ctypes-enum.rs:147:51
145171
|
146172
LL | fn result_1zst_non_exhaustive_no_variant_t(x: Result<num::NonZero<u8>, NonExhaustive>);
147173
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -150,7 +176,7 @@ LL | fn result_1zst_non_exhaustive_no_variant_t(x: Result<num::NonZero<u8>,
150176
= note: enum has no representation hint
151177

152178
error: `extern` block uses type `Result<NonZero<u8>, Field>`, which is not FFI-safe
153-
--> $DIR/lint-ctypes-enum.rs:132:49
179+
--> $DIR/lint-ctypes-enum.rs:150:49
154180
|
155181
LL | fn result_1zst_exhaustive_single_field_t(x: Result<num::NonZero<u8>, Field>);
156182
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -159,7 +185,7 @@ LL | fn result_1zst_exhaustive_single_field_t(x: Result<num::NonZero<u8>, Fi
159185
= note: enum has no representation hint
160186

161187
error: `extern` block uses type `Result<Result<(), NonZero<u8>>, ()>`, which is not FFI-safe
162-
--> $DIR/lint-ctypes-enum.rs:134:30
188+
--> $DIR/lint-ctypes-enum.rs:152:30
163189
|
164190
LL | fn result_cascading_t(x: Result<Result<(), num::NonZero<u8>>, ()>);
165191
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -168,23 +194,23 @@ LL | fn result_cascading_t(x: Result<Result<(), num::NonZero<u8>>, ()>);
168194
= note: enum has no representation hint
169195

170196
error: `extern` block uses type `u128`, which is not FFI-safe
171-
--> $DIR/lint-ctypes-enum.rs:145:33
197+
--> $DIR/lint-ctypes-enum.rs:163:33
172198
|
173199
LL | fn result_nonzero_u128_e(x: Result<(), num::NonZero<u128>>);
174200
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
175201
|
176202
= note: 128-bit integers don't currently have a known stable ABI
177203

178204
error: `extern` block uses type `i128`, which is not FFI-safe
179-
--> $DIR/lint-ctypes-enum.rs:152:33
205+
--> $DIR/lint-ctypes-enum.rs:170:33
180206
|
181207
LL | fn result_nonzero_i128_e(x: Result<(), num::NonZero<i128>>);
182208
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
183209
|
184210
= note: 128-bit integers don't currently have a known stable ABI
185211

186212
error: `extern` block uses type `Result<(), TransparentUnion<NonZero<u8>>>`, which is not FFI-safe
187-
--> $DIR/lint-ctypes-enum.rs:157:38
213+
--> $DIR/lint-ctypes-enum.rs:175:38
188214
|
189215
LL | fn result_transparent_union_e(x: Result<(), TransparentUnion<num::NonZero<u8>>>);
190216
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -193,7 +219,7 @@ LL | fn result_transparent_union_e(x: Result<(), TransparentUnion<num::NonZe
193219
= note: enum has no representation hint
194220

195221
error: `extern` block uses type `Result<(), Rust<NonZero<u8>>>`, which is not FFI-safe
196-
--> $DIR/lint-ctypes-enum.rs:159:30
222+
--> $DIR/lint-ctypes-enum.rs:177:30
197223
|
198224
LL | fn result_repr_rust_e(x: Result<(), Rust<num::NonZero<u8>>>);
199225
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -202,7 +228,7 @@ LL | fn result_repr_rust_e(x: Result<(), Rust<num::NonZero<u8>>>);
202228
= note: enum has no representation hint
203229

204230
error: `extern` block uses type `Result<U, NonZero<u8>>`, which is not FFI-safe
205-
--> $DIR/lint-ctypes-enum.rs:163:51
231+
--> $DIR/lint-ctypes-enum.rs:181:51
206232
|
207233
LL | fn result_1zst_exhaustive_single_variant_e(x: Result<U, num::NonZero<u8>>);
208234
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -211,7 +237,7 @@ LL | fn result_1zst_exhaustive_single_variant_e(x: Result<U, num::NonZero<u8
211237
= note: enum has no representation hint
212238

213239
error: `extern` block uses type `Result<B, NonZero<u8>>`, which is not FFI-safe
214-
--> $DIR/lint-ctypes-enum.rs:165:53
240+
--> $DIR/lint-ctypes-enum.rs:183:53
215241
|
216242
LL | fn result_1zst_exhaustive_multiple_variant_e(x: Result<B, num::NonZero<u8>>);
217243
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -220,7 +246,7 @@ LL | fn result_1zst_exhaustive_multiple_variant_e(x: Result<B, num::NonZero<
220246
= note: enum has no representation hint
221247

222248
error: `extern` block uses type `Result<NonExhaustive, NonZero<u8>>`, which is not FFI-safe
223-
--> $DIR/lint-ctypes-enum.rs:167:51
249+
--> $DIR/lint-ctypes-enum.rs:185:51
224250
|
225251
LL | fn result_1zst_non_exhaustive_no_variant_e(x: Result<NonExhaustive, num::NonZero<u8>>);
226252
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -229,7 +255,7 @@ LL | fn result_1zst_non_exhaustive_no_variant_e(x: Result<NonExhaustive, num
229255
= note: enum has no representation hint
230256

231257
error: `extern` block uses type `Result<Field, NonZero<u8>>`, which is not FFI-safe
232-
--> $DIR/lint-ctypes-enum.rs:170:49
258+
--> $DIR/lint-ctypes-enum.rs:188:49
233259
|
234260
LL | fn result_1zst_exhaustive_single_field_e(x: Result<Field, num::NonZero<u8>>);
235261
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -238,7 +264,7 @@ LL | fn result_1zst_exhaustive_single_field_e(x: Result<Field, num::NonZero<
238264
= note: enum has no representation hint
239265

240266
error: `extern` block uses type `Result<(), Result<(), NonZero<u8>>>`, which is not FFI-safe
241-
--> $DIR/lint-ctypes-enum.rs:172:30
267+
--> $DIR/lint-ctypes-enum.rs:190:30
242268
|
243269
LL | fn result_cascading_e(x: Result<(), Result<(), num::NonZero<u8>>>);
244270
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
@@ -247,13 +273,13 @@ LL | fn result_cascading_e(x: Result<(), Result<(), num::NonZero<u8>>>);
247273
= note: enum has no representation hint
248274

249275
error: `extern` block uses type `Result<(), ()>`, which is not FFI-safe
250-
--> $DIR/lint-ctypes-enum.rs:174:27
276+
--> $DIR/lint-ctypes-enum.rs:192:27
251277
|
252278
LL | fn result_unit_t_e(x: Result<(), ()>);
253279
| ^^^^^^^^^^^^^^ not FFI-safe
254280
|
255281
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
256282
= note: enum has no representation hint
257283

258-
error: aborting due to 27 previous errors
284+
error: aborting due to 29 previous errors
259285

0 commit comments

Comments
 (0)
Failed to load comments.