Skip to content

Commit 16b3f47

Browse files
Rollup merge of #143868 - jdonszelmann:fix-align-on-fields, r=workingjubilee
warn on align on fields to avoid breaking changes r? `@workingjubilee`
2 parents bbda0c9 + a5ab682 commit 16b3f47

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

compiler/rustc_passes/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ passes_align_attr_application =
1717
`#[align(...)]` should be applied to a function item
1818
.label = not a function item
1919
20+
passes_align_on_fields =
21+
attribute should be applied to a function or method
22+
.warn = {-passes_previously_accepted}
23+
2024
passes_align_should_be_repr_align =
2125
`#[align(...)]` is not supported on {$item} items
2226
.suggestion = use `#[repr(align(...))]` instead

compiler/rustc_passes/src/check_attr.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
4545
use rustc_trait_selection::traits::ObligationCtxt;
4646
use tracing::debug;
4747

48+
use crate::errors::AlignOnFields;
4849
use crate::{errors, fluent_generated as fluent};
4950

5051
#[derive(LintDiagnostic)]
@@ -207,8 +208,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
207208
Attribute::Parsed(AttributeKind::ExportName { span: attr_span, .. }) => {
208209
self.check_export_name(hir_id, *attr_span, span, target)
209210
}
210-
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => {
211-
self.check_align(span, target, *align, *repr_span)
211+
Attribute::Parsed(AttributeKind::Align { align, span: attr_span }) => {
212+
self.check_align(span, hir_id, target, *align, *attr_span)
212213
}
213214
Attribute::Parsed(AttributeKind::LinkSection { span: attr_span, .. }) => {
214215
self.check_link_section(hir_id, *attr_span, span, target)
@@ -1953,22 +1954,37 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19531954
}
19541955

19551956
/// Checks if the `#[align]` attributes on `item` are valid.
1956-
fn check_align(&self, span: Span, target: Target, align: Align, repr_span: Span) {
1957+
fn check_align(
1958+
&self,
1959+
span: Span,
1960+
hir_id: HirId,
1961+
target: Target,
1962+
align: Align,
1963+
attr_span: Span,
1964+
) {
19571965
match target {
19581966
Target::Fn | Target::Method(_) | Target::ForeignFn => {}
1967+
Target::Field => {
1968+
self.tcx.emit_node_span_lint(
1969+
UNUSED_ATTRIBUTES,
1970+
hir_id,
1971+
attr_span,
1972+
AlignOnFields { span },
1973+
);
1974+
}
19591975
Target::Struct | Target::Union | Target::Enum => {
19601976
self.dcx().emit_err(errors::AlignShouldBeReprAlign {
1961-
span: repr_span,
1977+
span: attr_span,
19621978
item: target.name(),
19631979
align_bytes: align.bytes(),
19641980
});
19651981
}
19661982
_ => {
1967-
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: repr_span, span });
1983+
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: attr_span, span });
19681984
}
19691985
}
19701986

1971-
self.check_align_value(align, repr_span);
1987+
self.check_align_value(align, attr_span);
19721988
}
19731989

19741990
/// Checks if the `#[repr]` attributes on `item` are valid.

compiler/rustc_passes/src/errors.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,14 @@ pub(crate) struct NoMangle {
604604
pub span: Span,
605605
}
606606

607+
#[derive(LintDiagnostic)]
608+
#[diag(passes_align_on_fields)]
609+
#[warning]
610+
pub(crate) struct AlignOnFields {
611+
#[label]
612+
pub span: Span,
613+
}
614+
607615
#[derive(Diagnostic)]
608616
#[diag(passes_repr_conflicting, code = E0566)]
609617
pub(crate) struct ReprConflicting {

0 commit comments

Comments
 (0)