@@ -201,21 +201,27 @@ fn cs_op(less: bool,
201
201
cx. expr_path ( cx. path_global ( span, cx. std_path ( & [ "cmp" , "Ordering" , name] ) ) )
202
202
} ;
203
203
204
- let par_cmp = |cx : & mut ExtCtxt , span : Span , self_f : P < Expr > , other_fs : & [ P < Expr > ] | {
204
+ let par_cmp = |cx : & mut ExtCtxt , span, self_f : P < Expr > , other_fs : & [ P < Expr > ] , default | {
205
205
let other_f = match ( other_fs. len ( ) , other_fs. get ( 0 ) ) {
206
206
( 1 , Some ( o_f) ) => o_f,
207
207
_ => cx. span_bug ( span, "not exactly 2 arguments in `derive(PartialOrd)`" ) ,
208
208
} ;
209
209
210
- // `self.fi.partial_cmp(other.fi)`
211
- let cmp = cx. expr_method_call ( span,
212
- cx. expr_addr_of ( span, self_f) ,
213
- Ident :: from_str ( "partial_cmp" ) ,
214
- vec ! [ cx. expr_addr_of( span, other_f. clone( ) ) ] ) ;
210
+ // `PartialOrd::partial_cmp(self.fi, other.fi)`
211
+ let cmp_path = cx. expr_path ( cx. path_global ( span, cx. std_path ( & [ "cmp" ,
212
+ "PartialOrd" ,
213
+ "partial_cmp" ] ) ) ) ;
214
+ let cmp = cx. expr_call ( span,
215
+ cmp_path,
216
+ vec ! [ cx. expr_addr_of( span, self_f) ,
217
+ cx. expr_addr_of( span, other_f. clone( ) ) ] ) ;
215
218
216
- let default = ordering_path ( cx, "Equal" ) ;
217
- // `_.unwrap_or(Ordering::Equal)`
218
- cx. expr_method_call ( span, cmp, Ident :: from_str ( "unwrap_or" ) , vec ! [ default ] )
219
+ let default = ordering_path ( cx, default) ;
220
+ // `Option::unwrap_or(_, Ordering::Equal)`
221
+ let unwrap_path = cx. expr_path ( cx. path_global ( span, cx. std_path ( & [ "option" ,
222
+ "Option" ,
223
+ "unwrap_or" ] ) ) ) ;
224
+ cx. expr_call ( span, unwrap_path, vec ! [ cmp, default ] )
219
225
} ;
220
226
221
227
let fold = cs_fold1 ( false , // need foldr
@@ -244,7 +250,7 @@ fn cs_op(less: bool,
244
250
// layers of pointers, if the type includes pointers.
245
251
246
252
// `self.fi.partial_cmp(other.fi).unwrap_or(Ordering::Equal)`
247
- let par_cmp = par_cmp ( cx, span, self_f, other_fs) ;
253
+ let par_cmp = par_cmp ( cx, span, self_f, other_fs, "Equal" ) ;
248
254
249
255
// `self.fi.partial_cmp(other.fi).unwrap_or(Ordering::Equal).then_with(...)`
250
256
cx. expr_method_call ( span,
@@ -254,7 +260,10 @@ fn cs_op(less: bool,
254
260
} ,
255
261
|cx, args| {
256
262
match args {
257
- Some ( ( span, self_f, other_fs) ) => par_cmp ( cx, span, self_f, other_fs) ,
263
+ Some ( ( span, self_f, other_fs) ) => {
264
+ let opposite = if less { "Greater" } else { "Less" } ;
265
+ par_cmp ( cx, span, self_f, other_fs, opposite)
266
+ } ,
258
267
None => cx. expr_bool ( span, inclusive)
259
268
}
260
269
} ,
0 commit comments