Skip to content

Commit

Permalink
Fix lint in a very hacky, fragile way.
Browse files Browse the repository at this point in the history
  • Loading branch information
reitermarkus committed Mar 2, 2024
1 parent f683b96 commit 178a505
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 188 deletions.
25 changes: 17 additions & 8 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,8 +1041,23 @@ fn get_nullable_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>>
// must use an Option<fn(..) -> _> to represent it.
ty
}
ty::Alias(ty::Projection, alias_ty) => {
let ty::AliasTy { def_id, args, .. } = alias_ty;
debug!("get_nullable_type: alias def_id: {:?}", def_id);

// We should only ever reach this case if ty_is_known_nonnull is extended
let assoc_item = tcx.associated_item(def_id);
debug!("get_nullable_type: alias associated_item: {:?}", assoc_item);

// FIXME: Call `get_nullable_type` with the resolved `alias_ty`.
if assoc_item.name.as_str() == "NonZeroInner" {
// `<T as ZeroablePrimitive>::NonZeroInner`'s inner value is the same as `T`.
return Some(args.type_at(0));
}

debug!("get_nullable_type: unhandled alias: {:?} while checking {:?}", alias_ty, ty);
return None;
}
// We should only ever reach this case if `ty_is_known_nonnull` is extended
// to other types.
ref unhandled => {
debug!(
Expand Down Expand Up @@ -1102,13 +1117,7 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
return Some(get_nullable_type(tcx, field_ty).unwrap());
}
WrappingRange { start: 1, .. } => {
let nullable_type = get_nullable_type(tcx, field_ty);

if nullable_type.is_none() {
bug!("cannot get nullable type for field_ty {field_ty:?}");
}

return Some(nullable_type.unwrap());
return Some(get_nullable_type(tcx, field_ty).unwrap());
}
WrappingRange { start, end } => {
unreachable!("Unhandled start and end range: ({}, {})", start, end)
Expand Down
67 changes: 2 additions & 65 deletions tests/ui/lint/clashing-extern-fn.stderr
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
warning: `extern` block uses type `Option<NonZero<usize>>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:369:43
|
LL | fn option_non_zero_usize() -> Option<core::num::NonZero<usize>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
= note: `#[warn(improper_ctypes)]` on by default

warning: `extern` block uses type `Option<NonZero<isize>>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:370:43
|
LL | fn option_non_zero_isize() -> Option<core::num::NonZero<isize>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

warning: `extern` block uses type `Option<Transparent>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:428:46
|
LL | fn hidden_niche_transparent() -> Option<Transparent>;
| ^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

warning: `extern` block uses type `Option<TransparentNoNiche>`, which is not FFI-safe
--> $DIR/clashing-extern-fn.rs:430:55
|
Expand All @@ -34,6 +6,7 @@ LL | fn hidden_niche_transparent_no_niche() -> Option<TransparentNoN
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
= note: `#[warn(improper_ctypes)]` on by default

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

warning: `option_non_zero_usize` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:369:13
|
LL | fn option_non_zero_usize() -> usize;
| ----------------------------------- `option_non_zero_usize` previously declared here
...
LL | fn option_non_zero_usize() -> Option<core::num::NonZero<usize>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> usize`
found `unsafe extern "C" fn() -> Option<NonZero<usize>>`

warning: `option_non_zero_isize` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:370:13
|
LL | fn option_non_zero_isize() -> isize;
| ----------------------------------- `option_non_zero_isize` previously declared here
...
LL | fn option_non_zero_isize() -> Option<core::num::NonZero<isize>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> isize`
found `unsafe extern "C" fn() -> Option<NonZero<isize>>`

warning: `option_non_zero_usize_incorrect` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:374:13
|
Expand All @@ -253,18 +202,6 @@ LL | fn option_non_null_ptr_incorrect() -> *const isize;
= note: expected `unsafe extern "C" fn() -> *const usize`
found `unsafe extern "C" fn() -> *const isize`

warning: `hidden_niche_transparent` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:428:13
|
LL | fn hidden_niche_transparent() -> usize;
| -------------------------------------- `hidden_niche_transparent` previously declared here
...
LL | fn hidden_niche_transparent() -> Option<Transparent>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ this signature doesn't match the previous declaration
|
= note: expected `unsafe extern "C" fn() -> usize`
found `unsafe extern "C" fn() -> Option<Transparent>`

warning: `hidden_niche_transparent_no_niche` redeclared with a different signature
--> $DIR/clashing-extern-fn.rs:430:13
|
Expand All @@ -289,5 +226,5 @@ LL | fn hidden_niche_unsafe_cell() -> Option<UnsafeCell<NonZero<usiz
= note: expected `unsafe extern "C" fn() -> usize`
found `unsafe extern "C" fn() -> Option<UnsafeCell<NonZero<usize>>>`

warning: 25 warnings emitted
warning: 19 warnings emitted

120 changes: 5 additions & 115 deletions tests/ui/lint/lint-ctypes-enum.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -45,131 +45,21 @@ note: the type is defined here
LL | enum T {
| ^^^^^^

error: `extern` block uses type `Option<NonZero<u8>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:71:21
|
LL | fn nonzero_u8(x: Option<num::NonZero<u8>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<u16>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:72:22
|
LL | fn nonzero_u16(x: Option<num::NonZero<u16>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<u32>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:73:22
|
LL | fn nonzero_u32(x: Option<num::NonZero<u32>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<u64>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:74:22
|
LL | fn nonzero_u64(x: Option<num::NonZero<u64>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<u128>>`, which is not FFI-safe
error: `extern` block uses type `u128`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:75:23
|
LL | fn nonzero_u128(x: Option<num::NonZero<u128>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<usize>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:77:24
|
LL | fn nonzero_usize(x: Option<num::NonZero<usize>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<i8>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:78:21
|
LL | fn nonzero_i8(x: Option<num::NonZero<i8>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<i16>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:79:22
|
LL | fn nonzero_i16(x: Option<num::NonZero<i16>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<i32>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:80:22
|
LL | fn nonzero_i32(x: Option<num::NonZero<i32>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<i64>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:81:22
|
LL | fn nonzero_i64(x: Option<num::NonZero<i64>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
= note: 128-bit integers don't currently have a known stable ABI

error: `extern` block uses type `Option<NonZero<i128>>`, which is not FFI-safe
error: `extern` block uses type `i128`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:82:23
|
LL | fn nonzero_i128(x: Option<num::NonZero<i128>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<NonZero<isize>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:84:24
|
LL | fn nonzero_isize(x: Option<num::NonZero<isize>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<TransparentStruct<NonZero<u8>>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:85:29
|
LL | fn transparent_struct(x: Option<TransparentStruct<num::NonZero<u8>>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint

error: `extern` block uses type `Option<TransparentEnum<NonZero<u8>>>`, which is not FFI-safe
--> $DIR/lint-ctypes-enum.rs:86:27
|
LL | fn transparent_enum(x: Option<TransparentEnum<num::NonZero<u8>>>);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ not FFI-safe
|
= help: consider adding a `#[repr(C)]`, `#[repr(transparent)]`, or integer `#[repr(...)]` attribute to this enum
= note: enum has no representation hint
= note: 128-bit integers don't currently have a known stable ABI

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

error: aborting due to 20 previous errors
error: aborting due to 8 previous errors

0 comments on commit 178a505

Please sign in to comment.