Skip to content

Tweak spans for self arg, fix borrow suggestion for signature mismatch #112508

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

Merged
merged 4 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,12 @@ impl Param {
/// Builds a `Param` object from `ExplicitSelf`.
pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
let span = eself.span.to(eself_ident.span);
let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span, tokens: None });
let infer_ty = P(Ty {
id: DUMMY_NODE_ID,
kind: TyKind::ImplicitSelf,
span: eself_ident.span,
tokens: None,
});
let (mutbl, ty) = match eself.node {
SelfKind::Explicit(ty, mutbl) => (mutbl, ty),
SelfKind::Value(mutbl) => (mutbl, infer_ty),
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_trait_selection/messages.ftl
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
trait_selection_adjust_signature_borrow = consider adjusting the signature so it borrows its {$len ->
[one] argument
*[other] arguments
}

trait_selection_adjust_signature_remove_borrow = consider adjusting the signature so it does not borrow its {$len ->
[one] argument
*[other] arguments
}

trait_selection_dump_vtable_entries = vtable entries for `{$trait_ref}`: {$entries}

trait_selection_empty_on_clause_in_rustc_on_unimplemented = empty `on`-clause in `#[rustc_on_unimplemented]`
Expand Down
36 changes: 35 additions & 1 deletion compiler/rustc_trait_selection/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::fluent_generated as fluent;
use rustc_errors::{ErrorGuaranteed, Handler, IntoDiagnostic};
use rustc_errors::{
AddToDiagnostic, Applicability, Diagnostic, ErrorGuaranteed, Handler, IntoDiagnostic,
SubdiagnosticMessage,
};
use rustc_macros::Diagnostic;
use rustc_middle::ty::{self, PolyTraitRef, Ty};
use rustc_span::{Span, Symbol};
Expand Down Expand Up @@ -97,3 +100,34 @@ pub struct InherentProjectionNormalizationOverflow {
pub span: Span,
pub ty: String,
}

pub enum AdjustSignatureBorrow {
Borrow { to_borrow: Vec<(Span, String)> },
RemoveBorrow { remove_borrow: Vec<(Span, String)> },
}

impl AddToDiagnostic for AdjustSignatureBorrow {
fn add_to_diagnostic_with<F>(self, diag: &mut Diagnostic, _: F)
where
F: Fn(&mut Diagnostic, SubdiagnosticMessage) -> SubdiagnosticMessage,
{
match self {
AdjustSignatureBorrow::Borrow { to_borrow } => {
diag.set_arg("len", to_borrow.len());
diag.multipart_suggestion_verbose(
fluent::trait_selection_adjust_signature_borrow,
to_borrow,
Applicability::MaybeIncorrect,
);
}
AdjustSignatureBorrow::RemoveBorrow { remove_borrow } => {
diag.set_arg("len", remove_borrow.len());
diag.multipart_suggestion_verbose(
fluent::trait_selection_adjust_signature_remove_borrow,
remove_borrow,
Applicability::MaybeIncorrect,
);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{
PredicateObligation,
};

use crate::errors;
use crate::infer::InferCtxt;
use crate::traits::{NormalizeExt, ObligationCtxt};

Expand Down Expand Up @@ -3959,6 +3960,10 @@ fn hint_missing_borrow<'tcx>(
found_node: Node<'_>,
err: &mut Diagnostic,
) {
if matches!(found_node, Node::TraitItem(..)) {
return;
}

let found_args = match found.kind() {
ty::FnPtr(f) => infcx.instantiate_binder_with_placeholders(*f).inputs().iter(),
kind => {
Expand Down Expand Up @@ -4028,19 +4033,11 @@ fn hint_missing_borrow<'tcx>(
}

if !to_borrow.is_empty() {
err.multipart_suggestion_verbose(
"consider borrowing the argument",
to_borrow,
Applicability::MaybeIncorrect,
);
err.subdiagnostic(errors::AdjustSignatureBorrow::Borrow { to_borrow });
}

if !remove_borrow.is_empty() {
err.multipart_suggestion_verbose(
"do not borrow the argument",
remove_borrow,
Applicability::MaybeIncorrect,
);
err.subdiagnostic(errors::AdjustSignatureBorrow::RemoveBorrow { remove_borrow });
}
}

Expand Down
22 changes: 11 additions & 11 deletions tests/ui/anonymous-higher-ranked-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ note: required by a bound in `f1`
|
LL | fn f1<F>(_: F) where F: Fn(&(), &()) {}
| ^^^^^^^^^^^^ required by this bound in `f1`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its arguments
|
LL | f1(|_: &(), _: &()| {});
| + +
Expand All @@ -33,7 +33,7 @@ note: required by a bound in `f2`
|
LL | fn f2<F>(_: F) where F: for<'a> Fn(&'a (), &()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f2`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its arguments
|
LL | f2(|_: &(), _: &()| {});
| + +
Expand All @@ -53,7 +53,7 @@ note: required by a bound in `f3`
|
LL | fn f3<'a, F>(_: F) where F: Fn(&'a (), &()) {}
| ^^^^^^^^^^^^^^^ required by this bound in `f3`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its arguments
|
LL | f3(|_: &(), _: &()| {});
| + +
Expand All @@ -73,7 +73,7 @@ note: required by a bound in `f4`
|
LL | fn f4<F>(_: F) where F: for<'r> Fn(&(), &'r ()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f4`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its arguments
|
LL | f4(|_: &(), _: &()| {});
| + +
Expand All @@ -93,7 +93,7 @@ note: required by a bound in `f5`
|
LL | fn f5<F>(_: F) where F: for<'r> Fn(&'r (), &'r ()) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `f5`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its arguments
|
LL | f5(|_: &(), _: &()| {});
| + +
Expand All @@ -113,7 +113,7 @@ note: required by a bound in `g1`
|
LL | fn g1<F>(_: F) where F: Fn(&(), Box<dyn Fn(&())>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g1`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | g1(|_: &(), _: ()| {});
| +
Expand All @@ -133,7 +133,7 @@ note: required by a bound in `g2`
|
LL | fn g2<F>(_: F) where F: Fn(&(), fn(&())) {}
| ^^^^^^^^^^^^^^^^ required by this bound in `g2`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | g2(|_: &(), _: ()| {});
| +
Expand All @@ -153,7 +153,7 @@ note: required by a bound in `g3`
|
LL | fn g3<F>(_: F) where F: for<'s> Fn(&'s (), Box<dyn Fn(&())>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g3`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | g3(|_: &(), _: ()| {});
| +
Expand All @@ -173,7 +173,7 @@ note: required by a bound in `g4`
|
LL | fn g4<F>(_: F) where F: Fn(&(), for<'r> fn(&'r ())) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `g4`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | g4(|_: &(), _: ()| {});
| +
Expand All @@ -193,7 +193,7 @@ note: required by a bound in `h1`
|
LL | fn h1<F>(_: F) where F: Fn(&(), Box<dyn Fn(&())>, &(), fn(&(), &())) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h1`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its arguments
|
LL | h1(|_: &(), _: (), _: &(), _: ()| {});
| + +
Expand All @@ -213,7 +213,7 @@ note: required by a bound in `h2`
|
LL | fn h2<F>(_: F) where F: for<'t0> Fn(&(), Box<dyn Fn(&())>, &'t0 (), fn(&(), &())) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `h2`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its arguments
|
LL | h2(|_: &(), _: (), _: &(), _: ()| {});
| + +
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/closures/multiple-fn-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ note: required by a bound in `foo`
|
LL | fn foo<F: Fn(&char) -> bool + Fn(char) -> bool>(f: F) {
| ^^^^^^^^^^^^^^^^ required by this bound in `foo`
help: do not borrow the argument
help: consider adjusting the signature so it does not borrow its argument
|
LL | foo(move |char| v);
| ~~~~
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/dropck/explicit-drop-bounds.bad1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ LL | [T; 1]: Copy, T: std::marker::Copy // But `[T; 1]: Copy` does not imply
| ~~~~~~~~~~~~~~~~~~~~~~

error[E0277]: the trait bound `T: Copy` is not satisfied
--> $DIR/explicit-drop-bounds.rs:32:13
--> $DIR/explicit-drop-bounds.rs:32:18
|
LL | fn drop(&mut self) {}
| ^^^^^^^^^ the trait `Copy` is not implemented for `T`
| ^^^^ the trait `Copy` is not implemented for `T`
|
note: required by a bound in `DropMe`
--> $DIR/explicit-drop-bounds.rs:7:18
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/dropck/explicit-drop-bounds.bad2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ LL | impl<T: std::marker::Copy> Drop for DropMe<T>
| +++++++++++++++++++

error[E0277]: the trait bound `T: Copy` is not satisfied
--> $DIR/explicit-drop-bounds.rs:40:13
--> $DIR/explicit-drop-bounds.rs:40:18
|
LL | fn drop(&mut self) {}
| ^^^^^^^^^ the trait `Copy` is not implemented for `T`
| ^^^^ the trait `Copy` is not implemented for `T`
|
note: required by a bound in `DropMe`
--> $DIR/explicit-drop-bounds.rs:7:18
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | let _ = (-10..=10).find(|x: i32| x.signum() == 0);
found closure signature `fn(i32) -> _`
note: required by a bound in `find`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | let _ = (-10..=10).find(|x: &i32| x.signum() == 0);
| +
Expand All @@ -27,7 +27,7 @@ LL | let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
found closure signature `for<'a, 'b, 'c> fn(&'a &'b &'c i32) -> _`
note: required by a bound in `find`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: do not borrow the argument
help: consider adjusting the signature so it does not borrow its argument
|
LL - let _ = (-10..=10).find(|x: &&&i32| x.signum() == 0);
LL + let _ = (-10..=10).find(|x: &i32| x.signum() == 0);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/closure-arg-type-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | a.iter().map(|_: (u32, u32)| 45);
found closure signature `fn((u32, u32)) -> _`
note: required by a bound in `map`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | a.iter().map(|_: &(u32, u32)| 45);
| +
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/issue-36053-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ LL | once::<&str>("str").fuse().filter(|a: &str| true).count();
found closure signature `for<'a> fn(&'a str) -> _`
note: required by a bound in `filter`
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | once::<&str>("str").fuse().filter(|a: &&str| true).count();
| +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ LL | let _has_inference_vars: Option<i32> = Some(0).map(deref_int);
found function signature `for<'a> fn(&'a i32) -> _`
note: required by a bound in `Option::<T>::map`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: do not borrow the argument
help: consider adjusting the signature so it does not borrow its argument
|
LL - fn deref_int(a: &i32) -> i32 {
LL + fn deref_int(a: i32) -> i32 {
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/suggest-option-asderef.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn generic<T>(_: T) -> Option<()> {
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/suggest-option-asderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fn generic<T>(_: T) -> Option<()> {
}

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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/mismatched_types/suggest-option-asderef.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ LL | let _ = produces_string().and_then(generic_ref);
found function signature `for<'a> fn(&'a _) -> _`
note: required by a bound in `Option::<T>::and_then`
--> $SRC_DIR/core/src/option.rs:LL:COL
help: do not borrow the argument
help: consider adjusting the signature so it does not borrow its argument
|
LL - fn generic_ref<T>(_: &T) -> Option<()> {
LL + fn generic_ref<T>(_: T) -> Option<()> {
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/specialization/min_specialization/issue-79224.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ LL | impl<B: ?Sized + std::clone::Clone> Display for Cow<'_, B> {
| +++++++++++++++++++

error[E0277]: the trait bound `B: Clone` is not satisfied
--> $DIR/issue-79224.rs:20:12
--> $DIR/issue-79224.rs:20:13
|
LL | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
| ^^^^^ the trait `Clone` is not implemented for `B`
| ^^^^ the trait `Clone` is not implemented for `B`
|
= note: required for `B` to implement `ToOwned`
help: consider further restricting this bound
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ note: required by a bound in `Trader::<'a>::set_closure`
|
LL | pub fn set_closure(&mut self, function: impl Fn(&mut Trader) + 'a) {
| ^^^^^^^^^^^^^^^ required by this bound in `Trader::<'a>::set_closure`
help: consider borrowing the argument
help: consider adjusting the signature so it borrows its argument
|
LL | let closure = |trader : &mut Trader| {
| ++++
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/typeck/mismatched-map-under-self.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub trait Insertable {
type Values;

fn values(&self) -> Self::Values;
}

impl<T> Insertable for Option<T> {
type Values = ();

fn values(self) -> Self::Values {
//~^ ERROR method `values` has an incompatible type for trait
self.map(Insertable::values).unwrap_or_default()
//~^ ERROR type mismatch in function arguments
}
}

fn main() {}
37 changes: 37 additions & 0 deletions tests/ui/typeck/mismatched-map-under-self.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
error[E0053]: method `values` has an incompatible type for trait
--> $DIR/mismatched-map-under-self.rs:10:15
|
LL | fn values(self) -> Self::Values {
| ^^^^
| |
| expected `&Option<T>`, found `Option<T>`
| help: change the self-receiver type to match the trait: `&self`
|
note: type in trait
--> $DIR/mismatched-map-under-self.rs:4:15
|
LL | fn values(&self) -> Self::Values;
| ^^^^^
= note: expected signature `fn(&Option<T>)`
found signature `fn(Option<T>)`

error[E0631]: type mismatch in function arguments
--> $DIR/mismatched-map-under-self.rs:12:18
|
LL | fn values(&self) -> Self::Values;
| --------------------------------- found signature defined here
...
LL | self.map(Insertable::values).unwrap_or_default()
| --- ^^^^^^^^^^^^^^^^^^ expected due to this
| |
| required by a bound introduced by this call
|
= note: expected function signature `fn(T) -> _`
found function signature `for<'a> fn(&'a _) -> _`
note: required by a bound in `Option::<T>::map`
--> $SRC_DIR/core/src/option.rs:LL:COL

error: aborting due to 2 previous errors

Some errors have detailed explanations: E0053, E0631.
For more information about an error, try `rustc --explain E0053`.