@@ -20,7 +20,7 @@ use interpret::{const_val_field, const_variant_index, self};
2020
2121use rustc:: middle:: const_val:: ConstVal ;
2222use rustc:: mir:: { fmt_const_val, Field , BorrowKind , Mutability } ;
23- use rustc:: mir:: interpret:: { PrimVal , GlobalId , ConstValue } ;
23+ use rustc:: mir:: interpret:: { PrimVal , GlobalId , ConstValue , Value } ;
2424use rustc:: ty:: { self , TyCtxt , AdtDef , Ty , Region } ;
2525use rustc:: ty:: layout:: Size ;
2626use rustc:: ty:: subst:: { Substs , Kind } ;
@@ -1040,11 +1040,27 @@ pub fn compare_const_vals<'a, 'tcx>(
10401040 ty : Ty < ' tcx > ,
10411041) -> Option < Ordering > {
10421042 trace ! ( "compare_const_vals: {:?}, {:?}" , a, b) ;
1043+
1044+ let from_bool = |v : bool | {
1045+ if v {
1046+ Some ( Ordering :: Equal )
1047+ } else {
1048+ None
1049+ }
1050+ } ;
1051+
1052+ let fallback = || from_bool ( a == b) ;
1053+
1054+ // Use the fallback if any type differs
1055+ if a. ty != b. ty || a. ty != ty {
1056+ return fallback ( ) ;
1057+ }
1058+
10431059 // FIXME: This should use assert_bits(ty) instead of use_bits
10441060 // but triggers possibly bugs due to mismatching of arrays and slices
10451061 if let ( Some ( a) , Some ( b) ) = ( a. to_bits ( ty) , b. to_bits ( ty) ) {
10461062 use :: rustc_apfloat:: Float ;
1047- match ty. sty {
1063+ return match ty. sty {
10481064 ty:: TyFloat ( ast:: FloatTy :: F32 ) => {
10491065 let l = :: rustc_apfloat:: ieee:: Single :: from_bits ( a) ;
10501066 let r = :: rustc_apfloat:: ieee:: Single :: from_bits ( b) ;
@@ -1062,13 +1078,36 @@ pub fn compare_const_vals<'a, 'tcx>(
10621078 } ,
10631079 _ => Some ( a. cmp ( & b) ) ,
10641080 }
1065- } else {
1066- if a == b {
1067- Some ( Ordering :: Equal )
1068- } else {
1069- None
1081+ }
1082+
1083+ if let ty:: TyRef ( _, rty, _) = ty. sty {
1084+ if let ty:: TyStr = rty. sty {
1085+ match ( a. to_byval_value ( ) , b. to_byval_value ( ) ) {
1086+ (
1087+ Some ( Value :: ByValPair (
1088+ PrimVal :: Ptr ( ptr_a) ,
1089+ PrimVal :: Bytes ( size_a) )
1090+ ) ,
1091+ Some ( Value :: ByValPair (
1092+ PrimVal :: Ptr ( ptr_b) ,
1093+ PrimVal :: Bytes ( size_b) )
1094+ )
1095+ ) if size_a == size_b => {
1096+ if ptr_a. offset == Size :: from_bytes ( 0 ) && ptr_b. offset == Size :: from_bytes ( 0 ) {
1097+ let map = tcx. alloc_map . lock ( ) ;
1098+ let alloc_a = map. unwrap_memory ( ptr_a. alloc_id ) ;
1099+ let alloc_b = map. unwrap_memory ( ptr_b. alloc_id ) ;
1100+ if alloc_a. bytes . len ( ) as u64 == size_a as u64 {
1101+ return from_bool ( alloc_a == alloc_b) ;
1102+ }
1103+ }
1104+ }
1105+ _ => ( ) ,
1106+ }
10701107 }
10711108 }
1109+
1110+ fallback ( )
10721111}
10731112
10741113// FIXME: Combine with rustc_mir::hair::cx::const_eval_literal
@@ -1083,15 +1122,15 @@ fn lit_to_const<'a, 'tcx>(lit: &'tcx ast::LitKind,
10831122 let lit = match * lit {
10841123 LitKind :: Str ( ref s, _) => {
10851124 let s = s. as_str ( ) ;
1086- let id = tcx. allocate_cached ( s. as_bytes ( ) ) ;
1125+ let id = tcx. allocate_bytes ( s. as_bytes ( ) ) ;
10871126 let ptr = MemoryPointer :: new ( id, Size :: from_bytes ( 0 ) ) ;
10881127 ConstValue :: ByValPair (
10891128 PrimVal :: Ptr ( ptr) ,
10901129 PrimVal :: from_u128 ( s. len ( ) as u128 ) ,
10911130 )
10921131 } ,
10931132 LitKind :: ByteStr ( ref data) => {
1094- let id = tcx. allocate_cached ( data) ;
1133+ let id = tcx. allocate_bytes ( data) ;
10951134 let ptr = MemoryPointer :: new ( id, Size :: from_bytes ( 0 ) ) ;
10961135 ConstValue :: ByVal ( PrimVal :: Ptr ( ptr) )
10971136 } ,
0 commit comments