gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Parser error on RangeTo patterns

Open philberty opened this issue 3 years ago • 0 comments

I tried this code:

mod intrinsics {
    extern "rust-intrinsic" {
        pub fn wrapping_add<T>(a: T, b: T) -> T;
        pub fn rotate_left<T>(a: T, b: T) -> T;
        pub fn rotate_right<T>(a: T, b: T) -> T;
        pub fn offset<T>(ptr: *const T, count: isize) -> *const T;
    }
}

mod mem {
    extern "rust-intrinsic" {
        fn transmute<T, U>(_: T) -> U;
        fn size_of<T>() -> usize;
    }
}

#[lang = "RangeTo"]
pub struct RangeTo<Idx> {
    pub end: Idx,
}

pub unsafe trait SliceIndex<T> {
    type Output;

    fn get(self, slice: &T) -> Option<&Self::Output>;

    unsafe fn get_unchecked(self, slice: *const T) -> *const Self::Output;

    fn index(self, slice: &T) -> &Self::Output;
}

unsafe impl<T> SliceIndex<[T]> for RangeTo<usize> {
    type Output = [T];

    fn get(self, slice: &[T]) -> Option<&[T]> {
        (0..self.end).get(slice)
    }

    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
        unsafe { (0..self.end).get_unchecked(slice) }
    }

    fn index(self, slice: &[T]) -> &[T] {
        (0..self.end).index(slice)
    }
}

unsafe impl<T> SliceIndex<[T]> for RangeTo<usize> {
    type Output = [T];

    fn get(self, slice: &[T]) -> Option<&[T]> {
        (0..self.end).get(slice)
    }

    unsafe fn get_unchecked(self, slice: *const [T]) -> *const [T] {
        // SAFETY: the caller has to uphold the safety contract for `get_unchecked`.
        unsafe { (0..self.end).get_unchecked(slice) }
    }

    fn index(self, slice: &[T]) -> &[T] {
        (0..self.end).index(slice)
    }
}

see: https://godbolt.org/z/bYM5fnsKs

I expected to see this happen: compile without errror

Instead, this happened: parser error

Meta

  • What version of Rust GCC were you using, git sha if possible. 5ad0ea3e0ed288569d52556b9aa796beea73d8a3

philberty avatar May 24 '22 11:05 philberty