Skip to content

Commit abfe64f

Browse files
authored
Merge pull request #4110 from tgross35/backport-format-macro-bodies
[0.2] ci: use some tricks to format macro bodies
2 parents e757788 + 7cffa23 commit abfe64f

File tree

110 files changed

+3059
-2846
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+3059
-2846
lines changed

.git-blame-ignore-revs

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Format macro bodies
2+
50f26e08e146b7e9c7d1af9614486eba327d1e31

ci/style.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ enum State {
9797

9898
fn check_style(file: &str, path: &Path, err: &mut Errors) {
9999
let mut state = State::Start;
100-
let mut s_macros = 0;
100+
101+
// FIXME: see below
102+
// let mut s_macros = 0;
103+
101104
let mut f_macros = 0;
102105
let mut in_impl = false;
103106

@@ -140,7 +143,8 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
140143
} else if line.starts_with("type ") && !in_impl {
141144
State::Typedefs
142145
} else if line.starts_with("s! {") {
143-
s_macros += 1;
146+
// FIXME: see below
147+
// s_macros += 1;
144148
State::Structs
145149
} else if line.starts_with("s_no_extra_traits! {") {
146150
// multiple macros of this type are allowed
@@ -175,10 +179,13 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
175179
f_macros += 1;
176180
err.error(path, i, "multiple f! macros in one module");
177181
}
178-
if s_macros == 2 {
179-
s_macros += 1;
180-
err.error(path, i, "multiple s! macros in one module");
181-
}
182+
183+
// FIXME(#4109): multiple should be allowed if at least one is `cfg(not) within `cfg_if`.
184+
// For now just disable this and check by hand.
185+
// if s_macros == 2 {
186+
// s_macros += 1;
187+
// err.error(path, i, "multiple s! macros in one module");
188+
// }
182189

183190
state = line_state;
184191
}

ci/style.sh

+54-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,64 @@
22

33
set -ex
44

5+
if [ -n "${CI:-}" ]; then
6+
rustup toolchain install nightly -c rustfmt --allow-downgrade
7+
rustup override set nightly
8+
9+
check="--check"
10+
fi
11+
512
rustc ci/style.rs && ./style src
613

7-
rustup toolchain install nightly -c rustfmt --allow-downgrade
8-
rustup override set nightly
914
command -v rustfmt
1015
rustfmt -V
11-
cargo fmt --all -- --check
16+
17+
# Save a list of all source files
18+
tmpfile="file-list~" # trailing tilde for gitignore
19+
find src -name '*.rs' > "$tmpfile"
20+
21+
# Before formatting, replace all macro identifiers with a function signature.
22+
# This allows `rustfmt` to format it.
23+
while IFS= read -r file; do
24+
if [ "$file" = "src/macros.rs" ]; then
25+
# Too much special syntax in `macros.rs` that we don't want to format
26+
continue
27+
fi
28+
29+
# Turn all braced macro `foo! { /* ... */ }` invocations into
30+
# `fn foo_fmt_tmp() { /* ... */ }`.
31+
perl -pi -e 's/(?!macro_rules)\b(\w+)!\s*\{/fn $1_fmt_tmp() {/g' "$file"
32+
33+
# Replace `if #[cfg(...)]` within `cfg_if` with `if cfg_tmp!([...])` which
34+
# `rustfmt` will format. We put brackets within the parens so it is easy to
35+
# match (trying to match parentheses would catch the first closing `)` which
36+
# wouldn't be correct for something like `all(any(...), ...)`).
37+
perl -pi -0777 -e 's/if #\[cfg\((.*?)\)\]/if cfg_tmp!([$1])/gms' "$file"
38+
39+
# We have some instances of `{const}` that make macros happy but aren't
40+
# valid syntax. Replace this with just the keyword, plus an indicator
41+
# comment on the preceding line (which is where rustfmt puts it. Also
42+
# rust-lang/rustfmt#5464).
43+
perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file"
44+
45+
# Format the file. We need to invoke `rustfmt` directly since `cargo fmt`
46+
# can't figure out the module tree with the hacks in place.
47+
failed=false
48+
rustfmt --config-path rustfmt.toml "$file" ${check:+"$check"} || failed=true
49+
50+
# Restore all changes to the files.
51+
perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file"
52+
perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file"
53+
perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file"
54+
55+
# Defer emitting the failure until after the files get reset
56+
if [ "$failed" != "false" ]; then
57+
echo "Formatting failed"
58+
exit 1
59+
fi
60+
done < "$tmpfile"
61+
62+
rm "$tmpfile"
1263

1364
if shellcheck --version ; then
1465
find . -name '*.sh' -print0 | xargs -0 shellcheck

rustfmt.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
error_on_line_overflow = true
2+
edition = "2021"

0 commit comments

Comments
 (0)