Skip to content

Commit bf7cde4

Browse files
Manishearthalexcrichton
authored andcommitted
Rollup merge of #22538 - nagisa:properise-trans-asserts, r=nikomatsakis
2 parents f1a6d67 + 9be8ec8 commit bf7cde4

File tree

2 files changed

+58
-12
lines changed

2 files changed

+58
-12
lines changed

src/librustc_trans/trans/base.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ pub fn decl_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
301301
self_type.repr(ccx.tcx()));
302302
(&function_type.sig, RustCall, Some(llenvironment_type))
303303
}
304-
_ => panic!("expected closure or fn")
304+
_ => ccx.sess().bug("expected closure or fn")
305305
};
306306

307307
let sig = ty::erase_late_bound_regions(ccx.tcx(), sig);
@@ -2410,12 +2410,15 @@ fn register_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
24102410
node_id: ast::NodeId,
24112411
node_type: Ty<'tcx>)
24122412
-> ValueRef {
2413-
match node_type.sty {
2414-
ty::ty_bare_fn(_, ref f) => {
2415-
assert!(f.abi == Rust || f.abi == RustCall);
2413+
if let ty::ty_bare_fn(_, ref f) = node_type.sty {
2414+
if f.abi != Rust && f.abi != RustCall {
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()));
24162418
}
2417-
_ => panic!("expected bare rust fn")
2418-
};
2419+
} else {
2420+
ccx.sess().span_bug(sp, "expected bare rust function")
2421+
}
24192422

24202423
let llfn = decl_rust_fn(ccx, node_type, &sym[..]);
24212424
finish_register_fn(ccx, sp, sym, node_id, llfn);
@@ -2802,7 +2805,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
28022805
llfn
28032806
}
28042807

2805-
_ => panic!("get_item_val: weird result in table")
2808+
_ => ccx.sess().bug("get_item_val: weird result in table")
28062809
};
28072810

28082811
match attr::first_attr_value_str_by_name(&i.attrs,
@@ -2866,7 +2869,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
28662869
let args = match v.node.kind {
28672870
ast::TupleVariantKind(ref args) => args,
28682871
ast::StructVariantKind(_) => {
2869-
panic!("struct variant kind unexpected in get_item_val")
2872+
ccx.sess().bug("struct variant kind unexpected in get_item_val")
28702873
}
28712874
};
28722875
assert!(args.len() != 0);
@@ -2882,7 +2885,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef {
28822885
ast::ItemEnum(_, _) => {
28832886
register_fn(ccx, (*v).span, sym, id, ty)
28842887
}
2885-
_ => panic!("NodeVariant, shouldn't happen")
2888+
_ => ccx.sess().bug("NodeVariant, shouldn't happen")
28862889
};
28872890
set_inline_hint(llfn);
28882891
llfn
@@ -2935,9 +2938,17 @@ fn register_method(ccx: &CrateContext, id: ast::NodeId,
29352938

29362939
let sym = exported_name(ccx, id, mty, &m.attrs);
29372940

2938-
let llfn = register_fn(ccx, m.span, sym, id, mty);
2939-
set_llvm_fn_attrs(ccx, &m.attrs, llfn);
2940-
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+
}
29412952
}
29422953

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

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

Lines changed: 35 additions & 0 deletions
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)