@@ -43,7 +43,6 @@ use std::cell::RefCell;
4343use std:: cmp:: { self , Ordering } ;
4444use std:: fmt;
4545use std:: hash:: { Hash , Hasher } ;
46- use std:: marker:: PhantomData ;
4746use std:: ops:: Deref ;
4847use rustc_data_structures:: sync:: Lrc ;
4948use std:: slice;
@@ -592,20 +591,13 @@ extern {
592591/// A wrapper for slices with the additional invariant
593592/// that the slice is interned and no other slice with
594593/// the same contents can exist in the same context.
595- /// This means we can use pointer + length for both
594+ /// This means we can use pointer for both
596595/// equality comparisons and hashing.
597- pub struct Slice < T > ( PhantomData < T > , OpaqueSliceContents ) ;
598-
599- impl < T > Slice < T > {
600- /// Returns the offset of the array
601- #[ inline( always) ]
602- fn offset ( ) -> usize {
603- // Align up the size of the len (usize) field
604- let align = mem:: align_of :: < T > ( ) ;
605- let align_mask = align - 1 ;
606- let offset = mem:: size_of :: < usize > ( ) ;
607- ( offset + align_mask) & !align_mask
608- }
596+ #[ repr( C ) ]
597+ pub struct Slice < T > {
598+ len : usize ,
599+ data : [ T ; 0 ] ,
600+ opaque : OpaqueSliceContents ,
609601}
610602
611603impl < T : Copy > Slice < T > {
@@ -615,24 +607,27 @@ impl<T: Copy> Slice<T> {
615607 assert ! ( mem:: size_of:: <T >( ) != 0 ) ;
616608 assert ! ( slice. len( ) != 0 ) ;
617609
618- let offset = Slice :: < T > :: offset ( ) ;
610+ // Align up the size of the len (usize) field
611+ let align = mem:: align_of :: < T > ( ) ;
612+ let align_mask = align - 1 ;
613+ let offset = mem:: size_of :: < usize > ( ) ;
614+ let offset = ( offset + align_mask) & !align_mask;
615+
619616 let size = offset + slice. len ( ) * mem:: size_of :: < T > ( ) ;
620617
621- let mem: * mut u8 = arena. alloc_raw (
618+ let mem = arena. alloc_raw (
622619 size,
623- cmp:: max ( mem:: align_of :: < T > ( ) , mem:: align_of :: < usize > ( ) ) ) . as_mut_ptr ( ) ;
624-
620+ cmp:: max ( mem:: align_of :: < T > ( ) , mem:: align_of :: < usize > ( ) ) ) ;
625621 unsafe {
622+ let result = & mut * ( mem. as_mut_ptr ( ) as * mut Slice < T > ) ;
626623 // Write the length
627- * ( mem as * mut usize ) = slice. len ( ) ;
624+ result . len = slice. len ( ) ;
628625
629626 // Write the elements
630- let arena_slice = slice:: from_raw_parts_mut (
631- mem. offset ( offset as isize ) as * mut T ,
632- slice. len ( ) ) ;
627+ let arena_slice = slice:: from_raw_parts_mut ( result. data . as_mut_ptr ( ) , result. len ) ;
633628 arena_slice. copy_from_slice ( slice) ;
634629
635- & * ( mem as * const Slice < T > )
630+ result
636631 }
637632 }
638633}
@@ -686,10 +681,7 @@ impl<T> Deref for Slice<T> {
686681 #[ inline( always) ]
687682 fn deref ( & self ) -> & [ T ] {
688683 unsafe {
689- let raw = self as * const _ as * const u8 ;
690- let len = * ( raw as * const usize ) ;
691- let slice = raw. offset ( Slice :: < T > :: offset ( ) as isize ) ;
692- slice:: from_raw_parts ( slice as * const T , len)
684+ slice:: from_raw_parts ( self . data . as_ptr ( ) , self . len )
693685 }
694686 }
695687}
0 commit comments