Stream: t-compiler

Topic: PartialOrd for *const T


Wesley Wiser (Nov 06 2018 at 01:48, on Zulip):

Does anybody know how the implementation of PartialOrd works for *const T pointers? It looks recursive to me. For example, less than is defined here as

impl<T: ?Sized> PartialOrd for *const T {
    fn lt(&self, other: &*mut T) -> bool { *self < *other }
}

So doesn't the body of lt() call itself since *&*const T is just a *const T? Or is there deref coercion magic going on?

oli (Nov 06 2018 at 07:37, on Zulip):

This is true for all primitives. These operations are not actually lowered to a call to the trait method. You can see this "exposed" if you add a println to the method and do the operation. The println won't show up for the primitive

Wesley Wiser (Nov 06 2018 at 14:02, on Zulip):

Thanks @Oli! I don't see any special attributes or anything on these methods. How does the compiler know not to lower these to method calls?

oli (Nov 06 2018 at 14:19, on Zulip):

The transformation in HAIR checks whether some earlier passes decided this. Not sure where exactly that decision is happening, but I'm fairly certain it's simply hardcoded

nikomatsakis (Nov 08 2018 at 15:45, on Zulip):

The code is here:

https://github.com/rust-lang/rust/blob/653da4fd006c97625247acd7e076d0782cdc149b/src/librustc_typeck/check/writeback.rs#L122-L172

Last update: Nov 16 2019 at 02:10UTC