Skip to content

Align attr fixes #143206

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
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
10 changes: 1 addition & 9 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_abi::{Align, ExternAbi};
use rustc_ast::expand::autodiff_attrs::{AutoDiffAttrs, DiffActivity, DiffMode};
use rustc_ast::{LitKind, MetaItem, MetaItemInner, attr};
use rustc_attr_data_structures::{
AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr, ReprAttr, UsedBy, find_attr,
AttributeKind, InlineAttr, InstructionSetAttr, OptimizeAttr, UsedBy, find_attr,
};
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
Expand Down Expand Up @@ -109,14 +109,6 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {

if let hir::Attribute::Parsed(p) = attr {
match p {
AttributeKind::Repr(reprs) => {
codegen_fn_attrs.alignment = reprs
.iter()
.filter_map(
|(r, _)| if let ReprAttr::ReprAlign(x) = r { Some(*x) } else { None },
)
.max();
}
AttributeKind::Cold(_) => codegen_fn_attrs.flags |= CodegenFnAttrFlags::COLD,
AttributeKind::ExportName { name, .. } => {
codegen_fn_attrs.export_name = Some(*name);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ passes_abi_ne =
passes_abi_of =
fn_abi_of({$fn_name}) = {$fn_abi}
passes_align_attr_application =
`#[align(...)]` should be applied to a function item
.label = not a function item
passes_align_should_be_repr_align =
`#[align(...)]` is not supported on {$item} items
.suggestion = use `#[repr(align(...))]` instead
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,7 @@ 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::Fn | Target::Method(_) | Target::ForeignFn => {}
Target::Struct | Target::Union | Target::Enum => {
self.dcx().emit_err(errors::AlignShouldBeReprAlign {
span: repr_span,
Expand All @@ -1896,10 +1896,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
});
}
_ => {
self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
hint_span: repr_span,
span,
});
self.dcx().emit_err(errors::AlignAttrApplication { hint_span: repr_span, span });
}
}

Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,3 +1838,12 @@ pub(crate) struct AlignShouldBeReprAlign {
pub item: &'static str,
pub align_bytes: u64,
}

#[derive(Diagnostic)]
#[diag(passes_align_attr_application)]
pub(crate) struct AlignAttrApplication {
#[primary_span]
pub hint_span: Span,
#[label]
pub span: Span,
}
21 changes: 21 additions & 0 deletions tests/codegen/align-fn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ compile-flags: -C no-prepopulate-passes -Z mir-opt-level=0 -Clink-dead-code
//@ edition: 2024

#![crate_type = "lib"]
#![feature(fn_align)]
Expand Down Expand Up @@ -116,3 +117,23 @@ pub fn align_specified_twice_2() {}
#[align(32)]
#[align(256)]
pub fn align_specified_twice_3() {}

const _: () = {
// CHECK-LABEL: align_unmangled
// CHECK-SAME: align 256
#[unsafe(no_mangle)]
#[align(32)]
#[align(256)]
extern "C" fn align_unmangled() {}
Comment on lines +125 to +127
Copy link
Member

Choose a reason for hiding this comment

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

Weren't we going to warn on applying align twice?

I mean that shouldn't affect the codegen test, I'm just realizing that I don't know for sure if we have a test that does check that we warn.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nice to have a warning, but it’s not required for the MVP. It hasn’t been implemented yet.

};

unsafe extern "C" {
#[align(256)]
fn align_unmangled();
}

// CHECK-LABEL: async_align
// CHECK-SAME: align 64
#[unsafe(no_mangle)]
#[align(64)]
pub async fn async_align() {}
15 changes: 15 additions & 0 deletions tests/ui/attributes/fn-align-dyn.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//@ run-pass
#![feature(fn_align)]

trait Test {
#[align(4096)]
Copy link
Member

Choose a reason for hiding this comment

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

4096 is pretty big but I am realizing I don't see a test for oversized aligns that we should definitely reject, I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

4096 I believe is the highest align that should work more or less everywhere. Because this test relies on converting to a function pointer, choosing a high value minimizes the chance of “getting lucky”.

Rejecting high alignments on platforms that don’t support them is being worked on by others, I think.

fn foo(&self);

#[align(4096)]
fn foo1(&self);
}

fn main() {
assert_eq!((<dyn Test>::foo as fn(_) as usize & !1) % 4096, 0);
assert_eq!((<dyn Test>::foo1 as fn(_) as usize & !1) % 4096, 0);
}
9 changes: 9 additions & 0 deletions tests/ui/attributes/malformed-fn-align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,12 @@ fn f4() {}

#[align(16)] //~ ERROR `#[align(...)]` is not supported on struct items
struct S1;

#[align(32)] //~ ERROR `#[align(...)]` should be applied to a function item
const FOO: i32 = 42;

#[align(32)] //~ ERROR `#[align(...)]` should be applied to a function item
mod test {}

#[align(32)] //~ ERROR `#[align(...)]` should be applied to a function item
use ::std::iter;
26 changes: 25 additions & 1 deletion tests/ui/attributes/malformed-fn-align.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,31 @@ LL - #[align(16)]
LL + #[repr(align(16))]
|

error: aborting due to 7 previous errors
error: `#[align(...)]` should be applied to a function item
--> $DIR/malformed-fn-align.rs:27:1
|
LL | #[align(32)]
| ^^^^^^^^^^^^
LL | const FOO: i32 = 42;
| -------------------- not a function item

error: `#[align(...)]` should be applied to a function item
--> $DIR/malformed-fn-align.rs:30:1
|
LL | #[align(32)]
| ^^^^^^^^^^^^
LL | mod test {}
| ----------- not a function item

error: `#[align(...)]` should be applied to a function item
--> $DIR/malformed-fn-align.rs:33:1
|
LL | #[align(32)]
| ^^^^^^^^^^^^
LL | use ::std::iter;
| ---------------- not a function item

error: aborting due to 10 previous errors

Some errors have detailed explanations: E0539, E0589, E0805.
For more information about an error, try `rustc --explain E0539`.
Loading