Skip to content

Commit 8e6411b

Browse files
bugadaniDominaezzz
andauthored
Random cleanups in non-checked packages (#2034)
* Deduplicate feature check macros * Re-enable rust-analyzer for most of the workspace * Cargo fix * Turn off defmt * Only build xtask * Clippy pls * Fix CI * Fix paths * Always create doc directory first * Revert r-a * Update esp-hal-procmacros/src/lp_core.rs Co-authored-by: Dominic Fischer <[email protected]> --------- Co-authored-by: Dominic Fischer <[email protected]>
1 parent fce510f commit 8e6411b

File tree

11 files changed

+142
-106
lines changed

11 files changed

+142
-106
lines changed

.github/workflows/hil.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ jobs:
6060
run: cargo install cross
6161

6262
- name: Build xtasks
63-
run: cross build --release --target ${{ matrix.host.rust-target }}
63+
run: cross build --release --target ${{ matrix.host.rust-target }} -p xtask
6464

6565
- name: Upload artifact
6666
uses: actions/upload-artifact@v4

esp-build/src/lib.rs

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::{io::Write as _, process};
66

77
use proc_macro::TokenStream;
8+
use quote::ToTokens;
89
use syn::{parse_macro_input, punctuated::Punctuated, LitStr, Token};
910
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};
1011

@@ -58,22 +59,10 @@ pub fn assert_unique_features(input: TokenStream) -> TokenStream {
5859
.into_iter()
5960
.collect::<Vec<_>>();
6061

61-
let pairs = unique_pairs(&features);
62-
let unique_cfgs = pairs
63-
.iter()
64-
.map(|(a, b)| quote::quote! { all(feature = #a, feature = #b) });
65-
66-
let message = format!(
67-
r#"
68-
ERROR: expected exactly zero or one enabled feature from feature group:
69-
{:?}
70-
"#,
71-
features.iter().map(|lit| lit.value()).collect::<Vec<_>>(),
72-
);
62+
let unique = impl_unique_features(&features, "exactly zero or one");
7363

7464
quote::quote! {
75-
#[cfg(any(#(#unique_cfgs),*))]
76-
::esp_build::error! { #message }
65+
#unique
7766
}
7867
.into()
7968
}
@@ -91,17 +80,10 @@ pub fn assert_used_features(input: TokenStream) -> TokenStream {
9180
.into_iter()
9281
.collect::<Vec<_>>();
9382

94-
let message = format!(
95-
r#"
96-
ERROR: expected at least one enabled feature from feature group:
97-
{:?}
98-
"#,
99-
features.iter().map(|lit| lit.value()).collect::<Vec<_>>()
100-
);
83+
let used = impl_used_features(&features, "at least one");
10184

10285
quote::quote! {
103-
#[cfg(not(any(#(feature = #features),*)))]
104-
::esp_build::error! { #message }
86+
#used
10587
}
10688
.into()
10789
}
@@ -118,29 +100,54 @@ pub fn assert_unique_used_features(input: TokenStream) -> TokenStream {
118100
.into_iter()
119101
.collect::<Vec<_>>();
120102

121-
let pairs = unique_pairs(&features);
103+
let unique = impl_unique_features(&features, "exactly one");
104+
let used = impl_used_features(&features, "exactly one");
105+
106+
quote::quote! {
107+
#unique
108+
#used
109+
}
110+
.into()
111+
}
112+
113+
// ----------------------------------------------------------------------------
114+
// Helper Functions
115+
116+
fn impl_unique_features(features: &[LitStr], expectation: &str) -> impl ToTokens {
117+
let pairs = unique_pairs(features);
122118
let unique_cfgs = pairs
123119
.iter()
124120
.map(|(a, b)| quote::quote! { all(feature = #a, feature = #b) });
125121

126122
let message = format!(
127123
r#"
128-
ERROR: expected exactly one enabled feature from feature group:
124+
ERROR: expected {expectation} enabled feature from feature group:
125+
{:?}
126+
"#,
127+
features.iter().map(|lit| lit.value()).collect::<Vec<_>>(),
128+
);
129+
130+
quote::quote! {
131+
#[cfg(any(#(#unique_cfgs),*))]
132+
::esp_build::error! { #message }
133+
}
134+
}
135+
136+
fn impl_used_features(features: &[LitStr], expectation: &str) -> impl ToTokens {
137+
let message = format!(
138+
r#"
139+
ERROR: expected {expectation} enabled feature from feature group:
129140
{:?}
130141
"#,
131142
features.iter().map(|lit| lit.value()).collect::<Vec<_>>()
132143
);
133144

134145
quote::quote! {
135-
#[cfg(any(any(#(#unique_cfgs),*), not(any(#(feature = #features),*))))]
146+
#[cfg(not(any(#(feature = #features),*)))]
136147
::esp_build::error! { #message }
137148
}
138-
.into()
139149
}
140150

141-
// ----------------------------------------------------------------------------
142-
// Helper Functions
143-
144151
// Adapted from:
145152
// https://github.com/dtolnay/build-alert/blob/49d060e/src/lib.rs#L54-L93
146153
fn do_alert(color: Color, input: TokenStream) -> TokenStream {

esp-hal-procmacros/src/embassy.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ pub(crate) mod main {
4646
if !f.sig.generics.params.is_empty() {
4747
ctxt.error_spanned_by(&f.sig, "main function must not be generic");
4848
}
49-
if !f.sig.generics.where_clause.is_none() {
49+
if f.sig.generics.where_clause.is_some() {
5050
ctxt.error_spanned_by(&f.sig, "main function must not have `where` clauses");
5151
}
52-
if !f.sig.abi.is_none() {
52+
if f.sig.abi.is_some() {
5353
ctxt.error_spanned_by(&f.sig, "main function must not have an ABI qualifier");
5454
}
55-
if !f.sig.variadic.is_none() {
55+
if f.sig.variadic.is_some() {
5656
ctxt.error_spanned_by(&f.sig, "main function must not be variadic");
5757
}
5858
match &f.sig.output {

esp-hal-procmacros/src/enum_dispatch.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,18 @@ impl Parse for MakeGpioEnumDispatchMacro {
3030

3131
let mut elements = vec![];
3232

33-
let mut stream = input.parse::<Group>()?.stream().into_iter();
33+
let stream = input.parse::<Group>()?.stream().into_iter();
3434
let mut element_name = String::new();
35-
loop {
36-
match stream.next() {
37-
Some(v) => match v {
38-
TokenTree::Ident(ident) => {
39-
element_name = ident.to_string();
40-
}
41-
TokenTree::Literal(lit) => {
42-
let index = lit.to_string().parse().unwrap();
43-
elements.push((element_name.clone(), index));
44-
}
45-
_ => (),
46-
},
47-
None => break,
35+
for v in stream {
36+
match v {
37+
TokenTree::Ident(ident) => {
38+
element_name = ident.to_string();
39+
}
40+
TokenTree::Literal(lit) => {
41+
let index = lit.to_string().parse().unwrap();
42+
elements.push((element_name.clone(), index));
43+
}
44+
_ => (),
4845
}
4946
}
5047

esp-hal-procmacros/src/interrupt.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub(crate) fn check_attr_whitelist(
2424

2525
'o: for attr in attrs {
2626
for val in whitelist {
27-
if eq(&attr, &val) {
27+
if eq(attr, val) {
2828
continue 'o;
2929
}
3030
}
@@ -35,7 +35,7 @@ pub(crate) fn check_attr_whitelist(
3535
}
3636
};
3737

38-
return Err(Error::new(attr.span(), &err_str).to_compile_error().into());
38+
return Err(Error::new(attr.span(), err_str).to_compile_error().into());
3939
}
4040

4141
Ok(())

esp-hal-procmacros/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub fn ram(args: TokenStream, input: TokenStream) -> TokenStream {
210210

211211
let hal = proc_macro2::Ident::new(
212212
if let Ok(FoundCrate::Name(ref name)) = crate_name("esp-hal") {
213-
&name
213+
name
214214
} else {
215215
"crate"
216216
},
@@ -278,7 +278,7 @@ pub fn handler(args: TokenStream, input: TokenStream) -> TokenStream {
278278

279279
let root = Ident::new(
280280
if let Ok(FoundCrate::Name(ref name)) = crate_name("esp-hal") {
281-
&name
281+
name
282282
} else {
283283
"crate"
284284
},

esp-hal-procmacros/src/lp_core.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
2323
let mut res = String::from("__ULP_MAGIC_");
2424
for &a in args {
2525
let t = &a.ty;
26-
let quoted = to_string(&t);
26+
let quoted = to_string(t);
2727
res.push_str(&quoted);
28-
res.push_str("$");
28+
res.push('$');
2929
}
3030

3131
res
3232
}
3333

3434
pub(crate) fn get_simplename(t: &Type) -> String {
35-
String::from(match t {
36-
Type::Path(p) => String::from(&p.path.segments.last().unwrap().ident.to_string()),
35+
match t {
36+
Type::Path(p) => p.path.segments.last().unwrap().ident.to_string(),
3737
_ => String::new(),
38-
})
38+
}
3939
}
4040

4141
pub(crate) fn extract_pin(ty: &Type) -> u8 {
@@ -49,7 +49,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
4949
res = extract_pin(t);
5050
}
5151
GenericArgument::Const(c) => {
52-
res = (&quote! { #c }.to_string()).parse().unwrap();
52+
res = quote! { #c }.to_string().parse().unwrap();
5353
}
5454
_ => (),
5555
}
@@ -68,11 +68,11 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
6868
res.push_str(&segment.ident.to_string());
6969

7070
if let PathArguments::AngleBracketed(g) = &segment.arguments {
71-
res.push_str("<");
71+
res.push('<');
7272
let mut pushed = false;
7373
for arg in &g.args {
7474
if pushed {
75-
res.push_str(",");
75+
res.push(',');
7676
}
7777

7878
match arg {
@@ -87,7 +87,7 @@ pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
8787
_ => (),
8888
}
8989
}
90-
res.push_str(">");
90+
res.push('>');
9191
}
9292
}
9393

@@ -219,7 +219,7 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {
219219
};
220220

221221
let hal_crate = if let Ok(FoundCrate::Name(ref name)) = hal_crate {
222-
let ident = Ident::new(&name, Span::call_site().into());
222+
let ident = Ident::new(name, Span::call_site().into());
223223
quote!( #ident )
224224
} else {
225225
quote!(crate)
@@ -261,12 +261,14 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {
261261

262262
let mut sections: Vec<Section> = sections
263263
.into_iter()
264-
.filter(|section| match section.kind() {
265-
SectionKind::Text
266-
| SectionKind::ReadOnlyData
267-
| SectionKind::Data
268-
| SectionKind::UninitializedData => true,
269-
_ => false,
264+
.filter(|section| {
265+
matches!(
266+
section.kind(),
267+
SectionKind::Text
268+
| SectionKind::ReadOnlyData
269+
| SectionKind::Data
270+
| SectionKind::UninitializedData
271+
)
270272
})
271273
.collect();
272274
sections.sort_by(|a, b| a.address().partial_cmp(&b.address()).unwrap());
@@ -280,9 +282,8 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {
280282

281283
for section in sections {
282284
if section.address() > last_address {
283-
for _ in 0..(section.address() - last_address) {
284-
binary.push(0);
285-
}
285+
let fill = section.address() - last_address;
286+
binary.extend(std::iter::repeat(0).take(fill as usize));
286287
}
287288

288289
binary.extend_from_slice(section.data().unwrap());
@@ -293,21 +294,20 @@ pub fn load_lp_code(input: TokenStream) -> TokenStream {
293294
.symbols()
294295
.find(|s| s.name().unwrap().starts_with("__ULP_MAGIC_"));
295296

296-
if let None = magic_symbol {
297+
let magic_symbol = if let Some(magic_symbol) = magic_symbol {
298+
magic_symbol.name().unwrap()
299+
} else {
297300
return Error::new(
298301
Span::call_site().into(),
299302
"Given file doesn't seem to be an LP/ULP core application.",
300303
)
301304
.to_compile_error()
302305
.into();
303-
}
304-
305-
let magic_symbol = magic_symbol.unwrap().name().unwrap();
306+
};
306307

307308
let magic_symbol = magic_symbol.trim_start_matches("__ULP_MAGIC_");
308309
let args: Vec<proc_macro2::TokenStream> = magic_symbol
309310
.split("$")
310-
.into_iter()
311311
.map(|t| {
312312
let t = if t.contains("OutputOpenDrain") {
313313
t.replace("OutputOpenDrain", "LowPowerOutputOpenDrain")

esp-lp-hal/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ description = "HAL for low-power RISC-V coprocessors found in ESP32 devices"
77
repository = "https://github.com/esp-rs/esp-hal"
88
license = "MIT OR Apache-2.0"
99

10-
[lib]
11-
bench = false
12-
test = false
13-
1410
keywords = [
1511
"embedded",
1612
"embedded-hal",
@@ -24,6 +20,10 @@ categories = [
2420
"no-std",
2521
]
2622

23+
[lib]
24+
bench = false
25+
test = false
26+
2727
[dependencies]
2828
cfg-if = "1.0.0"
2929
document-features = "0.2.10"

0 commit comments

Comments
 (0)