@@ -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 tracing:: debug;
@@ -49,7 +49,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
4949 . lookup_op_method (
5050 ( lhs, lhs_deref_ty) ,
5151 Some ( ( rhs, rhs_ty) ) ,
52- Op :: Binary ( op, IsAssign :: Yes ) ,
52+ lang_item_for_binop ( self . tcx , op, IsAssign :: Yes ) ,
53+ op. span ,
5354 expected,
5455 )
5556 . is_ok ( )
@@ -60,7 +61,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
6061 . lookup_op_method (
6162 ( lhs, lhs_ty) ,
6263 Some ( ( rhs, rhs_ty) ) ,
63- Op :: Binary ( op, IsAssign :: Yes ) ,
64+ lang_item_for_binop ( self . tcx , op, IsAssign :: Yes ) ,
65+ op. span ,
6466 expected,
6567 )
6668 . is_err ( )
@@ -242,7 +244,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
242244 let result = self . lookup_op_method (
243245 ( lhs_expr, lhs_ty) ,
244246 Some ( ( rhs_expr, rhs_ty_var) ) ,
245- Op :: Binary ( op, is_assign) ,
247+ lang_item_for_binop ( self . tcx , op, is_assign) ,
248+ op. span ,
246249 expected,
247250 ) ;
248251
@@ -301,8 +304,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
301304 Ty :: new_misc_error ( self . tcx )
302305 }
303306 Err ( errors) => {
304- let ( _, trait_def_id) =
305- lang_item_for_op ( self . tcx , Op :: Binary ( op, is_assign) , op. span ) ;
307+ let ( _, trait_def_id) = lang_item_for_binop ( self . tcx , op, is_assign) ;
306308 let missing_trait = trait_def_id
307309 . map ( |def_id| with_no_trimmed_paths ! ( self . tcx. def_path_str( def_id) ) ) ;
308310 let mut path = None ;
@@ -409,7 +411,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
409411 . lookup_op_method (
410412 ( lhs_expr, lhs_deref_ty) ,
411413 Some ( ( rhs_expr, rhs_ty) ) ,
412- Op :: Binary ( op, is_assign) ,
414+ lang_item_for_binop ( self . tcx , op, is_assign) ,
415+ op. span ,
413416 expected,
414417 )
415418 . is_ok ( )
@@ -442,7 +445,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
442445 . lookup_op_method (
443446 ( lhs_expr, lhs_adjusted_ty) ,
444447 Some ( ( rhs_expr, rhs_adjusted_ty) ) ,
445- Op :: Binary ( op, is_assign) ,
448+ lang_item_for_binop ( self . tcx , op, is_assign) ,
449+ op. span ,
446450 expected,
447451 )
448452 . is_ok ( )
@@ -499,7 +503,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
499503 self . lookup_op_method (
500504 ( lhs_expr, lhs_ty) ,
501505 Some ( ( rhs_expr, rhs_ty) ) ,
502- Op :: Binary ( op, is_assign) ,
506+ lang_item_for_binop ( self . tcx , op, is_assign) ,
507+ op. span ,
503508 expected,
504509 )
505510 . is_ok ( )
@@ -592,7 +597,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
592597 . lookup_op_method (
593598 ( lhs_expr, lhs_ty) ,
594599 Some ( ( rhs_expr, rhs_ty) ) ,
595- Op :: Binary ( op, is_assign) ,
600+ lang_item_for_binop ( self . tcx , op, is_assign) ,
601+ op. span ,
596602 expected,
597603 )
598604 . unwrap_err ( ) ;
@@ -799,7 +805,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
799805 expected : Expectation < ' tcx > ,
800806 ) -> Ty < ' tcx > {
801807 assert ! ( op. is_by_value( ) ) ;
802- match self . lookup_op_method ( ( ex, operand_ty) , None , Op :: Unary ( op, ex. span ) , expected) {
808+ match self . lookup_op_method (
809+ ( ex, operand_ty) ,
810+ None ,
811+ lang_item_for_unop ( self . tcx , op) ,
812+ ex. span ,
813+ expected,
814+ ) {
803815 Ok ( method) => {
804816 self . write_method_call_and_enforce_effects ( ex. hir_id , ex. span , method) ;
805817 method. sig . output ( )
@@ -898,21 +910,18 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
898910 & self ,
899911 ( lhs_expr, lhs_ty) : ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) ,
900912 opt_rhs : Option < ( & ' tcx hir:: Expr < ' tcx > , Ty < ' tcx > ) > ,
901- op : Op ,
913+ ( opname, trait_did) : ( Symbol , Option < hir:: def_id:: DefId > ) ,
914+ span : Span ,
902915 expected : Expectation < ' tcx > ,
903916 ) -> Result < MethodCallee < ' tcx > , Vec < FulfillmentError < ' tcx > > > {
904- let span = match op {
905- Op :: Binary ( op, _) => op. span ,
906- Op :: Unary ( _, span) => span,
907- } ;
908- let ( opname, Some ( trait_did) ) = lang_item_for_op ( self . tcx , op, span) else {
917+ let Some ( trait_did) = trait_did else {
909918 // Bail if the operator trait is not defined.
910919 return Err ( vec ! [ ] ) ;
911920 } ;
912921
913922 debug ! (
914- "lookup_op_method(lhs_ty={:?}, op={:?}, opname={:?}, trait_did={:?})" ,
915- lhs_ty, op , opname, trait_did
923+ "lookup_op_method(lhs_ty={:?}, opname={:?}, trait_did={:?})" ,
924+ lhs_ty, opname, trait_did
916925 ) ;
917926
918927 let opname = Ident :: with_dummy_span ( opname) ;
@@ -980,13 +989,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
980989 }
981990}
982991
983- fn lang_item_for_op (
992+ fn lang_item_for_binop (
984993 tcx : TyCtxt < ' _ > ,
985- op : Op ,
986- span : Span ,
987- ) -> ( rustc_span :: Symbol , Option < hir:: def_id:: DefId > ) {
994+ op : hir :: BinOp ,
995+ is_assign : IsAssign ,
996+ ) -> ( Symbol , Option < hir:: def_id:: DefId > ) {
988997 let lang = tcx. lang_items ( ) ;
989- if let Op :: Binary ( op , IsAssign :: Yes ) = op {
998+ if is_assign == IsAssign :: Yes {
990999 match op. node {
9911000 hir:: BinOpKind :: Add => ( sym:: add_assign, lang. add_assign_trait ( ) ) ,
9921001 hir:: BinOpKind :: Sub => ( sym:: sub_assign, lang. sub_assign_trait ( ) ) ,
@@ -1006,10 +1015,10 @@ fn lang_item_for_op(
10061015 | hir:: BinOpKind :: Ne
10071016 | hir:: BinOpKind :: And
10081017 | hir:: BinOpKind :: Or => {
1009- span_bug ! ( span , "impossible assignment operation: {}=" , op. node. as_str( ) )
1018+ bug ! ( "impossible assignment operation: {}=" , op. node. as_str( ) )
10101019 }
10111020 }
1012- } else if let Op :: Binary ( op , IsAssign :: No ) = op {
1021+ } else {
10131022 match op. node {
10141023 hir:: BinOpKind :: Add => ( sym:: add, lang. add_trait ( ) ) ,
10151024 hir:: BinOpKind :: Sub => ( sym:: sub, lang. sub_trait ( ) ) ,
@@ -1028,15 +1037,18 @@ fn lang_item_for_op(
10281037 hir:: BinOpKind :: Eq => ( sym:: eq, lang. eq_trait ( ) ) ,
10291038 hir:: BinOpKind :: Ne => ( sym:: ne, lang. eq_trait ( ) ) ,
10301039 hir:: BinOpKind :: And | hir:: BinOpKind :: Or => {
1031- span_bug ! ( span , "&& and || are not overloadable" )
1040+ bug ! ( "&& and || are not overloadable" )
10321041 }
10331042 }
1034- } else if let Op :: Unary ( hir:: UnOp :: Not , _) = op {
1035- ( sym:: not, lang. not_trait ( ) )
1036- } else if let Op :: Unary ( hir:: UnOp :: Neg , _) = op {
1037- ( sym:: neg, lang. neg_trait ( ) )
1038- } else {
1039- bug ! ( "lookup_op_method: op not supported: {:?}" , op)
1043+ }
1044+ }
1045+
1046+ fn lang_item_for_unop ( tcx : TyCtxt < ' _ > , op : hir:: UnOp ) -> ( Symbol , Option < hir:: def_id:: DefId > ) {
1047+ let lang = tcx. lang_items ( ) ;
1048+ match op {
1049+ hir:: UnOp :: Not => ( sym:: not, lang. not_trait ( ) ) ,
1050+ hir:: UnOp :: Neg => ( sym:: neg, lang. neg_trait ( ) ) ,
1051+ hir:: UnOp :: Deref => bug ! ( "Deref is not overloadable" ) ,
10401052 }
10411053}
10421054
@@ -1097,12 +1109,6 @@ enum IsAssign {
10971109 Yes ,
10981110}
10991111
1100- #[ derive( Clone , Copy , Debug ) ]
1101- enum Op {
1102- Binary ( hir:: BinOp , IsAssign ) ,
1103- Unary ( hir:: UnOp , Span ) ,
1104- }
1105-
11061112/// Dereferences a single level of immutable referencing.
11071113fn deref_ty_if_possible ( ty : Ty < ' _ > ) -> Ty < ' _ > {
11081114 match ty. kind ( ) {
0 commit comments