Skip to content

Commit 06658ab

Browse files
committed
cmse: do not calculate the layout of a type with infer types
1 parent 23f7081 commit 06658ab

File tree

6 files changed

+139
-6
lines changed

6 files changed

+139
-6
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/cmse.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ fn is_valid_cmse_inputs<'tcx>(
8686
let fn_sig = tcx.erase_and_anonymize_regions(fn_sig);
8787

8888
for (ty, hir_ty) in fn_sig.inputs().iter().zip(fn_decl.inputs) {
89+
if ty.has_infer_types() {
90+
let err = LayoutError::Unknown(*ty);
91+
return Err((hir_ty.span, tcx.arena.alloc(err)));
92+
}
93+
8994
let layout = tcx
9095
.layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(*ty))
9196
.map_err(|e| (hir_ty.span, e))?;
@@ -138,6 +143,11 @@ fn is_valid_cmse_output<'tcx>(
138143
return Ok(());
139144
}
140145

146+
if return_type.has_infer_types() {
147+
let err = LayoutError::Unknown(return_type);
148+
return Err(tcx.arena.alloc(err));
149+
}
150+
141151
let typing_env = ty::TypingEnv::fully_monomorphized();
142152
let layout = tcx.layout_of(typing_env.as_query_input(return_type))?;
143153

tests/crashes/130104.rs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//@ add-minicore
2+
//@ compile-flags: --target thumbv8m.main-none-eabi --crate-type lib
3+
//@ needs-llvm-components: arm
4+
#![feature(abi_cmse_nonsecure_call, no_core, lang_items)]
5+
#![no_core]
6+
7+
// Infer variables cause panics in layout generation, so the argument/return type is checked for
8+
// whether it contains an infer var, and `LayoutError::Unknown` is emitted if so.
9+
//
10+
// See https://github.com/rust-lang/rust/issues/130104.
11+
12+
extern crate minicore;
13+
use minicore::*;
14+
15+
fn infer_1() {
16+
let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-call" fn() -> _>;
17+
//~^ ERROR type annotations needed
18+
}
19+
20+
fn infer_2() {
21+
let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-call" fn() -> (i32, _)>;
22+
//~^ ERROR type annotations needed
23+
}
24+
25+
fn infer_3() {
26+
let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-call" fn(_: _) -> ()>;
27+
//~^ ERROR type annotations needed
28+
}
29+
30+
fn infer_4() {
31+
let _ =
32+
mem::transmute::<fn(_: (i32, _)) -> (), extern "cmse-nonsecure-call" fn(_: (i32, _)) -> ()>;
33+
//~^ ERROR type annotations needed
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/infer.rs:16:13
3+
|
4+
LL | let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-call" fn() -> _>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
6+
7+
error[E0282]: type annotations needed
8+
--> $DIR/infer.rs:21:13
9+
|
10+
LL | let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-call" fn() -> (i32, _)>;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
12+
13+
error[E0282]: type annotations needed
14+
--> $DIR/infer.rs:26:13
15+
|
16+
LL | let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-call" fn(_: _) -> ()>;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
18+
19+
error[E0282]: type annotations needed
20+
--> $DIR/infer.rs:32:9
21+
|
22+
LL | mem::transmute::<fn(_: (i32, _)) -> (), extern "cmse-nonsecure-call" fn(_: (i32, _)) -> ()>;
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
24+
25+
error: aborting due to 4 previous errors
26+
27+
For more information about this error, try `rustc --explain E0282`.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//@ add-minicore
2+
//@ compile-flags: --target thumbv8m.main-none-eabi --crate-type lib
3+
//@ needs-llvm-components: arm
4+
#![feature(cmse_nonsecure_entry, no_core, lang_items)]
5+
#![no_core]
6+
7+
// Infer variables cause panics in layout generation, so the argument/return type is checked for
8+
// whether it contains an infer var, and `LayoutError::Unknown` is emitted if so.
9+
//
10+
// See https://github.com/rust-lang/rust/issues/130104.
11+
12+
extern crate minicore;
13+
use minicore::*;
14+
15+
fn infer_1() {
16+
let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-entry" fn() -> _>;
17+
//~^ ERROR type annotations needed
18+
}
19+
20+
fn infer_2() {
21+
let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-entry" fn() -> (i32, _)>;
22+
//~^ ERROR type annotations needed
23+
}
24+
25+
fn infer_3() {
26+
let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-entry" fn(_: _) -> ()>;
27+
//~^ ERROR type annotations needed
28+
}
29+
30+
fn infer_4() {
31+
let _ = mem::transmute::<
32+
//~^ ERROR type annotations needed
33+
fn(_: (i32, _)) -> (),
34+
extern "cmse-nonsecure-entry" fn(_: (i32, _)) -> (),
35+
>;
36+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
error[E0282]: type annotations needed
2+
--> $DIR/infer.rs:16:13
3+
|
4+
LL | let _ = mem::transmute::<fn() -> _, extern "cmse-nonsecure-entry" fn() -> _>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
6+
7+
error[E0282]: type annotations needed
8+
--> $DIR/infer.rs:21:13
9+
|
10+
LL | let _ = mem::transmute::<fn() -> (i32, _), extern "cmse-nonsecure-entry" fn() -> (i32, _)>;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
12+
13+
error[E0282]: type annotations needed
14+
--> $DIR/infer.rs:26:13
15+
|
16+
LL | let _ = mem::transmute::<fn(_: _) -> (), extern "cmse-nonsecure-entry" fn(_: _) -> ()>;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type of the type parameter `Src` declared on the function `transmute`
18+
19+
error[E0282]: type annotations needed
20+
--> $DIR/infer.rs:31:13
21+
|
22+
LL | let _ = mem::transmute::<
23+
| _____________^
24+
LL | |
25+
LL | | fn(_: (i32, _)) -> (),
26+
LL | | extern "cmse-nonsecure-entry" fn(_: (i32, _)) -> (),
27+
LL | | >;
28+
| |_____^ cannot infer type of the type parameter `Src` declared on the function `transmute`
29+
30+
error: aborting due to 4 previous errors
31+
32+
For more information about this error, try `rustc --explain E0282`.

0 commit comments

Comments
 (0)