Rollup merge of #149269 - folkertdev:cmse-infer, r=davidtwco

cmse: do not calculate the layout of a type with infer types

tracking issue: https://github.com/rust-lang/rust/issues/81391
tracking issue: https://github.com/rust-lang/rust/issues/75835
fixes https://github.com/rust-lang/rust/issues/130104

Don't calculate the layout of a type with an infer type (`_`). This now emits `LayoutError::Unknown`, causing an error similar to when any other calling convention is used in this location.

The tests use separate functions because only the first such error in a function body is reported.

r? `@davidtwco` (might need some T-types assistance)
This commit is contained in:
Matthias Krüger 2025-12-01 17:55:07 +01:00 committed by GitHub
commit 6e5661eefc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 139 additions and 6 deletions

View file

@ -86,6 +86,11 @@ fn is_valid_cmse_inputs<'tcx>(
let fn_sig = tcx.erase_and_anonymize_regions(fn_sig);
for (ty, hir_ty) in fn_sig.inputs().iter().zip(fn_decl.inputs) {
if ty.has_infer_types() {
let err = LayoutError::Unknown(*ty);
return Err((hir_ty.span, tcx.arena.alloc(err)));
}
let layout = tcx
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(*ty))
.map_err(|e| (hir_ty.span, e))?;
@ -138,6 +143,11 @@ fn is_valid_cmse_output<'tcx>(
return Ok(());
}
if return_type.has_infer_types() {
let err = LayoutError::Unknown(return_type);
return Err(tcx.arena.alloc(err));
}
let typing_env = ty::TypingEnv::fully_monomorphized();
let layout = tcx.layout_of(typing_env.as_query_input(return_type))?;

View file

@ -1,6 +0,0 @@
//@ known-bug: rust-lang/rust#130104
fn main() {
let non_secure_function =
core::mem::transmute::<fn() -> _, extern "cmse-nonsecure-call" fn() -> _>;
}

View file

@ -0,0 +1,34 @@
//@ add-minicore
//@ compile-flags: --target thumbv8m.main-none-eabi --crate-type lib
//@ needs-llvm-components: arm
#![feature(abi_cmse_nonsecure_call, no_core, lang_items)]
#![no_core]
// Infer variables cause panics in layout generation, so the argument/return type is checked for
// whether it contains an infer var, and `LayoutError::Unknown` is emitted if so.
//
// See https://github.com/rust-lang/rust/issues/130104.
extern crate minicore;
use minicore::*;
fn infer_1() {
let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-call" fn() -> _>;
//~^ ERROR type annotations needed
}
fn infer_2() {
let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-call" fn() -> (i32, _)>;
//~^ ERROR type annotations needed
}
fn infer_3() {
let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-call" fn(_: _) -> ()>;
//~^ ERROR type annotations needed
}
fn infer_4() {
let _ =
mem::transmute::<fn(_: (i32, _)) -> (), extern "cmse-nonsecure-call" fn(_: (i32, _)) -> ()>;
//~^ ERROR type annotations needed
}

View file

@ -0,0 +1,27 @@
error[E0282]: type annotations needed
--> $DIR/infer.rs:16:13
|
LL | let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-call" fn() -> _>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error[E0282]: type annotations needed
--> $DIR/infer.rs:21:13
|
LL | let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-call" fn() -> (i32, _)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error[E0282]: type annotations needed
--> $DIR/infer.rs:26:13
|
LL | let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-call" fn(_: _) -> ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error[E0282]: type annotations needed
--> $DIR/infer.rs:32:9
|
LL | mem::transmute::<fn(_: (i32, _)) -> (), extern "cmse-nonsecure-call" fn(_: (i32, _)) -> ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0282`.

View file

@ -0,0 +1,36 @@
//@ add-minicore
//@ compile-flags: --target thumbv8m.main-none-eabi --crate-type lib
//@ needs-llvm-components: arm
#![feature(cmse_nonsecure_entry, no_core, lang_items)]
#![no_core]
// Infer variables cause panics in layout generation, so the argument/return type is checked for
// whether it contains an infer var, and `LayoutError::Unknown` is emitted if so.
//
// See https://github.com/rust-lang/rust/issues/130104.
extern crate minicore;
use minicore::*;
fn infer_1() {
let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-entry" fn() -> _>;
//~^ ERROR type annotations needed
}
fn infer_2() {
let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-entry" fn() -> (i32, _)>;
//~^ ERROR type annotations needed
}
fn infer_3() {
let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-entry" fn(_: _) -> ()>;
//~^ ERROR type annotations needed
}
fn infer_4() {
let _ = mem::transmute::<
//~^ ERROR type annotations needed
fn(_: (i32, _)) -> (),
extern "cmse-nonsecure-entry" fn(_: (i32, _)) -> (),
>;
}

View file

@ -0,0 +1,32 @@
error[E0282]: type annotations needed
--> $DIR/infer.rs:16:13
|
LL | let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-entry" fn() -> _>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error[E0282]: type annotations needed
--> $DIR/infer.rs:21:13
|
LL | let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-entry" fn() -> (i32, _)>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error[E0282]: type annotations needed
--> $DIR/infer.rs:26:13
|
LL | let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-entry" fn(_: _) -> ()>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error[E0282]: type annotations needed
--> $DIR/infer.rs:31:13
|
LL | let _ = mem::transmute::<
| _____________^
LL | |
LL | | fn(_: (i32, _)) -> (),
LL | | extern "cmse-nonsecure-entry" fn(_: (i32, _)) -> (),
LL | | >;
| |_____^ cannot infer type of the type parameter `Src` declared on the function `transmute`
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0282`.