Skip to content

Commit 6f71bda

Browse files
committed
Accept 2 or less arguments
1 parent 6cfc6c7 commit 6f71bda

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed

riscv-rt/macros/src/lib.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,16 @@ use proc_macro::TokenStream;
5252
pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
5353
let f = parse_macro_input!(input as ItemFn);
5454

55-
// check the function arguments
5655
#[cfg(not(feature = "u-boot"))]
57-
if f.sig.inputs.len() > 3 {
58-
return parse::Error::new(
59-
f.sig.inputs.last().unwrap().span(),
60-
"`#[entry]` function has too many arguments",
61-
)
62-
.to_compile_error()
63-
.into();
64-
}
56+
let arguments_limit = 3;
6557
#[cfg(feature = "u-boot")]
66-
if f.sig.inputs.len() != 2 {
58+
let arguments_limit = 2;
59+
60+
// check the function arguments
61+
if f.sig.inputs.len() > arguments_limit {
6762
return parse::Error::new(
6863
f.sig.inputs.last().unwrap().span(),
69-
"`#[entry]` function must have exactly two arguments",
64+
"`#[entry]` function has too many arguments",
7065
)
7166
.to_compile_error()
7267
.into();
@@ -90,10 +85,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
9085
}
9186
}
9287
#[cfg(feature = "u-boot")]
93-
{
94-
// We have checked that there are two arguments above.
95-
let (a1, a2) = (&f.sig.inputs[0], &f.sig.inputs[1]);
96-
88+
if let Some(a1) = f.sig.inputs.get(0) {
9789
match a1 {
9890
FnArg::Receiver(_) => {
9991
return parse::Error::new(a1.span(), "invalid argument")
@@ -108,18 +100,20 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
108100
}
109101
}
110102
}
111-
match a2 {
112-
FnArg::Receiver(_) => {
113-
return parse::Error::new(a2.span(), "invalid argument")
114-
.to_compile_error()
115-
.into();
116-
}
117-
FnArg::Typed(t) => {
118-
if !is_simple_type(&t.ty, "usize") {
119-
return parse::Error::new(t.ty.span(), "argument type must be usize")
103+
if let Some(a2) = f.sig.inputs.get(1) {
104+
match a2 {
105+
FnArg::Receiver(_) => {
106+
return parse::Error::new(a2.span(), "invalid argument")
120107
.to_compile_error()
121108
.into();
122109
}
110+
FnArg::Typed(t) => {
111+
if !is_simple_type(&t.ty, "usize") {
112+
return parse::Error::new(t.ty.span(), "argument type must be usize")
113+
.to_compile_error()
114+
.into();
115+
}
116+
}
123117
}
124118
}
125119
}

0 commit comments

Comments
 (0)