-
Notifications
You must be signed in to change notification settings - Fork 13.3k
When encountering unexpected closure return type, point at return type/expression #132156
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d3a148f
When encountering unexpected closure return type, point at return typ…
estebank 03e9a38
On E0271 for a closure behind a binding, point at binding in call too
estebank 87d323c
Add closure labels
estebank d2a781a
Remove `unwrap()`s
estebank d116767
review comment: change `span` argument
estebank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -649,6 +649,7 @@ pub fn check_function_signature<'tcx>( | |
}))), | ||
err, | ||
false, | ||
None, | ||
); | ||
return Err(diag.emit()); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1147,6 +1147,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { | |
Some(self.param_env.and(trace.values)), | ||
e, | ||
true, | ||
None, | ||
); | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -708,6 +708,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
None, | ||
TypeError::Sorts(ty::error::ExpectedFound::new(expected_ty, ct_ty)), | ||
false, | ||
None, | ||
); | ||
diag | ||
} | ||
|
@@ -931,14 +932,9 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
} | ||
} | ||
let hir_id = self.tcx.local_def_id_to_hir_id(obligation.cause.body_id); | ||
let body_id = match self.tcx.hir_node(hir_id) { | ||
hir::Node::Item(hir::Item { | ||
kind: hir::ItemKind::Fn { body: body_id, .. }, .. | ||
}) => body_id, | ||
_ => return false, | ||
}; | ||
let ControlFlow::Break(expr) = (FindMethodSubexprOfTry { search_span: span }) | ||
.visit_body(self.tcx.hir().body(*body_id)) | ||
let Some(body_id) = self.tcx.hir_node(hir_id).body_id() else { return false }; | ||
let ControlFlow::Break(expr) = | ||
(FindMethodSubexprOfTry { search_span: span }).visit_body(self.tcx.hir().body(body_id)) | ||
else { | ||
return false; | ||
}; | ||
|
@@ -1385,22 +1381,54 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
_ => (None, error.err), | ||
}; | ||
|
||
let msg = values | ||
let (msg, span, closure_span) = values | ||
.and_then(|(predicate, normalized_term, expected_term)| { | ||
self.maybe_detailed_projection_msg(predicate, normalized_term, expected_term) | ||
self.maybe_detailed_projection_msg( | ||
obligation.cause.span, | ||
predicate, | ||
normalized_term, | ||
expected_term, | ||
) | ||
}) | ||
.unwrap_or_else(|| { | ||
let mut cx = FmtPrinter::new_with_limit( | ||
self.tcx, | ||
Namespace::TypeNS, | ||
rustc_session::Limit(10), | ||
); | ||
with_forced_trimmed_paths!(format!("type mismatch resolving `{}`", { | ||
self.resolve_vars_if_possible(predicate).print(&mut cx).unwrap(); | ||
cx.into_buffer() | ||
})) | ||
( | ||
with_forced_trimmed_paths!(format!("type mismatch resolving `{}`", { | ||
self.resolve_vars_if_possible(predicate).print(&mut cx).unwrap(); | ||
cx.into_buffer() | ||
})), | ||
obligation.cause.span, | ||
None, | ||
) | ||
}); | ||
let mut diag = struct_span_code_err!(self.dcx(), obligation.cause.span, E0271, "{msg}"); | ||
let mut diag = struct_span_code_err!(self.dcx(), span, E0271, "{msg}"); | ||
if let Some(span) = closure_span { | ||
// Mark the closure decl so that it is seen even if we are pointing at the return | ||
// type or expression. | ||
// | ||
// error[E0271]: expected `{[email protected]:41:16}` to be a closure that returns | ||
// `Unit3`, but it returns `Unit4` | ||
// --> $DIR/foo.rs:43:17 | ||
// | | ||
// LL | let v = Unit2.m( | ||
// | - required by a bound introduced by this call | ||
// ... | ||
// LL | f: |x| { | ||
// | --- /* this span */ | ||
// LL | drop(x); | ||
// LL | Unit4 | ||
// | ^^^^^ expected `Unit3`, found `Unit4` | ||
// | | ||
diag.span_label(span, "this closure"); | ||
if !span.overlaps(obligation.cause.span) { | ||
// Point at the binding corresponding to the closure where it is used. | ||
diag.span_label(obligation.cause.span, "closure used here"); | ||
} | ||
} | ||
|
||
let secondary_span = (|| { | ||
let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)) = | ||
|
@@ -1471,6 +1499,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
}), | ||
err, | ||
false, | ||
Some(span), | ||
); | ||
self.note_obligation_cause(&mut diag, obligation); | ||
diag.emit() | ||
|
@@ -1479,34 +1508,66 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> { | |
|
||
fn maybe_detailed_projection_msg( | ||
&self, | ||
mut span: Span, | ||
projection_term: ty::AliasTerm<'tcx>, | ||
normalized_ty: ty::Term<'tcx>, | ||
expected_ty: ty::Term<'tcx>, | ||
) -> Option<String> { | ||
) -> Option<(String, Span, Option<Span>)> { | ||
let trait_def_id = projection_term.trait_def_id(self.tcx); | ||
let self_ty = projection_term.self_ty(); | ||
|
||
with_forced_trimmed_paths! { | ||
if self.tcx.is_lang_item(projection_term.def_id, LangItem::FnOnceOutput) { | ||
let fn_kind = self_ty.prefix_string(self.tcx); | ||
let (span, closure_span) = if let ty::Closure(def_id, _) = self_ty.kind() { | ||
let def_span = self.tcx.def_span(def_id); | ||
if let Some(local_def_id) = def_id.as_local() | ||
&& let node = self.tcx.hir_node_by_def_id(local_def_id) | ||
&& let Some(fn_decl) = node.fn_decl() | ||
&& let Some(id) = node.body_id() | ||
{ | ||
span = match fn_decl.output { | ||
hir::FnRetTy::Return(ty) => ty.span, | ||
hir::FnRetTy::DefaultReturn(_) => { | ||
let body = self.tcx.hir().body(id); | ||
match body.value.kind { | ||
hir::ExprKind::Block( | ||
hir::Block { expr: Some(expr), .. }, | ||
_, | ||
) => expr.span, | ||
hir::ExprKind::Block( | ||
hir::Block { | ||
expr: None, stmts: [.., last], .. | ||
}, | ||
_, | ||
) => last.span, | ||
_ => body.value.span, | ||
} | ||
} | ||
}; | ||
} | ||
(span, Some(def_span)) | ||
} else { | ||
(span, None) | ||
}; | ||
let item = match self_ty.kind() { | ||
ty::FnDef(def, _) => self.tcx.item_name(*def).to_string(), | ||
_ => self_ty.to_string(), | ||
}; | ||
Some(format!( | ||
Some((format!( | ||
"expected `{item}` to be a {fn_kind} that returns `{expected_ty}`, but it \ | ||
returns `{normalized_ty}`", | ||
)) | ||
), span, closure_span)) | ||
} else if self.tcx.is_lang_item(trait_def_id, LangItem::Future) { | ||
Some(format!( | ||
Some((format!( | ||
"expected `{self_ty}` to be a future that resolves to `{expected_ty}`, but it \ | ||
resolves to `{normalized_ty}`" | ||
)) | ||
), span, None)) | ||
} else if Some(trait_def_id) == self.tcx.get_diagnostic_item(sym::Iterator) { | ||
Some(format!( | ||
Some((format!( | ||
"expected `{self_ty}` to be an iterator that yields `{expected_ty}`, but it \ | ||
yields `{normalized_ty}`" | ||
)) | ||
), span, None)) | ||
} else { | ||
None | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,18 +21,14 @@ LL | true | |
found type `bool` | ||
|
||
error[E0271]: expected `{[email protected]:6:10}` to be a closure that returns `bool`, but it returns `Option<()>` | ||
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:6:10 | ||
--> $DIR/dont-ice-for-type-mismatch-in-closure-in-async.rs:6:16 | ||
| | ||
LL | call(|| -> Option<()> { | ||
| _____----_^ | ||
| | | | ||
| | required by a bound introduced by this call | ||
LL | | | ||
LL | | if true { | ||
LL | | false | ||
... | | ||
LL | | }) | ||
| |_____^ expected `bool`, found `Option<()>` | ||
LL | call(|| -> Option<()> { | ||
| ---- ------^^^^^^^^^^ | ||
| | | | | ||
| | | expected `bool`, found `Option<()>` | ||
| | this closure | ||
| required by a bound introduced by this call | ||
| | ||
= note: expected type `bool` | ||
found enum `Option<()>` | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use std::error::Error; | ||
use std::process::exit; | ||
|
||
fn foo<F>(f: F) -> () | ||
where | ||
F: FnOnce() -> Result<(), Box<dyn Error>>, | ||
{ | ||
f().or_else(|e| -> ! { //~ ERROR to be a closure that returns | ||
eprintln!("{:?}", e); | ||
exit(1) | ||
}); | ||
} | ||
|
||
fn bar<F>(f: F) -> () | ||
where | ||
F: FnOnce() -> Result<(), Box<dyn Error>>, | ||
{ | ||
let c = |e| -> ! { //~ ERROR to be a closure that returns | ||
eprintln!("{:?}", e); | ||
exit(1) | ||
}; | ||
f().or_else(c); | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
error[E0271]: expected `{[email protected]:8:17}` to be a closure that returns `Result<(), _>`, but it returns `!` | ||
--> $DIR/return-type-doesnt-match-bound.rs:8:24 | ||
| | ||
LL | f().or_else(|e| -> ! { | ||
| ------- -------^ | ||
| | | | | ||
| | | expected `Result<(), _>`, found `!` | ||
| | this closure | ||
| required by a bound introduced by this call | ||
| | ||
= note: expected enum `Result<(), _>` | ||
found type `!` | ||
note: required by a bound in `Result::<T, E>::or_else` | ||
--> $SRC_DIR/core/src/result.rs:LL:COL | ||
|
||
error[E0271]: expected `{[email protected]:18:13}` to be a closure that returns `Result<(), _>`, but it returns `!` | ||
--> $DIR/return-type-doesnt-match-bound.rs:18:20 | ||
| | ||
LL | let c = |e| -> ! { | ||
| -------^ | ||
| | | | ||
| | expected `Result<(), _>`, found `!` | ||
| this closure | ||
... | ||
LL | f().or_else(c); | ||
| ------- - closure used here | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= note: expected enum `Result<(), _>` | ||
found type `!` | ||
note: required by a bound in `Result::<T, E>::or_else` | ||
--> $SRC_DIR/core/src/result.rs:LL:COL | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0271`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out that
.span
and.span()
do different things! This is kinda what I meant when I said that these APIs are unmaintainable today.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should fix the regression in spans, afaict.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#132243 removes the need for that pointless/confusing method.