Skip to content

Commit 9f65b05

Browse files
committed
Only suggest signature fixes for non-local traits
1 parent 1f44d4a commit 9f65b05

File tree

9 files changed

+18
-76
lines changed

9 files changed

+18
-76
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,21 +1823,24 @@ fn compare_number_of_method_arguments<'tcx>(
18231823
})
18241824
});
18251825

1826-
if let (Some(span), Some(missing)) = (impl_inputs_span, missing_params) {
1827-
let sep = if impl_number_args == 0 { "" } else { ", " };
1828-
err.span_suggestion_verbose(
1829-
span.shrink_to_hi(),
1830-
"add the missing parameter from the trait",
1831-
format!("{sep}{missing}"),
1832-
Applicability::MaybeIncorrect,
1833-
);
1834-
} else if let (Some(span), Some(suggestion)) = (impl_inputs_span, full_suggestion) {
1835-
err.span_suggestion_verbose(
1836-
span,
1837-
"modify the signature to match the trait definition",
1838-
suggestion,
1839-
Applicability::MaybeIncorrect,
1840-
);
1826+
// Only emit verbose suggestions when the trait span isn’t local (e.g., cross-crate).
1827+
if trait_span.is_none() {
1828+
if let (Some(span), Some(missing)) = (impl_inputs_span, missing_params) {
1829+
let sep = if impl_number_args == 0 { "" } else { ", " };
1830+
err.span_suggestion_verbose(
1831+
span.shrink_to_hi(),
1832+
"add the missing parameter from the trait",
1833+
format!("{sep}{missing}"),
1834+
Applicability::MaybeIncorrect,
1835+
);
1836+
} else if let (Some(span), Some(suggestion)) = (impl_inputs_span, full_suggestion) {
1837+
err.span_suggestion_verbose(
1838+
span,
1839+
"modify the signature to match the trait definition",
1840+
suggestion,
1841+
Applicability::MaybeIncorrect,
1842+
);
1843+
}
18411844
}
18421845

18431846
return Err(err.emit_unless_delay(delay));

tests/ui/error-codes/E0050.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ struct Bar;
88

99
impl Foo for Bar {
1010
fn foo(&self) -> bool { true } //~ ERROR E0050
11-
//~| HELP: modify the signature to match the trait definition
1211
fn bar(&self) { } //~ ERROR E0050
13-
//~| HELP: modify the signature to match the trait definition
1412
fn less(&self, x: u8, y: u8, z: u8) { } //~ ERROR E0050
15-
//~| HELP: modify the signature to match the trait definition
1613
}
1714

1815
fn main() {

tests/ui/error-codes/E0050.stderr

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ LL | fn foo(&self, x: u8) -> bool;
66
...
77
LL | fn foo(&self) -> bool { true }
88
| ^^^^^ expected 2 parameters, found 1
9-
|
10-
help: modify the signature to match the trait definition
11-
|
12-
LL | fn foo(&self, x: u8) -> bool { true }
13-
| +++++++
149

1510
error[E0050]: method `bar` has 1 parameter but the declaration in trait `Foo::bar` has 4
1611
--> $DIR/E0050.rs:12:12
@@ -20,11 +15,6 @@ LL | fn bar(&self, x: u8, y: u8, z: u8);
2015
...
2116
LL | fn bar(&self) { }
2217
| ^^^^^ expected 4 parameters, found 1
23-
|
24-
help: modify the signature to match the trait definition
25-
|
26-
LL | fn bar(&self, x: u8, y: u8, z: u8) { }
27-
| +++++++++++++++++++++
2818

2919
error[E0050]: method `less` has 4 parameters but the declaration in trait `Foo::less` has 1
3020
--> $DIR/E0050.rs:14:13
@@ -34,12 +24,6 @@ LL | fn less(&self);
3424
...
3525
LL | fn less(&self, x: u8, y: u8, z: u8) { }
3626
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 1 parameter, found 4
37-
|
38-
help: modify the signature to match the trait definition
39-
|
40-
LL - fn less(&self, x: u8, y: u8, z: u8) { }
41-
LL + fn less(&self) { }
42-
|
4327

4428
error: aborting due to 3 previous errors
4529

tests/ui/impl-trait/in-trait/method-signature-matches.too_few.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ LL | fn come_on_a_little_more_effort(_: (), _: (), _: ()) -> impl Sized;
66
...
77
LL | fn come_on_a_little_more_effort() {}
88
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 3 parameters, found 0
9-
|
10-
help: add the missing parameter from the trait
11-
|
12-
LL | fn come_on_a_little_more_effort(_: (), _: (), _: ()) {}
13-
| +++++++++++++++++++
149

1510
error: aborting due to 1 previous error
1611

tests/ui/impl-trait/in-trait/method-signature-matches.too_many.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@ LL | fn calm_down_please() -> impl Sized;
66
...
77
LL | fn calm_down_please(_: (), _: (), _: ()) {}
88
| ^^^^^^^^^^^^^^^^ expected 0 parameters, found 3
9-
|
10-
help: modify the signature to match the trait definition
11-
|
12-
LL - fn calm_down_please(_: (), _: (), _: ()) {}
13-
LL + fn calm_down_please() {}
14-
|
159

1610
error: aborting due to 1 previous error
1711

tests/ui/suggestions/bad-infer-in-trait-impl.stderr

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ LL | fn bar();
1212
...
1313
LL | fn bar(s: _) {}
1414
| ^ expected 0 parameters, found 1
15-
|
16-
help: modify the signature to match the trait definition
17-
|
18-
LL - fn bar(s: _) {}
19-
LL + fn bar() {}
20-
|
2115

2216
error: aborting due to 2 previous errors
2317

tests/ui/suggestions/trait-impl-mismatched-with-alias.stderr

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ LL | fn foo(a: i32, b: u32);
66
...
77
LL | fn foo(a: AliasOfI32) {}
88
| ^^^^^^^^^^ expected 2 parameters, found 1
9-
|
10-
help: add the missing parameter from the trait
11-
|
12-
LL | fn foo(a: AliasOfI32, b: u32) {}
13-
| ++++++++
149

1510
error[E0050]: method `foo` has 1 parameter but the declaration in trait `Foo::foo` has 2
1611
--> $DIR/trait-impl-mismatched-with-alias.rs:15:15
@@ -20,12 +15,6 @@ LL | fn foo(a: i32, b: u32);
2015
...
2116
LL | fn foo(a: AliasOfU32) {}
2217
| ^^^^^^^^^^ expected 2 parameters, found 1
23-
|
24-
help: modify the signature to match the trait definition
25-
|
26-
LL - fn foo(a: AliasOfU32) {}
27-
LL + fn foo(a: i32, b: u32) {}
28-
|
2918

3019
error: aborting due to 2 previous errors
3120

tests/ui/traits/impl-different-num-params.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ LL | fn bar(&self, x: usize) -> Self;
66
...
77
LL | fn bar(&self) -> isize {
88
| ^^^^^ expected 2 parameters, found 1
9-
|
10-
help: modify the signature to match the trait definition
11-
|
12-
LL | fn bar(&self, x: usize) -> isize {
13-
| ++++++++++
149

1510
error: aborting due to 1 previous error
1611

tests/ui/traits/trait-method-signature-mismatch.stderr

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ LL | fn foo(&mut self, x: i32, y: i32) -> i32;
77
LL | / &mut self,
88
LL | | x: i32,
99
| |______________^ expected 3 parameters, found 2
10-
|
11-
help: modify the signature to match the trait definition
12-
|
13-
LL - fn foo(
14-
LL - &mut self,
15-
LL - x: i32,
16-
LL - ) {
17-
LL + fn foo(&mut self, x: i32, y: i32) {
18-
|
1910

2011
error: aborting due to 1 previous error
2112

0 commit comments

Comments
 (0)