Skip to content

Commit abdbaa2

Browse files
committed
Correctly set return type attributes on foreign function declarations
The ArgType type gives us a generic way to specify an attribute for a type to ensure ABI conformance for foreign functions. But the code that actually sets the argument attributes in the function declaration only sets the attribute for the return type when the type is indirect. Since LLVMAddAttribute() doesn't allow to set attributes on the return type, we have to use LLVMAddFunctionAttribute() instead. This didn't cause problems yet, because currently only some indirect types require attributes to be set.
1 parent f556c8c commit abdbaa2

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

src/librustc/middle/trans/foreign.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -934,22 +934,17 @@ pub fn lltype_for_foreign_fn(ccx: &CrateContext, ty: ty::t) -> Type {
934934

935935
fn add_argument_attributes(tys: &ForeignTypes,
936936
llfn: ValueRef) {
937-
let mut i = 0;
938-
939-
if tys.fn_ty.ret_ty.is_indirect() {
940-
match tys.fn_ty.ret_ty.attr {
941-
Some(attr) => {
942-
let llarg = get_param(llfn, i);
943-
unsafe {
944-
llvm::LLVMAddAttribute(llarg, attr as c_uint);
945-
}
946-
}
947-
None => {}
948-
}
937+
let mut i = if tys.fn_ty.ret_ty.is_indirect() { 1 } else { 0 };
949938

950-
i += 1;
939+
match tys.fn_ty.ret_ty.attr {
940+
Some(attr) => unsafe {
941+
llvm::LLVMAddFunctionAttribute(llfn, i as c_uint, attr as u64);
942+
},
943+
None => {}
951944
}
952945

946+
i += 1;
947+
953948
for &arg_ty in tys.fn_ty.arg_tys.iter() {
954949
if arg_ty.is_ignore() {
955950
continue;
@@ -958,12 +953,9 @@ fn add_argument_attributes(tys: &ForeignTypes,
958953
if arg_ty.pad.is_some() { i += 1; }
959954

960955
match arg_ty.attr {
961-
Some(attr) => {
962-
let llarg = get_param(llfn, i);
963-
unsafe {
964-
llvm::LLVMAddAttribute(llarg, attr as c_uint);
965-
}
966-
}
956+
Some(attr) => unsafe {
957+
llvm::LLVMAddFunctionAttribute(llfn, i as c_uint, attr as u64);
958+
},
967959
None => ()
968960
}
969961

0 commit comments

Comments
 (0)