Skip to content

implicit Deref coercion not happening #109829

Open
@rbtcollins

Description

@rbtcollins
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>

Metadata

Metadata

Assignees

Labels

A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-discussionCategory: Discussion or questions that doesn't represent real issues.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions