suggest lifetime for closure parameter type when mismatch
This commit is contained in:
parent
b0884a3528
commit
6e17349b12
6 changed files with 106 additions and 2 deletions
|
@ -1927,6 +1927,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
{
|
{
|
||||||
let span = self.tcx.def_span(def_id);
|
let span = self.tcx.def_span(def_id);
|
||||||
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
|
diag.span_note(span, "this closure does not fulfill the lifetime requirements");
|
||||||
|
self.suggest_for_all_lifetime_closure(span, &exp_found, diag);
|
||||||
}
|
}
|
||||||
|
|
||||||
// It reads better to have the error origin as the final
|
// It reads better to have the error origin as the final
|
||||||
|
|
|
@ -8,7 +8,7 @@ use rustc_middle::traits::{
|
||||||
StatementAsExpression,
|
StatementAsExpression,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::print::with_no_trimmed_paths;
|
use rustc_middle::ty::print::with_no_trimmed_paths;
|
||||||
use rustc_middle::ty::{self as ty, IsSuggestable, Ty, TypeVisitableExt};
|
use rustc_middle::ty::{self as ty, GenericArgKind, IsSuggestable, Ty, TypeVisitableExt};
|
||||||
use rustc_span::{sym, BytePos, Span};
|
use rustc_span::{sym, BytePos, Span};
|
||||||
use rustc_target::abi::FieldIdx;
|
use rustc_target::abi::FieldIdx;
|
||||||
|
|
||||||
|
@ -536,6 +536,62 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// For "one type is more general than the other" errors on closures, suggest changing the lifetime
|
||||||
|
/// of the parameters to accept all lifetimes.
|
||||||
|
pub(super) fn suggest_for_all_lifetime_closure(
|
||||||
|
&self,
|
||||||
|
span: Span,
|
||||||
|
exp_found: &ty::error::ExpectedFound<ty::PolyTraitRef<'tcx>>,
|
||||||
|
diag: &mut Diagnostic,
|
||||||
|
) {
|
||||||
|
// 1. Get the substs of the closure.
|
||||||
|
// 2. Assume exp_found is FnOnce / FnMut / Fn, we can extract function parameters from [1].
|
||||||
|
let expected = exp_found.expected.map_bound(|x| x.substs.get(1).cloned()).transpose();
|
||||||
|
let found = exp_found.found.map_bound(|x| x.substs.get(1).cloned()).transpose();
|
||||||
|
|
||||||
|
// 3. Extract the tuple type from Fn trait and suggest the change.
|
||||||
|
if let (Some(expected), Some(found)) = (expected, found) {
|
||||||
|
let expected = expected.skip_binder().unpack();
|
||||||
|
let found = found.skip_binder().unpack();
|
||||||
|
if let (GenericArgKind::Type(expected), GenericArgKind::Type(found)) = (expected, found)
|
||||||
|
&& let (ty::Tuple(expected), ty::Tuple(found)) = (expected.kind(), found.kind())
|
||||||
|
&& expected.len() == found.len() {
|
||||||
|
let mut suggestion = "|".to_string();
|
||||||
|
let mut is_first = true;
|
||||||
|
let mut has_suggestion = false;
|
||||||
|
|
||||||
|
for (expected, found) in expected.iter().zip(found.iter()) {
|
||||||
|
if is_first {
|
||||||
|
is_first = true;
|
||||||
|
} else {
|
||||||
|
suggestion += ", ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if let (ty::Ref(expected_region, _, _), ty::Ref(found_region, _, _)) = (expected.kind(), found.kind())
|
||||||
|
&& expected_region.is_late_bound() && !found_region.is_late_bound() {
|
||||||
|
// If the expected region is late bound, and the found region is not, we can suggest adding `: &_`.
|
||||||
|
// FIXME: use the actual type + variable name provided by user instead of `_`.
|
||||||
|
suggestion += "_: &_";
|
||||||
|
has_suggestion = true;
|
||||||
|
} else {
|
||||||
|
// Otherwise, keep it as-is.
|
||||||
|
suggestion += "_";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
suggestion += "|";
|
||||||
|
|
||||||
|
if has_suggestion {
|
||||||
|
diag.span_suggestion_verbose(
|
||||||
|
span,
|
||||||
|
"consider changing the type of the closure parameters",
|
||||||
|
suggestion,
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
impl<'tcx> TypeErrCtxt<'_, 'tcx> {
|
||||||
|
|
|
@ -43,6 +43,10 @@ note: the lifetime requirement is introduced here
|
||||||
|
|
|
|
||||||
LL | fn take_foo(_: impl Foo) {}
|
LL | fn take_foo(_: impl Foo) {}
|
||||||
| ^^^
|
| ^^^
|
||||||
|
help: consider changing the type of the closure parameters
|
||||||
|
|
|
||||||
|
LL | take_foo(|_: &_| a);
|
||||||
|
| ~~~~~~~
|
||||||
|
|
||||||
error[E0308]: mismatched types
|
error[E0308]: mismatched types
|
||||||
--> $DIR/issue-79187-2.rs:11:5
|
--> $DIR/issue-79187-2.rs:11:5
|
||||||
|
|
|
@ -16,6 +16,10 @@ note: the lifetime requirement is introduced here
|
||||||
|
|
|
|
||||||
LL | fn thing(x: impl FnOnce(&u32)) {}
|
LL | fn thing(x: impl FnOnce(&u32)) {}
|
||||||
| ^^^^^^^^^^^^
|
| ^^^^^^^^^^^^
|
||||||
|
help: consider changing the type of the closure parameters
|
||||||
|
|
|
||||||
|
LL | let f = |_: &_| ();
|
||||||
|
| ~~~~~~~
|
||||||
|
|
||||||
error: implementation of `FnOnce` is not general enough
|
error: implementation of `FnOnce` is not general enough
|
||||||
--> $DIR/issue-79187.rs:5:5
|
--> $DIR/issue-79187.rs:5:5
|
||||||
|
|
|
@ -8,4 +8,7 @@ fn main() {
|
||||||
baz(|_| ());
|
baz(|_| ());
|
||||||
//~^ ERROR implementation of `FnOnce` is not general enough
|
//~^ ERROR implementation of `FnOnce` is not general enough
|
||||||
//~| ERROR mismatched types
|
//~| ERROR mismatched types
|
||||||
|
baz(|x| ());
|
||||||
|
//~^ ERROR implementation of `FnOnce` is not general enough
|
||||||
|
//~| ERROR mismatched types
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,43 @@ note: the lifetime requirement is introduced here
|
||||||
|
|
|
|
||||||
LL | fn baz<T: Foo>(_: T) {}
|
LL | fn baz<T: Foo>(_: T) {}
|
||||||
| ^^^
|
| ^^^
|
||||||
|
help: consider changing the type of the closure parameters
|
||||||
|
|
|
||||||
|
LL | baz(|_: &_| ());
|
||||||
|
| ~~~~~~~
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: implementation of `FnOnce` is not general enough
|
||||||
|
--> $DIR/closure-mismatch.rs:11:5
|
||||||
|
|
|
||||||
|
LL | baz(|x| ());
|
||||||
|
| ^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
||||||
|
|
|
||||||
|
= note: closure with signature `fn(&'2 ())` must implement `FnOnce<(&'1 (),)>`, for any lifetime `'1`...
|
||||||
|
= note: ...but it actually implements `FnOnce<(&'2 (),)>`, for some specific lifetime `'2`
|
||||||
|
|
||||||
|
error[E0308]: mismatched types
|
||||||
|
--> $DIR/closure-mismatch.rs:11:5
|
||||||
|
|
|
||||||
|
LL | baz(|x| ());
|
||||||
|
| ^^^^^^^^^^^ one type is more general than the other
|
||||||
|
|
|
||||||
|
= note: expected trait `for<'a> Fn<(&'a (),)>`
|
||||||
|
found trait `Fn<(&(),)>`
|
||||||
|
note: this closure does not fulfill the lifetime requirements
|
||||||
|
--> $DIR/closure-mismatch.rs:11:9
|
||||||
|
|
|
||||||
|
LL | baz(|x| ());
|
||||||
|
| ^^^
|
||||||
|
note: the lifetime requirement is introduced here
|
||||||
|
--> $DIR/closure-mismatch.rs:5:11
|
||||||
|
|
|
||||||
|
LL | fn baz<T: Foo>(_: T) {}
|
||||||
|
| ^^^
|
||||||
|
help: consider changing the type of the closure parameters
|
||||||
|
|
|
||||||
|
LL | baz(|_: &_| ());
|
||||||
|
| ~~~~~~~
|
||||||
|
|
||||||
|
error: aborting due to 4 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0308`.
|
For more information about this error, try `rustc --explain E0308`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue