Skip to content

Enhance type inference errors involving the ? operator #80517

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
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
10 changes: 9 additions & 1 deletion compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ use rustc_middle::ty::{
subst::{Subst, SubstsRef},
Region, Ty, TyCtxt, TypeFoldable,
};
use rustc_span::{BytePos, DesugaringKind, Pos, Span};
use rustc_span::{sym, BytePos, DesugaringKind, Pos, Span};
use rustc_target::spec::abi;
use std::ops::ControlFlow;
use std::{cmp, fmt};
Expand Down Expand Up @@ -2282,6 +2282,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
self.note_region_origin(&mut err, &sub_origin);
err.emit();
}

/// Determine whether an error associated with the given span and definition
/// should be treated as being caused by the implicit `From` conversion
/// within `?` desugaring.
pub fn is_try_conversion(&self, span: Span, trait_def_id: DefId) -> bool {
span.is_desugaring(DesugaringKind::QuestionMark)
&& self.tcx.is_diagnostic_item(sym::from_trait, trait_def_id)
}
}

impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
Expand Down
365 changes: 212 additions & 153 deletions compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

Large diffs are not rendered by default.

14 changes: 3 additions & 11 deletions compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,18 +280,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
let OnUnimplementedNote { message, label, note, enclosing_scope } =
self.on_unimplemented_note(trait_ref, obligation);
let have_alt_message = message.is_some() || label.is_some();
let is_try = self
.tcx
.sess
.source_map()
.span_to_snippet(span)
.map(|s| &s == "?")
.unwrap_or(false);
let is_from = self.tcx.get_diagnostic_item(sym::from_trait)
== Some(trait_ref.def_id());
let is_try_conversion = self.is_try_conversion(span, trait_ref.def_id());
let is_unsize =
{ Some(trait_ref.def_id()) == self.tcx.lang_items().unsize_trait() };
let (message, note) = if is_try && is_from {
let (message, note) = if is_try_conversion {
(
Some(format!(
"`?` couldn't convert the error to `{}`",
Expand Down Expand Up @@ -319,7 +311,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
))
);

if is_try && is_from {
if is_try_conversion {
let none_error = self
.tcx
.get_diagnostic_item(sym::none_error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ error[E0282]: type annotations needed for `impl Future`
LL | let fut = async {
| --- consider giving `fut` the explicit type `impl Future`, with the type parameters specified
LL | make_unit()?;
| ^ cannot infer type
| ^ cannot infer type of error for `?` operator
|
= note: `?` implicitly converts the error value into a type implementing `From<std::io::Error>`

error: aborting due to previous error; 1 warning emitted

Expand Down
4 changes: 3 additions & 1 deletion src/test/ui/inference/cannot-infer-async.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ error[E0282]: type annotations needed
LL | let fut = async {
| --- consider giving `fut` a type
LL | make_unit()?;
| ^ cannot infer type
| ^ cannot infer type of error for `?` operator
|
= note: `?` implicitly converts the error value into a type implementing `From<std::io::Error>`

error: aborting due to previous error

Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/inference/cannot-infer-closure-circular.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fn main() {
// Below we call the closure with its own return as the argument, unifying
// its inferred input and return types. We want to make sure that the generated
// error handles this gracefully, and in particular doesn't generate an extra
// note about the `?` operator in the closure body, which isn't relevant to
// the inference.
let x = |r| {
//~^ ERROR type annotations needed
let v = r?;
Ok(v)
};

let _ = x(x(Ok(())));
}
9 changes: 9 additions & 0 deletions src/test/ui/inference/cannot-infer-closure-circular.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
error[E0282]: type annotations needed for `std::result::Result<(), E>`
--> $DIR/cannot-infer-closure-circular.rs:7:14
|
LL | let x = |r| {
| ^ consider giving this closure parameter the explicit type `std::result::Result<(), E>`, with the type parameters specified

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.
3 changes: 2 additions & 1 deletion src/test/ui/inference/cannot-infer-closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ error[E0282]: type annotations needed for the closure `fn((), ()) -> std::result
--> $DIR/cannot-infer-closure.rs:3:15
|
LL | Err(a)?;
| ^ cannot infer type
| ^ cannot infer type of error for `?` operator
|
= note: `?` implicitly converts the error value into a type implementing `From<()>`
help: give this closure an explicit return type without `_` placeholders
|
LL | let x = |a: (), b: ()| -> std::result::Result<(), _> {
Expand Down
22 changes: 22 additions & 0 deletions src/test/ui/inference/cannot-infer-partial-try-return.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
struct QualifiedError<E>(E);

impl<E, T> From<E> for QualifiedError<T>
where
E: std::error::Error,
T: From<E>,
{
fn from(e: E) -> QualifiedError<T> {
QualifiedError(e.into())
}
}

fn infallible() -> Result<(), std::convert::Infallible> {
Ok(())
}

fn main() {
let x = || -> Result<_, QualifiedError<_>> {
infallible()?; //~ERROR type annotations needed
Ok(())
};
}
15 changes: 15 additions & 0 deletions src/test/ui/inference/cannot-infer-partial-try-return.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0282]: type annotations needed for the closure `fn() -> std::result::Result<(), QualifiedError<_>>`
--> $DIR/cannot-infer-partial-try-return.rs:19:9
|
LL | infallible()?;
| ^^^^^^^^^^^^^ cannot infer type of error for `?` operator
|
= note: `?` implicitly converts the error value into `QualifiedError<_>` using its implementation of `From<Infallible>`
help: give this closure an explicit return type without `_` placeholders
|
LL | let x = || -> std::result::Result<(), QualifiedError<_>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0282`.