Skip to content

warn on align on fields to avoid breaking changes #143868

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

Merged
merged 1 commit into from
Jul 14, 2025
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
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ passes_align_attr_application =
`#[align(...)]` should be applied to a function item
.label = not a function item
passes_align_on_fields =
attribute should be applied to a function or method
.warn = {-passes_previously_accepted}
passes_align_should_be_repr_align =
`#[align(...)]` is not supported on {$item} items
.suggestion = use `#[repr(align(...))]` instead
Expand Down
28 changes: 22 additions & 6 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
use rustc_trait_selection::traits::ObligationCtxt;
use tracing::debug;

use crate::errors::AlignOnFields;
use crate::{errors, fluent_generated as fluent};

#[derive(LintDiagnostic)]
Expand Down Expand Up @@ -197,8 +198,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
Attribute::Parsed(AttributeKind::ExportName { span: attr_span, .. }) => {
self.check_export_name(hir_id, *attr_span, span, target)
}
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => {
self.check_align(span, target, *align, *repr_span)
Attribute::Parsed(AttributeKind::Align { align, span: attr_span }) => {
self.check_align(span, hir_id, target, *align, *attr_span)
}
Attribute::Parsed(AttributeKind::LinkSection { span: attr_span, .. }) => {
self.check_link_section(hir_id, *attr_span, span, target)
Expand Down Expand Up @@ -1933,22 +1934,37 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
}

/// Checks if the `#[align]` attributes on `item` are valid.
fn check_align(&self, span: Span, target: Target, align: Align, repr_span: Span) {
fn check_align(
&self,
span: Span,
hir_id: HirId,
target: Target,
align: Align,
attr_span: Span,
) {
match target {
Target::Fn | Target::Method(_) | Target::ForeignFn => {}
Target::Field => {
self.tcx.emit_node_span_lint(
UNUSED_ATTRIBUTES,
hir_id,
attr_span,
AlignOnFields { span },
);
}
Comment on lines +1947 to +1954
Copy link
Member

Choose a reason for hiding this comment

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

Question: do we already have test coverage for this case?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We have about 20 of these cases and to the best of my knowledge no test coverage for any :ferris clueless:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Don't really have time to atm, am on holiday. Just knew how to fix this issue well

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we may wish to land this and maybe open an issue about getting test coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

feel free to assign me or @JonathanBrouwer (who I think was interested in this sort of thing)

Comment on lines +1947 to +1954
Copy link
Member

@jieyouxu jieyouxu Jul 14, 2025

Choose a reason for hiding this comment

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

Question: wait hold on, what does this PR intend to do? If the intention was to not error on #[align] in various positions where we previously did not do, then this PR doesn't do that, because #142507 is a breaking change even if #[align] is an unstable built-in attribute, because unstable built-in attributes still participate in name resolution, and thus will still cause the ambiguity. This PR additionally produces another warning (i.e. unused_attributes) but does not prevent the name resolution ambiguity error from happening, cf. #134963.

Counter-example (I tried this test which fails on master, and also the commit from this PR, ignoring the irrelevant unstable fn_align feature gate warnings):

// tests/ui/whatever_subdir/test.rs

//@ proc-macro: my_derive.rs
//@ edition: 2024

use my_derive::MyDerive;

#[derive(MyDerive)]
#[align]
//~^ ERROR `align` is ambiguous
pub struct Foo;

fn main() {}
// tests/ui/whatever_subdir/auxiliary/my_derive.rs

//@ edition: 2024

extern crate proc_macro;
use proc_macro::{Span, TokenStream};

#[proc_macro_derive(
    MyDerive,
    attributes(
        align
    )
)]
pub fn derive_custom(_item: TokenStream) -> TokenStream {
    TokenStream::new()
}

I think if we wanted to prevent that ambiguity error from happening entirely, we'd need to pick one of the following options (or more) or some alternative solution:

  1. Have to fix how built-in attributes are resolved versus user attributes
  2. Revert use #[align] attribute for fn_align #142507
  3. Rename #[align] to something less common.

Copy link
Member

Choose a reason for hiding this comment

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

(That is, I think this PR itself is correct, but doesn't address #143834.)

Target::Struct | Target::Union | Target::Enum => {
self.dcx().emit_err(errors::AlignShouldBeReprAlign {
span: repr_span,
span: attr_span,
item: target.name(),
align_bytes: align.bytes(),
});
}
_ => {
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: repr_span, span });
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: attr_span, span });
}
}

self.check_align_value(align, repr_span);
self.check_align_value(align, attr_span);
}

/// Checks if the `#[repr]` attributes on `item` are valid.
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,14 @@ pub(crate) struct NoMangle {
pub span: Span,
}

#[derive(LintDiagnostic)]
#[diag(passes_align_on_fields)]
#[warning]
pub(crate) struct AlignOnFields {
#[label]
pub span: Span,
}

#[derive(Diagnostic)]
#[diag(passes_repr_conflicting, code = E0566)]
pub(crate) struct ReprConflicting {
Expand Down
Loading