@@ -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 path = None ;
@@ -410,7 +412,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
410
412
. lookup_op_method (
411
413
( lhs_expr, lhs_deref_ty) ,
412
414
Some ( ( rhs_expr, rhs_ty) ) ,
413
- Op :: Binary ( op, is_assign) ,
415
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
416
+ op. span ,
414
417
expected,
415
418
)
416
419
. is_ok ( )
@@ -443,7 +446,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
443
446
. lookup_op_method (
444
447
( lhs_expr, lhs_adjusted_ty) ,
445
448
Some ( ( rhs_expr, rhs_adjusted_ty) ) ,
446
- Op :: Binary ( op, is_assign) ,
449
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
450
+ op. span ,
447
451
expected,
448
452
)
449
453
. is_ok ( )
@@ -500,7 +504,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
500
504
self . lookup_op_method (
501
505
( lhs_expr, lhs_ty) ,
502
506
Some ( ( rhs_expr, rhs_ty) ) ,
503
- Op :: Binary ( op, is_assign) ,
507
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
508
+ op. span ,
504
509
expected,
505
510
)
506
511
. is_ok ( )
@@ -593,7 +598,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
593
598
. lookup_op_method (
594
599
( lhs_expr, lhs_ty) ,
595
600
Some ( ( rhs_expr, rhs_ty) ) ,
596
- Op :: Binary ( op, is_assign) ,
601
+ lang_item_for_binop ( self . tcx , op, is_assign) ,
602
+ op. span ,
597
603
expected,
598
604
)
599
605
. unwrap_err ( ) ;
@@ -800,7 +806,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
800
806
expected : Expectation < ' tcx > ,
801
807
) -> Ty < ' tcx > {
802
808
assert ! ( op. is_by_value( ) ) ;
803
- match self . lookup_op_method ( ( ex, operand_ty) , None , Op :: Unary ( op, ex. span ) , expected) {
809
+ match self . lookup_op_method (
810
+ ( ex, operand_ty) ,
811
+ None ,
812
+ lang_item_for_unop ( self . tcx , op) ,
813
+ ex. span ,
814
+ expected,
815
+ ) {
804
816
Ok ( method) => {
805
817
self . write_method_call_and_enforce_effects ( ex. hir_id , ex. span , method) ;
806
818
method. sig . output ( )
@@ -899,21 +911,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
899
911
& self ,
900
912
( lhs_expr, lhs_ty) : ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) ,
901
913
opt_rhs : Option < ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) > ,
902
- op : Op ,
914
+ ( opname, trait_did) : ( Symbol , Option < hir:: def_id:: DefId > ) ,
915
+ span : Span ,
903
916
expected : Expectation < ' tcx > ,
904
917
) -> Result < MethodCallee < ' tcx > , Vec < FulfillmentError < ' tcx > > > {
905
- let span = match op {
906
- Op :: Binary ( op, _) => op. span ,
907
- Op :: Unary ( _, span) => span,
908
- } ;
909
- let ( opname, Some ( trait_did) ) = lang_item_for_op ( self . tcx , op, span) else {
918
+ let Some ( trait_did) = trait_did else {
910
919
// Bail if the operator trait is not defined.
911
920
return Err ( vec ! [ ] ) ;
912
921
} ;
913
922
914
923
debug ! (
915
- "lookup_op_method(lhs_ty={:?}, op={:?}, opname={:?}, trait_did={:?})" ,
916
- lhs_ty, op , opname, trait_did
924
+ "lookup_op_method(lhs_ty={:?}, opname={:?}, trait_did={:?})" ,
925
+ lhs_ty, opname, trait_did
917
926
) ;
918
927
919
928
let opname = Ident :: with_dummy_span ( opname) ;
@@ -981,13 +990,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
981
990
}
982
991
}
983
992
984
- fn lang_item_for_op (
993
+ fn lang_item_for_binop (
985
994
tcx : TyCtxt < ' _ > ,
986
- op : Op ,
987
- span : Span ,
988
- ) -> ( rustc_span :: Symbol , Option < hir:: def_id:: DefId > ) {
995
+ op : hir :: BinOp ,
996
+ is_assign : IsAssign ,
997
+ ) -> ( Symbol , Option < hir:: def_id:: DefId > ) {
989
998
let lang = tcx. lang_items ( ) ;
990
- if let Op :: Binary ( op , IsAssign :: Yes ) = op {
999
+ if is_assign == IsAssign :: Yes {
991
1000
match op. node {
992
1001
hir:: BinOpKind :: Add => ( sym:: add_assign, lang. add_assign_trait ( ) ) ,
993
1002
hir:: BinOpKind :: Sub => ( sym:: sub_assign, lang. sub_assign_trait ( ) ) ,
@@ -1007,10 +1016,10 @@ fn lang_item_for_op(
1007
1016
| hir:: BinOpKind :: Ne
1008
1017
| hir:: BinOpKind :: And
1009
1018
| hir:: BinOpKind :: Or => {
1010
- span_bug ! ( span , "impossible assignment operation: {}=" , op. node. as_str( ) )
1019
+ bug ! ( "impossible assignment operation: {}=" , op. node. as_str( ) )
1011
1020
}
1012
1021
}
1013
- } else if let Op :: Binary ( op , IsAssign :: No ) = op {
1022
+ } else {
1014
1023
match op. node {
1015
1024
hir:: BinOpKind :: Add => ( sym:: add, lang. add_trait ( ) ) ,
1016
1025
hir:: BinOpKind :: Sub => ( sym:: sub, lang. sub_trait ( ) ) ,
@@ -1029,15 +1038,18 @@ fn lang_item_for_op(
1029
1038
hir:: BinOpKind :: Eq => ( sym:: eq, lang. eq_trait ( ) ) ,
1030
1039
hir:: BinOpKind :: Ne => ( sym:: ne, lang. eq_trait ( ) ) ,
1031
1040
hir:: BinOpKind :: And | hir:: BinOpKind :: Or => {
1032
- span_bug ! ( span , "&& and || are not overloadable" )
1041
+ bug ! ( "&& and || are not overloadable" )
1033
1042
}
1034
1043
}
1035
- } else if let Op :: Unary ( hir:: UnOp :: Not , _) = op {
1036
- ( sym:: not, lang. not_trait ( ) )
1037
- } else if let Op :: Unary ( hir:: UnOp :: Neg , _) = op {
1038
- ( sym:: neg, lang. neg_trait ( ) )
1039
- } else {
1040
- bug ! ( "lookup_op_method: op not supported: {:?}" , op)
1044
+ }
1045
+ }
1046
+
1047
+ fn lang_item_for_unop ( tcx : TyCtxt < ' _ > , op : hir:: UnOp ) -> ( Symbol , Option < hir:: def_id:: DefId > ) {
1048
+ let lang = tcx. lang_items ( ) ;
1049
+ match op {
1050
+ hir:: UnOp :: Not => ( sym:: not, lang. not_trait ( ) ) ,
1051
+ hir:: UnOp :: Neg => ( sym:: neg, lang. neg_trait ( ) ) ,
1052
+ hir:: UnOp :: Deref => bug ! ( "Deref is not overloadable" ) ,
1041
1053
}
1042
1054
}
1043
1055
@@ -1098,12 +1110,6 @@ enum IsAssign {
1098
1110
Yes ,
1099
1111
}
1100
1112
1101
- #[ derive( Clone , Copy , Debug ) ]
1102
- enum Op {
1103
- Binary ( hir:: BinOp , IsAssign ) ,
1104
- Unary ( hir:: UnOp , Span ) ,
1105
- }
1106
-
1107
1113
/// Dereferences a single level of immutable referencing.
1108
1114
fn deref_ty_if_possible ( ty : Ty < ' _ > ) -> Ty < ' _ > {
1109
1115
match ty. kind ( ) {
0 commit comments