@@ -4,15 +4,15 @@ use rustc_data_structures::packed::Pu128;
4
4
use rustc_errors:: codes:: * ;
5
5
use rustc_errors:: { Applicability , Diag , struct_span_code_err} ;
6
6
use rustc_infer:: traits:: ObligationCauseCode ;
7
+ use rustc_middle:: bug;
7
8
use rustc_middle:: ty:: adjustment:: {
8
9
Adjust , Adjustment , AllowTwoPhase , AutoBorrow , AutoBorrowMutability ,
9
10
} ;
10
11
use rustc_middle:: ty:: print:: with_no_trimmed_paths;
11
12
use rustc_middle:: ty:: { self , IsSuggestable , Ty , TyCtxt , TypeVisitableExt } ;
12
- use rustc_middle:: { bug, span_bug} ;
13
13
use rustc_session:: errors:: ExprParenthesesNeeded ;
14
14
use rustc_span:: source_map:: Spanned ;
15
- use rustc_span:: { Ident , Span , sym} ;
15
+ use rustc_span:: { Ident , Span , Symbol , sym} ;
16
16
use rustc_trait_selection:: infer:: InferCtxtExt ;
17
17
use rustc_trait_selection:: traits:: { FulfillmentError , Obligation , ObligationCtxt } ;
18
18
use rustc_type_ir:: TyKind :: * ;
@@ -50,7 +50,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
50
50
. lookup_op_method (
51
51
( lhs, lhs_deref_ty) ,
52
52
Some ( ( rhs, rhs_ty) ) ,
53
- Op :: Binary ( op, IsAssign :: Yes ) ,
53
+ lang_item_for_binop ( self . tcx , op, IsAssign :: Yes ) ,
54
+ op. span ,
54
55
expected,
55
56
)
56
57
. is_ok ( )
@@ -61,7 +62,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
61
62
. lookup_op_method (
62
63
( lhs, lhs_ty) ,
63
64
Some ( ( rhs, rhs_ty) ) ,
64
- Op :: Binary ( op, IsAssign :: Yes ) ,
65
+ lang_item_for_binop ( self . tcx , op, IsAssign :: Yes ) ,
66
+ op. span ,
65
67
expected,
66
68
)
67
69
. is_err ( )
@@ -243,7 +245,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
243
245
let result = self . lookup_op_method (
244
246
( lhs_expr, lhs_ty) ,
245
247
Some ( ( rhs_expr, rhs_ty_var) ) ,
246
- Op :: Binary ( op, is_assign) ,
248
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
249
+ op. span ,
247
250
expected,
248
251
) ;
249
252
@@ -302,8 +305,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
302
305
Ty :: new_misc_error ( self . tcx )
303
306
}
304
307
Err ( errors) => {
305
- let ( _, trait_def_id) =
306
- lang_item_for_op ( self . tcx , Op :: Binary ( op, is_assign) , op. span ) ;
308
+ let ( _, trait_def_id) = lang_item_for_binop ( self . tcx , op, is_assign) ;
307
309
let missing_trait = trait_def_id
308
310
. map ( |def_id| with_no_trimmed_paths ! ( self . tcx. def_path_str( def_id) ) ) ;
309
311
let ( mut err, output_def_id) = match is_assign {
@@ -405,7 +407,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
405
407
. lookup_op_method (
406
408
( lhs_expr, lhs_deref_ty) ,
407
409
Some ( ( rhs_expr, rhs_ty) ) ,
408
- Op :: Binary ( op, is_assign) ,
410
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
411
+ op. span ,
409
412
expected,
410
413
)
411
414
. is_ok ( )
@@ -438,7 +441,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
438
441
. lookup_op_method (
439
442
( lhs_expr, lhs_adjusted_ty) ,
440
443
Some ( ( rhs_expr, rhs_adjusted_ty) ) ,
441
- Op :: Binary ( op, is_assign) ,
444
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
445
+ op. span ,
442
446
expected,
443
447
)
444
448
. is_ok ( )
@@ -493,7 +497,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
493
497
self . lookup_op_method (
494
498
( lhs_expr, lhs_ty) ,
495
499
Some ( ( rhs_expr, rhs_ty) ) ,
496
- Op :: Binary ( op, is_assign) ,
500
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
501
+ op. span ,
497
502
expected,
498
503
)
499
504
. is_ok ( )
@@ -586,7 +591,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
586
591
. lookup_op_method (
587
592
( lhs_expr, lhs_ty) ,
588
593
Some ( ( rhs_expr, rhs_ty) ) ,
589
- Op :: Binary ( op, is_assign) ,
594
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
595
+ op. span ,
590
596
expected,
591
597
)
592
598
. unwrap_err ( ) ;
@@ -785,7 +791,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
785
791
expected : Expectation < ' tcx > ,
786
792
) -> Ty < ' tcx > {
787
793
assert ! ( op. is_by_value( ) ) ;
788
- match self . lookup_op_method ( ( ex, operand_ty) , None , Op :: Unary ( op, ex. span ) , expected) {
794
+ match self . lookup_op_method (
795
+ ( ex, operand_ty) ,
796
+ None ,
797
+ lang_item_for_unop ( self . tcx , op) ,
798
+ ex. span ,
799
+ expected,
800
+ ) {
789
801
Ok ( method) => {
790
802
self . write_method_call_and_enforce_effects ( ex. hir_id , ex. span , method) ;
791
803
method. sig . output ( )
@@ -882,21 +894,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
882
894
& self ,
883
895
( lhs_expr, lhs_ty) : ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) ,
884
896
opt_rhs : Option < ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) > ,
885
- op : Op ,
897
+ ( opname, trait_did) : ( Symbol , Option < hir:: def_id:: DefId > ) ,
898
+ span : Span ,
886
899
expected : Expectation < ' tcx > ,
887
900
) -> Result < MethodCallee < ' tcx > , Vec < FulfillmentError < ' tcx > > > {
888
- let span = match op {
889
- Op :: Binary ( op, _) => op. span ,
890
- Op :: Unary ( _, span) => span,
891
- } ;
892
- let ( opname, Some ( trait_did) ) = lang_item_for_op ( self . tcx , op, span) else {
901
+ let Some ( trait_did) = trait_did else {
893
902
// Bail if the operator trait is not defined.
894
903
return Err ( vec ! [ ] ) ;
895
904
} ;
896
905
897
906
debug ! (
898
- "lookup_op_method(lhs_ty={:?}, op={:?}, opname={:?}, trait_did={:?})" ,
899
- lhs_ty, op , opname, trait_did
907
+ "lookup_op_method(lhs_ty={:?}, opname={:?}, trait_did={:?})" ,
908
+ lhs_ty, opname, trait_did
900
909
) ;
901
910
902
911
let opname = Ident :: with_dummy_span ( opname) ;
@@ -960,13 +969,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
960
969
}
961
970
}
962
971
963
- fn lang_item_for_op (
972
+ fn lang_item_for_binop (
964
973
tcx : TyCtxt < ' _ > ,
965
- op : Op ,
966
- span : Span ,
967
- ) -> ( rustc_span :: Symbol , Option < hir:: def_id:: DefId > ) {
974
+ op : hir :: BinOp ,
975
+ is_assign : IsAssign ,
976
+ ) -> ( Symbol , Option < hir:: def_id:: DefId > ) {
968
977
let lang = tcx. lang_items ( ) ;
969
- if let Op :: Binary ( op , IsAssign :: Yes ) = op {
978
+ if is_assign == IsAssign :: Yes {
970
979
match op. node {
971
980
hir:: BinOpKind :: Add => ( sym:: add_assign, lang. add_assign_trait ( ) ) ,
972
981
hir:: BinOpKind :: Sub => ( sym:: sub_assign, lang. sub_assign_trait ( ) ) ,
@@ -986,10 +995,10 @@ fn lang_item_for_op(
986
995
| hir:: BinOpKind :: Ne
987
996
| hir:: BinOpKind :: And
988
997
| hir:: BinOpKind :: Or => {
989
- span_bug ! ( span , "impossible assignment operation: {}=" , op. node. as_str( ) )
998
+ bug ! ( "impossible assignment operation: {}=" , op. node. as_str( ) )
990
999
}
991
1000
}
992
- } else if let Op :: Binary ( op , IsAssign :: No ) = op {
1001
+ } else {
993
1002
match op. node {
994
1003
hir:: BinOpKind :: Add => ( sym:: add, lang. add_trait ( ) ) ,
995
1004
hir:: BinOpKind :: Sub => ( sym:: sub, lang. sub_trait ( ) ) ,
@@ -1008,15 +1017,18 @@ fn lang_item_for_op(
1008
1017
hir:: BinOpKind :: Eq => ( sym:: eq, lang. eq_trait ( ) ) ,
1009
1018
hir:: BinOpKind :: Ne => ( sym:: ne, lang. eq_trait ( ) ) ,
1010
1019
hir:: BinOpKind :: And | hir:: BinOpKind :: Or => {
1011
- span_bug ! ( span , "&& and || are not overloadable" )
1020
+ bug ! ( "&& and || are not overloadable" )
1012
1021
}
1013
1022
}
1014
- } else if let Op :: Unary ( hir:: UnOp :: Not , _) = op {
1015
- ( sym:: not, lang. not_trait ( ) )
1016
- } else if let Op :: Unary ( hir:: UnOp :: Neg , _) = op {
1017
- ( sym:: neg, lang. neg_trait ( ) )
1018
- } else {
1019
- bug ! ( "lookup_op_method: op not supported: {:?}" , op)
1023
+ }
1024
+ }
1025
+
1026
+ fn lang_item_for_unop ( tcx : TyCtxt < ' _ > , op : hir:: UnOp ) -> ( Symbol , Option < hir:: def_id:: DefId > ) {
1027
+ let lang = tcx. lang_items ( ) ;
1028
+ match op {
1029
+ hir:: UnOp :: Not => ( sym:: not, lang. not_trait ( ) ) ,
1030
+ hir:: UnOp :: Neg => ( sym:: neg, lang. neg_trait ( ) ) ,
1031
+ hir:: UnOp :: Deref => bug ! ( "Deref is not overloadable" ) ,
1020
1032
}
1021
1033
}
1022
1034
@@ -1077,12 +1089,6 @@ enum IsAssign {
1077
1089
Yes ,
1078
1090
}
1079
1091
1080
- #[ derive( Clone , Copy , Debug ) ]
1081
- enum Op {
1082
- Binary ( hir:: BinOp , IsAssign ) ,
1083
- Unary ( hir:: UnOp , Span ) ,
1084
- }
1085
-
1086
1092
/// Dereferences a single level of immutable referencing.
1087
1093
fn deref_ty_if_possible ( ty : Ty < ' _ > ) -> Ty < ' _ > {
1088
1094
match ty. kind ( ) {
0 commit comments