Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,12 @@ pub(crate) fn repr_nullable_ptr<'tcx>(
}

// Return the nullable type this Option-like enum can be safely represented with.
let field_ty_abi = &tcx.layout_of(param_env.and(field_ty)).unwrap().abi;
let field_ty_layout = tcx.layout_of(param_env.and(field_ty));
if field_ty_layout.is_err() && !field_ty.has_non_region_param() {
bug!("should be able to compute the layout of non-polymorphic type");
}

let field_ty_abi = &field_ty_layout.ok()?.abi;
if let Abi::Scalar(field_ty_scalar) = field_ty_abi {
match field_ty_scalar.valid_range(&tcx) {
WrappingRange { start: 0, end }
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/lint/lint-ctypes-option-nonnull-unsized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![deny(improper_ctypes_definitions)]

extern "C" fn foo<T: ?Sized + 'static>() -> Option<&'static T> {
//~^ ERROR `extern` fn uses type `Option<&T>`, which is not FFI-safe
None
}

fn main() {}
16 changes: 16 additions & 0 deletions tests/ui/lint/lint-ctypes-option-nonnull-unsized.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: `extern` fn uses type `Option<&T>`, which is not FFI-safe
--> $DIR/lint-ctypes-option-nonnull-unsized.rs:3:45
|
LL | extern "C" fn foo<T: ?Sized + 'static>() -> Option<&'static T> {
| ^^^^^^^^^^^^^^^^^^ 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: the lint level is defined here
--> $DIR/lint-ctypes-option-nonnull-unsized.rs:1:9
|
LL | #![deny(improper_ctypes_definitions)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error