Stream: t-compiler/help

Topic: How empty lang item trait works internally?

Derek (Dec 04 2019 at 19:40, on Zulip):

Hi I'm trying to understand how How empty lang item trait works internally? e.g.

oli (Dec 04 2019 at 19:48, on Zulip):

the copy trait does not generate any code. It solely exists for the type system

oli (Dec 04 2019 at 19:48, on Zulip):

the copy intrinsic is unrelated to the copy trait

oli (Dec 04 2019 at 19:48, on Zulip):

they have no connection

Derek (Dec 04 2019 at 19:54, on Zulip):

@oli, Thanks a lots, I really appreciate! Then if I have struct Person {}, then, have Person alice, then, let bob = alice. this "=" here, need generate some code to do bitwise copy, right? how does that works internally. e.g. after I #[derive(Copy, Clone)] or, I have impl Copy for Person {}, this is again an empty Copy.

oli (Dec 04 2019 at 19:55, on Zulip):

it uses llvm assignments, which LLVM may make arbitrary decisions on how to translate

oli (Dec 04 2019 at 19:56, on Zulip):

you can observe and play with this on the playground ( by using the LLVM-IR and asm buttons under the ... menu in the left top corner

Derek (Dec 04 2019 at 19:59, on Zulip):

thanks, let me try, Then why we need a empty statement here, like impl Copy for Person {}. is that just a rule? to let compiler know we allow it to do bitwise copy? it I want dig into how compiler use this statement, do you happen to know which crate or source file I should look into.

Christian Poveda (Dec 04 2019 at 20:49, on Zulip):

yes, Copy just tells the compiler that the type can be copied implicitly

oli (Dec 04 2019 at 20:57, on Zulip):

this is to make sure you can do let x = y; let z = y; without erroring that y has been moved. The Copy trait has semantic value, but does not change what happens on the bits on the machine

Derek (Dec 04 2019 at 21:07, on Zulip):

Got it. Thanks, @oli, @Christian Poveda

Derek (Dec 04 2019 at 21:19, on Zulip):

Is implementation of the rule related to this piece of source code?
language_item_table! {
CopyTraitLangItem, "copy", copy_trait, Target::Trait;

oli (Dec 04 2019 at 21:40, on Zulip):

That's "just" an index of all the lang items. The actual logic can be found by grepping for copy_trait() and CopyTraitLangItem

Last update: Apr 06 2020 at 03:15UTC