Skip to content

Commit a5ab682

Browse files
committed
warn on align on fields to avoid breaking changes
1 parent b1d2f2c commit a5ab682

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
@@ -43,6 +43,7 @@ use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
4343
use rustc_trait_selection::traits::ObligationCtxt;
4444
use tracing::debug;
4545

46+
use crate::errors::AlignOnFields;
4647
use crate::{errors, fluent_generated as fluent};
4748

4849
#[derive(LintDiagnostic)]
@@ -197,8 +198,8 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
197198
Attribute::Parsed(AttributeKind::ExportName { span: attr_span, .. }) => {
198199
self.check_export_name(hir_id, *attr_span, span, target)
199200
}
200-
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => {
201-
self.check_align(span, target, *align, *repr_span)
201+
Attribute::Parsed(AttributeKind::Align { align, span: attr_span }) => {
202+
self.check_align(span, hir_id, target, *align, *attr_span)
202203
}
203204
Attribute::Parsed(AttributeKind::LinkSection { span: attr_span, .. }) => {
204205
self.check_link_section(hir_id, *attr_span, span, target)
@@ -1933,22 +1934,37 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
19331934
}
19341935

19351936
/// Checks if the `#[align]` attributes on `item` are valid.
1936-
fn check_align(&self, span: Span, target: Target, align: Align, repr_span: Span) {
1937+
fn check_align(
1938+
&self,
1939+
span: Span,
1940+
hir_id: HirId,
1941+
target: Target,
1942+
align: Align,
1943+
attr_span: Span,
1944+
) {
19371945
match target {
19381946
Target::Fn | Target::Method(_) | Target::ForeignFn => {}
1947+
Target::Field => {
1948+
self.tcx.emit_node_span_lint(
1949+
UNUSED_ATTRIBUTES,
1950+
hir_id,
1951+
attr_span,
1952+
AlignOnFields { span },
1953+
);
1954+
}
19391955
Target::Struct | Target::Union | Target::Enum => {
19401956
self.dcx().emit_err(errors::AlignShouldBeReprAlign {
1941-
span: repr_span,
1957+
span: attr_span,
19421958
item: target.name(),
19431959
align_bytes: align.bytes(),
19441960
});
19451961
}
19461962
_ => {
1947-
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: repr_span, span });
1963+
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: attr_span, span });
19481964
}
19491965
}
19501966

1951-
self.check_align_value(align, repr_span);
1967+
self.check_align_value(align, attr_span);
19521968
}
19531969

19541970
/// 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)