aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jonas Schievink <jonasschievink@gmail.com> 2019-11-21 22:06:48 +0100
committerGravatar Jonas Schievink <jonasschievink@gmail.com> 2019-11-21 22:27:44 +0100
commit4f3f906c32e065918bf2d1566f8b1b6e7b3b71ce (patch)
treeb10af69f6639d92190b266ef84b0189f6dda41dd
parent1d790bd2858b2c1827322eb872a2e05f05bcd694 (diff)
downloadcortex-m-4f3f906c32e065918bf2d1566f8b1b6e7b3b71ce.tar.gz
cortex-m-4f3f906c32e065918bf2d1566f8b1b6e7b3b71ce.tar.zst
cortex-m-4f3f906c32e065918bf2d1566f8b1b6e7b3b71ce.zip
Add some compile-fail tests
-rw-r--r--cortex-m-rt/tests/compile-fail/non-static-resource.rs43
-rw-r--r--cortex-m-rt/tests/compile-fail/unsafe-init-static.rs45
2 files changed, 88 insertions, 0 deletions
diff --git a/cortex-m-rt/tests/compile-fail/non-static-resource.rs b/cortex-m-rt/tests/compile-fail/non-static-resource.rs
new file mode 100644
index 0000000..ae009a9
--- /dev/null
+++ b/cortex-m-rt/tests/compile-fail/non-static-resource.rs
@@ -0,0 +1,43 @@
+//! Tests that no `&'static mut` to static mutable resources can be obtained, which would be
+//! unsound.
+//!
+//! Regression test for https://github.com/rust-embedded/cortex-m-rt/issues/212
+
+#![no_std]
+#![no_main]
+
+extern crate cortex_m;
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception, interrupt, ExceptionFrame};
+
+#[allow(non_camel_case_types)]
+enum interrupt {
+ UART0,
+}
+
+#[exception]
+fn SVCall() {
+ static mut STAT: u8 = 0;
+
+ let _stat: &'static mut u8 = STAT; //~ ERROR explicit lifetime required in the type of `STAT`
+}
+
+#[interrupt]
+fn UART0() {
+ static mut STAT: u8 = 0;
+
+ let _stat: &'static mut u8 = STAT; //~ ERROR explicit lifetime required in the type of `STAT`
+}
+
+#[entry]
+fn you_died_of_dis_entry() -> ! {
+ static mut STAT: u8 = 0;
+
+ // Allowed. This is sound for the entry point since it is only ever called once, and it makes
+ // resources far more useful.
+ let _stat: &'static mut u8 = STAT;
+
+ loop {}
+}
diff --git a/cortex-m-rt/tests/compile-fail/unsafe-init-static.rs b/cortex-m-rt/tests/compile-fail/unsafe-init-static.rs
new file mode 100644
index 0000000..c040173
--- /dev/null
+++ b/cortex-m-rt/tests/compile-fail/unsafe-init-static.rs
@@ -0,0 +1,45 @@
+//! Makes sure that the expansion of the attributes doesn't put the resource initializer in an
+//! implicit `unsafe` block.
+
+#![no_main]
+#![no_std]
+
+extern crate cortex_m_rt;
+extern crate panic_halt;
+
+use cortex_m_rt::{entry, exception, interrupt};
+
+#[allow(non_camel_case_types)]
+enum interrupt {
+ UART0,
+}
+
+const unsafe fn init() -> u32 { 0 }
+
+#[entry]
+fn foo() -> ! {
+ static mut X: u32 = init(); //~ ERROR requires unsafe
+
+ loop {}
+}
+
+#[exception]
+fn SVCall() {
+ static mut X: u32 = init(); //~ ERROR requires unsafe
+}
+
+#[exception]
+fn DefaultHandler(_irq: i16) {
+ static mut X: u32 = init(); //~ ERROR requires unsafe
+}
+
+#[exception]
+fn HardFault(_frame: &cortex_m_rt::ExceptionFrame) -> ! {
+ static mut X: u32 = init(); //~ ERROR requires unsafe
+ loop {}
+}
+
+#[interrupt]
+fn UART0() {
+ static mut X: u32 = init(); //~ ERROR requires unsafe
+}