Skip to content

Commit d46c3e3

Browse files
committed
Tweak ? inference error messages
1 parent 62a39ed commit d46c3e3

File tree

5 files changed

+16
-19
lines changed

5 files changed

+16
-19
lines changed

compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs

+8-11
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,14 @@ impl UseDiagnostic<'_> {
166166
/// Return a descriptor of the value at the use site
167167
fn descr(&self) -> &'static str {
168168
match self {
169-
Self::TryConversion { .. } => "`?` error",
169+
Self::TryConversion { .. } => "error for `?` operator",
170170
}
171171
}
172172

173173
/// Return a descriptor of the type at the use site
174174
fn type_descr(&self) -> &'static str {
175175
match self {
176-
Self::TryConversion { .. } => "`?` error type",
176+
Self::TryConversion { .. } => "error type for `?` operator",
177177
}
178178
}
179179

@@ -188,20 +188,17 @@ impl UseDiagnostic<'_> {
188188
fn attach_note(&self, err: &mut DiagnosticBuilder<'_>) {
189189
match *self {
190190
Self::TryConversion { pre_ty, post_ty, .. } => {
191-
let pre_ty = pre_ty.to_string();
192-
let post_ty = post_ty.to_string();
191+
let intro = "`?` implicitly converts the error value";
193192

194-
let intro = "the `?` operation implicitly converts the error value";
195-
196-
let msg = match (pre_ty.as_str(), post_ty.as_str()) {
197-
("_", "_") => format!("{} using the `From` trait", intro),
198-
(_, "_") => {
193+
let msg = match (pre_ty.is_ty_infer(), post_ty.is_ty_infer()) {
194+
(true, true) => format!("{} using the `From` trait", intro),
195+
(false, true) => {
199196
format!("{} into a type implementing `From<{}>`", intro, pre_ty)
200197
}
201-
("_", _) => {
198+
(true, false) => {
202199
format!("{} into `{}` using the `From` trait", intro, post_ty)
203200
}
204-
(_, _) => {
201+
(false, false) => {
205202
format!(
206203
"{} into `{}` using its implementation of `From<{}>`",
207204
intro, post_ty, pre_ty

src/test/ui/inference/cannot-infer-async-enabled-impl-trait-bindings.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ error[E0282]: type annotations needed for `impl Future`
1313
LL | let fut = async {
1414
| --- consider giving `fut` the explicit type `impl Future`, with the type parameters specified
1515
LL | make_unit()?;
16-
| ^ cannot infer type of `?` error
16+
| ^ cannot infer type of error for `?` operator
1717
|
18-
= note: the `?` operation implicitly converts the error value into a type implementing `From<std::io::Error>`
18+
= note: `?` implicitly converts the error value into a type implementing `From<std::io::Error>`
1919

2020
error: aborting due to previous error; 1 warning emitted
2121

src/test/ui/inference/cannot-infer-async.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ error[E0282]: type annotations needed
44
LL | let fut = async {
55
| --- consider giving `fut` a type
66
LL | make_unit()?;
7-
| ^ cannot infer type of `?` error
7+
| ^ cannot infer type of error for `?` operator
88
|
9-
= note: the `?` operation implicitly converts the error value into a type implementing `From<std::io::Error>`
9+
= note: `?` implicitly converts the error value into a type implementing `From<std::io::Error>`
1010

1111
error: aborting due to previous error
1212

src/test/ui/inference/cannot-infer-closure.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0282]: type annotations needed for the closure `fn((), ()) -> std::result
22
--> $DIR/cannot-infer-closure.rs:3:15
33
|
44
LL | Err(a)?;
5-
| ^ cannot infer type of `?` error
5+
| ^ cannot infer type of error for `?` operator
66
|
7-
= note: the `?` operation implicitly converts the error value into a type implementing `From<()>`
7+
= note: `?` implicitly converts the error value into a type implementing `From<()>`
88
help: give this closure an explicit return type without `_` placeholders
99
|
1010
LL | let x = |a: (), b: ()| -> std::result::Result<(), _> {

src/test/ui/inference/cannot-infer-partial-try-return.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0282]: type annotations needed for the closure `fn() -> std::result::Resu
22
--> $DIR/cannot-infer-partial-try-return.rs:19:9
33
|
44
LL | infallible()?;
5-
| ^^^^^^^^^^^^^ cannot infer type of `?` error
5+
| ^^^^^^^^^^^^^ cannot infer type of error for `?` operator
66
|
7-
= note: the `?` operation implicitly converts the error value into `QualifiedError<_>` using its implementation of `From<Infallible>`
7+
= note: `?` implicitly converts the error value into `QualifiedError<_>` using its implementation of `From<Infallible>`
88
help: give this closure an explicit return type without `_` placeholders
99
|
1010
LL | let x = || -> std::result::Result<(), QualifiedError<_>> {

0 commit comments

Comments
 (0)