@@ -4,15 +4,15 @@ use rustc_data_structures::packed::Pu128;
44use rustc_errors:: codes:: * ;
55use rustc_errors:: { Applicability , Diag , struct_span_code_err} ;
66use rustc_infer:: traits:: ObligationCauseCode ;
7+ use rustc_middle:: bug;
78use rustc_middle:: ty:: adjustment:: {
89 Adjust , Adjustment , AllowTwoPhase , AutoBorrow , AutoBorrowMutability ,
910} ;
1011use rustc_middle:: ty:: print:: with_no_trimmed_paths;
1112use rustc_middle:: ty:: { self , IsSuggestable , Ty , TyCtxt , TypeVisitableExt } ;
12- use rustc_middle:: { bug, span_bug} ;
1313use rustc_session:: errors:: ExprParenthesesNeeded ;
1414use rustc_span:: source_map:: Spanned ;
15- use rustc_span:: { Ident , Span , sym} ;
15+ use rustc_span:: { Ident , Span , Symbol , sym} ;
1616use rustc_trait_selection:: infer:: InferCtxtExt ;
1717use rustc_trait_selection:: traits:: { FulfillmentError , Obligation , ObligationCtxt } ;
1818use rustc_type_ir:: TyKind :: * ;
@@ -50,7 +50,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5050 . lookup_op_method (
5151 ( lhs, lhs_deref_ty) ,
5252 Some ( ( rhs, rhs_ty) ) ,
53- Op :: Binary ( op, IsAssign :: Yes ) ,
53+ lang_item_for_binop ( self . tcx , op, IsAssign :: Yes ) ,
54+ op. span ,
5455 expected,
5556 )
5657 . is_ok ( )
@@ -61,7 +62,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6162 . lookup_op_method (
6263 ( lhs, lhs_ty) ,
6364 Some ( ( rhs, rhs_ty) ) ,
64- Op :: Binary ( op, IsAssign :: Yes ) ,
65+ lang_item_for_binop ( self . tcx , op, IsAssign :: Yes ) ,
66+ op. span ,
6567 expected,
6668 )
6769 . is_err ( )
@@ -243,7 +245,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
243245 let result = self . lookup_op_method (
244246 ( lhs_expr, lhs_ty) ,
245247 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 ,
247250 expected,
248251 ) ;
249252
@@ -302,8 +305,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
302305 Ty :: new_misc_error ( self . tcx )
303306 }
304307 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) ;
307309 let missing_trait = trait_def_id
308310 . map ( |def_id| with_no_trimmed_paths ! ( self . tcx. def_path_str( def_id) ) ) ;
309311 let ( mut err, output_def_id) = match is_assign {
@@ -405,7 +407,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
405407 . lookup_op_method (
406408 ( lhs_expr, lhs_deref_ty) ,
407409 Some ( ( rhs_expr, rhs_ty) ) ,
408- Op :: Binary ( op, is_assign) ,
410+ lang_item_for_binop ( self . tcx , op, is_assign) ,
411+ op. span ,
409412 expected,
410413 )
411414 . is_ok ( )
@@ -438,7 +441,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
438441 . lookup_op_method (
439442 ( lhs_expr, lhs_adjusted_ty) ,
440443 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 ,
442446 expected,
443447 )
444448 . is_ok ( )
@@ -493,7 +497,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
493497 self . lookup_op_method (
494498 ( lhs_expr, lhs_ty) ,
495499 Some ( ( rhs_expr, rhs_ty) ) ,
496- Op :: Binary ( op, is_assign) ,
500+ lang_item_for_binop ( self . tcx , op, is_assign) ,
501+ op. span ,
497502 expected,
498503 )
499504 . is_ok ( )
@@ -586,7 +591,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
586591 . lookup_op_method (
587592 ( lhs_expr, lhs_ty) ,
588593 Some ( ( rhs_expr, rhs_ty) ) ,
589- Op :: Binary ( op, is_assign) ,
594+ lang_item_for_binop ( self . tcx , op, is_assign) ,
595+ op. span ,
590596 expected,
591597 )
592598 . unwrap_err ( ) ;
@@ -785,7 +791,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
785791 expected : Expectation < ' tcx > ,
786792 ) -> Ty < ' tcx > {
787793 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+ ) {
789801 Ok ( method) => {
790802 self . write_method_call_and_enforce_effects ( ex. hir_id , ex. span , method) ;
791803 method. sig . output ( )
@@ -882,21 +894,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
882894 & self ,
883895 ( lhs_expr, lhs_ty) : ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) ,
884896 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 ,
886899 expected : Expectation < ' tcx > ,
887900 ) -> 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 {
893902 // Bail if the operator trait is not defined.
894903 return Err ( vec ! [ ] ) ;
895904 } ;
896905
897906 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
900909 ) ;
901910
902911 let opname = Ident :: with_dummy_span ( opname) ;
@@ -960,13 +969,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
960969 }
961970}
962971
963- fn lang_item_for_op (
972+ fn lang_item_for_binop (
964973 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 > ) {
968977 let lang = tcx. lang_items ( ) ;
969- if let Op :: Binary ( op , IsAssign :: Yes ) = op {
978+ if is_assign == IsAssign :: Yes {
970979 match op. node {
971980 hir:: BinOpKind :: Add => ( sym:: add_assign, lang. add_assign_trait ( ) ) ,
972981 hir:: BinOpKind :: Sub => ( sym:: sub_assign, lang. sub_assign_trait ( ) ) ,
@@ -986,10 +995,10 @@ fn lang_item_for_op(
986995 | hir:: BinOpKind :: Ne
987996 | hir:: BinOpKind :: And
988997 | hir:: BinOpKind :: Or => {
989- span_bug ! ( span , "impossible assignment operation: {}=" , op. node. as_str( ) )
998+ bug ! ( "impossible assignment operation: {}=" , op. node. as_str( ) )
990999 }
9911000 }
992- } else if let Op :: Binary ( op , IsAssign :: No ) = op {
1001+ } else {
9931002 match op. node {
9941003 hir:: BinOpKind :: Add => ( sym:: add, lang. add_trait ( ) ) ,
9951004 hir:: BinOpKind :: Sub => ( sym:: sub, lang. sub_trait ( ) ) ,
@@ -1008,15 +1017,18 @@ fn lang_item_for_op(
10081017 hir:: BinOpKind :: Eq => ( sym:: eq, lang. eq_trait ( ) ) ,
10091018 hir:: BinOpKind :: Ne => ( sym:: ne, lang. eq_trait ( ) ) ,
10101019 hir:: BinOpKind :: And | hir:: BinOpKind :: Or => {
1011- span_bug ! ( span , "&& and || are not overloadable" )
1020+ bug ! ( "&& and || are not overloadable" )
10121021 }
10131022 }
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" ) ,
10201032 }
10211033}
10221034
@@ -1077,12 +1089,6 @@ enum IsAssign {
10771089 Yes ,
10781090}
10791091
1080- #[ derive( Clone , Copy , Debug ) ]
1081- enum Op {
1082- Binary ( hir:: BinOp , IsAssign ) ,
1083- Unary ( hir:: UnOp , Span ) ,
1084- }
1085-
10861092/// Dereferences a single level of immutable referencing.
10871093fn deref_ty_if_possible ( ty : Ty < ' _ > ) -> Ty < ' _ > {
10881094 match ty. kind ( ) {
0 commit comments