-
-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
make defmt attributes forward input attributes #293
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
30b0192
make defmt attributes forward input attributes
japaric 57d3ed1
reject incompatible attributes
japaric 9dfda02
also reject `#[no_mangle]`
japaric 8da13fe
add compile fail tests
japaric 95e0914
document conflicts
japaric 659442e
document new helper function
japaric File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,35 @@ use syn::{ | |
parse_macro_input, | ||
punctuated::Punctuated, | ||
spanned::Spanned as _, | ||
Data, DeriveInput, Expr, ExprPath, Fields, FieldsNamed, FieldsUnnamed, GenericParam, ItemFn, | ||
ItemStruct, LitStr, Path, PathArguments, PathSegment, ReturnType, Token, Type, WhereClause, | ||
WherePredicate, | ||
Attribute, Data, DeriveInput, Expr, ExprPath, Fields, FieldsNamed, FieldsUnnamed, GenericParam, | ||
ItemFn, ItemStruct, LitStr, Path, PathArguments, PathSegment, ReturnType, Token, Type, | ||
WhereClause, WherePredicate, | ||
}; | ||
|
||
fn reject_attributes( | ||
// appears in the error message | ||
attr_name: &str, | ||
attrs_to_check: &[Attribute], | ||
reject_list: &[&str], | ||
) -> parse::Result<()> { | ||
for attr in attrs_to_check { | ||
if let Some(ident) = attr.path.get_ident() { | ||
let ident = ident.to_string(); | ||
if reject_list.contains(&&*ident) { | ||
return Err(parse::Error::new( | ||
attr.span(), | ||
format!( | ||
"`#[{}]` attribute cannot be used together with `#[{}]`", | ||
attr_name, ident | ||
), | ||
)); | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn global_logger(args: TokenStream, input: TokenStream) -> TokenStream { | ||
if !args.is_empty() { | ||
|
@@ -42,8 +66,10 @@ pub fn global_logger(args: TokenStream, input: TokenStream) -> TokenStream { | |
.into(); | ||
} | ||
|
||
let attrs = &s.attrs; | ||
let vis = &s.vis; | ||
quote!( | ||
#(#attrs)* | ||
#vis struct #ident; | ||
|
||
#[no_mangle] | ||
|
@@ -95,8 +121,13 @@ pub fn panic_handler(args: TokenStream, input: TokenStream) -> TokenStream { | |
.into(); | ||
} | ||
|
||
let attrs = &f.attrs; | ||
if let Err(e) = reject_attributes("panic_handler", attrs, &["export_name", "no_mangle"]) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. imo possible attribute clashes should be documented in https://defmt.ferrous-systems.com/panic.html?highlight=panic_han#defmtpanic_handler There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good call |
||
return e.to_compile_error().into(); | ||
} | ||
let block = &f.block; | ||
quote!( | ||
#(#attrs)* | ||
#[export_name = "_defmt_panic"] | ||
fn #ident() -> ! { | ||
#block | ||
|
@@ -141,9 +172,14 @@ pub fn timestamp(args: TokenStream, input: TokenStream) -> TokenStream { | |
.into(); | ||
} | ||
|
||
let attrs = &f.attrs; | ||
if let Err(e) = reject_attributes("timestamp", attrs, &["export_name", "no_mangle"]) { | ||
return e.to_compile_error().into(); | ||
} | ||
let block = &f.block; | ||
quote!( | ||
#[export_name = "_defmt_timestamp"] | ||
#(#attrs)* | ||
fn #ident() -> u64 { | ||
#block | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#[test] | ||
fn ui() { | ||
let t = trybuild::TestCases::new(); | ||
t.compile_fail("tests/ui/*.rs"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[defmt::panic_handler] | ||
#[export_name = "hello"] | ||
fn foo() -> ! { | ||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: `#[panic_handler]` attribute cannot be used together with `#[export_name]` | ||
--> $DIR/panic-handler-export-name.rs:5:1 | ||
| | ||
5 | #[export_name = "hello"] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[defmt::panic_handler] | ||
#[no_mangle] | ||
fn panic() -> ! { | ||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: `#[panic_handler]` attribute cannot be used together with `#[no_mangle]` | ||
--> $DIR/panic-handler-no-mangle.rs:5:1 | ||
| | ||
5 | #[no_mangle] | ||
| ^^^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[defmt::timestamp] | ||
#[export_name = "hello"] | ||
fn foo() -> u64 { | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: `#[timestamp]` attribute cannot be used together with `#[export_name]` | ||
--> $DIR/timestamp-export-name.rs:5:1 | ||
| | ||
5 | #[export_name = "hello"] | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
#[defmt::timestamp] | ||
#[no_mangle] | ||
fn foo() -> u64 { | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
error: `#[timestamp]` attribute cannot be used together with `#[no_mangle]` | ||
--> $DIR/timestamp-no-mangle.rs:5:1 | ||
| | ||
5 | #[no_mangle] | ||
| ^^^^^^^^^^^^ |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add a short description of what this function does?
(nit: the function name is a bit misleading, I would've found something like
check_attribute_conflict
more helpful but that's really splitting hairs.)