Skip to content

Commit 9be8ec8

Browse files
committed
Properly translate methods with foreign CC
This fixes a general issue of trying to define extern functions inside impl blocks resulting in ICE. Fixes #21238 Fixes #20734 Fixes #19047
1 parent 0c25e6f commit 9be8ec8

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

src/librustc_trans/trans/base.rs

+14-6
Original file line numberDiff line numberDiff line change
@@ -2412,9 +2412,9 @@ fn register_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
24122412
-> ValueRef {
24132413
if let ty::ty_bare_fn(_, ref f) = node_type.sty {
24142414
if f.abi != Rust && f.abi != RustCall {
2415-
ccx.sess().span_bug(sp, &format!("only `Rust` or `rust-call` calling conventions \
2416-
are valid for this function, but it uses `{:?}`",
2417-
f.abi.name));
2415+
ccx.sess().span_bug(sp, &format!("only the `{}` or `{}` calling conventions are valid \
2416+
for this function; `{}` was specified",
2417+
Rust.name(), RustCall.name(), f.abi.name()));
24182418
}
24192419
} else {
24202420
ccx.sess().span_bug(sp, "expected bare rust function")
@@ -2938,9 +2938,17 @@ fn register_method(ccx: &CrateContext, id: ast::NodeId,
29382938

29392939
let sym = exported_name(ccx, id, mty, &m.attrs);
29402940

2941-
let llfn = register_fn(ccx, m.span, sym, id, mty);
2942-
set_llvm_fn_attrs(ccx, &m.attrs, llfn);
2943-
llfn
2941+
if let ty::ty_bare_fn(_, ref f) = mty.sty {
2942+
let llfn = if f.abi == Rust || f.abi == RustCall {
2943+
register_fn(ccx, m.span, sym, id, mty)
2944+
} else {
2945+
foreign::register_rust_fn_with_foreign_abi(ccx, m.span, sym, id)
2946+
};
2947+
set_llvm_fn_attrs(ccx, &m.attrs, llfn);
2948+
return llfn;
2949+
} else {
2950+
ccx.sess().span_bug(m.span, "expected bare rust function");
2951+
}
29442952
}
29452953

29462954
pub fn crate_ctxt_to_encode_parms<'a, 'tcx>(cx: &'a SharedCrateContext<'tcx>,

src/test/run-pass/extern-methods.rs

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
trait A {
11+
extern "fastcall" fn test1(i: i32);
12+
extern fn test2(i: i32);
13+
}
14+
15+
struct S;
16+
impl S {
17+
extern "stdcall" fn test3(i: i32) {
18+
assert_eq!(i, 3);
19+
}
20+
}
21+
22+
impl A for S {
23+
extern "fastcall" fn test1(i: i32) {
24+
assert_eq!(i, 1);
25+
}
26+
extern fn test2(i: i32) {
27+
assert_eq!(i, 2);
28+
}
29+
}
30+
31+
fn main() {
32+
<S as A>::test1(1);
33+
<S as A>::test2(2);
34+
S::test3(3);
35+
}

0 commit comments

Comments
 (0)