Skip to content

Commit 4f3f906

Browse files
Add some compile-fail tests
1 parent 1d790bd commit 4f3f906

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//! Tests that no `&'static mut` to static mutable resources can be obtained, which would be
2+
//! unsound.
3+
//!
4+
//! Regression test for https://github.com/rust-embedded/cortex-m-rt/issues/212
5+
6+
#![no_std]
7+
#![no_main]
8+
9+
extern crate cortex_m;
10+
extern crate cortex_m_rt;
11+
extern crate panic_halt;
12+
13+
use cortex_m_rt::{entry, exception, interrupt, ExceptionFrame};
14+
15+
#[allow(non_camel_case_types)]
16+
enum interrupt {
17+
UART0,
18+
}
19+
20+
#[exception]
21+
fn SVCall() {
22+
static mut STAT: u8 = 0;
23+
24+
let _stat: &'static mut u8 = STAT; //~ ERROR explicit lifetime required in the type of `STAT`
25+
}
26+
27+
#[interrupt]
28+
fn UART0() {
29+
static mut STAT: u8 = 0;
30+
31+
let _stat: &'static mut u8 = STAT; //~ ERROR explicit lifetime required in the type of `STAT`
32+
}
33+
34+
#[entry]
35+
fn you_died_of_dis_entry() -> ! {
36+
static mut STAT: u8 = 0;
37+
38+
// Allowed. This is sound for the entry point since it is only ever called once, and it makes
39+
// resources far more useful.
40+
let _stat: &'static mut u8 = STAT;
41+
42+
loop {}
43+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Makes sure that the expansion of the attributes doesn't put the resource initializer in an
2+
//! implicit `unsafe` block.
3+
4+
#![no_main]
5+
#![no_std]
6+
7+
extern crate cortex_m_rt;
8+
extern crate panic_halt;
9+
10+
use cortex_m_rt::{entry, exception, interrupt};
11+
12+
#[allow(non_camel_case_types)]
13+
enum interrupt {
14+
UART0,
15+
}
16+
17+
const unsafe fn init() -> u32 { 0 }
18+
19+
#[entry]
20+
fn foo() -> ! {
21+
static mut X: u32 = init(); //~ ERROR requires unsafe
22+
23+
loop {}
24+
}
25+
26+
#[exception]
27+
fn SVCall() {
28+
static mut X: u32 = init(); //~ ERROR requires unsafe
29+
}
30+
31+
#[exception]
32+
fn DefaultHandler(_irq: i16) {
33+
static mut X: u32 = init(); //~ ERROR requires unsafe
34+
}
35+
36+
#[exception]
37+
fn HardFault(_frame: &cortex_m_rt::ExceptionFrame) -> ! {
38+
static mut X: u32 = init(); //~ ERROR requires unsafe
39+
loop {}
40+
}
41+
42+
#[interrupt]
43+
fn UART0() {
44+
static mut X: u32 = init(); //~ ERROR requires unsafe
45+
}

0 commit comments

Comments
 (0)