Skip to content

Commit b37ad66

Browse files
Rollup merge of #56641 - GuillaumeGomez:span-trait-method-invalid-nb-parameters, r=estebank
fix span for invalid number of parameters in trait method Fixes #56582.
2 parents 33bf291 + 05cea31 commit b37ad66

File tree

5 files changed

+61
-11
lines changed

5 files changed

+61
-11
lines changed

src/librustc_typeck/check/compare_method.rs

+20-6
Original file line numberDiff line numberDiff line change
@@ -648,12 +648,19 @@ fn compare_number_of_method_arguments<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
648648
let trait_span = if let Some(trait_id) = trait_m_node_id {
649649
match tcx.hir().expect_trait_item(trait_id).node {
650650
TraitItemKind::Method(ref trait_m_sig, _) => {
651-
if let Some(arg) = trait_m_sig.decl.inputs.get(if trait_number_args > 0 {
651+
let pos = if trait_number_args > 0 {
652652
trait_number_args - 1
653653
} else {
654654
0
655-
}) {
656-
Some(arg.span)
655+
};
656+
if let Some(arg) = trait_m_sig.decl.inputs.get(pos) {
657+
Some(if pos == 0 {
658+
arg.span
659+
} else {
660+
Span::new(trait_m_sig.decl.inputs[0].span.lo(),
661+
arg.span.hi(),
662+
arg.span.ctxt())
663+
})
657664
} else {
658665
trait_item_span
659666
}
@@ -666,12 +673,19 @@ fn compare_number_of_method_arguments<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
666673
let impl_m_node_id = tcx.hir().as_local_node_id(impl_m.def_id).unwrap();
667674
let impl_span = match tcx.hir().expect_impl_item(impl_m_node_id).node {
668675
ImplItemKind::Method(ref impl_m_sig, _) => {
669-
if let Some(arg) = impl_m_sig.decl.inputs.get(if impl_number_args > 0 {
676+
let pos = if impl_number_args > 0 {
670677
impl_number_args - 1
671678
} else {
672679
0
673-
}) {
674-
arg.span
680+
};
681+
if let Some(arg) = impl_m_sig.decl.inputs.get(pos) {
682+
if pos == 0 {
683+
arg.span
684+
} else {
685+
Span::new(impl_m_sig.decl.inputs[0].span.lo(),
686+
arg.span.hi(),
687+
arg.span.ctxt())
688+
}
675689
} else {
676690
impl_m_span
677691
}

src/test/ui/error-codes/E0050.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0050]: method `foo` has 1 parameter but the declaration in trait `Foo::fo
22
--> $DIR/E0050.rs:20:12
33
|
44
LL | fn foo(&self, x: u8) -> bool;
5-
| -- trait requires 2 parameters
5+
| ------------ trait requires 2 parameters
66
...
77
LL | fn foo(&self) -> bool { true } //~ ERROR E0050
88
| ^^^^^ expected 2 parameters, found 1
@@ -11,19 +11,19 @@ error[E0050]: method `bar` has 1 parameter but the declaration in trait `Foo::ba
1111
--> $DIR/E0050.rs:21:12
1212
|
1313
LL | fn bar(&self, x: u8, y: u8, z: u8);
14-
| -- trait requires 4 parameters
14+
| -------------------------- trait requires 4 parameters
1515
...
1616
LL | fn bar(&self) { } //~ ERROR E0050
1717
| ^^^^^ expected 4 parameters, found 1
1818

1919
error[E0050]: method `less` has 4 parameters but the declaration in trait `Foo::less` has 1
20-
--> $DIR/E0050.rs:22:37
20+
--> $DIR/E0050.rs:22:13
2121
|
2222
LL | fn less(&self);
2323
| ----- trait requires 1 parameter
2424
...
2525
LL | fn less(&self, x: u8, y: u8, z: u8) { } //~ ERROR E0050
26-
| ^^ expected 1 parameter, found 4
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 parameter, found 4
2727

2828
error: aborting due to 3 previous errors
2929

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 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+
11+
trait Foo {
12+
fn foo(&mut self, x: i32, y: i32) -> i32;
13+
}
14+
15+
impl Foo for i32 {
16+
fn foo(
17+
&mut self, //~ ERROR
18+
x: i32,
19+
) {
20+
}
21+
}
22+
23+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error[E0050]: method `foo` has 2 parameters but the declaration in trait `Foo::foo` has 3
2+
--> $DIR/trait-method-number-parameters.rs:17:9
3+
|
4+
LL | fn foo(&mut self, x: i32, y: i32) -> i32;
5+
| ------------------------- trait requires 3 parameters
6+
...
7+
LL | / &mut self, //~ ERROR
8+
LL | | x: i32,
9+
| |______________^ expected 3 parameters, found 2
10+
11+
error: aborting due to previous error
12+
13+
For more information about this error, try `rustc --explain E0050`.

src/test/ui/traits/trait-impl-different-num-params.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0050]: method `bar` has 1 parameter but the declaration in trait `foo::ba
22
--> $DIR/trait-impl-different-num-params.rs:15:12
33
|
44
LL | fn bar(&self, x: usize) -> Self;
5-
| ----- trait requires 2 parameters
5+
| --------------- trait requires 2 parameters
66
...
77
LL | fn bar(&self) -> isize {
88
| ^^^^^ expected 2 parameters, found 1

0 commit comments

Comments
 (0)