Skip to content
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

std incorrectly states that ? uses Into on the underlying error type #111655

Closed
memoryruins opened this issue May 16, 2023 · 2 comments · Fixed by #112141 or #112164
Closed

std incorrectly states that ? uses Into on the underlying error type #111655

memoryruins opened this issue May 16, 2023 · 2 comments · Fixed by #112141 or #112164
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@memoryruins
Copy link
Contributor

Location

/// underlying cause. The '?' operator automatically converts the underlying error type to our
/// custom error type by calling `Into<CliError>::into` which is automatically provided when
/// implementing `From`. The compiler then infers which implementation of `Into` should be used.

Summary

According to the reference and rustc, the ? operator uses the From trait on the underlying error.

For example (playground),

struct A;
struct B;

impl Into<B> for A {
    fn into(self) -> B {
        B
    }
}

fn f(x: Result<(), A>) -> Result<(), B> {
    x?;
    Ok(())
}

rustc 1.71.0-nightly (ce5919fce 2023-05-15)

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): `?` couldn't convert the error to `B`
  --> src/lib.rs:17:6
   |
16 | fn f(x: Result<(), A>) -> Result<(), B> {
   |                           ------------- expected `B` because of this
17 |     x?;
   |      ^ the trait `From<A>` is not implemented for `B`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following other types implement trait `FromResidual<R>`:
             <Result<T, F> as FromResidual<Result<Infallible, E>>>
             <Result<T, F> as FromResidual<Yeet<E>>>
   = note: required for `Result<(), B>` to implement `FromResidual<Result<Infallible, A>>`

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

If the Into<B> impl is replaced with a From<A> impl, the example will successfully compile.

The docs in core/std should no longer state that ? uses Into.

@memoryruins memoryruins added the A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools label May 16, 2023
@MasterAwesome
Copy link
Contributor

Why does Rust use From for ? wouldn't it be better to use Into considering that there are more places the ? will apply to?

@memoryruins
Copy link
Contributor Author

memoryruins commented May 17, 2023

Switching to Into was attempted in #60796 (proposed by #38751), but it caused inference failures in various places.

@jyn514 jyn514 added the E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. label May 24, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Jun 1, 2023
…=thomcc

remove reference to Into in ? operator core/std docs, fix rust-lang#111655

remove the text stating that `?` uses `Into::into` and add text stating it uses `From::from` instead. This closes rust-lang#111655.
bors added a commit to rust-lang-ci/rust that referenced this issue Jun 1, 2023
Rollup of 7 pull requests

Successful merges:

 - rust-lang#108459 (rustdoc: Fix LinkReplacer link matching)
 - rust-lang#111318 (Add a distinct `OperandValue::ZeroSized` variant for ZSTs)
 - rust-lang#111892 (rustdoc: add interaction delays for tooltip popovers)
 - rust-lang#111980 (Preserve substs in opaques recorded in typeck results)
 - rust-lang#112024 (Don't suggest break through nested items)
 - rust-lang#112128 (Don't compute inlining status of mono items in advance.)
 - rust-lang#112141 (remove reference to Into in ? operator core/std docs, fix rust-lang#111655)

Failed merges:

 - rust-lang#112071 (Group rfcs tests)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors closed this as completed in 129c559 Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
3 participants