-
Notifications
You must be signed in to change notification settings - Fork 13.4k
use #[align]
attribute for fn_align
#142507
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
Open
folkertdev
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
folkertdev:fn-align-align-attribute
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+293
−132
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,10 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
} | ||
Attribute::Parsed(AttributeKind::Repr(_)) => { /* handled below this loop and elsewhere */ | ||
} | ||
Attribute::Parsed(AttributeKind::Align { align, span: repr_span }) => { | ||
self.check_align(span, target, *align, *repr_span) | ||
} | ||
|
||
Attribute::Parsed( | ||
AttributeKind::BodyStability { .. } | ||
| AttributeKind::ConstStabilityIndirect | ||
|
@@ -636,6 +640,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
sym::naked, | ||
sym::instruction_set, | ||
sym::repr, | ||
sym::align, | ||
sym::rustc_std_internal_symbol, | ||
// code generation | ||
sym::cold, | ||
|
@@ -672,7 +677,9 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
// this check can be part of the parser and be removed here | ||
match other_attr { | ||
Attribute::Parsed( | ||
AttributeKind::Deprecation { .. } | AttributeKind::Repr { .. }, | ||
AttributeKind::Deprecation { .. } | ||
folkertdev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| AttributeKind::Repr { .. } | ||
| AttributeKind::Align { .. }, | ||
) => { | ||
continue; | ||
} | ||
|
@@ -1947,6 +1954,28 @@ 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) { | ||
match target { | ||
Target::Fn | Target::Method(_) => {} | ||
Target::Struct | Target::Union | Target::Enum => { | ||
self.dcx().emit_err(errors::AlignShouldBeReprAlign { | ||
span: repr_span, | ||
item: target.name(), | ||
align_bytes: align.bytes(), | ||
}); | ||
} | ||
_ => { | ||
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion { | ||
hint_span: repr_span, | ||
span, | ||
}); | ||
} | ||
} | ||
|
||
self.check_align_value(align, repr_span); | ||
} | ||
|
||
/// Checks if the `#[repr]` attributes on `item` are valid. | ||
fn check_repr( | ||
&self, | ||
|
@@ -1999,23 +2028,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
match target { | ||
Target::Struct | Target::Union | Target::Enum => {} | ||
Target::Fn | Target::Method(_) => { | ||
if !self.tcx.features().fn_align() { | ||
feature_err( | ||
&self.tcx.sess, | ||
sym::fn_align, | ||
*repr_span, | ||
fluent::passes_repr_align_function, | ||
) | ||
.emit(); | ||
} | ||
self.dcx().emit_err(errors::ReprAlignShouldBeAlign { | ||
span: *repr_span, | ||
item: target.name(), | ||
}); | ||
} | ||
_ => { | ||
self.dcx().emit_err( | ||
errors::AttrApplication::StructEnumFunctionMethodUnion { | ||
hint_span: *repr_span, | ||
span, | ||
}, | ||
); | ||
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion { | ||
hint_span: *repr_span, | ||
span, | ||
}); | ||
} | ||
} | ||
|
||
|
@@ -2073,21 +2095,16 @@ impl<'tcx> CheckAttrVisitor<'tcx> { | |
match target { | ||
Target::Struct | Target::Union | Target::Enum => continue, | ||
Target::Fn | Target::Method(_) => { | ||
feature_err( | ||
&self.tcx.sess, | ||
sym::fn_align, | ||
*repr_span, | ||
fluent::passes_repr_align_function, | ||
) | ||
.emit(); | ||
self.dcx().emit_err(errors::ReprAlignShouldBeAlign { | ||
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. love these helpful diagnostics :) |
||
span: *repr_span, | ||
item: target.name(), | ||
}); | ||
} | ||
_ => { | ||
self.dcx().emit_err( | ||
errors::AttrApplication::StructEnumFunctionMethodUnion { | ||
hint_span: *repr_span, | ||
span, | ||
}, | ||
); | ||
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion { | ||
hint_span: *repr_span, | ||
span, | ||
}); | ||
} | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
//@compile-flags: -Zmin-function-alignment=8 | ||
#![feature(fn_align)] | ||
|
||
// When a function uses `repr(align(N))`, the function address should be a multiple of `N`. | ||
// When a function uses `align(N)`, the function address should be a multiple of `N`. | ||
|
||
#[repr(align(256))] | ||
#[align(256)] | ||
fn foo() {} | ||
|
||
#[repr(align(16))] | ||
#[align(16)] | ||
fn bar() {} | ||
|
||
#[repr(align(4))] | ||
#[align(4)] | ||
fn baz() {} | ||
|
||
fn main() { | ||
assert!((foo as usize).is_multiple_of(256)); | ||
assert!((bar as usize).is_multiple_of(16)); | ||
|
||
// The maximum of `repr(align(N))` and `-Zmin-function-alignment=N` is used. | ||
// The maximum of `align(N)` and `-Zmin-function-alignment=N` is used. | ||
assert!((baz as usize).is_multiple_of(8)); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
by the template logic? I'd quite like to move those checks here at some point, including the templates. For converted attributes I usually have added an exception there to ignore attributes with new-style parsers so that these checks do do something, and all checks happen in one place. If you wouldn't mind, could you do the same for align?
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.
With #138165 you don't need to make a new diagnostic for this either which should make your life easier.
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.
Exactly
Do you have an example here? E.g
ConfusablesParser
also seems to ignore 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.
yea! I've been making exceptions to new attributes here:
rust/compiler/rustc_parse/src/validate_attr.rs
Line 288 in 1e8aac1
and then add proper validation for them here:
rust/compiler/rustc_attr_parsing/src/attributes/inline.rs
Line 24 in 1e8aac1
(n.b. I exhaustively match and error on all cases)
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.
I think those links are to #138165? Should I rebase on that now? (happy to, but could also do a follow-up later)
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.
I think rebasing would be nice, then this parser is in a much nicer state. Otherwise I'll go back to fix it next week again. That #138165 seems to make good progress.