Skip to content
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

Make bad "rust-call" arguments no longer ICE #78961

Merged
merged 5 commits into from
Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 31 additions & 1 deletion compiler/rustc_typeck/src/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ use rustc_hir::def::Res;
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::Visitor;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{HirIdMap, Node};
use rustc_hir::{HirIdMap, ImplicitSelfKind, Node};
use rustc_index::bit_set::BitSet;
use rustc_index::vec::Idx;
use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
Expand Down Expand Up @@ -137,6 +137,7 @@ use crate::util::common::indenter;

use self::coercion::DynamicCoerceMany;
pub use self::Expectation::*;
use rustc_target::spec::abi;

#[macro_export]
macro_rules! type_error_struct {
Expand Down Expand Up @@ -520,6 +521,35 @@ fn typeck_with_fallback<'tcx>(

let fn_sig = fixup_opaque_types(tcx, &fn_sig);

if fn_sig.abi == abi::Abi::RustCall {
CraftSpider marked this conversation as resolved.
Show resolved Hide resolved
let expected_args = if let ImplicitSelfKind::None = decl.implicit_self { 1 } else { 2 };

let err = || {
if let Node::Item(item) = tcx.hir().get(id) {
jyn514 marked this conversation as resolved.
Show resolved Hide resolved
if let hir::ItemKind::Fn(header, ..) = &item.kind {
tcx.sess.span_err(header.span, "A function with the \"rust-call\" ABI must take a single non-self argument that is a tuple")
}
} else {
bug!("Couldn't get span of FnHeader being checked")
}
};

if fn_sig.inputs().len() != expected_args {
err()
} else {
match fn_sig.inputs()[expected_args - 1].kind() {
jyn514 marked this conversation as resolved.
Show resolved Hide resolved
ty::Tuple(_) => (),
// FIXME(CraftSpider) Add a check on parameter expansion, so we don't just make the ICE happen later on
// This will probably require wide-scale changes to support a TupleKind obligation
// We can't resolve this without knowing the type of the param
ty::Param(_) => (),
_ => {
err()
}
}
}
}

let fcx = check_fn(&inh, param_env, fn_sig, decl, id, body, None).0;
fcx
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/abi/issues/issue-22565-rust-call.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(unboxed_closures)]

extern "rust-call" fn b(_i: i32) {}
//~^ ERROR A function with the "rust-call" ABI must take a single non-self argument that is a tuple

fn main () {
b(10);
}
8 changes: 8 additions & 0 deletions src/test/ui/abi/issues/issue-22565-rust-call.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: A function with the "rust-call" ABI must take a single non-self argument that is a tuple
--> $DIR/issue-22565-rust-call.rs:3:1
|
LL | extern "rust-call" fn b(_i: i32) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

9 changes: 9 additions & 0 deletions src/test/ui/abi/rustcall-generic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// check-pass
#![feature(unboxed_closures)]

extern "rust-call" fn foo<T>(_: T) {}

fn main() {
foo(());
foo((1, 2));
}
12 changes: 6 additions & 6 deletions src/test/ui/feature-gates/feature-gate-abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extern "rust-intrinsic" fn f1() {} //~ ERROR intrinsics are subject to change
extern "platform-intrinsic" fn f2() {} //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn f3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn f4() {} //~ ERROR rust-call ABI is subject to change
extern "rust-call" fn f4(_: ()) {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn f5() {} //~ ERROR msp430-interrupt ABI is experimental
extern "ptx-kernel" fn f6() {} //~ ERROR PTX ABIs are experimental and subject to change
extern "x86-interrupt" fn f7() {} //~ ERROR x86-interrupt ABI is experimental
Expand All @@ -30,7 +30,7 @@ trait Tr {
extern "platform-intrinsic" fn m2(); //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn m3(); //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn m4(); //~ ERROR rust-call ABI is subject to change
extern "rust-call" fn m4(_: ()); //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn m5(); //~ ERROR msp430-interrupt ABI is experimental
extern "ptx-kernel" fn m6(); //~ ERROR PTX ABIs are experimental and subject to change
extern "x86-interrupt" fn m7(); //~ ERROR x86-interrupt ABI is experimental
Expand All @@ -39,7 +39,7 @@ trait Tr {
extern "efiapi" fn m10(); //~ ERROR efiapi ABI is experimental and subject to change

extern "vectorcall" fn dm3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn dm4() {} //~ ERROR rust-call ABI is subject to change
extern "rust-call" fn dm4(_: ()) {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn dm5() {} //~ ERROR msp430-interrupt ABI is experimental
extern "ptx-kernel" fn dm6() {} //~ ERROR PTX ABIs are experimental and subject to change
extern "x86-interrupt" fn dm7() {} //~ ERROR x86-interrupt ABI is experimental
Expand All @@ -57,7 +57,7 @@ impl Tr for S {
extern "platform-intrinsic" fn m2() {} //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn m3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn m4() {} //~ ERROR rust-call ABI is subject to change
extern "rust-call" fn m4(_: ()) {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn m5() {} //~ ERROR msp430-interrupt ABI is experimental
extern "ptx-kernel" fn m6() {} //~ ERROR PTX ABIs are experimental and subject to change
extern "x86-interrupt" fn m7() {} //~ ERROR x86-interrupt ABI is experimental
Expand All @@ -73,7 +73,7 @@ impl S {
extern "platform-intrinsic" fn im2() {} //~ ERROR platform intrinsics are experimental
//~^ ERROR intrinsic must be in
extern "vectorcall" fn im3() {} //~ ERROR vectorcall is experimental and subject to change
extern "rust-call" fn im4() {} //~ ERROR rust-call ABI is subject to change
extern "rust-call" fn im4(_: ()) {} //~ ERROR rust-call ABI is subject to change
extern "msp430-interrupt" fn im5() {} //~ ERROR msp430-interrupt ABI is experimental
extern "ptx-kernel" fn im6() {} //~ ERROR PTX ABIs are experimental and subject to change
extern "x86-interrupt" fn im7() {} //~ ERROR x86-interrupt ABI is experimental
Expand All @@ -86,7 +86,7 @@ impl S {
type A1 = extern "rust-intrinsic" fn(); //~ ERROR intrinsics are subject to change
type A2 = extern "platform-intrinsic" fn(); //~ ERROR platform intrinsics are experimental
type A3 = extern "vectorcall" fn(); //~ ERROR vectorcall is experimental and subject to change
type A4 = extern "rust-call" fn(); //~ ERROR rust-call ABI is subject to change
type A4 = extern "rust-call" fn(_: ()); //~ ERROR rust-call ABI is subject to change
type A5 = extern "msp430-interrupt" fn(); //~ ERROR msp430-interrupt ABI is experimental
type A6 = extern "ptx-kernel" fn (); //~ ERROR PTX ABIs are experimental and subject to change
type A7 = extern "x86-interrupt" fn(); //~ ERROR x86-interrupt ABI is experimental
Expand Down
12 changes: 6 additions & 6 deletions src/test/ui/feature-gates/feature-gate-abi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ LL | extern "vectorcall" fn f3() {}
error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:18:8
|
LL | extern "rust-call" fn f4() {}
LL | extern "rust-call" fn f4(_: ()) {}
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
Expand Down Expand Up @@ -113,7 +113,7 @@ LL | extern "vectorcall" fn m3();
error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:33:12
|
LL | extern "rust-call" fn m4();
LL | extern "rust-call" fn m4(_: ());
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
Expand Down Expand Up @@ -183,7 +183,7 @@ LL | extern "vectorcall" fn dm3() {}
error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:42:12
|
LL | extern "rust-call" fn dm4() {}
LL | extern "rust-call" fn dm4(_: ()) {}
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
Expand Down Expand Up @@ -270,7 +270,7 @@ LL | extern "vectorcall" fn m3() {}
error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:60:12
|
LL | extern "rust-call" fn m4() {}
LL | extern "rust-call" fn m4(_: ()) {}
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
Expand Down Expand Up @@ -357,7 +357,7 @@ LL | extern "vectorcall" fn im3() {}
error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:76:12
|
LL | extern "rust-call" fn im4() {}
LL | extern "rust-call" fn im4(_: ()) {}
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
Expand Down Expand Up @@ -444,7 +444,7 @@ LL | type A3 = extern "vectorcall" fn();
error[E0658]: rust-call ABI is subject to change
--> $DIR/feature-gate-abi.rs:89:18
|
LL | type A4 = extern "rust-call" fn();
LL | type A4 = extern "rust-call" fn(_: ());
| ^^^^^^^^^^^
|
= note: see issue #29625 <https://github.com/rust-lang/rust/issues/29625> for more information
Expand Down