Skip to content

Commit dec6232

Browse files
authored
Rollup merge of rust-lang#139917 - folkertdev:fn-align-multiple, r=jdonszelmann
fix for multiple `#[repr(align(N))]` on functions tracking issue: rust-lang#82232 fixes rust-lang#132464 The behavior of align is specified at https://doc.rust-lang.org/reference/type-layout.html#r-layout.repr.alignment.align > For align, if the specified alignment is less than the alignment of the type without the align modifier, then the alignment is unaffected. So in effect that means that the maximum of the specified alignments should be chosen. That is also the current behavior for `align` on ADTs: ```rust #![feature(fn_align)] #[repr(C, align(32), align(64))] struct Foo { x: u64, } const _: () = assert!(core::mem::align_of::<Foo>() == 64); // See the godbolt LLVM output: the alignment of this function is 32 #[no_mangle] #[repr(align(32))] #[repr(align(64))] fn foo() {} // The current logic just picks the first alignment: the alignment of this function is 64 #[no_mangle] #[repr(align(64))] #[repr(align(32))] fn bar() {} ``` https://godbolt.org/z/scco435jE https://github.com/rust-lang/rust/blob/afa859f8121bf2985362a2c8414dc71a825ccf2d/compiler/rustc_middle/src/ty/mod.rs#L1529-L1532 The rust-lang#132464 issue is really about parsing/representing the attribute, which has already been improved and now uses the "parse, don't validate" attribute approach. That means the behavior is already different from what the issue describes: on current `main`, the first value is chosen. This PR fixes a logic error, where we just did not check for the effect of two or more `align` modifiers. In combination, that fixes the issue. cc `@jdonszelmann` if you do have further thoughs here
2 parents 89d5dcf + a6dcd51 commit dec6232

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
114114
AttributeKind::Repr(reprs) => {
115115
codegen_fn_attrs.alignment = reprs
116116
.iter()
117-
.find_map(|(r, _)| if let ReprAlign(x) = r { Some(*x) } else { None });
117+
.filter_map(|(r, _)| if let ReprAlign(x) = r { Some(*x) } else { None })
118+
.max();
118119
}
119120

120121
_ => {}

tests/codegen/align-fn.rs

+19
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,22 @@ impl T for () {}
4747
pub fn foo() {
4848
().trait_method();
4949
}
50+
51+
// CHECK-LABEL: align_specified_twice_1
52+
// CHECK-SAME: align 64
53+
#[no_mangle]
54+
#[repr(align(32), align(64))]
55+
pub fn align_specified_twice_1() {}
56+
57+
// CHECK-LABEL: align_specified_twice_2
58+
// CHECK-SAME: align 128
59+
#[no_mangle]
60+
#[repr(align(128), align(32))]
61+
pub fn align_specified_twice_2() {}
62+
63+
// CHECK-LABEL: align_specified_twice_3
64+
// CHECK-SAME: align 256
65+
#[no_mangle]
66+
#[repr(align(32))]
67+
#[repr(align(256))]
68+
pub fn align_specified_twice_3() {}

0 commit comments

Comments
 (0)