@@ -122,21 +122,32 @@ impl Hash for Constant {
122
122
}
123
123
}
124
124
125
- impl PartialOrd for Constant {
126
- fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > {
127
- match ( self , other ) {
125
+ impl Constant {
126
+ pub fn partial_cmp ( tcx : TyCtxt , cmp_type : & ty :: TypeVariants , left : & Self , right : & Self ) -> Option < Ordering > {
127
+ match ( left , right ) {
128
128
( & Constant :: Str ( ref ls) , & Constant :: Str ( ref rs) ) => Some ( ls. cmp ( rs) ) ,
129
129
( & Constant :: Char ( ref l) , & Constant :: Char ( ref r) ) => Some ( l. cmp ( r) ) ,
130
- ( & Constant :: Int ( l) , & Constant :: Int ( r) ) => Some ( l. cmp ( & r) ) ,
130
+ ( & Constant :: Int ( l) , & Constant :: Int ( r) ) => {
131
+ if let ty:: TyInt ( int_ty) = * cmp_type {
132
+ Some ( sext ( tcx, l, int_ty) . cmp ( & sext ( tcx, r, int_ty) ) )
133
+ } else {
134
+ Some ( l. cmp ( & r) )
135
+ }
136
+ } ,
131
137
( & Constant :: F64 ( l) , & Constant :: F64 ( r) ) => l. partial_cmp ( & r) ,
132
138
( & Constant :: F32 ( l) , & Constant :: F32 ( r) ) => l. partial_cmp ( & r) ,
133
139
( & Constant :: Bool ( ref l) , & Constant :: Bool ( ref r) ) => Some ( l. cmp ( r) ) ,
134
- ( & Constant :: Tuple ( ref l) , & Constant :: Tuple ( ref r) ) | ( & Constant :: Vec ( ref l) , & Constant :: Vec ( ref r) ) => {
135
- l. partial_cmp ( r)
136
- } ,
137
- ( & Constant :: Repeat ( ref lv, ref ls) , & Constant :: Repeat ( ref rv, ref rs) ) => match lv. partial_cmp ( rv) {
138
- Some ( Equal ) => Some ( ls. cmp ( rs) ) ,
139
- x => x,
140
+ ( & Constant :: Tuple ( ref l) , & Constant :: Tuple ( ref r) ) | ( & Constant :: Vec ( ref l) , & Constant :: Vec ( ref r) ) => l
141
+ . iter ( )
142
+ . zip ( r. iter ( ) )
143
+ . map ( |( li, ri) | Constant :: partial_cmp ( tcx, cmp_type, li, ri) )
144
+ . find ( |r| r. map_or ( true , |o| o != Ordering :: Equal ) )
145
+ . unwrap_or_else ( || Some ( l. len ( ) . cmp ( & r. len ( ) ) ) ) ,
146
+ ( & Constant :: Repeat ( ref lv, ref ls) , & Constant :: Repeat ( ref rv, ref rs) ) => {
147
+ match Constant :: partial_cmp ( tcx, cmp_type, lv, rv) {
148
+ Some ( Equal ) => Some ( ls. cmp ( rs) ) ,
149
+ x => x,
150
+ }
140
151
} ,
141
152
_ => None , // TODO: Are there any useful inter-type orderings?
142
153
}
0 commit comments