Skip to content

Commit dd60b6c

Browse files
authored
Rollup merge of #137232 - estebank:from-residual-note, r=petrochenkov
Don't mention `FromResidual` on bad `?` Unless `try_trait_v2` is enabled, don't mention that `FromResidual` isn't implemented for a specific type when the implicit `From` conversion of a `?` fails. For the end user on stable, `?` might as well be a compiler intrinsic, so we remove that note to avoid further confusion and allowing other parts of the error to be more prominent. ``` error[E0277]: `?` couldn't convert the error to `u8` --> $DIR/bad-interconversion.rs:4:20 | LL | fn result_to_result() -> Result<u64, u8> { | --------------- expected `u8` because of this LL | Ok(Err(123_i32)?) | ------------^ the trait `From<i32>` is not implemented for `u8` | | | this can't be annotated with `?` because it has type `Result<_, i32>` | = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait = help: the following other types implement trait `From<T>`: `u8` implements `From<Char>` `u8` implements `From<bool>` ```
2 parents fa10786 + 6eb4882 commit dd60b6c

File tree

6 files changed

+12
-6
lines changed

6 files changed

+12
-6
lines changed

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+8
Original file line numberDiff line numberDiff line change
@@ -3289,6 +3289,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
32893289
let mut parent_trait_pred =
32903290
self.resolve_vars_if_possible(data.derived.parent_trait_pred);
32913291
let parent_def_id = parent_trait_pred.def_id();
3292+
if tcx.is_diagnostic_item(sym::FromResidual, parent_def_id)
3293+
&& !tcx.features().enabled(sym::try_trait_v2)
3294+
{
3295+
// If `#![feature(try_trait_v2)]` is not enabled, then there's no point on
3296+
// talking about `FromResidual<Result<A, B>>`, as the end user has nothing they
3297+
// can do about it. As far as they are concerned, `?` is compiler magic.
3298+
return;
3299+
}
32923300
let self_ty_str =
32933301
tcx.short_string(parent_trait_pred.skip_binder().self_ty(), err.long_ty_path());
32943302
let trait_name = parent_trait_pred.print_modifiers_and_trait_path().to_string();

tests/ui/traits/question-mark-result-err-mismatch.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(try_trait_v2)]
12
fn foo() -> Result<String, String> { //~ NOTE expected `String` because of this
23
let test = String::from("one,two");
34
let x = test

tests/ui/traits/question-mark-result-err-mismatch.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0277]: `?` couldn't convert the error to `String`
2-
--> $DIR/question-mark-result-err-mismatch.rs:14:22
2+
--> $DIR/question-mark-result-err-mismatch.rs:15:22
33
|
44
LL | fn foo() -> Result<String, String> {
55
| ---------------------- expected `String` because of this
@@ -17,7 +17,7 @@ LL | .map(|()| "")?;
1717
= note: required for `Result<String, String>` to implement `FromResidual<Result<Infallible, ()>>`
1818

1919
error[E0277]: `?` couldn't convert the error to `String`
20-
--> $DIR/question-mark-result-err-mismatch.rs:28:25
20+
--> $DIR/question-mark-result-err-mismatch.rs:29:25
2121
|
2222
LL | fn bar() -> Result<(), String> {
2323
| ------------------ expected `String` because of this
@@ -40,7 +40,7 @@ LL | .map_err(|_| ())?;
4040
= note: required for `Result<(), String>` to implement `FromResidual<Result<Infallible, ()>>`
4141

4242
error[E0277]: `?` couldn't convert the error to `String`
43-
--> $DIR/question-mark-result-err-mismatch.rs:48:11
43+
--> $DIR/question-mark-result-err-mismatch.rs:49:11
4444
|
4545
LL | fn baz() -> Result<String, String> {
4646
| ---------------------- expected `String` because of this

tests/ui/try-block/try-block-bad-type.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ LL | Err("")?;
1010
= help: the trait `From<&str>` is not implemented for `TryFromSliceError`
1111
but trait `From<Infallible>` is implemented for it
1212
= help: for that trait implementation, expected `Infallible`, found `&str`
13-
= note: required for `Result<u32, TryFromSliceError>` to implement `FromResidual<Result<Infallible, &str>>`
1413

1514
error[E0271]: type mismatch resolving `<Result<i32, i32> as Try>::Output == &str`
1615
--> $DIR/try-block-bad-type.rs:12:9

tests/ui/try-trait/bad-interconversion.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LL | Ok(Err(123_i32)?)
1212
= help: the following other types implement trait `From<T>`:
1313
`u8` implements `From<Char>`
1414
`u8` implements `From<bool>`
15-
= note: required for `Result<u64, u8>` to implement `FromResidual<Result<Infallible, i32>>`
1615

1716
error[E0277]: the `?` operator can only be used on `Result`s, not `Option`s, in a function that returns `Result`
1817
--> $DIR/bad-interconversion.rs:9:12

tests/ui/try-trait/issue-32709.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ LL | Err(5)?;
1919
`(T, T, T, T, T, T, T, T)` implements `From<[T; 8]>`
2020
`(T, T, T, T, T, T, T, T, T)` implements `From<[T; 9]>`
2121
and 4 others
22-
= note: required for `Result<i32, ()>` to implement `FromResidual<Result<Infallible, {integer}>>`
2322

2423
error: aborting due to 1 previous error
2524

0 commit comments

Comments
 (0)