-
Notifications
You must be signed in to change notification settings - Fork 13.5k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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)] | ||
|
@@ -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) | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Counter-example (I tried this test which fails on // 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:
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)