Skip to content
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

ci: use some tricks to format macro bodies #4107

Merged
merged 6 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Format macro bodies
a0c7f8017b964a2de8bc3aabebdabd4a8f2c0905
19 changes: 13 additions & 6 deletions ci/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ enum State {

fn check_style(file: &str, path: &Path, err: &mut Errors) {
let mut state = State::Start;
let mut s_macros = 0;

// FIXME: see below
// let mut s_macros = 0;

let mut f_macros = 0;
let mut in_impl = false;

Expand Down Expand Up @@ -140,7 +143,8 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
} else if line.starts_with("type ") && !in_impl {
State::Typedefs
} else if line.starts_with("s! {") {
s_macros += 1;
// FIXME: see below
// s_macros += 1;
State::Structs
} else if line.starts_with("s_no_extra_traits! {") {
// multiple macros of this type are allowed
Expand Down Expand Up @@ -175,10 +179,13 @@ fn check_style(file: &str, path: &Path, err: &mut Errors) {
f_macros += 1;
err.error(path, i, "multiple f! macros in one module");
}
if s_macros == 2 {
s_macros += 1;
err.error(path, i, "multiple s! macros in one module");
}

// FIXME(#4109): multiple should be allowed if at least one is `cfg(not) within `cfg_if`.
// For now just disable this and check by hand.
// if s_macros == 2 {
// s_macros += 1;
// err.error(path, i, "multiple s! macros in one module");
// }

state = line_state;
}
Expand Down
57 changes: 54 additions & 3 deletions ci/style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,64 @@

set -eux

if [ -n "${CI:-}" ]; then
rustup toolchain install nightly -c rustfmt --allow-downgrade
rustup override set nightly

check="--check"
fi

rustc ci/style.rs && ./style src

rustup toolchain install nightly -c rustfmt --allow-downgrade
rustup override set nightly
command -v rustfmt
rustfmt -V
cargo fmt --all -- --check

# Save a list of all source files
tmpfile="file-list~" # trailing tilde for gitignore
find src -name '*.rs' > "$tmpfile"

# Before formatting, replace all macro identifiers with a function signature.
# This allows `rustfmt` to format it.
while IFS= read -r file; do
if [ "$file" = "src/macros.rs" ]; then
# Too much special syntax in `macros.rs` that we don't want to format
continue
fi

# Turn all braced macro `foo! { /* ... */ }` invocations into
# `fn foo_fmt_tmp() { /* ... */ }`.
perl -pi -e 's/(?!macro_rules)\b(\w+)!\s*\{/fn $1_fmt_tmp() {/g' "$file"

# Replace `if #[cfg(...)]` within `cfg_if` with `if cfg_tmp!([...])` which
# `rustfmt` will format. We put brackets within the parens so it is easy to
# match (trying to match parentheses would catch the first closing `)` which
# wouldn't be correct for something like `all(any(...), ...)`).
perl -pi -0777 -e 's/if #\[cfg\((.*?)\)\]/if cfg_tmp!([$1])/gms' "$file"

# We have some instances of `{const}` that make macros happy but aren't
# valid syntax. Replace this with just the keyword, plus an indicator
# comment on the preceding line (which is where rustfmt puts it. Also
# rust-lang/rustfmt#5464).
perl -pi -e 's/^(\s*)(.*)\{const\}/$1\/\* FMT-CONST \*\/\n$1$2const/g' "$file"

# Format the file. We need to invoke `rustfmt` directly since `cargo fmt`
# can't figure out the module tree with the hacks in place.
failed=false
rustfmt --config-path rustfmt.toml "$file" ${check:+"$check"} || failed=true

# Restore all changes to the files.
perl -pi -e 's/fn (\w+)_fmt_tmp\(\)/$1!/g' "$file"
perl -pi -0777 -e 's/cfg_tmp!\(\[(.*?)\]\)/#[cfg($1)]/gms' "$file"
perl -pi -0777 -e 's/\/\* FMT-CONST \*\/(?:\n\s*)?(.*?)const/$1\{const\}/gms' "$file"

# Defer emitting the failure until after the files get reset
if [ "$failed" != "false" ]; then
echo "Formatting failed"
exit 1
fi
done < "$tmpfile"

rm "$tmpfile"

if shellcheck --version ; then
find . -name '*.sh' -print0 | xargs -0 shellcheck
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
error_on_line_overflow = true
edition = "2021"
21 changes: 19 additions & 2 deletions src/fixed_width_ints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ pub type uint32_t = u32;
pub type uint64_t = u64;

cfg_if! {
if #[cfg(all(target_arch = "aarch64", not(any(target_os = "windows", target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos"))))] {
if #[cfg(all(
target_arch = "aarch64",
not(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
))
))] {
// This introduces partial support for FFI with __int128 and
// equivalent types on platforms where Rust's definition is validated
// to match the standard C ABI of that platform.
Expand Down Expand Up @@ -93,7 +102,15 @@ cfg_if! {

// static_assert_eq!(core::mem::size_of::<__uint128_t>(), _SIZE_128);
// static_assert_eq!(core::mem::align_of::<__uint128_t>(), _ALIGN_128);
} else if #[cfg(all(target_arch = "aarch64", any(target_os = "macos", target_os = "ios", target_os = "tvos", target_os = "watchos")))] {
} else if #[cfg(all(
target_arch = "aarch64",
any(
target_os = "macos",
target_os = "ios",
target_os = "tvos",
target_os = "watchos"
)
))] {
/// C `__int128_t`
pub type __int128_t = i128;
/// C `__uint128_t`
Expand Down
Loading