Skip to content

refactor: add move check_align to parse_alignment#153189

Open
JayanAXHF wants to merge 1 commit intorust-lang:mainfrom
JayanAXHF:refactor/check_attrs_reftor_1
Open

refactor: add move check_align to parse_alignment#153189
JayanAXHF wants to merge 1 commit intorust-lang:mainfrom
JayanAXHF:refactor/check_attrs_reftor_1

Conversation

@JayanAXHF
Copy link
Member

@JayanAXHF JayanAXHF commented Feb 27, 2026

Part of #153101

r? @JonathanBrouwer

PS: jonathan i'm not sure about what to do with check_align now

@rustbot
Copy link
Collaborator

rustbot commented Feb 27, 2026

Some changes occurred in compiler/rustc_attr_parsing

cc @jdonszelmann, @JonathanBrouwer

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 27, 2026
@JayanAXHF JayanAXHF added C-cleanup Category: PRs that clean code up or issues documenting cleanup. A-AST Area: AST labels Feb 27, 2026
let max = Size::from_bits(cx.sess.target.pointer_width).signed_int_max() as u64;
// lit must be < 2^29
let lit_u64 = literal.get().try_into().map_err(|_| "alignment larger than 2^29")?;
let align = Align::from_bytes(lit_u64).map_err(|_| "alignment larger than 2^29")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change this code back to how it was with the and_then so that the error message isn't duplicated?

return Err("not a power of two");
}
// alignment must not be larger than the pointer width (`isize::MAX`)
let max = Size::from_bits(cx.sess.target.pointer_width).signed_int_max() as u64;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Could you put this variable next to if statement where it is used?

let lit_u64 = literal.get().try_into().map_err(|_| "alignment larger than 2^29")?;
let align = Align::from_bytes(lit_u64).map_err(|_| "alignment larger than 2^29")?;
if align.bytes() > max {
return Err("alignment larger than pointer width: {max}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not how formatting works, you need to make this a format string :P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh how did that get though :3

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out, since this needs to return a &'static str, i can't include the max in the error message

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning a String is also fine :) I don't think this needs to return a 'static str, that's just what it happens to atm

Copy link
Member Author

@JayanAXHF JayanAXHF Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it needs the &'static str because session_diagnostics::InvalidAlignmentValue<'a> takes an &'a str. I could probably change that since it isn't used in many other places
Note: maybe a Cow<'static, str> would be better?

Copy link
Contributor

@JonathanBrouwer JonathanBrouwer Feb 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also just make that take a String :)
These are diagnostics so performance is not significant, just do what is easiest

}
}

fn parse_alignment(node: &LitKind) -> Result<Align, &'static str> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should just be able to remove the old check_align function, and do nothing in the place where it is called

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 27, 2026
@rust-log-analyzer
Copy link
Collaborator

The job aarch64-gnu-llvm-20-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
/dev/sda15       98M  6.4M   92M   7% /boot/efi
tmpfs           1.6G   16K  1.6G   1% /run/user/1001
================================================================================

Sufficient disk space available (120899580KB >= 52428800KB). Skipping cleanup.
##[group]Run echo "[CI_PR_NUMBER=$num]"
echo "[CI_PR_NUMBER=$num]"
shell: /usr/bin/bash --noprofile --norc -e -o pipefail {0}
---
set -ex

# Run a subset of tests. Used to run tests in parallel in multiple jobs.

# When this job partition is run as part of PR CI, skip tidy to allow revealing more failures. The
# dedicated `tidy` job failing won't block other PR CI jobs from completing, and so tidy failures
# shouldn't inhibit revealing other failures in PR CI jobs.
if [ "$PR_CI_JOB" == "1" ]; then
  echo "PR_CI_JOB set; skipping tidy"
  SKIP_TIDY="--skip tidy"
fi

../x.py --stage 2 test \
  ${SKIP_TIDY:+$SKIP_TIDY} \
  --skip compiler \
  --skip src
#!/bin/bash

set -ex

# Run a subset of tests. Used to run tests in parallel in multiple jobs.

# When this job partition is run as part of PR CI, skip tidy to allow revealing more failures. The
# dedicated `tidy` job failing won't block other PR CI jobs from completing, and so tidy failures
# shouldn't inhibit revealing other failures in PR CI jobs.
if [ "$PR_CI_JOB" == "1" ]; then
  echo "PR_CI_JOB set; skipping tidy"
  SKIP_TIDY="--skip tidy"
fi

../x.py --stage 2 test \
  ${SKIP_TIDY:+$SKIP_TIDY} \
  --skip tests \
  --skip coverage-map \
  --skip coverage-run \
  --skip library \
  --skip tidyselftest
---
##[endgroup]
Executing "/scripts/stage_2_test_set1.sh"
+ /scripts/stage_2_test_set1.sh
+ '[' 1 == 1 ']'
+ echo 'PR_CI_JOB set; skipping tidy'
+ SKIP_TIDY='--skip tidy'
+ ../x.py --stage 2 test --skip tidy --skip compiler --skip src
PR_CI_JOB set; skipping tidy
##[group]Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.04s
##[endgroup]
---
test [ui] tests/ui/abi/abi-sysv64-register-usage.rs ... ignored, ignored when the architecture is aarch64
test [ui] tests/ui/abi/abi-typo-unstable.rs#feature_disabled ... ok
test [ui] tests/ui/abi/abi-typo-unstable.rs#feature_enabled ... ok
test [ui] tests/ui/abi/anon-extern-mod.rs ... ok
test [ui] tests/ui/abi/avr-sram.rs#disable_sram ... ok
test [ui] tests/ui/abi/avr-sram.rs#has_sram ... ok
test [ui] tests/ui/abi/arm-unadjusted-intrinsic.rs#aarch64 ... ok
test [ui] tests/ui/abi/avr-sram.rs#no_sram ... ok
test [ui] tests/ui/abi/arm-unadjusted-intrinsic.rs#arm ... ok
test [ui] tests/ui/abi/bad-custom.rs ... ok
test [ui] tests/ui/abi/c-stack-as-value.rs ... ok
test [ui] tests/ui/abi/c-zst.rs#aarch64-darwin ... ok
test [ui] tests/ui/abi/c-zst.rs#s390x-linux ... ok
---
test [ui] tests/ui/asm/aarch64/type-check-2.rs ... ok
test [ui] tests/ui/asm/aarch64/may_unwind.rs ... ok
test [ui] tests/ui/asm/aarch64/type-check-3.rs ... ok
test [ui] tests/ui/asm/aarch64/type-f16.rs ... ok
test [ui] tests/ui/asm/aarch64v8r.rs#r82 ... ok
test [ui] tests/ui/asm/aarch64v8r.rs#hf ... ok
test [ui] tests/ui/asm/aarch64/sym.rs ... ok
test [ui] tests/ui/asm/asm-with-nested-closure.rs ... ok
test [ui] tests/ui/asm/aarch64v8r.rs#sf ... ok
test [ui] tests/ui/asm/binary_asm_labels.rs ... ignored, only executed when the architecture is x86_64
test [ui] tests/ui/asm/arm-low-dreg.rs ... ok
test [ui] tests/ui/asm/cfg-parse-error.rs ... ok
test [ui] tests/ui/asm/cfg.rs#reva ... ignored, only executed when the architecture is x86_64
test [ui] tests/ui/asm/cfg.rs#revb ... ignored, only executed when the architecture is x86_64
---
test [ui] tests/ui/const-generics/occurs-check/unused-substs-1.rs ... ok
test [ui] tests/ui/const-generics/occurs-check/unused-substs-3.rs ... ok
test [ui] tests/ui/const-generics/occurs-check/unused-substs-5.rs ... ok
test [ui] tests/ui/const-generics/occurs-check/unused-substs-4.rs ... ok
test [ui] tests/ui/const-generics/ogca/basic-fail.rs ... ok
test [ui] tests/ui/const-generics/ogca/basic.rs ... ok
test [ui] tests/ui/const-generics/ogca/rhs-but-not-root.rs ... ok
test [ui] tests/ui/const-generics/ogca/generic-param-rhs.rs ... ok
test [ui] tests/ui/const-generics/ogca/coherence-ambiguous.rs ... ok
test [ui] tests/ui/const-generics/outer-lifetime-in-const-generic-default.rs ... ok
test [ui] tests/ui/const-generics/opaque_types.rs ... ok
test [ui] tests/ui/const-generics/overlapping_impls.rs ... ok
test [ui] tests/ui/const-generics/opaque_types2.rs ... ok
test [ui] tests/ui/const-generics/params-in-ct-in-ty-param-lazy-norm.rs#full ... ok
---
test [ui] tests/ui/extern/issue-64655-extern-rust-must-allow-unwind.rs#fat3 ... ok
test [ui] tests/ui/extern/issue-80074.rs ... ok
test [ui] tests/ui/extern/issue-95829.rs ... ok
test [ui] tests/ui/extern/issue-64655-extern-rust-must-allow-unwind.rs#thin1 ... ok
test [ui] tests/ui/extern/lgamma-linkage.rs ... ok
test [ui] tests/ui/extern/not-in-block.rs ... ok
test [ui] tests/ui/extern/unsized-extern-derefmove.rs ... ok
test [ui] tests/ui/extern/no-mangle-associated-fn.rs ... ok
test [ui] tests/ui/extern/windows-tcb-trash-13259.rs ... ok
test [ui] tests/ui/feature-gates/allow-features-empty.rs ... ok
---
test [ui] tests/ui/feature-gates/feature-gate-macro-derive.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-macro-metavar-expr-concat.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-marker_trait_attr.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-may-dangle.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-mgca-type-const-syntax.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-min-generic-const-args.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-movrs_target_feature.rs ... ignored, only executed when the architecture is x86_64
test [ui] tests/ui/feature-gates/feature-gate-min_const_fn.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-more-maybe-bounds.rs ... ok
test [ui] tests/ui/feature-gates/feature-gate-naked_functions_rustic_abi.rs ... ignored, only executed when the architecture is x86_64
---
test [ui] tests/ui/imports/ambiguous-7.rs ... ok
test [ui] tests/ui/imports/ambiguous-import-visibility-module.rs ... ok
test [ui] tests/ui/imports/ambiguous-8.rs ... ok
test [ui] tests/ui/imports/ambiguous-glob-vs-expanded-extern.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-glob-vs-multiouter.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-globvsglob.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-no-implicit-prelude.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-non-prelude-core-glob.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-non-prelude-std-glob.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-pick-core.rs ... ok
test [ui] tests/ui/imports/ambiguous-panic-pick-std.rs ... ok
---
test [ui] tests/ui/layout/randomize.rs#randomize-layout ... ok
test [ui] tests/ui/layout/null-pointer-optimization.rs ... ok
test [ui] tests/ui/layout/reprc-power-alignment.rs ... ok
test [ui] tests/ui/layout/size-of-val-raw-too-big.rs ... ignored, only executed when the pointer width is 32bit (Layout computation rejects this layout for different reasons on 64-bit.)
test [ui] tests/ui/layout/rigid-alias-due-to-broken-impl.rs ... ok
test [ui] tests/ui/layout/struct.rs ... ok
test [ui] tests/ui/layout/thaw-transmute-invalid-enum.rs ... ok
test [ui] tests/ui/layout/rust-call-abi-not-a-tuple-ice-81974.rs ... ok
test [ui] tests/ui/layout/thaw-validate-invalid-enum.rs ... ok
test [ui] tests/ui/layout/rigid-alias-no-params.rs ... ok
---

10 LL | #[repr(align(15))]
11    |              ^^
12 
- error[E0589]: invalid `repr(align)` attribute: larger than 2^29
+ error[E0589]: invalid `repr(align)` attribute: alignment larger than 2^29
14   --> $DIR/repr-align.rs:9:14
15    |
16 LL | #[repr(align(4294967296))]

34 LL | #[repr(align(15))]
35    |              ^^
36 
- error[E0589]: invalid `repr(align)` attribute: larger than 2^29
+ error[E0589]: invalid `repr(align)` attribute: alignment larger than 2^29
38   --> $DIR/repr-align.rs:24:14
39    |
40 LL | #[repr(align(4294967296))]


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args repr/repr-align.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/repr/repr-align.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--check-cfg" "cfg(test,FALSE)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/repr/repr-align" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:3:14
   |
LL | #[repr(align(16.0))] //~ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer
   |              ^^^^

error[E0589]: invalid `repr(align)` attribute: not a power of two
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:6:14
   |
LL | #[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
   |              ^^

error[E0589]: invalid `repr(align)` attribute: alignment larger than 2^29
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:9:14
   |
LL | #[repr(align(4294967296))] //~ ERROR: invalid `repr(align)` attribute: larger than 2^29
   |              ^^^^^^^^^^

error[E0589]: invalid `repr(align)` attribute: not a power of two
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:15:14
   |
LL | #[repr(align(0))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
   |              ^

error[E0589]: invalid `repr(align)` attribute: not an unsuffixed integer
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:18:14
   |
LL | #[repr(align(16.0))] //~ ERROR: invalid `repr(align)` attribute: not an unsuffixed integer
   |              ^^^^

error[E0589]: invalid `repr(align)` attribute: not a power of two
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:21:14
   |
LL | #[repr(align(15))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
   |              ^^

error[E0589]: invalid `repr(align)` attribute: alignment larger than 2^29
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:24:14
   |
LL | #[repr(align(4294967296))] //~ ERROR: invalid `repr(align)` attribute: larger than 2^29
   |              ^^^^^^^^^^

error[E0589]: invalid `repr(align)` attribute: not a power of two
##[error]  --> /checkout/tests/ui/repr/repr-align.rs:30:14
   |
LL | #[repr(align(0))] //~ ERROR: invalid `repr(align)` attribute: not a power of two
   |              ^

error: aborting due to 8 previous errors

For more information about this error, try `rustc --explain E0589`.
------------------------------------------

---- [ui] tests/ui/repr/repr-align.rs stdout end ----
---- [ui] tests/ui/repr/repr_align_greater_usize.rs#msp430 stdout ----
Saved the actual stderr to `/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/repr/repr_align_greater_usize.msp430/repr_align_greater_usize.msp430.stderr`
diff of stderr:

- error[E0589]: alignment must not be greater than `isize::MAX` bytes
-   --> $DIR/repr_align_greater_usize.rs:23:8
+ error[E0589]: invalid `repr(align)` attribute: alignment larger than pointer width: {max}
+   --> $DIR/repr_align_greater_usize.rs:23:14
3    |
4 LL | #[repr(align(32768))]
-    |        ^^^^^^^^^^^^
-    |
-    = note: `isize::MAX` is 32767 for the current target
+    |              ^^^^^
8 
- error[E0589]: alignment must not be greater than `isize::MAX` bytes
-   --> $DIR/repr_align_greater_usize.rs:26:8
+ error[E0589]: invalid `repr(align)` attribute: alignment larger than pointer width: {max}
+   --> $DIR/repr_align_greater_usize.rs:26:14
11    |
12 LL | #[repr(align(65536))]
-    |        ^^^^^^^^^^^^
-    |
---

Note: some mismatched output was normalized before being compared
-   --> /checkout/tests/ui/repr/repr_align_greater_usize.rs:23:14
-   --> /checkout/tests/ui/repr/repr_align_greater_usize.rs:26:14
+ error[E0589]: invalid `repr(align)` attribute: alignment larger than pointer width: {max}
+   --> $DIR/repr_align_greater_usize.rs:23:14
+    |              ^^^^^
+ error[E0589]: invalid `repr(align)` attribute: alignment larger than pointer width: {max}
+   --> $DIR/repr_align_greater_usize.rs:26:14
+    |              ^^^^^


The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args repr/repr_align_greater_usize.rs`

error in revision `msp430`: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/repr/repr_align_greater_usize.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--cfg" "msp430" "--check-cfg" "cfg(test,FALSE,msp430,aarch32)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/repr/repr_align_greater_usize.msp430" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers" "-Cpanic=abort" "-Cforce-unwind-tables=yes" "--target=msp430-none-elf" "--extern" "minicore=/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/repr/repr_align_greater_usize.msp430/libminicore.rlib"
stdout: none
--- stderr -------------------------------
error[E0589]: invalid `repr(align)` attribute: alignment larger than pointer width: {max}
##[error]  --> /checkout/tests/ui/repr/repr_align_greater_usize.rs:23:14
   |
LL | #[repr(align(32768))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
   |              ^^^^^

error[E0589]: invalid `repr(align)` attribute: alignment larger than pointer width: {max}
##[error]  --> /checkout/tests/ui/repr/repr_align_greater_usize.rs:26:14
   |
LL | #[repr(align(65536))] //[msp430]~ ERROR alignment must not be greater than `isize::MAX`
   |              ^^^^^

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0589`.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-AST Area: AST A-attributes Area: Attributes (`#[…]`, `#![…]`) C-cleanup Category: PRs that clean code up or issues documenting cleanup. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants