Skip to content

Commit 3ba1632

Browse files
datdenkiknietAfoHT
authored andcommitted
rtic-macros: forward attributes applied to app module
Instead of ignoring additional attributes applied to the app module, we can forward them to the generated code.
1 parent fa2c8c1 commit 3ba1632

File tree

8 files changed

+78
-2
lines changed

8 files changed

+78
-2
lines changed

rtic-macros/src/codegen.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ pub fn app(app: &App, analysis: &Analysis) -> TokenStream2 {
4444
let user_code = &app.user_code;
4545
let name = &app.name;
4646
let device = &app.args.device;
47+
let attribute_metas = &app.attribute_metas;
4748

4849
let rt_err = util::rt_err_ident();
4950
let async_limit = bindings::async_prio_limit(app, analysis);
5051

5152
quote!(
5253
/// The RTIC application module
54+
#(#[#attribute_metas])*
5355
pub mod #name {
5456
/// Always include the device crate which contains the vector table
5557
use #device as #rt_err;

rtic-macros/src/syntax/ast.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Abstract Syntax Tree
22
3-
use syn::{Attribute, Expr, Ident, Item, ItemUse, Pat, PatType, Path, Stmt, Type};
3+
use syn::{Attribute, Expr, Ident, Item, ItemUse, Meta, Pat, PatType, Path, Stmt, Type};
44

55
use crate::syntax::{backend::BackendArgs, Map};
66

@@ -11,6 +11,9 @@ pub struct App {
1111
/// The arguments to the `#[app]` attribute
1212
pub args: AppArgs,
1313

14+
/// All attributes applied to the `#[app]` module (meta only)
15+
pub attribute_metas: Vec<Meta>,
16+
1417
/// The name of the `const` item on which the `#[app]` attribute has been placed
1518
pub name: Ident,
1619

rtic-macros/src/syntax/parse.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syn::{
1111
braced,
1212
parse::{self, Parse, ParseStream, Parser},
1313
token::Brace,
14-
Ident, Item, LitInt, Token,
14+
Attribute, Ident, Item, LitInt, Meta, Token,
1515
};
1616

1717
use crate::syntax::{
@@ -28,6 +28,7 @@ pub fn app(args: TokenStream2, input: TokenStream2) -> parse::Result<App> {
2828
}
2929

3030
pub(crate) struct Input {
31+
pub attribute_metas: Vec<Meta>,
3132
_mod_token: Token![mod],
3233
pub ident: Ident,
3334
_brace_token: Brace,
@@ -48,12 +49,18 @@ impl Parse for Input {
4849

4950
let content;
5051

52+
let mut attributes = input.call(Attribute::parse_outer)?;
5153
let _mod_token = input.parse()?;
5254
let ident = input.parse()?;
5355
let _brace_token = braced!(content in input);
56+
let inner_attributes = content.call(Attribute::parse_inner)?;
5457
let items = content.call(parse_items)?;
5558

59+
attributes.extend(inner_attributes);
60+
let attribute_metas = attributes.into_iter().map(|a| a.meta).collect();
61+
5662
Ok(Input {
63+
attribute_metas,
5764
_mod_token,
5865
ident,
5966
_brace_token,

rtic-macros/src/syntax/parse/app.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,7 @@ impl App {
531531
}
532532

533533
Ok(App {
534+
attribute_metas: input.attribute_metas,
534535
args,
535536
name: input.ident,
536537
init,

rtic/ui/inner_attribute.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#![no_main]
2+
#![deny(unfulfilled_lint_expectations)]
3+
4+
#[rtic::app(device = lm3s6965)]
5+
mod app {
6+
#![expect(while_true)]
7+
8+
#[shared]
9+
struct Shared {
10+
#[unsafe(link_section = ".custom_section")]
11+
foo: (),
12+
}
13+
14+
#[local]
15+
struct Local {}
16+
17+
#[init]
18+
fn init(_cx: init::Context) -> (Shared, Local) {
19+
(Shared { foo: () }, Local {})
20+
}
21+
}

rtic/ui/inner_attribute.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: this lint expectation is unfulfilled
2+
--> ui/inner_attribute.rs:6:15
3+
|
4+
6 | #![expect(while_true)]
5+
| ^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> ui/inner_attribute.rs:2:9
9+
|
10+
2 | #![deny(unfulfilled_lint_expectations)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

rtic/ui/outer_attribute.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#![no_main]
2+
#![deny(unfulfilled_lint_expectations)]
3+
4+
#[rtic::app(device = lm3s6965)]
5+
#[expect(while_true)]
6+
mod app {
7+
#[shared]
8+
struct Shared {
9+
#[unsafe(link_section = ".custom_section")]
10+
foo: (),
11+
}
12+
13+
#[local]
14+
struct Local {}
15+
16+
#[init]
17+
fn init(_cx: init::Context) -> (Shared, Local) {
18+
(Shared { foo: () }, Local {})
19+
}
20+
}

rtic/ui/outer_attribute.stderr

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: this lint expectation is unfulfilled
2+
--> ui/outer_attribute.rs:5:10
3+
|
4+
5 | #[expect(while_true)]
5+
| ^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> ui/outer_attribute.rs:2:9
9+
|
10+
2 | #![deny(unfulfilled_lint_expectations)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)