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

implicit Deref coercion not happening #109829

Open
rbtcollins opened this issue Apr 1, 2023 · 5 comments
Open

implicit Deref coercion not happening #109829

rbtcollins opened this issue Apr 1, 2023 · 5 comments
Assignees
Labels
A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-discussion Category: Discussion or questions that doesn't represent real issues. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Comments

@rbtcollins
Copy link
Contributor

rbtcollins commented Apr 1, 2023

use std::path::{Path,PathBuf};

#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub(crate) struct PathBasedToolchain(PathBuf);

impl TryFrom<&Path> for PathBasedToolchain {
    type Error = anyhow::Error;

    fn try_from(value: &Path) -> std::result::Result<Self, Self::Error> {
                Ok(PathBasedToolchain(value.into()))
    }
}


fn main() -> anyhow::Result<()> {
    let path = PathBuf::new();
    let t =  PathBasedToolchain::try_from(&path);
    let u =  PathBasedToolchain::try_from(&path as &Path);
    
    Ok(())
}

(playground link)[https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=53696b2684233ffd81680129f6c076d2]

I expected to see this happen: compile with no error (because of https://doc.rust-lang.org/book/ch15-02-deref.html#implicit-deref-coercions-with-functions-and-methods and https://doc.rust-lang.org/stable/std/path/struct.PathBuf.html#impl-Deref-for-PathBuf ).

Instead, this happened:

Compiling playground v0.0.1 (/playground)
error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `PathBasedToolchain: From<&PathBuf>` is not satisfied
  --> src/lib.rs:17:43
   |
17 |     let t =  PathBasedToolchain::try_from(&path);
   |              ---------------------------- ^^^^^ the trait `From<&PathBuf>` is not implemented for `PathBasedToolchain`
   |              |
   |              required by a bound introduced by this call
   |
   = help: the trait `TryFrom<&Path>` is implemented for `PathBasedToolchain`
   = note: required for `&PathBuf` to implement `Into<PathBasedToolchain>`
   = note: required for `PathBasedToolchain` to implement `TryFrom<&PathBuf>`

error[[E0277]](https://doc.rust-lang.org/nightly/error_codes/E0277.html): the trait bound `PathBasedToolchain: From<&PathBuf>` is not satisfied
  --> src/lib.rs:17:14
   |
17 |     let t =  PathBasedToolchain::try_from(&path);
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<&PathBuf>` is not implemented for `PathBasedToolchain`
   |
   = help: the trait `TryFrom<&Path>` is implemented for `PathBasedToolchain`
   = note: required for `&PathBuf` to implement `Into<PathBasedToolchain>`
   = note: required for `PathBasedToolchain` to implement `TryFrom<&PathBuf>`

Meta

rustc --version --verbose:

Nightly channel

Build using the Nightly version: 1.70.0-nightly

(2023-03-31 5e1d3299a290026b8578)

This doesn't cause a backtrace from the compiler.

Backtrace

<backtrace>

@rbtcollins rbtcollins added the C-bug Category: This is a bug. label Apr 1, 2023
@SkiFire13
Copy link
Contributor

AFAIK this is expected, the compiler doesn't do coercions when matching traits (third paragraph), i.e. here the compiler is looking for an implementation of TryFrom<typeof(&path)> for PathBasedToolchain (the syntax typeof(&path) doesn't exist but I hope the meaning is clear), but that doesn't exist, and the implementation of TryFrom<&Path> does not constitute an implementation of TryFrom<&PathBuf> too.

@rbtcollins
Copy link
Contributor Author

Thank you. It might be nice to link to the nomicon from the docs on implicit coercions, because while the nomicon is clear, I didn't find it after non-trivial googling.

@jyn514 jyn514 added A-docs Area: documentation for any part of the project, including the compiler, standard library, and tools C-discussion Category: Discussion or questions that doesn't represent real issues. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. and removed C-bug Category: This is a bug. labels Apr 3, 2023
@chenyukang
Copy link
Member

chenyukang commented Apr 3, 2023

But why there are two almost the same diagnostics show up? (only the span is different)
I think the second one could be removed.

@Spartan2909
Copy link
Contributor

I've opened rust-lang/book#3670 to try and clarify this behaviour.

@Aditya-PS-05
Copy link

@rustbot claim

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 C-discussion Category: Discussion or questions that doesn't represent real issues. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
Projects
None yet
Development

No branches or pull requests

6 participants