Skip to content

Commit dd3754d

Browse files
committed
Fix ICE in label_fn_like when splatted arg index is out of bounds
1 parent 0e4aed3 commit dd3754d

3 files changed

Lines changed: 86 additions & 1 deletion

File tree

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1715,7 +1715,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17151715
spans.push_span_label(expected_param.span(), "");
17161716
}
17171717
None => {
1718-
if !tuple_arguments.is_splatted() {
1718+
if tuple_arguments.is_splatted() {
17191719
// FIXME(splat): when the arg is splatted, adjust its index
17201720
use_splat_fallback = true;
17211721
} else {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// tests/ui/splat/splat-overload-at-home-fail.rs
2+
3+
// ignore-tidy-linelength
4+
//! Test error cases for `#[splat]` "overloading at home" example code.
5+
//! Splatted calls that don't match any registered MethodArgs impl should fail.
6+
#![allow(incomplete_features)]
7+
#![feature(splat)]
8+
#![feature(tuple_trait)]
9+
10+
struct Foo;
11+
12+
trait MethodArgs: std::marker::Tuple {
13+
fn call_method(self, _this: &Foo);
14+
}
15+
16+
impl MethodArgs for () {
17+
fn call_method(self, _this: &Foo) {}
18+
}
19+
20+
impl MethodArgs for (i32,) {
21+
fn call_method(self, _this: &Foo) {}
22+
}
23+
24+
impl MethodArgs for (i32, String) {
25+
fn call_method(self, _this: &Foo) {}
26+
}
27+
28+
impl Foo {
29+
fn method<T: MethodArgs>(&self, #[splat] args: T) {
30+
args.call_method(self)
31+
}
32+
}
33+
34+
fn main() {
35+
let foo = Foo;
36+
37+
// No impl for (f32,) — wrong type
38+
foo.method(42f32);
39+
//~^ ERROR mismatched types
40+
41+
// No impl for (i32,i32) - wrong type
42+
foo.method(42i32, 42i32);
43+
//~^ ERROR mismatched types
44+
45+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/splat-overload-at-home-fail.rs:38:16
3+
|
4+
LL | foo.method(42f32);
5+
| ------ ^^^^^ expected `i32`, found `f32`
6+
| |
7+
| arguments to this method are incorrect
8+
|
9+
note: method defined here
10+
--> $DIR/splat-overload-at-home-fail.rs:29:8
11+
|
12+
LL | fn method<T: MethodArgs>(&self, #[splat] args: T) {
13+
| ^^^^^^ ----------------
14+
help: change the type of the numeric literal from `f32` to `i32`
15+
|
16+
LL - foo.method(42f32);
17+
LL + foo.method(42i32);
18+
|
19+
20+
error[E0308]: mismatched types
21+
--> $DIR/splat-overload-at-home-fail.rs:42:23
22+
|
23+
LL | foo.method(42i32, 42i32);
24+
| ------ ^^^^^ expected `String`, found `i32`
25+
| |
26+
| arguments to this method are incorrect
27+
|
28+
note: method defined here
29+
--> $DIR/splat-overload-at-home-fail.rs:29:8
30+
|
31+
LL | fn method<T: MethodArgs>(&self, #[splat] args: T) {
32+
| ^^^^^^
33+
help: try using a conversion method
34+
|
35+
LL | foo.method(42i32, 42i32.to_string());
36+
| ++++++++++++
37+
38+
error: aborting due to 2 previous errors
39+
40+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)