ui tests added

This commit is contained in:
Per Lindgren 2021-10-26 19:35:41 +02:00 committed by Henrik Tjäder
parent c0e9da8642
commit 32c11567b3
17 changed files with 229 additions and 227 deletions

View file

@ -1,53 +0,0 @@
//! examples/lockall_soundness.rs
// #![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
use cortex_m_semihosting::{debug, hprintln};
#[shared]
struct Shared {
a: u32,
b: i64,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
foo::spawn().unwrap();
(Shared { a: 1, b: 2 }, Local {}, init::Monotonics())
}
// when omitted priority is assumed to be `1`
#[task(shared = [a, b])]
fn foo(mut c: foo::Context) {
static mut X: Option<&'static mut u32> = None;
static mut Y: u32 = 0;
c.shared.lock(|s| {
hprintln!("s.a = {}, s.b = {}", s.a, s.b).ok();
*s.a += 1;
// soundness check
// c.shared.lock(|s| {}); // borrow error
// c.shared.a.lock(|s| {}); // borrow error
unsafe {
X = Some(&mut Y);
// X = Some(s.a); // lifetime issue
// X = Some(&mut *s.a); // lifetime issue
// X = Some(&'static mut *s.a); // not rust
}
hprintln!("s.a = {}, s.b = {}", s.a, s.b).ok();
});
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}

View file

@ -1,53 +0,0 @@
//! examples/lockall_soundness2.rs
// #![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
use cortex_m_semihosting::{debug, hprintln};
#[shared]
struct Shared {
a: u32,
b: i64,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
foo::spawn().unwrap();
(Shared { a: 1, b: 2 }, Local {}, init::Monotonics())
}
// when omitted priority is assumed to be `1`
#[task(shared = [a, b])]
fn foo(mut c: foo::Context) {
static mut X: Option<&'static mut u32> = None;
static mut Y: u32 = 0;
c.shared.lock(|foo::Shared { a, b }| {
hprintln!("s.a = {}, s.b = {}", a, b).ok();
**a += 1;
// soundness check
// c.shared.lock(|s| {}); // borrow error
// c.shared.a.lock(|s| {}); // borrow error
unsafe {
X = Some(&mut Y);
// X = Some(*a); // lifetime issue
// X = Some(&mut **a); // lifetime issue
// X = Some(&'static mut **a); // not rust
}
hprintln!("s.a = {}, s.b = {}", a, b).ok();
});
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}

View file

@ -1,40 +0,0 @@
//! examples/lockall_soundness3.rs
#![deny(unsafe_code)]
// #![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
use cortex_m_semihosting::{debug, hprintln};
#[shared]
struct Shared {
a: u32,
b: i64,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
foo::spawn().unwrap();
(Shared { a: 1, b: 2 }, Local {}, init::Monotonics())
}
// when omitted priority is assumed to be `1`
#[task(shared = [a, b])]
fn foo(mut c: foo::Context) {
// let s = c.shared.lock(|s| s); // lifetime error
// hprintln!("a {}", s.a).ok();
// let a = c.shared.lock(|foo::Shared { a, b: _ }| a); // lifetime error
// hprintln!("a {}", a).ok();
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}

View file

@ -1,79 +0,0 @@
//! examples/multilock_vs_lockall.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
use cortex_m_semihosting::{debug, hprintln};
#[shared]
struct Shared {
shared1: u32,
shared2: u32,
shared3: u32,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
locks::spawn().unwrap();
(
Shared {
shared1: 0,
shared2: 0,
shared3: 0,
},
Local {},
init::Monotonics(),
)
}
// when omitted priority is assumed to be `1`
#[task(shared = [shared1, shared2, shared3])]
fn locks(c: locks::Context) {
// nested multi-lock
let s1 = c.shared.shared1;
let s2 = c.shared.shared2;
let s3 = c.shared.shared3;
(s1, s2, s3).lock(|s1, s2, s3| {
*s1 += 1;
*s2 += 2;
*s3 += 3;
hprintln!("Multiple locks, s1: {}, s2: {}, s3: {}", s1, s2, s3).ok();
});
// re-construct gives error (consumed by above lock)
let s = locks::SharedResources {
shared1: s1,
shared2: s2,
shared3: s3,
};
// nested multi-lock destruct
let locks::SharedResources {
shared1,
shared2,
shared3,
} = s;
(shared1, shared2, shared3).lock(|s1, s2, s3| {
*s1 += 1;
*s2 += 2;
*s3 += 3;
hprintln!("Multiple locks, s1: {}, s2: {}, s3: {}", s1, s2, s3).ok();
});
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}

View file

@ -52,14 +52,14 @@ mod app {
hprintln!("Multiple locks, s1: {}, s2: {}, s3: {}", s1, s2, s3).ok(); hprintln!("Multiple locks, s1: {}, s2: {}, s3: {}", s1, s2, s3).ok();
}); });
// re-construct gives error (consumed by above lock) // re-construct
let s = locks::SharedResources { let s = locks::SharedResources {
shared1: s1, shared1: s1,
shared2: s2, shared2: s2,
shared3: s3, shared3: s3,
}; };
// nested multi-lock destruct // second nested multi-lock destruct
let locks::SharedResources { let locks::SharedResources {
shared1, shared1,
shared2, shared2,

View file

@ -0,0 +1,24 @@
#![no_main]
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
#[shared]
struct Shared {
a: u32,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
(Shared { a: 0 }, Local {}, init::Monotonics())
}
#[task(shared = [a])]
fn foo(mut c: foo::Context) {
c.shared.lock(|_| {
c.shared.lock(|_| {}); // borrow error
});
}
}

View file

@ -0,0 +1,25 @@
error[E0499]: cannot borrow `c.shared` as mutable more than once at a time
--> ui/lockall_borrow_shared.rs:20:9
|
20 | c.shared.lock(|_| {
| ^ ---- --- first mutable borrow occurs here
| | |
| _________| first borrow later used by call
| |
21 | | c.shared.lock(|_| {}); // borrow error
| | -------- first borrow occurs due to use of `c` in closure
22 | | });
| |__________^ second mutable borrow occurs here
error[E0499]: cannot borrow `c` as mutable more than once at a time
--> ui/lockall_borrow_shared.rs:20:23
|
20 | c.shared.lock(|_| {
| - ---- ^^^ second mutable borrow occurs here
| | |
| _________| first borrow later used by call
| |
21 | | c.shared.lock(|_| {}); // borrow error
| | -------- second borrow occurs due to use of `c` in closure
22 | | });
| |__________- first mutable borrow occurs here

View file

@ -0,0 +1,24 @@
#![no_main]
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
#[shared]
struct Shared {
a: u32,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
(Shared { a: 0 }, Local {}, init::Monotonics())
}
#[task(shared = [a])]
fn foo(mut c: foo::Context) {
c.shared.lock(|_| {
c.shared.a.lock(|_| {}); // borrow error
});
}
}

View file

@ -0,0 +1,25 @@
error[E0499]: cannot borrow `c.shared` as mutable more than once at a time
--> ui/lockall_borrow_shared_a.rs:20:9
|
20 | c.shared.lock(|_| {
| ^ ---- --- first mutable borrow occurs here
| | |
| _________| first borrow later used by call
| |
21 | | c.shared.a.lock(|_| {}); // borrow error
| | ---------- first borrow occurs due to use of `c` in closure
22 | | });
| |__________^ second mutable borrow occurs here
error[E0499]: cannot borrow `c` as mutable more than once at a time
--> ui/lockall_borrow_shared_a.rs:20:23
|
20 | c.shared.lock(|_| {
| - ---- ^^^ second mutable borrow occurs here
| | |
| _________| first borrow later used by call
| |
21 | | c.shared.a.lock(|_| {}); // borrow error
| | ---------- second borrow occurs due to use of `c` in closure
22 | | });
| |__________- first mutable borrow occurs here

22
ui/lockall_lifetime.rs Normal file
View file

@ -0,0 +1,22 @@
#![no_main]
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
#[shared]
struct Shared {
a: u32,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
(Shared { a: 0 }, Local {}, init::Monotonics())
}
#[task(shared = [a])]
fn foo(mut c: foo::Context) {
let _ = c.shared.lock(|s| s); // lifetime
}
}

View file

@ -0,0 +1,8 @@
error: lifetime may not live long enough
--> ui/lockall_lifetime.rs:20:35
|
20 | let _ = c.shared.lock(|s| s); // lifetime
| -- ^ returning this value requires that `'1` must outlive `'2`
| ||
| |return type of closure is &'2 mut __rtic_internal_fooShared
| has type `&'1 mut __rtic_internal_fooShared`

View file

@ -0,0 +1,31 @@
//! examples/lockall_soundness.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
#[shared]
struct Shared {
a: u32,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
(Shared { a: 0 }, Local {}, init::Monotonics())
}
#[task(shared = [a])]
fn foo(mut c: foo::Context) {
let _ = c.shared.lock(|foo::Shared { a }| {
a // lifetime
});
}
}

View file

@ -0,0 +1,9 @@
error: lifetime may not live long enough
--> ui/lockall_lifetime_destruct_field.rs:28:13
|
27 | let _ = c.shared.lock(|foo::Shared { a }| {
| ------------------ return type of closure is &'2 mut &mut u32
| |
| has type `&'1 mut __rtic_internal_fooShared`
28 | a // lifetime
| ^ returning this value requires that `'1` must outlive `'2`

View file

@ -0,0 +1,22 @@
#![no_main]
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
#[shared]
struct Shared {
a: u32,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
(Shared { a: 0 }, Local {}, init::Monotonics())
}
#[task(shared = [a])]
fn foo(mut c: foo::Context) {
let _ = c.shared.lock(|s| &mut *s.a); // lifetime
}
}

View file

@ -0,0 +1,8 @@
error: lifetime may not live long enough
--> ui/lockall_lifetime_reborrow.rs:20:35
|
20 | let _ = c.shared.lock(|s| &mut *s.a); // lifetime
| -- ^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
| ||
| |return type of closure is &'2 mut u32
| has type `&'1 mut __rtic_internal_fooShared`

View file

@ -0,0 +1,24 @@
#![no_main]
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
mod app {
#[shared]
struct Shared {
a: u32,
}
#[local]
struct Local {}
#[init]
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
foo::spawn().unwrap();
(Shared { a: 0 }, Local {}, init::Monotonics())
}
#[task(shared = [a])]
fn foo(mut c: foo::Context) {
let _ = c.shared.lock(|s| s.a);
}
}

View file

@ -0,0 +1,5 @@
error[E0507]: cannot move out of `s.a` which is behind a mutable reference
--> ui/lockall_move_out_field.rs:22:35
|
22 | let _ = c.shared.lock(|s| s.a);
| ^^^ move occurs because `s.a` has type `&mut u32`, which does not implement the `Copy` trait