Skip to content

can the ? operator use into rather than from? #38751

Description

@nrc

Since implementing From implies an implementation for Into, but not vice versa, this seems like it should admit strictly more programs. Not entirely convinced this would be backwards compatible though.

Activity

  1. added
    T-langRelevant to the language team
    on Jan 1, 2017
  2. Mark-Simulacrum commented on May 18, 2017

    @Mark-Simulacrum
    Member

    Nominating for discussion. I feel like this might cause inference failures.

  3. nikomatsakis commented on May 25, 2017

    @nikomatsakis
    Contributor

    Conclusion from @rust-lang/lang meeting: we should try it and see!

  4. CAD97 commented on Apr 19, 2019

    @CAD97
    Contributor

    This issue should probably get an update with the current state.

    IIRC, there were some ugly inference issues when we last tried this, but I'm unable to find the discussion that tried.

  5. added a commit that references this issue on May 17, 2019
  6. cuviper commented on Jul 22, 2019

    @cuviper
    Member

    I tried this in #60796, and indeed type inference was a problem. Maybe the compiler can learn some tricks to make that work better, but for now this change doesn't seem feasible.

  7. That3Percent commented on Apr 17, 2020

    @That3Percent

    The need for Into rather than From has come up for me in a real-world codebase. Here's a simplified motivating example:

    struct E;
    
    trait Fallible {
        type Error: Into<E>;
        fn e(&self) -> Result<(), Self::Error>;
    }
    
    fn test(f: &impl Fallible) -> Result<(), E> {
        Ok(f.e()?)
    }
    

    What the trait is trying to express is simple - any Error type that can be thrown needs to be convertible to E because of the way this trait is going to be used. Presently, this doesn't compile:

    error[E0277]: `?` couldn't convert the error to `E`
     --> src/lib.rs:9:13
      |
    9 |     Ok(f.e()?)
      |             ^ the trait `std::convert::From<<impl Fallible as Fallible>::Error>` is not implemented for `E`
      |
      = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
      = note: required by `std::convert::From::from`
    

    But, there's no straightforward way to fix this since there is no way to express the "reverse bound" type Error: (E : From<Self::Error>). The only option is to decorate all of the call sites with something verbose like the following, which is taken from the actual code:

    where
            ReadError : From<<<K as Readable>::ReaderArray as ReaderArray>::Error>,
            ReadError : From<<<V as Readable>::ReaderArray as ReaderArray>::Error> {
    

    Link to project: Tree-Buf

  8. cuviper commented on Apr 17, 2020

    @cuviper
    Member

    FWIW, you can put where E: From<Self::Error> on the trait, or even directly on the type declaration with #![feature(generic_associated_types)], but I think call sites still have trouble with #20671.

  9. nikomatsakis commented on Apr 20, 2020

    @nikomatsakis
    Contributor

    There is also a hard-coded limit in the trait system. If you have to solve a goal like ?X: Into<ReturnType>, we will fail to resolve, but if you have to solve a goal like ReturnType: From<?X>, we will potentially succeed and infer a value for ?X.

    Edit: Here, ?X refers to some unknown inference variable. The hard-coded limit in today's trait system is that the Self type must be at least partly inferred for us to explore that option.

    So I could see that changing ? to use Into could cause inference failures where none existed before.

    I'm inclined to close this issue, myself.

  10. Mark-Simulacrum commented on Apr 20, 2020

    @Mark-Simulacrum
    Member

    I am also inclined to close this -- it seems clear that we cannot change this now anyway.

  11. CAD97 commented on Apr 25, 2020

    @CAD97
    Contributor

    @nikomatsakis would proper chalk integration for the trait solver be able to relax this limitation? (Could the relaxation be targeted exclusively to ??)

    It would be nice if we could "fix" ? to use Into in an edition after rustc's trait solver is exclusively chalk (as far in the future as that is).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions