Skip to content

try_into from custom trait misresolved for prelude TryInto in 2021 edition #10677

Closed
@johnmave126

Description

@johnmave126

Environment

rust 1.56.1
rust-analyzer 2021-11-01
edition 2021

Steps to reproduce

pub struct GreaterThanZero(i32);

impl TryFrom<i32> for GreaterThanZero {
    type Error = &'static str;

    fn try_from(value: i32) -> Result<Self, Self::Error> {        
        Ok(GreaterThanZero(value))
    }
}

pub trait MyTrait {
    // This has the same name as `TryInto::try_into`
    fn try_into(&self, addition: i32) -> Result<GreaterThanZero, ()>;
}

impl MyTrait for i32 {
    fn try_into(&self, addition: i32) -> Result<GreaterThanZero, ()> {
        Ok(GreaterThanZero(*self + addition))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn internal() {
        let a: &dyn MyTrait = &2;
        let v: GreaterThanZero = (&*a).try_into(1).unwrap();
        assert_eq!(3, v.0);
    }
}
  • Expected: No error. try_into in internal() resolves to MyTrait::try_into
  • What happened: cargo compiles fine but RA reports mismatched-arg-count. try_into in internal() resolves to TryInto::try_into from the prelude.

Courtesy of minstrel1977@rustcc

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolutionC-bugCategory: bugS-actionableSomeone could pick this issue up and work on it right now

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions