Skip to content

Commit 0ed5f09

Browse files
authored
Rollup merge of rust-lang#112508 - compiler-errors:trait-sig-lifetime-sugg-ice, r=cjgillot
Tweak spans for self arg, fix borrow suggestion for signature mismatch 1. Adjust a suggestion message that was annoying me 2. Fix rust-lang#112503 by recording the right spans for the `self` part of the `&self` 0th argument 3. Remove the suggestion for adjusting a trait signature on type mismatch, bc that's gonna probably break all the other impls of the trait even if it fixes its one usage 😅
2 parents ab0f3e6 + 8745ae2 commit 0ed5f09

19 files changed

+139
-39
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2353,7 +2353,12 @@ impl Param {
23532353
/// Builds a `Param` object from `ExplicitSelf`.
23542354
pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
23552355
let span = eself.span.to(eself_ident.span);
2356-
let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span, tokens: None });
2356+
let infer_ty = P(Ty {
2357+
id: DUMMY_NODE_ID,
2358+
kind: TyKind::ImplicitSelf,
2359+
span: eself_ident.span,
2360+
tokens: None,
2361+
});
23572362
let (mutbl, ty) = match eself.node {
23582363
SelfKind::Explicit(ty, mutbl) => (mutbl, ty),
23592364
SelfKind::Value(mutbl) => (mutbl, infer_ty),

compiler/rustc_trait_selection/messages.ftl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
trait_selection_adjust_signature_borrow = consider adjusting the signature so it borrows its {$len ->
2+
[one] argument
3+
*[other] arguments
4+
}
5+
6+
trait_selection_adjust_signature_remove_borrow = consider adjusting the signature so it does not borrow its {$len ->
7+
[one] argument
8+
*[other] arguments
9+
}
10+
111
trait_selection_dump_vtable_entries = vtable entries for `{$trait_ref}`: {$entries}
212
313
trait_selection_empty_on_clause_in_rustc_on_unimplemented = empty `on`-clause in `#[rustc_on_unimplemented]`

compiler/rustc_trait_selection/src/errors.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
use crate::fluent_generated as fluent;
2-
use rustc_errors::{ErrorGuaranteed, Handler, IntoDiagnostic};
2+
use rustc_errors::{
3+
AddToDiagnostic, Applicability, Diagnostic, ErrorGuaranteed, Handler, IntoDiagnostic,
4+
SubdiagnosticMessage,
5+
};
36
use rustc_macros::Diagnostic;
47
use rustc_middle::ty::{self, PolyTraitRef, Ty};
58
use rustc_span::{Span, Symbol};
@@ -97,3 +100,34 @@ pub struct InherentProjectionNormalizationOverflow {
97100
pub span: Span,
98101
pub ty: String,
99102
}
103+
104+
pub enum AdjustSignatureBorrow {
105+
Borrow { to_borrow: Vec<(Span, String)> },
106+
RemoveBorrow { remove_borrow: Vec<(Span, String)> },
107+
}
108+
109+
impl AddToDiagnostic for AdjustSignatureBorrow {
110+
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
111+
where
112+
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
113+
{
114+
match self {
115+
AdjustSignatureBorrow::Borrow { to_borrow } => {
116+
diag.set_arg("len", to_borrow.len());
117+
diag.multipart_suggestion_verbose(
118+
fluent::trait_selection_adjust_signature_borrow,
119+
to_borrow,
120+
Applicability::MaybeIncorrect,
121+
);
122+
}
123+
AdjustSignatureBorrow::RemoveBorrow { remove_borrow } => {
124+
diag.set_arg("len", remove_borrow.len());
125+
diag.multipart_suggestion_verbose(
126+
fluent::trait_selection_adjust_signature_remove_borrow,
127+
remove_borrow,
128+
Applicability::MaybeIncorrect,
129+
);
130+
}
131+
}
132+
}
133+
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::{
55
PredicateObligation,
66
};
77

8+
use crate::errors;
89
use crate::infer::InferCtxt;
910
use crate::traits::{NormalizeExt, ObligationCtxt};
1011

@@ -4031,6 +4032,10 @@ fn hint_missing_borrow<'tcx>(
40314032
found_node: Node<'_>,
40324033
err: &mut Diagnostic,
40334034
) {
4035+
if matches!(found_node, Node::TraitItem(..)) {
4036+
return;
4037+
}
4038+
40344039
let found_args = match found.kind() {
40354040
ty::FnPtr(f) => infcx.instantiate_binder_with_placeholders(*f).inputs().iter(),
40364041
kind => {
@@ -4102,19 +4107,11 @@ fn hint_missing_borrow<'tcx>(
41024107
}
41034108

41044109
if !to_borrow.is_empty() {
4105-
err.multipart_suggestion_verbose(
4106-
"consider borrowing the argument",
4107-
to_borrow,
4108-
Applicability::MaybeIncorrect,
4109-
);
4110+
err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow });
41104111
}
41114112

41124113
if !remove_borrow.is_empty() {
4113-
err.multipart_suggestion_verbose(
4114-
"do not borrow the argument",
4115-
remove_borrow,
4116-
Applicability::MaybeIncorrect,
4117-
);
4114+
err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow });
41184115
}
41194116
}
41204117

tests/ui/anonymous-higher-ranked-lifetime.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ note: required by a bound in `f1`
1313
|
1414
LL | fn f1<F>(_: F) where F: Fn(&(), &()) {}
1515
| ^^^^^^^^^^^^ required by this bound in `f1`
16-
help: consider borrowing the argument
16+
help: consider adjusting the signature so it borrows its arguments
1717
|
1818
LL | f1(|_: &(), _: &()| {});
1919
| + +
@@ -33,7 +33,7 @@ note: required by a bound in `f2`
3333
|
3434
LL | fn f2<F>(_: F) where F: for<'a> Fn(&'a (), &()) {}
3535
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f2`
36-
help: consider borrowing the argument
36+
help: consider adjusting the signature so it borrows its arguments
3737
|
3838
LL | f2(|_: &(), _: &()| {});
3939
| + +
@@ -53,7 +53,7 @@ note: required by a bound in `f3`
5353
|
5454
LL | fn f3<'a, F>(_: F) where F: Fn(&'a (), &()) {}
5555
| ^^^^^^^^^^^^^^^ required by this bound in `f3`
56-
help: consider borrowing the argument
56+
help: consider adjusting the signature so it borrows its arguments
5757
|
5858
LL | f3(|_: &(), _: &()| {});
5959
| + +
@@ -73,7 +73,7 @@ note: required by a bound in `f4`
7373
|
7474
LL | fn f4<F>(_: F) where F: for<'r> Fn(&(), &'r ()) {}
7575
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f4`
76-
help: consider borrowing the argument
76+
help: consider adjusting the signature so it borrows its arguments
7777
|
7878
LL | f4(|_: &(), _: &()| {});
7979
| + +
@@ -93,7 +93,7 @@ note: required by a bound in `f5`
9393
|
9494
LL | fn f5<F>(_: F) where F: for<'r> Fn(&'r (), &'r ()) {}
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f5`
96-
help: consider borrowing the argument
96+
help: consider adjusting the signature so it borrows its arguments
9797
|
9898
LL | f5(|_: &(), _: &()| {});
9999
| + +
@@ -113,7 +113,7 @@ note: required by a bound in `g1`
113113
|
114114
LL | fn g1<F>(_: F) where F: Fn(&(), Box<dyn Fn(&())>) {}
115115
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g1`
116-
help: consider borrowing the argument
116+
help: consider adjusting the signature so it borrows its argument
117117
|
118118
LL | g1(|_: &(), _: ()| {});
119119
| +
@@ -133,7 +133,7 @@ note: required by a bound in `g2`
133133
|
134134
LL | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
135135
| ^^^^^^^^^^^^^^^^ required by this bound in `g2`
136-
help: consider borrowing the argument
136+
help: consider adjusting the signature so it borrows its argument
137137
|
138138
LL | g2(|_: &(), _: ()| {});
139139
| +
@@ -153,7 +153,7 @@ note: required by a bound in `g3`
153153
|
154154
LL | fn g3<F>(_: F) where F: for<'s> Fn(&'s (), Box<dyn Fn(&())>) {}
155155
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g3`
156-
help: consider borrowing the argument
156+
help: consider adjusting the signature so it borrows its argument
157157
|
158158
LL | g3(|_: &(), _: ()| {});
159159
| +
@@ -173,7 +173,7 @@ note: required by a bound in `g4`
173173
|
174174
LL | fn g4<F>(_: F) where F: Fn(&(), for<'r> fn(&'r ())) {}
175175
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g4`
176-
help: consider borrowing the argument
176+
help: consider adjusting the signature so it borrows its argument
177177
|
178178
LL | g4(|_: &(), _: ()| {});
179179
| +
@@ -193,7 +193,7 @@ note: required by a bound in `h1`
193193
|
194194
LL | fn h1<F>(_: F) where F: Fn(&(), Box<dyn Fn(&())>, &(), fn(&(), &())) {}
195195
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h1`
196-
help: consider borrowing the argument
196+
help: consider adjusting the signature so it borrows its arguments
197197
|
198198
LL | h1(|_: &(), _: (), _: &(), _: ()| {});
199199
| + +
@@ -213,7 +213,7 @@ note: required by a bound in `h2`
213213
|
214214
LL | fn h2<F>(_: F) where F: for<'t0> Fn(&(), Box<dyn Fn(&())>, &'t0 (), fn(&(), &())) {}
215215
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h2`
216-
help: consider borrowing the argument
216+
help: consider adjusting the signature so it borrows its arguments
217217
|
218218
LL | h2(|_: &(), _: (), _: &(), _: ()| {});
219219
| + +

tests/ui/closures/multiple-fn-bounds.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ note: required by a bound in `foo`
1818
|
1919
LL | fn foo<F: Fn(&char) -> bool + Fn(char) -> bool>(f: F) {
2020
| ^^^^^^^^^^^^^^^^ required by this bound in `foo`
21-
help: do not borrow the argument
21+
help: consider adjusting the signature so it does not borrow its argument
2222
|
2323
LL | foo(move |char| v);
2424
| ~~~~

tests/ui/dropck/explicit-drop-bounds.bad1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ LL | [T; 1]: Copy, T: std::marker::Copy // But `[T; 1]: Copy` does not imply
1515
| ~~~~~~~~~~~~~~~~~~~~~~
1616

1717
error[E0277]: the trait bound `T: Copy` is not satisfied
18-
--> $DIR/explicit-drop-bounds.rs:32:13
18+
--> $DIR/explicit-drop-bounds.rs:32:18
1919
|
2020
LL | fn drop(&mut self) {}
21-
| ^^^^^^^^^ the trait `Copy` is not implemented for `T`
21+
| ^^^^ the trait `Copy` is not implemented for `T`
2222
|
2323
note: required by a bound in `DropMe`
2424
--> $DIR/explicit-drop-bounds.rs:7:18

tests/ui/dropck/explicit-drop-bounds.bad2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ LL | impl<T: std::marker::Copy> Drop for DropMe<T>
1515
| +++++++++++++++++++
1616

1717
error[E0277]: the trait bound `T: Copy` is not satisfied
18-
--> $DIR/explicit-drop-bounds.rs:40:13
18+
--> $DIR/explicit-drop-bounds.rs:40:18
1919
|
2020
LL | fn drop(&mut self) {}
21-
| ^^^^^^^^^ the trait `Copy` is not implemented for `T`
21+
| ^^^^ the trait `Copy` is not implemented for `T`
2222
|
2323
note: required by a bound in `DropMe`
2424
--> $DIR/explicit-drop-bounds.rs:7:18

tests/ui/mismatched_types/closure-arg-type-mismatch-issue-45727.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | let _ = (-10..=10).find(|x: i32| x.signum() == 0);
1010
found closure signature `fn(i32) -> _`
1111
note: required by a bound in `find`
1212
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
13-
help: consider borrowing the argument
13+
help: consider adjusting the signature so it borrows its argument
1414
|
1515
LL | let _ = (-10..=10).find(|x: &i32| x.signum() == 0);
1616
| +
@@ -27,7 +27,7 @@ LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
2727
found closure signature `for<'a, 'b, 'c> fn(&'a &'b &'c i32) -> _`
2828
note: required by a bound in `find`
2929
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
30-
help: do not borrow the argument
30+
help: consider adjusting the signature so it does not borrow its argument
3131
|
3232
LL - let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
3333
LL + let _ = (-10..=10).find(|x: &i32| x.signum() == 0);

tests/ui/mismatched_types/closure-arg-type-mismatch.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | a.iter().map(|_: (u32, u32)| 45);
1010
found closure signature `fn((u32, u32)) -> _`
1111
note: required by a bound in `map`
1212
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
13-
help: consider borrowing the argument
13+
help: consider adjusting the signature so it borrows its argument
1414
|
1515
LL | a.iter().map(|_: &(u32, u32)| 45);
1616
| +

tests/ui/mismatched_types/issue-36053-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | once::<&str>("str").fuse().filter(|a: &str| true).count();
1010
found closure signature `for<'a> fn(&'a str) -> _`
1111
note: required by a bound in `filter`
1212
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
13-
help: consider borrowing the argument
13+
help: consider adjusting the signature so it borrows its argument
1414
|
1515
LL | once::<&str>("str").fuse().filter(|a: &&str| true).count();
1616
| +

tests/ui/mismatched_types/suggest-option-asderef-inference-var.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | let _has_inference_vars: Option<i32> = Some(0).map(deref_int);
1313
found function signature `for<'a> fn(&'a i32) -> _`
1414
note: required by a bound in `Option::<T>::map`
1515
--> $SRC_DIR/core/src/option.rs:LL:COL
16-
help: do not borrow the argument
16+
help: consider adjusting the signature so it does not borrow its argument
1717
|
1818
LL - fn deref_int(a: &i32) -> i32 {
1919
LL + fn deref_int(a: i32) -> i32 {

tests/ui/mismatched_types/suggest-option-asderef.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn generic<T>(_: T) -> Option<()> {
1717
}
1818

1919
fn generic_ref<T>(_: T) -> Option<()> {
20-
//~^ HELP do not borrow the argument
20+
//~^ HELP consider adjusting the signature so it does not borrow its argument
2121
Some(())
2222
}
2323

tests/ui/mismatched_types/suggest-option-asderef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn generic<T>(_: T) -> Option<()> {
1717
}
1818

1919
fn generic_ref<T>(_: &T) -> Option<()> {
20-
//~^ HELP do not borrow the argument
20+
//~^ HELP consider adjusting the signature so it does not borrow its argument
2121
Some(())
2222
}
2323

tests/ui/mismatched_types/suggest-option-asderef.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ LL | let _ = produces_string().and_then(generic_ref);
7373
found function signature `for<'a> fn(&'a _) -> _`
7474
note: required by a bound in `Option::<T>::and_then`
7575
--> $SRC_DIR/core/src/option.rs:LL:COL
76-
help: do not borrow the argument
76+
help: consider adjusting the signature so it does not borrow its argument
7777
|
7878
LL - fn generic_ref<T>(_: &T) -> Option<()> {
7979
LL + fn generic_ref<T>(_: T) -> Option<()> {

tests/ui/specialization/min_specialization/issue-79224.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ LL | impl<B: ?Sized + std::clone::Clone> Display for Cow<'_, B> {
1111
| +++++++++++++++++++
1212

1313
error[E0277]: the trait bound `B: Clone` is not satisfied
14-
--> $DIR/issue-79224.rs:20:12
14+
--> $DIR/issue-79224.rs:20:13
1515
|
1616
LL | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17-
| ^^^^^ the trait `Clone` is not implemented for `B`
17+
| ^^^^ the trait `Clone` is not implemented for `B`
1818
|
1919
= note: required for `B` to implement `ToOwned`
2020
help: consider further restricting this bound

tests/ui/suggestions/late-bound-in-borrow-closure-sugg.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ note: required by a bound in `Trader::<'a>::set_closure`
1616
|
1717
LL | pub fn set_closure(&mut self, function: impl Fn(&mut Trader) + 'a) {
1818
| ^^^^^^^^^^^^^^^ required by this bound in `Trader::<'a>::set_closure`
19-
help: consider borrowing the argument
19+
help: consider adjusting the signature so it borrows its argument
2020
|
2121
LL | let closure = |trader : &mut Trader| {
2222
| ++++
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
pub trait Insertable {
2+
type Values;
3+
4+
fn values(&self) -> Self::Values;
5+
}
6+
7+
impl<T> Insertable for Option<T> {
8+
type Values = ();
9+
10+
fn values(self) -> Self::Values {
11+
//~^ ERROR method `values` has an incompatible type for trait
12+
self.map(Insertable::values).unwrap_or_default()
13+
//~^ ERROR type mismatch in function arguments
14+
}
15+
}
16+
17+
fn main() {}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0053]: method `values` has an incompatible type for trait
2+
--> $DIR/mismatched-map-under-self.rs:10:15
3+
|
4+
LL | fn values(self) -> Self::Values {
5+
| ^^^^
6+
| |
7+
| expected `&Option<T>`, found `Option<T>`
8+
| help: change the self-receiver type to match the trait: `&self`
9+
|
10+
note: type in trait
11+
--> $DIR/mismatched-map-under-self.rs:4:15
12+
|
13+
LL | fn values(&self) -> Self::Values;
14+
| ^^^^^
15+
= note: expected signature `fn(&Option<T>)`
16+
found signature `fn(Option<T>)`
17+
18+
error[E0631]: type mismatch in function arguments
19+
--> $DIR/mismatched-map-under-self.rs:12:18
20+
|
21+
LL | fn values(&self) -> Self::Values;
22+
| --------------------------------- found signature defined here
23+
...
24+
LL | self.map(Insertable::values).unwrap_or_default()
25+
| --- ^^^^^^^^^^^^^^^^^^ expected due to this
26+
| |
27+
| required by a bound introduced by this call
28+
|
29+
= note: expected function signature `fn(T) -> _`
30+
found function signature `for<'a> fn(&'a _) -> _`
31+
note: required by a bound in `Option::<T>::map`
32+
--> $SRC_DIR/core/src/option.rs:LL:COL
33+
34+
error: aborting due to 2 previous errors
35+
36+
Some errors have detailed explanations: E0053, E0631.
37+
For more information about an error, try `rustc --explain E0053`.

0 commit comments

Comments
 (0)