Skip to content

Commit

Permalink
Unrolled build for rust-lang#115631
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#115631 - compiler-errors:ctypes-unsized, r=davidtwco

Don't ICE when computing ctype's `repr_nullable_ptr` for possibly-unsized ty

We may not always be able to compute the layout of a type like `&T` when `T: ?Sized`, even if we're able to estimate its size skeleton.

r? davidtwco

Fixes rust-lang#115628
  • Loading branch information
rust-timer committed Sep 11, 2023
2 parents e2b3676 + 086cf34 commit f7bf9f1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
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

0 comments on commit f7bf9f1

Please sign in to comment.