From 4f3f906c32e065918bf2d1566f8b1b6e7b3b71ce Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Thu, 21 Nov 2019 22:06:48 +0100 Subject: Add some compile-fail tests --- .../tests/compile-fail/non-static-resource.rs | 43 ++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 cortex-m-rt/tests/compile-fail/non-static-resource.rs (limited to 'cortex-m-rt/tests/compile-fail/non-static-resource.rs') 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 {} +} -- cgit v1.2.3 From f868247e5de24348e812df7cd324b26e5f8ceda0 Mon Sep 17 00:00:00 2001 From: Adam Greig Date: Wed, 12 Jan 2022 01:46:31 +0000 Subject: Fix cortex-m-rt compiletest tests --- cortex-m-rt/Cargo.toml | 4 +--- cortex-m-rt/tests/compile-fail/non-static-resource.rs | 1 - cortex-m-rt/tests/compiletest.rs | 6 +++--- 3 files changed, 4 insertions(+), 7 deletions(-) (limited to 'cortex-m-rt/tests/compile-fail/non-static-resource.rs') diff --git a/cortex-m-rt/Cargo.toml b/cortex-m-rt/Cargo.toml index 8e4de1f..be5f5fc 100644 --- a/cortex-m-rt/Cargo.toml +++ b/cortex-m-rt/Cargo.toml @@ -18,8 +18,6 @@ links = "cortex-m-rt" # Prevent multiple versions of cortex-m-rt being linked [dependencies] cortex-m-rt-macros = { path = "macros", version = "=0.7.0" } -# Note: Do not depend on `cortex-m` here. This crate is used for testing `cortex-m`, so we need to -# avoid pulling in multiple versions of `cortex-m`. [dev-dependencies] cortex-m = { version = "0.7.4", path = ".." } @@ -27,7 +25,7 @@ panic-halt = "0.2.0" cortex-m-semihosting = "0.3" [target.'cfg(not(target_os = "none"))'.dev-dependencies] -compiletest_rs = "0.4.0" +compiletest_rs = "0.7" [[example]] name = "device" diff --git a/cortex-m-rt/tests/compile-fail/non-static-resource.rs b/cortex-m-rt/tests/compile-fail/non-static-resource.rs index ae009a9..970bbae 100644 --- a/cortex-m-rt/tests/compile-fail/non-static-resource.rs +++ b/cortex-m-rt/tests/compile-fail/non-static-resource.rs @@ -6,7 +6,6 @@ #![no_std] #![no_main] -extern crate cortex_m; extern crate cortex_m_rt; extern crate panic_halt; diff --git a/cortex-m-rt/tests/compiletest.rs b/cortex-m-rt/tests/compiletest.rs index ba2ef73..cf63d22 100644 --- a/cortex-m-rt/tests/compiletest.rs +++ b/cortex-m-rt/tests/compiletest.rs @@ -7,11 +7,11 @@ fn run_mode(mode: &'static str) { config.mode = mode.parse().expect("Invalid mode"); config.src_base = PathBuf::from(format!("tests/{}", mode)); - // config.link_deps(); // Populate config.target_rustcflags with dependencies on the path config.target_rustcflags = Some( - "-L target/debug -L target/debug/deps -C panic=abort --cfg feature=\"device\"".to_owned(), + "-L ../target/debug -L ../target/debug/deps -C panic=abort --cfg feature=\"device\"" + .to_owned(), ); - // config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464 + config.clean_rmeta(); // If your tests import the parent crate, this helps with E0464 compiletest::run_tests(&config); } -- cgit v1.2.3 From 27434e0ce0b67abdd06c1a1b9a7b5ea9121d039e Mon Sep 17 00:00:00 2001 From: Adam Greig Date: Fri, 21 Jan 2022 00:49:59 +0000 Subject: Fix break in compilefail tests on 1.58 --- cortex-m-rt/tests/compile-fail/non-static-resource.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'cortex-m-rt/tests/compile-fail/non-static-resource.rs') diff --git a/cortex-m-rt/tests/compile-fail/non-static-resource.rs b/cortex-m-rt/tests/compile-fail/non-static-resource.rs index 970bbae..a603728 100644 --- a/cortex-m-rt/tests/compile-fail/non-static-resource.rs +++ b/cortex-m-rt/tests/compile-fail/non-static-resource.rs @@ -20,14 +20,16 @@ enum interrupt { fn SVCall() { static mut STAT: u8 = 0; - let _stat: &'static mut u8 = STAT; //~ ERROR explicit lifetime required in the type of `STAT` + let _stat: &'static mut u8 = STAT; + //~^ ERROR lifetime of reference outlives lifetime of borrowed content } #[interrupt] fn UART0() { static mut STAT: u8 = 0; - let _stat: &'static mut u8 = STAT; //~ ERROR explicit lifetime required in the type of `STAT` + let _stat: &'static mut u8 = STAT; + //~^ ERROR lifetime of reference outlives lifetime of borrowed content } #[entry] -- cgit v1.2.3