rust-analyzer icon indicating copy to clipboard operation
rust-analyzer copied to clipboard

RFC: goto-definition between `From::from` and `Into::into`

Open ShoyuVanilla opened this issue 1 year ago • 4 comments

Currently if we trigger goto defintion here;

struct Foo;

fn foo() -> Foo {
    struct Bar;
    impl From<Bar> for Foo {
        fn from(_: Bar) -> Self {
            Foo
        }
    }
    Bar.into$0()
}

it just takes us to std::convert::Into<T>::into, because we do not have manual implementation of impl Into<Foo> for Bar here.

I think that it would be reasonable to provide two definition sources, std::convert::Into<T>::into(..) with the above fn from(..) inside the impl From<Bar> for Foo { .. } block as well, and implement similar thing to the opposite direction from(..) -> into(..).

ShoyuVanilla avatar Oct 17 '24 06:10 ShoyuVanilla

Sounds reasonable to me

Veykril avatar Oct 17 '24 06:10 Veykril

CC #15315, #8373

flodiebold avatar Oct 17 '24 07:10 flodiebold

Those would be the ideal solution, but assuming this special case isn't much work to implement I think it would be fine for us to special case From/Into here? For the time being at least, as those issues seem a fair bit more complicated to pull off to me

Veykril avatar Oct 17 '24 08:10 Veykril

I'm asking this just out of curiosity 😅 I think that solving monomorphization in #15315 is preferable but could "skipping generic intermediates" in general without hard-coding for each well known traits be possible? I think that the relationship between such "dual" traits are not so much clear in code. Though Into's blanket implementation calls from(..) inside into(..) method but concluding such "dual" relationship from such calls inside blanket implementation's method body seems quite arbitrary, without our common sense that they are "dual".

ShoyuVanilla avatar Oct 17 '24 08:10 ShoyuVanilla