Stream: t-compiler/rust-analyzer

Topic: How to create fake trait


Coenen Benjamin (Nov 13 2020 at 15:02, on Zulip):

Hello I'm trying to solve this issue https://github.com/rust-analyzer/rust-analyzer/issues/6492 so I have the Ty and now want to use this method https://github.com/rust-analyzer/rust-analyzer/blob/master/crates/hir/src/code_model.rs#L1406:L1406 but I don't really know how I can create the Trait struct to correspond to my Default trait. Any idea to help me ? Thanks a lot

matklad (Nov 13 2020 at 15:04, on Zulip):

You need to use FamousDefs to find Default Trait

Coenen Benjamin (Nov 13 2020 at 15:06, on Zulip):

Yes I already try that, but when using find_trait from FamousDefs, it returns None, Maybe I'm missing something ? Should I include something in my tests code or not ?

matklad (Nov 13 2020 at 15:10, on Zulip):

The Default trait itself should be present in the test code, yeah

matklad (Nov 13 2020 at 15:10, on Zulip):

Not sure if FamousDEfs::Fixture already has it

Coenen Benjamin (Nov 13 2020 at 15:11, on Zulip):

No not yet, But I implemented it. Will try to include fake trait in my test code :)

Coenen Benjamin (Nov 13 2020 at 15:11, on Zulip):

derive trait doesn't work in test code ?

Coenen Benjamin (Nov 13 2020 at 15:14, on Zulip):
pub mod default {
    pub trait Default: Sized {
       fn default() -> Self;
    }
}

struct S { foo: u32, bar: usize }

impl default::Default for S {
    fn default() -> Self {
        S {
            foo: 0,
            bar: 0,
        }
    }
}

fn process(f: S) {
    let other = S {
        foo: 5,
        .<|>
    };
}
Coenen Benjamin (Nov 13 2020 at 15:14, on Zulip):

here is my test code

Coenen Benjamin (Nov 13 2020 at 15:15, on Zulip):

and I added this in FamousDefs

pub mod default {
    pub trait Default: Sized {
       fn default() -> Self;
    }
}

with the right export in prelude
It's weird

Coenen Benjamin (Nov 13 2020 at 16:09, on Zulip):

Ok got it, in fact I have to include FamousDefs::FIXTURES inside my code test fixture, my bad

Last update: Jul 28 2021 at 04:30UTC