Skip to content

Commit 178a505

Browse files
committed
Fix lint in a very hacky, fragile way.
1 parent f683b96 commit 178a505

File tree

3 files changed

+24
-188
lines changed

3 files changed

+24
-188
lines changed

compiler/rustc_lint/src/types.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -1041,8 +1041,23 @@ fn get_nullable_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>>
10411041
// must use an Option<fn(..) -> _> to represent it.
10421042
ty
10431043
}
1044+
ty::Alias(ty::Projection, alias_ty) => {
1045+
let ty::AliasTy { def_id, args, .. } = alias_ty;
1046+
debug!("get_nullable_type: alias def_id: {:?}", def_id);
10441047

1045-
// We should only ever reach this case if ty_is_known_nonnull is extended
1048+
let assoc_item = tcx.associated_item(def_id);
1049+
debug!("get_nullable_type: alias associated_item: {:?}", assoc_item);
1050+
1051+
// FIXME: Call `get_nullable_type` with the resolved `alias_ty`.
1052+
if assoc_item.name.as_str() == "NonZeroInner" {
1053+
// `<T as ZeroablePrimitive>::NonZeroInner`'s inner value is the same as `T`.
1054+
return Some(args.type_at(0));
1055+
}
1056+
1057+
debug!("get_nullable_type: unhandled alias: {:?} while checking {:?}", alias_ty, ty);
1058+
return None;
1059+
}
1060+
// We should only ever reach this case if `ty_is_known_nonnull` is extended
10461061
// to other types.
10471062
ref unhandled => {
10481063
debug!(
@@ -1102,13 +1117,7 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
11021117
return Some(get_nullable_type(tcx, field_ty).unwrap());
11031118
}
11041119
WrappingRange { start: 1, .. } => {
1105-
let nullable_type = get_nullable_type(tcx, field_ty);
1106-
1107-
if nullable_type.is_none() {
1108-
bug!("cannot get nullable type for field_ty {field_ty:?}");
1109-
}
1110-
1111-
return Some(nullable_type.unwrap());
1120+
return Some(get_nullable_type(tcx, field_ty).unwrap());
11121121
}
11131122
WrappingRange { start, end } => {
11141123
unreachable!("Unhandled start and end range: ({}, {})", start, end)

tests/ui/lint/clashing-extern-fn.stderr

+2-65
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,3 @@
1-
warning: `extern` block uses type `Option<NonZero<usize>>`, which is not FFI-safe
2-
--> $DIR/clashing-extern-fn.rs:369:43
3-
|
4-
LL | fn option_non_zero_usize() -> Option<core::num::NonZero<usize>>;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
6-
|
7-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
8-
= note: enum has no representation hint
9-
= note: `#[warn(improper_ctypes)]` on by default
10-
11-
warning: `extern` block uses type `Option<NonZero<isize>>`, which is not FFI-safe
12-
--> $DIR/clashing-extern-fn.rs:370:43
13-
|
14-
LL | fn option_non_zero_isize() -> Option<core::num::NonZero<isize>>;
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
16-
|
17-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
18-
= note: enum has no representation hint
19-
20-
warning: `extern` block uses type `Option<Transparent>`, which is not FFI-safe
21-
--> $DIR/clashing-extern-fn.rs:428:46
22-
|
23-
LL | fn hidden_niche_transparent() -> Option<Transparent>;
24-
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
25-
|
26-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
27-
= note: enum has no representation hint
28-
291
warning: `extern` block uses type `Option<TransparentNoNiche>`, which is not FFI-safe
302
--> $DIR/clashing-extern-fn.rs:430:55
313
|
@@ -34,6 +6,7 @@ LL | fn hidden_niche_transparent_no_niche() -> Option<TransparentNoN
346
|
357
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
368
= note: enum has no representation hint
9+
= note: `#[warn(improper_ctypes)]` on by default
3710

3811
warning: `extern` block uses type `Option<UnsafeCell<NonZero<usize>>>`, which is not FFI-safe
3912
--> $DIR/clashing-extern-fn.rs:434:46
@@ -205,30 +178,6 @@ LL | fn non_null_ptr() -> *const usize;
205178
= note: expected `unsafe extern "C" fn() -> NonNull<usize>`
206179
found `unsafe extern "C" fn() -> *const usize`
207180

208-
warning: `option_non_zero_usize` redeclared with a different signature
209-
--> $DIR/clashing-extern-fn.rs:369:13
210-
|
211-
LL | fn option_non_zero_usize() -> usize;
212-
| ----------------------------------- `option_non_zero_usize` previously declared here
213-
...
214-
LL | fn option_non_zero_usize() -> Option<core::num::NonZero<usize>>;
215-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
216-
|
217-
= note: expected `unsafe extern "C" fn() -> usize`
218-
found `unsafe extern "C" fn() -> Option<NonZero<usize>>`
219-
220-
warning: `option_non_zero_isize` redeclared with a different signature
221-
--> $DIR/clashing-extern-fn.rs:370:13
222-
|
223-
LL | fn option_non_zero_isize() -> isize;
224-
| ----------------------------------- `option_non_zero_isize` previously declared here
225-
...
226-
LL | fn option_non_zero_isize() -> Option<core::num::NonZero<isize>>;
227-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
228-
|
229-
= note: expected `unsafe extern "C" fn() -> isize`
230-
found `unsafe extern "C" fn() -> Option<NonZero<isize>>`
231-
232181
warning: `option_non_zero_usize_incorrect` redeclared with a different signature
233182
--> $DIR/clashing-extern-fn.rs:374:13
234183
|
@@ -253,18 +202,6 @@ LL | fn option_non_null_ptr_incorrect() -> *const isize;
253202
= note: expected `unsafe extern "C" fn() -> *const usize`
254203
found `unsafe extern "C" fn() -> *const isize`
255204

256-
warning: `hidden_niche_transparent` redeclared with a different signature
257-
--> $DIR/clashing-extern-fn.rs:428:13
258-
|
259-
LL | fn hidden_niche_transparent() -> usize;
260-
| -------------------------------------- `hidden_niche_transparent` previously declared here
261-
...
262-
LL | fn hidden_niche_transparent() -> Option<Transparent>;
263-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
264-
|
265-
= note: expected `unsafe extern "C" fn() -> usize`
266-
found `unsafe extern "C" fn() -> Option<Transparent>`
267-
268205
warning: `hidden_niche_transparent_no_niche` redeclared with a different signature
269206
--> $DIR/clashing-extern-fn.rs:430:13
270207
|
@@ -289,5 +226,5 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
289226
= note: expected `unsafe extern "C" fn() -> usize`
290227
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`
291228

292-
warning: 25 warnings emitted
229+
warning: 19 warnings emitted
293230

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

+5-115
Original file line numberDiff line numberDiff line change
@@ -45,131 +45,21 @@ note: the type is defined here
4545
LL | enum T {
4646
| ^^^^^^
4747

48-
error: `extern` block uses type `Option<NonZero<u8>>`, which is not FFI-safe
49-
--> $DIR/lint-ctypes-enum.rs:71:21
50-
|
51-
LL | fn nonzero_u8(x: Option<num::NonZero<u8>>);
52-
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
53-
|
54-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
55-
= note: enum has no representation hint
56-
57-
error: `extern` block uses type `Option<NonZero<u16>>`, which is not FFI-safe
58-
--> $DIR/lint-ctypes-enum.rs:72:22
59-
|
60-
LL | fn nonzero_u16(x: Option<num::NonZero<u16>>);
61-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
62-
|
63-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
64-
= note: enum has no representation hint
65-
66-
error: `extern` block uses type `Option<NonZero<u32>>`, which is not FFI-safe
67-
--> $DIR/lint-ctypes-enum.rs:73:22
68-
|
69-
LL | fn nonzero_u32(x: Option<num::NonZero<u32>>);
70-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
71-
|
72-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
73-
= note: enum has no representation hint
74-
75-
error: `extern` block uses type `Option<NonZero<u64>>`, which is not FFI-safe
76-
--> $DIR/lint-ctypes-enum.rs:74:22
77-
|
78-
LL | fn nonzero_u64(x: Option<num::NonZero<u64>>);
79-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
80-
|
81-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
82-
= note: enum has no representation hint
83-
84-
error: `extern` block uses type `Option<NonZero<u128>>`, which is not FFI-safe
48+
error: `extern` block uses type `u128`, which is not FFI-safe
8549
--> $DIR/lint-ctypes-enum.rs:75:23
8650
|
8751
LL | fn nonzero_u128(x: Option<num::NonZero<u128>>);
8852
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
8953
|
90-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
91-
= note: enum has no representation hint
92-
93-
error: `extern` block uses type `Option<NonZero<usize>>`, which is not FFI-safe
94-
--> $DIR/lint-ctypes-enum.rs:77:24
95-
|
96-
LL | fn nonzero_usize(x: Option<num::NonZero<usize>>);
97-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
98-
|
99-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
100-
= note: enum has no representation hint
101-
102-
error: `extern` block uses type `Option<NonZero<i8>>`, which is not FFI-safe
103-
--> $DIR/lint-ctypes-enum.rs:78:21
104-
|
105-
LL | fn nonzero_i8(x: Option<num::NonZero<i8>>);
106-
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
107-
|
108-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
109-
= note: enum has no representation hint
110-
111-
error: `extern` block uses type `Option<NonZero<i16>>`, which is not FFI-safe
112-
--> $DIR/lint-ctypes-enum.rs:79:22
113-
|
114-
LL | fn nonzero_i16(x: Option<num::NonZero<i16>>);
115-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
116-
|
117-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
118-
= note: enum has no representation hint
119-
120-
error: `extern` block uses type `Option<NonZero<i32>>`, which is not FFI-safe
121-
--> $DIR/lint-ctypes-enum.rs:80:22
122-
|
123-
LL | fn nonzero_i32(x: Option<num::NonZero<i32>>);
124-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
125-
|
126-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
127-
= note: enum has no representation hint
128-
129-
error: `extern` block uses type `Option<NonZero<i64>>`, which is not FFI-safe
130-
--> $DIR/lint-ctypes-enum.rs:81:22
131-
|
132-
LL | fn nonzero_i64(x: Option<num::NonZero<i64>>);
133-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
134-
|
135-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
136-
= note: enum has no representation hint
54+
= note: 128-bit integers don't currently have a known stable ABI
13755

138-
error: `extern` block uses type `Option<NonZero<i128>>`, which is not FFI-safe
56+
error: `extern` block uses type `i128`, which is not FFI-safe
13957
--> $DIR/lint-ctypes-enum.rs:82:23
14058
|
14159
LL | fn nonzero_i128(x: Option<num::NonZero<i128>>);
14260
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
14361
|
144-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
145-
= note: enum has no representation hint
146-
147-
error: `extern` block uses type `Option<NonZero<isize>>`, which is not FFI-safe
148-
--> $DIR/lint-ctypes-enum.rs:84:24
149-
|
150-
LL | fn nonzero_isize(x: Option<num::NonZero<isize>>);
151-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
152-
|
153-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
154-
= note: enum has no representation hint
155-
156-
error: `extern` block uses type `Option<TransparentStruct<NonZero<u8>>>`, which is not FFI-safe
157-
--> $DIR/lint-ctypes-enum.rs:85:29
158-
|
159-
LL | fn transparent_struct(x: Option<TransparentStruct<num::NonZero<u8>>>);
160-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
161-
|
162-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
163-
= note: enum has no representation hint
164-
165-
error: `extern` block uses type `Option<TransparentEnum<NonZero<u8>>>`, which is not FFI-safe
166-
--> $DIR/lint-ctypes-enum.rs:86:27
167-
|
168-
LL | fn transparent_enum(x: Option<TransparentEnum<num::NonZero<u8>>>);
169-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
170-
|
171-
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
172-
= note: enum has no representation hint
62+
= note: 128-bit integers don't currently have a known stable ABI
17363

17464
error: `extern` block uses type `Option<TransparentUnion<NonZero<u8>>>`, which is not FFI-safe
17565
--> $DIR/lint-ctypes-enum.rs:87:28
@@ -198,5 +88,5 @@ LL | fn no_result(x: Result<(), num::NonZero<i32>>);
19888
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
19989
= note: enum has no representation hint
20090

201-
error: aborting due to 20 previous errors
91+
error: aborting due to 8 previous errors
20292

0 commit comments

Comments
 (0)