Skip to content

Simpler diagnostic when passing arg to closure and missing borrow #102813

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 9 commits into from
Dec 13, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
_ => None,
};

let found_node = found_did.and_then(|did| self.tcx.hir().get_if_local(did));
let found_span = found_did.and_then(|did| self.tcx.hir().span_if_local(did));

if self.reported_closure_mismatch.borrow().contains(&(span, found_span)) {
Expand Down Expand Up @@ -1287,6 +1288,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
found_trait_ref,
expected_trait_ref,
obligation.cause.code(),
found_node,
)
} else {
let (closure_span, closure_arg_span, found) = found_did
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// ignore-tidy-filelength

use super::{DefIdOrName, Obligation, ObligationCause, ObligationCauseCode, PredicateObligation};

use crate::autoderef::Autoderef;
Expand Down Expand Up @@ -258,6 +259,7 @@ pub trait TypeErrCtxtExt<'tcx> {
found: ty::PolyTraitRef<'tcx>,
expected: ty::PolyTraitRef<'tcx>,
cause: &ObligationCauseCode<'tcx>,
found_node: Option<Node<'_>>,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed>;

fn note_conflicting_closure_bounds(
Expand Down Expand Up @@ -1695,6 +1697,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
found: ty::PolyTraitRef<'tcx>,
expected: ty::PolyTraitRef<'tcx>,
cause: &ObligationCauseCode<'tcx>,
found_node: Option<Node<'_>>,
) -> DiagnosticBuilder<'tcx, ErrorGuaranteed> {
pub(crate) fn build_fn_sig_ty<'tcx>(
infcx: &InferCtxt<'tcx>,
Expand Down Expand Up @@ -1756,6 +1759,10 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {

self.note_conflicting_closure_bounds(cause, &mut err);

if let Some(found_node) = found_node {
hint_missing_borrow(span, found_span, found, expected, found_node, &mut err);
}

err
}

Expand Down Expand Up @@ -3384,6 +3391,78 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
}
}

/// Add a hint to add a missing borrow or remove an unnecessary one.
fn hint_missing_borrow<'tcx>(
span: Span,
found_span: Span,
found: Ty<'tcx>,
expected: Ty<'tcx>,
found_node: Node<'_>,
err: &mut Diagnostic,
) {
let found_args = match found.kind() {
ty::FnPtr(f) => f.inputs().skip_binder().iter(),
kind => {
span_bug!(span, "found was converted to a FnPtr above but is now {:?}", kind)
}
};
let expected_args = match expected.kind() {
ty::FnPtr(f) => f.inputs().skip_binder().iter(),
kind => {
span_bug!(span, "expected was converted to a FnPtr above but is now {:?}", kind)
}
};

let fn_decl = found_node
.fn_decl()
.unwrap_or_else(|| span_bug!(found_span, "found node must be a function"));

let arg_spans = fn_decl.inputs.iter().map(|ty| ty.span);

fn get_deref_type_and_refs<'tcx>(mut ty: Ty<'tcx>) -> (Ty<'tcx>, usize) {
let mut refs = 0;

while let ty::Ref(_, new_ty, _) = ty.kind() {
ty = *new_ty;
refs += 1;
}

(ty, refs)
}

let mut to_borrow = Vec::new();
let mut remove_borrow = Vec::new();

for ((found_arg, expected_arg), arg_span) in found_args.zip(expected_args).zip(arg_spans) {
let (found_ty, found_refs) = get_deref_type_and_refs(*found_arg);
let (expected_ty, expected_refs) = get_deref_type_and_refs(*expected_arg);

if found_ty == expected_ty {
if found_refs < expected_refs {
to_borrow.push((arg_span, expected_arg.to_string()));
} else if found_refs > expected_refs {
remove_borrow.push((arg_span, expected_arg.to_string()));
}
}
}

if !to_borrow.is_empty() {
err.multipart_suggestion(
"consider borrowing the argument",
to_borrow,
Applicability::MaybeIncorrect,
);
}

if !remove_borrow.is_empty() {
err.multipart_suggestion(
"do not borrow the argument",
remove_borrow,
Applicability::MaybeIncorrect,
);
}
}

/// Collect all the returned expressions within the input expression.
/// Used to point at the return spans when we want to suggest some change to them.
#[derive(Default)]
Expand Down
52 changes: 44 additions & 8 deletions src/test/ui/anonymous-higher-ranked-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ 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
|
LL | f1(|_: &(), _: &()| {});
| ~~~ ~~~

error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:3:5
Expand All @@ -29,6 +33,10 @@ 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
|
LL | f2(|_: &'a (), _: &()| {});
| ~~~~~~ ~~~

error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:4:5
Expand All @@ -45,6 +53,10 @@ 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
|
LL | f3(|_: &(), _: &()| {});
| ~~~ ~~~

error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:5:5
Expand All @@ -61,6 +73,10 @@ 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
|
LL | f4(|_: &(), _: &'r ()| {});
| ~~~ ~~~~~~

error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:6:5
Expand All @@ -77,13 +93,19 @@ 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
|
LL | f5(|_: &'r (), _: &'r ()| {});
| ~~~~~~ ~~~~~~

error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:7:5
|
LL | g1(|_: (), _: ()| {});
| ^^ -------------- found signature defined here
| |
| ^^ --------------
| | | |
| | | help: consider borrowing the argument: `&()`
| | found signature defined here
| expected due to this
|
= note: expected closure signature `for<'a> fn(&'a (), Box<(dyn for<'a> Fn(&'a ()) + 'static)>) -> _`
Expand All @@ -98,8 +120,10 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:8:5
|
LL | g2(|_: (), _: ()| {});
| ^^ -------------- found signature defined here
| |
| ^^ --------------
| | | |
| | | help: consider borrowing the argument: `&()`
| | found signature defined here
| expected due to this
|
= note: expected closure signature `for<'a> fn(&'a (), for<'a> fn(&'a ())) -> _`
Expand All @@ -114,8 +138,10 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:9:5
|
LL | g3(|_: (), _: ()| {});
| ^^ -------------- found signature defined here
| |
| ^^ --------------
| | | |
| | | help: consider borrowing the argument: `&'s ()`
| | found signature defined here
| expected due to this
|
= note: expected closure signature `for<'s> fn(&'s (), Box<(dyn for<'a> Fn(&'a ()) + 'static)>) -> _`
Expand All @@ -130,8 +156,10 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:10:5
|
LL | g4(|_: (), _: ()| {});
| ^^ -------------- found signature defined here
| |
| ^^ --------------
| | | |
| | | help: consider borrowing the argument: `&()`
| | found signature defined here
| expected due to this
|
= note: expected closure signature `for<'a> fn(&'a (), for<'r> fn(&'r ())) -> _`
Expand All @@ -157,6 +185,10 @@ 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
|
LL | h1(|_: &(), _: (), _: &(), _: ()| {});
| ~~~ ~~~

error[E0631]: type mismatch in closure arguments
--> $DIR/anonymous-higher-ranked-lifetime.rs:12:5
Expand All @@ -173,6 +205,10 @@ 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
|
LL | h2(|_: &(), _: (), _: &'t0 (), _: ()| {});
| ~~~ ~~~~~~~

error: aborting due to 11 previous errors

Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/closures/multiple-fn-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/multiple-fn-bounds.rs:10:5
|
LL | foo(move |x| v);
| ^^^ -------- found signature defined here
| |
| ^^^ --------
| | | |
| | | help: do not borrow the argument: `char`
| | found signature defined here
| expected due to this
|
= note: expected closure signature `fn(char) -> _`
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/mismatched_types/closure-arg-type-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/closure-arg-type-mismatch.rs:3:14
|
LL | a.iter().map(|_: (u32, u32)| 45);
| ^^^ --------------- found signature defined here
| |
| ^^^ ---------------
| | | |
| | | help: consider borrowing the argument: `&(u32, u32)`
| | found signature defined here
| expected due to this
|
= note: expected closure signature `fn(&(u32, u32)) -> _`
Expand Down
6 changes: 4 additions & 2 deletions src/test/ui/mismatched_types/issue-36053-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ error[E0631]: type mismatch in closure arguments
--> $DIR/issue-36053-2.rs:7:32
|
LL | once::<&str>("str").fuse().filter(|a: &str| true).count();
| ^^^^^^ --------- found signature defined here
| |
| ^^^^^^ ---------
| | | |
| | | help: consider borrowing the argument: `&&str`
| | found signature defined here
| expected due to this
|
= note: expected closure signature `for<'a> fn(&'a &str) -> _`
Expand Down