Skip to content

Commit 6c0464c

Browse files
committed
Even better error messages
# Please enter the commit message for your changes. Lines starting Formating fix
1 parent 447798a commit 6c0464c

File tree

2 files changed

+50
-24
lines changed

2 files changed

+50
-24
lines changed

cortex-m-rt/macros/src/lib.rs

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
150150
})
151151
.collect::<Vec<_>>();
152152

153-
if let Err(error) = check_attr_whitelist(&f.attrs) {
153+
if let Err(error) = check_attr_whitelist(&f.attrs, WhiteListCaller::Entry) {
154154
return error;
155155
}
156156

@@ -294,11 +294,10 @@ pub fn exception(args: TokenStream, input: TokenStream) -> TokenStream {
294294
.into();
295295
}
296296

297-
if let Err(error) = check_attr_whitelist(&f.attrs) {
297+
if let Err(error) = check_attr_whitelist(&f.attrs, WhiteListCaller::Exception) {
298298
return error;
299299
}
300300

301-
302301
let fspan = f.span();
303302
let ident = f.sig.ident.clone();
304303

@@ -675,7 +674,7 @@ pub fn interrupt(args: TokenStream, input: TokenStream) -> TokenStream {
675674
})
676675
.collect::<Vec<_>>();
677676

678-
if let Err(error) = check_attr_whitelist(&f.attrs) {
677+
if let Err(error) = check_attr_whitelist(&f.attrs, WhiteListCaller::Interrupt) {
679678
return error;
680679
}
681680

@@ -757,6 +756,10 @@ pub fn pre_init(args: TokenStream, input: TokenStream) -> TokenStream {
757756
.into();
758757
}
759758

759+
if let Err(error) = check_attr_whitelist(&f.attrs, WhiteListCaller::PreInit) {
760+
return error;
761+
}
762+
760763
// XXX should we blacklist other attributes?
761764
let attrs = f.attrs;
762765
let ident = f.sig.ident;
@@ -823,8 +826,24 @@ fn extract_cfgs(attrs: Vec<Attribute>) -> (Vec<Attribute>, Vec<Attribute>) {
823826
(cfgs, not_cfgs)
824827
}
825828

826-
fn check_attr_whitelist(attrs: &[Attribute]) -> Result<(), TokenStream> {
827-
let whitelist = &["doc", "link_section", "cfg", "allow", "warn", "deny", "forbid", "cold"];
829+
enum WhiteListCaller {
830+
Entry,
831+
Exception,
832+
Interrupt,
833+
PreInit,
834+
}
835+
836+
fn check_attr_whitelist(attrs: &[Attribute], caller: WhiteListCaller) -> Result<(), TokenStream> {
837+
let whitelist = &[
838+
"doc",
839+
"link_section",
840+
"cfg",
841+
"allow",
842+
"warn",
843+
"deny",
844+
"forbid",
845+
"cold",
846+
];
828847
let cortex_m_rt_blacklist = &["entry", "exception", "interrupt", "pre_init"];
829848

830849
'o: for attr in attrs {
@@ -836,27 +855,34 @@ fn check_attr_whitelist(attrs: &[Attribute]) -> Result<(), TokenStream> {
836855

837856
for val in cortex_m_rt_blacklist {
838857
if eq(&attr, &val) {
839-
return Err(
840-
parse::Error::new(
841-
attr.span(),
842-
"multiple cortex-m-rt attributes are not supported on the same function",
843-
)
858+
let err_str = match caller {
859+
WhiteListCaller::Entry => {
860+
&"this attribute is not allowed on a cortex-m-rt entry point"
861+
}
862+
WhiteListCaller::Exception => {
863+
&"this attribute is not allowed on an exception handler"
864+
}
865+
WhiteListCaller::Interrupt => {
866+
&"this attribute is not allowed on an interrupt handler"
867+
}
868+
WhiteListCaller::PreInit => {
869+
&"this attribute is not allowed on an interrupt handler"
870+
}
871+
};
872+
873+
return Err(parse::Error::new(attr.span(), err_str)
844874
.to_compile_error()
845-
.into(),
846-
);
875+
.into());
847876
}
848877
}
849878

850-
return Err(
851-
parse::Error::new(
852-
attr.span(),
853-
"this attribute is not allowed on a function controlled by cortex-m-rt",
854-
)
855-
.to_compile_error()
856-
.into(),
857-
);
858-
859-
};
879+
return Err(parse::Error::new(
880+
attr.span(),
881+
"this attribute is not allowed on a function controlled by cortex-m-rt",
882+
)
883+
.to_compile_error()
884+
.into());
885+
}
860886

861887
Ok(())
862888
}

cortex-m-rt/tests/compile-fail/whitelist-double-attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate panic_halt;
77
use cortex_m_rt::{entry, exception};
88

99
#[exception]
10-
#[entry] //~ ERROR multiple cortex-m-rt attributes are not supported on the same function
10+
#[entry] //~ ERROR this attribute is not allowed on an exception handler
1111
fn SVCall() -> ! {
1212
loop {}
1313
}

0 commit comments

Comments
 (0)