Skip to content

Commit

Permalink
Merge #641
Browse files Browse the repository at this point in the history
641: More ergonomic error from static asserts messages r=perlindgren a=korken89

Closes #634

Co-authored-by: Emil Fresk <emil.fresk@gmail.com>
  • Loading branch information
bors[bot] and korken89 authored May 17, 2022
2 parents 6896749 + cd44516 commit 1a24c72
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
25 changes: 22 additions & 3 deletions macros/src/codegen/pre_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
Some((&task.args.priority, &task.args.binds))
}
})) {
let es = format!(
"Maximum priority used by interrupt vector '{}' is more than supported by hardware",
name
);
// Compile time assert that this priority is supported by the device
stmts.push(quote!(let _ = [(); ((1 << #nvic_prio_bits) - #priority as usize)];));
stmts.push(quote!(
const _: () = assert!((1 << #nvic_prio_bits) >= #priority as usize, #es);
));

stmts.push(quote!(
core.NVIC.set_priority(
Expand All @@ -72,8 +78,14 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
None
}
}) {
let es = format!(
"Maximum priority used by interrupt vector '{}' is more than supported by hardware",
name
);
// Compile time assert that this priority is supported by the device
stmts.push(quote!(let _ = [(); ((1 << #nvic_prio_bits) - #priority as usize)];));
stmts.push(quote!(
const _: () = assert!((1 << #nvic_prio_bits) >= #priority as usize, #es);
));

stmts.push(quote!(core.SCB.set_priority(
rtic::export::SystemHandler::#name,
Expand All @@ -90,8 +102,15 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
};
let binds = &monotonic.args.binds;

let name = &monotonic.ident;
let es = format!(
"Maximum priority used by monotonic '{}' is more than supported by hardware",
name
);
// Compile time assert that this priority is supported by the device
stmts.push(quote!(let _ = [(); ((1 << #nvic_prio_bits) - #priority as usize)];));
stmts.push(quote!(
const _: () = assert!((1 << #nvic_prio_bits) >= #priority as usize, #es);
));

let mono_type = &monotonic.ty;

Expand Down
14 changes: 11 additions & 3 deletions ui/task-priority-too-high.stderr
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
warning: unused variable: `cx`
--> ui/task-priority-too-high.rs:12:13
|
12 | fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
| ^^ help: if this is intentional, prefix it with an underscore: `_cx`
|
= note: `#[warn(unused_variables)]` on by default

error[E0080]: evaluation of constant value failed
--> $DIR/task-priority-too-high.rs:3:1
--> ui/task-priority-too-high.rs:3:1
|
3 | #[rtic::app(device = lm3s6965)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to compute `8_usize - 9_usize`, which would overflow
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'Maximum priority used by interrupt vector 'I2C0' is more than supported by hardware', $DIR/ui/task-priority-too-high.rs:3:1
|
= note: this error originates in the attribute macro `rtic::app` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 1a24c72

Please sign in to comment.