]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/op.rs
Rollup merge of #61599 - laumann:pin-docs-minor-edits, r=Centril
[rust.git] / src / librustc_typeck / check / op.rs
1 //! Code related to processing overloaded binary and unary operators.
2
3 use super::{FnCtxt, Needs};
4 use super::method::MethodCallee;
5 use rustc::ty::{self, Ty, TypeFoldable};
6 use rustc::ty::TyKind::{Ref, Adt, FnDef, Str, Uint, Never, Tuple, Char, Array};
7 use rustc::ty::adjustment::{Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
8 use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
9 use errors::{self,Applicability};
10 use syntax_pos::Span;
11 use syntax::ast::Ident;
12 use rustc::hir;
13
14 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
15     /// Checks a `a <op>= b`
16     pub fn check_binop_assign(&self,
17                               expr: &'gcx hir::Expr,
18                               op: hir::BinOp,
19                               lhs_expr: &'gcx hir::Expr,
20                               rhs_expr: &'gcx hir::Expr) -> Ty<'tcx>
21     {
22         let (lhs_ty, rhs_ty, return_ty) =
23             self.check_overloaded_binop(expr, lhs_expr, rhs_expr, op, IsAssign::Yes);
24
25         let ty = if !lhs_ty.is_ty_var() && !rhs_ty.is_ty_var()
26                     && is_builtin_binop(lhs_ty, rhs_ty, op) {
27             self.enforce_builtin_binop_types(lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
28             self.tcx.mk_unit()
29         } else {
30             return_ty
31         };
32
33         if !lhs_expr.is_place_expr() {
34             struct_span_err!(
35                 self.tcx.sess, lhs_expr.span,
36                 E0067, "invalid left-hand side expression")
37             .span_label(
38                 lhs_expr.span,
39                 "invalid expression for left-hand side")
40             .emit();
41         }
42         ty
43     }
44
45     /// Checks a potentially overloaded binary operator.
46     pub fn check_binop(&self,
47                        expr: &'gcx hir::Expr,
48                        op: hir::BinOp,
49                        lhs_expr: &'gcx hir::Expr,
50                        rhs_expr: &'gcx hir::Expr) -> Ty<'tcx>
51     {
52         let tcx = self.tcx;
53
54         debug!("check_binop(expr.hir_id={}, expr={:?}, op={:?}, lhs_expr={:?}, rhs_expr={:?})",
55                expr.hir_id,
56                expr,
57                op,
58                lhs_expr,
59                rhs_expr);
60
61         match BinOpCategory::from(op) {
62             BinOpCategory::Shortcircuit => {
63                 // && and || are a simple case.
64                 self.check_expr_coercable_to_type(lhs_expr, tcx.types.bool);
65                 let lhs_diverges = self.diverges.get();
66                 self.check_expr_coercable_to_type(rhs_expr, tcx.types.bool);
67
68                 // Depending on the LHS' value, the RHS can never execute.
69                 self.diverges.set(lhs_diverges);
70
71                 tcx.types.bool
72             }
73             _ => {
74                 // Otherwise, we always treat operators as if they are
75                 // overloaded. This is the way to be most flexible w/r/t
76                 // types that get inferred.
77                 let (lhs_ty, rhs_ty, return_ty) =
78                     self.check_overloaded_binop(expr, lhs_expr,
79                                                 rhs_expr, op, IsAssign::No);
80
81                 // Supply type inference hints if relevant. Probably these
82                 // hints should be enforced during select as part of the
83                 // `consider_unification_despite_ambiguity` routine, but this
84                 // more convenient for now.
85                 //
86                 // The basic idea is to help type inference by taking
87                 // advantage of things we know about how the impls for
88                 // scalar types are arranged. This is important in a
89                 // scenario like `1_u32 << 2`, because it lets us quickly
90                 // deduce that the result type should be `u32`, even
91                 // though we don't know yet what type 2 has and hence
92                 // can't pin this down to a specific impl.
93                 if
94                     !lhs_ty.is_ty_var() && !rhs_ty.is_ty_var() &&
95                     is_builtin_binop(lhs_ty, rhs_ty, op)
96                 {
97                     let builtin_return_ty =
98                         self.enforce_builtin_binop_types(lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
99                     self.demand_suptype(expr.span, builtin_return_ty, return_ty);
100                 }
101
102                 return_ty
103             }
104         }
105     }
106
107     fn enforce_builtin_binop_types(&self,
108                                    lhs_expr: &'gcx hir::Expr,
109                                    lhs_ty: Ty<'tcx>,
110                                    rhs_expr: &'gcx hir::Expr,
111                                    rhs_ty: Ty<'tcx>,
112                                    op: hir::BinOp)
113                                    -> Ty<'tcx>
114     {
115         debug_assert!(is_builtin_binop(lhs_ty, rhs_ty, op));
116
117         let tcx = self.tcx;
118         match BinOpCategory::from(op) {
119             BinOpCategory::Shortcircuit => {
120                 self.demand_suptype(lhs_expr.span, tcx.mk_bool(), lhs_ty);
121                 self.demand_suptype(rhs_expr.span, tcx.mk_bool(), rhs_ty);
122                 tcx.mk_bool()
123             }
124
125             BinOpCategory::Shift => {
126                 // result type is same as LHS always
127                 lhs_ty
128             }
129
130             BinOpCategory::Math |
131             BinOpCategory::Bitwise => {
132                 // both LHS and RHS and result will have the same type
133                 self.demand_suptype(rhs_expr.span, lhs_ty, rhs_ty);
134                 lhs_ty
135             }
136
137             BinOpCategory::Comparison => {
138                 // both LHS and RHS and result will have the same type
139                 self.demand_suptype(rhs_expr.span, lhs_ty, rhs_ty);
140                 tcx.mk_bool()
141             }
142         }
143     }
144
145     fn check_overloaded_binop(&self,
146                               expr: &'gcx hir::Expr,
147                               lhs_expr: &'gcx hir::Expr,
148                               rhs_expr: &'gcx hir::Expr,
149                               op: hir::BinOp,
150                               is_assign: IsAssign)
151                               -> (Ty<'tcx>, Ty<'tcx>, Ty<'tcx>)
152     {
153         debug!("check_overloaded_binop(expr.hir_id={}, op={:?}, is_assign={:?})",
154                expr.hir_id,
155                op,
156                is_assign);
157
158         let lhs_ty = match is_assign {
159             IsAssign::No => {
160                 // Find a suitable supertype of the LHS expression's type, by coercing to
161                 // a type variable, to pass as the `Self` to the trait, avoiding invariant
162                 // trait matching creating lifetime constraints that are too strict.
163                 // e.g., adding `&'a T` and `&'b T`, given `&'x T: Add<&'x T>`, will result
164                 // in `&'a T <: &'x T` and `&'b T <: &'x T`, instead of `'a = 'b = 'x`.
165                 let lhs_ty = self.check_expr_with_needs(lhs_expr, Needs::None);
166                 let fresh_var = self.next_ty_var(TypeVariableOrigin {
167                     kind: TypeVariableOriginKind::MiscVariable,
168                     span: lhs_expr.span,
169                 });
170                 self.demand_coerce(lhs_expr, lhs_ty, fresh_var,  AllowTwoPhase::No)
171             }
172             IsAssign::Yes => {
173                 // rust-lang/rust#52126: We have to use strict
174                 // equivalence on the LHS of an assign-op like `+=`;
175                 // overwritten or mutably-borrowed places cannot be
176                 // coerced to a supertype.
177                 self.check_expr_with_needs(lhs_expr, Needs::MutPlace)
178             }
179         };
180         let lhs_ty = self.resolve_type_vars_with_obligations(lhs_ty);
181
182         // N.B., as we have not yet type-checked the RHS, we don't have the
183         // type at hand. Make a variable to represent it. The whole reason
184         // for this indirection is so that, below, we can check the expr
185         // using this variable as the expected type, which sometimes lets
186         // us do better coercions than we would be able to do otherwise,
187         // particularly for things like `String + &String`.
188         let rhs_ty_var = self.next_ty_var(TypeVariableOrigin {
189             kind: TypeVariableOriginKind::MiscVariable,
190             span: rhs_expr.span,
191         });
192
193         let result = self.lookup_op_method(lhs_ty, &[rhs_ty_var], Op::Binary(op, is_assign));
194
195         // see `NB` above
196         let rhs_ty = self.check_expr_coercable_to_type(rhs_expr, rhs_ty_var);
197         let rhs_ty = self.resolve_type_vars_with_obligations(rhs_ty);
198
199         let return_ty = match result {
200             Ok(method) => {
201                 let by_ref_binop = !op.node.is_by_value();
202                 if is_assign == IsAssign::Yes || by_ref_binop {
203                     if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].sty {
204                         let mutbl = match mutbl {
205                             hir::MutImmutable => AutoBorrowMutability::Immutable,
206                             hir::MutMutable => AutoBorrowMutability::Mutable {
207                                 // Allow two-phase borrows for binops in initial deployment
208                                 // since they desugar to methods
209                                 allow_two_phase_borrow: AllowTwoPhase::Yes,
210                             }
211                         };
212                         let autoref = Adjustment {
213                             kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
214                             target: method.sig.inputs()[0]
215                         };
216                         self.apply_adjustments(lhs_expr, vec![autoref]);
217                     }
218                 }
219                 if by_ref_binop {
220                     if let ty::Ref(region, _, mutbl) = method.sig.inputs()[1].sty {
221                         let mutbl = match mutbl {
222                             hir::MutImmutable => AutoBorrowMutability::Immutable,
223                             hir::MutMutable => AutoBorrowMutability::Mutable {
224                                 // Allow two-phase borrows for binops in initial deployment
225                                 // since they desugar to methods
226                                 allow_two_phase_borrow: AllowTwoPhase::Yes,
227                             }
228                         };
229                         let autoref = Adjustment {
230                             kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
231                             target: method.sig.inputs()[1]
232                         };
233                         // HACK(eddyb) Bypass checks due to reborrows being in
234                         // some cases applied on the RHS, on top of which we need
235                         // to autoref, which is not allowed by apply_adjustments.
236                         // self.apply_adjustments(rhs_expr, vec![autoref]);
237                         self.tables
238                             .borrow_mut()
239                             .adjustments_mut()
240                             .entry(rhs_expr.hir_id)
241                             .or_default()
242                             .push(autoref);
243                     }
244                 }
245                 self.write_method_call(expr.hir_id, method);
246
247                 method.sig.output()
248             }
249             Err(()) => {
250                 // error types are considered "builtin"
251                 if !lhs_ty.references_error() {
252                     let source_map = self.tcx.sess.source_map();
253                     match is_assign {
254                         IsAssign::Yes => {
255                             let mut err = struct_span_err!(
256                                 self.tcx.sess,
257                                 expr.span,
258                                 E0368,
259                                 "binary assignment operation `{}=` cannot be applied to type `{}`",
260                                 op.node.as_str(),
261                                 lhs_ty,
262                             );
263                             err.span_label(
264                                 lhs_expr.span,
265                                 format!("cannot use `{}=` on type `{}`",
266                                 op.node.as_str(), lhs_ty),
267                             );
268                             let mut suggested_deref = false;
269                             if let Ref(_, mut rty, _) = lhs_ty.sty {
270                                 if {
271                                     self.infcx.type_is_copy_modulo_regions(self.param_env,
272                                                                            rty,
273                                                                            lhs_expr.span) &&
274                                         self.lookup_op_method(rty,
275                                                               &[rhs_ty],
276                                                               Op::Binary(op, is_assign))
277                                             .is_ok()
278                                 } {
279                                     if let Ok(lstring) = source_map.span_to_snippet(lhs_expr.span) {
280                                         while let Ref(_, rty_inner, _) = rty.sty {
281                                             rty = rty_inner;
282                                         }
283                                         let msg = &format!(
284                                             "`{}=` can be used on '{}', you can dereference `{}`",
285                                             op.node.as_str(),
286                                             rty,
287                                             lstring,
288                                         );
289                                         err.span_suggestion(
290                                             lhs_expr.span,
291                                             msg,
292                                             format!("*{}", lstring),
293                                             errors::Applicability::MachineApplicable,
294                                         );
295                                         suggested_deref = true;
296                                     }
297                                 }
298                             }
299                             let missing_trait = match op.node {
300                                 hir::BinOpKind::Add    => Some("std::ops::AddAssign"),
301                                 hir::BinOpKind::Sub    => Some("std::ops::SubAssign"),
302                                 hir::BinOpKind::Mul    => Some("std::ops::MulAssign"),
303                                 hir::BinOpKind::Div    => Some("std::ops::DivAssign"),
304                                 hir::BinOpKind::Rem    => Some("std::ops::RemAssign"),
305                                 hir::BinOpKind::BitAnd => Some("std::ops::BitAndAssign"),
306                                 hir::BinOpKind::BitXor => Some("std::ops::BitXorAssign"),
307                                 hir::BinOpKind::BitOr  => Some("std::ops::BitOrAssign"),
308                                 hir::BinOpKind::Shl    => Some("std::ops::ShlAssign"),
309                                 hir::BinOpKind::Shr    => Some("std::ops::ShrAssign"),
310                                 _                      => None
311                             };
312                             if let Some(missing_trait) = missing_trait {
313                                 if op.node == hir::BinOpKind::Add &&
314                                     self.check_str_addition(
315                                         lhs_expr, rhs_expr, lhs_ty, rhs_ty, &mut err, true, op) {
316                                     // This has nothing here because it means we did string
317                                     // concatenation (e.g., "Hello " += "World!"). This means
318                                     // we don't want the note in the else clause to be emitted
319                                 } else if let ty::Param(_) = lhs_ty.sty {
320                                     // FIXME: point to span of param
321                                     err.note(&format!(
322                                         "`{}` might need a bound for `{}`",
323                                         lhs_ty, missing_trait
324                                     ));
325                                 } else if !suggested_deref {
326                                     err.note(&format!(
327                                         "an implementation of `{}` might \
328                                          be missing for `{}`",
329                                         missing_trait, lhs_ty
330                                     ));
331                                 }
332                             }
333                             err.emit();
334                         }
335                         IsAssign::No => {
336                             let mut err = struct_span_err!(self.tcx.sess, op.span, E0369,
337                                 "binary operation `{}` cannot be applied to type `{}`",
338                                 op.node.as_str(),
339                                 lhs_ty);
340
341                             let mut involves_fn = false;
342                             if !lhs_expr.span.eq(&rhs_expr.span) {
343                                 involves_fn |= self.add_type_neq_err_label(
344                                     &mut err,
345                                     lhs_expr.span,
346                                     lhs_ty,
347                                     rhs_ty,
348                                     op,
349                                     is_assign
350                                 );
351                                 involves_fn |= self.add_type_neq_err_label(
352                                     &mut err,
353                                     rhs_expr.span,
354                                     rhs_ty,
355                                     lhs_ty,
356                                     op,
357                                     is_assign
358                                 );
359                             }
360
361                             let mut suggested_deref = false;
362                             if let Ref(_, mut rty, _) = lhs_ty.sty {
363                                 if {
364                                     self.infcx.type_is_copy_modulo_regions(self.param_env,
365                                                                            rty,
366                                                                            lhs_expr.span) &&
367                                         self.lookup_op_method(rty,
368                                                               &[rhs_ty],
369                                                               Op::Binary(op, is_assign))
370                                             .is_ok()
371                                 } {
372                                     if let Ok(lstring) = source_map.span_to_snippet(lhs_expr.span) {
373                                         while let Ref(_, rty_inner, _) = rty.sty {
374                                             rty = rty_inner;
375                                         }
376                                         let msg = &format!(
377                                                 "`{}` can be used on '{}', you can \
378                                                 dereference `{2}`: `*{2}`",
379                                                 op.node.as_str(),
380                                                 rty,
381                                                 lstring
382                                         );
383                                         err.help(msg);
384                                         suggested_deref = true;
385                                     }
386                                 }
387                             }
388                             let missing_trait = match op.node {
389                                 hir::BinOpKind::Add    => Some("std::ops::Add"),
390                                 hir::BinOpKind::Sub    => Some("std::ops::Sub"),
391                                 hir::BinOpKind::Mul    => Some("std::ops::Mul"),
392                                 hir::BinOpKind::Div    => Some("std::ops::Div"),
393                                 hir::BinOpKind::Rem    => Some("std::ops::Rem"),
394                                 hir::BinOpKind::BitAnd => Some("std::ops::BitAnd"),
395                                 hir::BinOpKind::BitXor => Some("std::ops::BitXor"),
396                                 hir::BinOpKind::BitOr  => Some("std::ops::BitOr"),
397                                 hir::BinOpKind::Shl    => Some("std::ops::Shl"),
398                                 hir::BinOpKind::Shr    => Some("std::ops::Shr"),
399                                 hir::BinOpKind::Eq |
400                                 hir::BinOpKind::Ne => Some("std::cmp::PartialEq"),
401                                 hir::BinOpKind::Lt |
402                                 hir::BinOpKind::Le |
403                                 hir::BinOpKind::Gt |
404                                 hir::BinOpKind::Ge => Some("std::cmp::PartialOrd"),
405                                 _ => None
406                             };
407                             if let Some(missing_trait) = missing_trait {
408                                 if op.node == hir::BinOpKind::Add &&
409                                     self.check_str_addition(
410                                         lhs_expr, rhs_expr, lhs_ty, rhs_ty, &mut err, false, op) {
411                                     // This has nothing here because it means we did string
412                                     // concatenation (e.g., "Hello " + "World!"). This means
413                                     // we don't want the note in the else clause to be emitted
414                                 } else if let ty::Param(_) = lhs_ty.sty {
415                                     // FIXME: point to span of param
416                                     err.note(&format!(
417                                         "`{}` might need a bound for `{}`",
418                                         lhs_ty, missing_trait
419                                     ));
420                                 } else if !suggested_deref && !involves_fn {
421                                     err.note(&format!(
422                                         "an implementation of `{}` might \
423                                          be missing for `{}`",
424                                         missing_trait, lhs_ty
425                                     ));
426                                 }
427                             }
428                             err.emit();
429                         }
430                     }
431                 }
432                 self.tcx.types.err
433             }
434         };
435
436         (lhs_ty, rhs_ty, return_ty)
437     }
438
439     /// If one of the types is an uncalled function and calling it would yield the other type,
440     /// suggest calling the function. Returns wether a suggestion was given.
441     fn add_type_neq_err_label(
442         &self,
443         err: &mut errors::DiagnosticBuilder<'_>,
444         span: Span,
445         ty: Ty<'tcx>,
446         other_ty: Ty<'tcx>,
447         op: hir::BinOp,
448         is_assign: IsAssign,
449     ) -> bool /* did we suggest to call a function because of missing parenthesis? */ {
450         err.span_label(span, ty.to_string());
451         if let FnDef(def_id, _) = ty.sty {
452             let source_map = self.tcx.sess.source_map();
453             let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
454                 Some(hir_id) => hir_id,
455                 None => return false,
456             };
457             if self.tcx.has_typeck_tables(def_id) == false {
458                 return false;
459             }
460             let fn_sig = {
461                 match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) {
462                     Some(f) => f.clone(),
463                     None => {
464                         bug!("No fn-sig entry for def_id={:?}", def_id);
465                     }
466                 }
467             };
468
469             let other_ty = if let FnDef(def_id, _) = other_ty.sty {
470                 let hir_id = match self.tcx.hir().as_local_hir_id(def_id) {
471                     Some(hir_id) => hir_id,
472                     None => return false,
473                 };
474                 if self.tcx.has_typeck_tables(def_id) == false {
475                     return false;
476                 }
477                 match self.tcx.typeck_tables_of(def_id).liberated_fn_sigs().get(hir_id) {
478                     Some(f) => f.clone().output(),
479                     None => {
480                         bug!("No fn-sig entry for def_id={:?}", def_id);
481                     }
482                 }
483             } else {
484                 other_ty
485             };
486
487             if self.lookup_op_method(fn_sig.output(),
488                                     &[other_ty],
489                                     Op::Binary(op, is_assign))
490                     .is_ok() {
491                 let (variable_snippet, applicability) = if fn_sig.inputs().len() > 0 {
492                     (format!("{}( /* arguments */ )", source_map.span_to_snippet(span).unwrap()),
493                     Applicability::HasPlaceholders)
494                 } else {
495                     (format!("{}()", source_map.span_to_snippet(span).unwrap()),
496                     Applicability::MaybeIncorrect)
497                 };
498
499                 err.span_suggestion(
500                     span,
501                     "you might have forgotten to call this function",
502                     variable_snippet,
503                     applicability,
504                 );
505                 return true;
506             }
507         }
508         false
509     }
510
511     /// Provide actionable suggestions when trying to add two strings with incorrect types,
512     /// like `&str + &str`, `String + String` and `&str + &String`.
513     ///
514     /// If this function returns `true` it means a note was printed, so we don't need
515     /// to print the normal "implementation of `std::ops::Add` might be missing" note
516     fn check_str_addition(
517         &self,
518         lhs_expr: &'gcx hir::Expr,
519         rhs_expr: &'gcx hir::Expr,
520         lhs_ty: Ty<'tcx>,
521         rhs_ty: Ty<'tcx>,
522         err: &mut errors::DiagnosticBuilder<'_>,
523         is_assign: bool,
524         op: hir::BinOp,
525     ) -> bool {
526         let source_map = self.tcx.sess.source_map();
527         let remove_borrow_msg = "String concatenation appends the string on the right to the \
528                                  string on the left and may require reallocation. This \
529                                  requires ownership of the string on the left";
530
531         let msg = "`to_owned()` can be used to create an owned `String` \
532                    from a string reference. String concatenation \
533                    appends the string on the right to the string \
534                    on the left and may require reallocation. This \
535                    requires ownership of the string on the left";
536
537         let is_std_string = |ty| &format!("{:?}", ty) == "std::string::String";
538
539         match (&lhs_ty.sty, &rhs_ty.sty) {
540             (&Ref(_, l_ty, _), &Ref(_, r_ty, _)) // &str or &String + &str, &String or &&str
541                 if (l_ty.sty == Str || is_std_string(l_ty)) && (
542                         r_ty.sty == Str || is_std_string(r_ty) ||
543                         &format!("{:?}", rhs_ty) == "&&str"
544                     ) =>
545             {
546                 if !is_assign { // Do not supply this message if `&str += &str`
547                     err.span_label(
548                         op.span,
549                         "`+` cannot be used to concatenate two `&str` strings",
550                     );
551                     match source_map.span_to_snippet(lhs_expr.span) {
552                         Ok(lstring) => {
553                             err.span_suggestion(
554                                 lhs_expr.span,
555                                 if lstring.starts_with("&") {
556                                     remove_borrow_msg
557                                 } else {
558                                     msg
559                                 },
560                                 if lstring.starts_with("&") {
561                                     // let a = String::new();
562                                     // let _ = &a + "bar";
563                                     format!("{}", &lstring[1..])
564                                 } else {
565                                     format!("{}.to_owned()", lstring)
566                                 },
567                                 Applicability::MachineApplicable,
568                             )
569                         }
570                         _ => err.help(msg),
571                     };
572                 }
573                 true
574             }
575             (&Ref(_, l_ty, _), &Adt(..)) // Handle `&str` & `&String` + `String`
576                 if (l_ty.sty == Str || is_std_string(l_ty)) && is_std_string(rhs_ty) =>
577             {
578                 err.span_label(
579                     op.span,
580                     "`+` cannot be used to concatenate a `&str` with a `String`",
581                 );
582                 match (
583                     source_map.span_to_snippet(lhs_expr.span),
584                     source_map.span_to_snippet(rhs_expr.span),
585                     is_assign,
586                 ) {
587                     (Ok(l), Ok(r), false) => {
588                         let to_string = if l.starts_with("&") {
589                             // let a = String::new(); let b = String::new();
590                             // let _ = &a + b;
591                             format!("{}", &l[1..])
592                         } else {
593                             format!("{}.to_owned()", l)
594                         };
595                         err.multipart_suggestion(
596                             msg,
597                             vec![
598                                 (lhs_expr.span, to_string),
599                                 (rhs_expr.span, format!("&{}", r)),
600                             ],
601                             Applicability::MachineApplicable,
602                         );
603                     }
604                     _ => {
605                         err.help(msg);
606                     }
607                 };
608                 true
609             }
610             _ => false,
611         }
612     }
613
614     pub fn check_user_unop(&self,
615                            ex: &'gcx hir::Expr,
616                            operand_ty: Ty<'tcx>,
617                            op: hir::UnOp)
618                            -> Ty<'tcx>
619     {
620         assert!(op.is_by_value());
621         match self.lookup_op_method(operand_ty, &[], Op::Unary(op, ex.span)) {
622             Ok(method) => {
623                 self.write_method_call(ex.hir_id, method);
624                 method.sig.output()
625             }
626             Err(()) => {
627                 let actual = self.resolve_vars_if_possible(&operand_ty);
628                 if !actual.references_error() {
629                     let mut err = struct_span_err!(self.tcx.sess, ex.span, E0600,
630                                      "cannot apply unary operator `{}` to type `{}`",
631                                      op.as_str(), actual);
632                     err.span_label(ex.span, format!("cannot apply unary \
633                                                     operator `{}`", op.as_str()));
634                     match actual.sty {
635                         Uint(_) if op == hir::UnNeg => {
636                             err.note("unsigned values cannot be negated");
637                         },
638                         Str | Never | Char | Tuple(_) | Array(_,_) => {},
639                         Ref(_, ref lty, _) if lty.sty == Str => {},
640                         _ => {
641                             let missing_trait = match op {
642                                 hir::UnNeg => "std::ops::Neg",
643                                 hir::UnNot => "std::ops::Not",
644                                 hir::UnDeref => "std::ops::UnDerf"
645                             };
646                             err.note(&format!("an implementation of `{}` might \
647                                                 be missing for `{}`",
648                                              missing_trait, operand_ty));
649                         }
650                     }
651                     err.emit();
652                 }
653                 self.tcx.types.err
654             }
655         }
656     }
657
658     fn lookup_op_method(&self, lhs_ty: Ty<'tcx>, other_tys: &[Ty<'tcx>], op: Op)
659                         -> Result<MethodCallee<'tcx>, ()>
660     {
661         let lang = self.tcx.lang_items();
662
663         let span = match op {
664             Op::Binary(op, _) => op.span,
665             Op::Unary(_, span) => span
666         };
667         let (opname, trait_did) = if let Op::Binary(op, IsAssign::Yes) = op {
668             match op.node {
669                 hir::BinOpKind::Add => ("add_assign", lang.add_assign_trait()),
670                 hir::BinOpKind::Sub => ("sub_assign", lang.sub_assign_trait()),
671                 hir::BinOpKind::Mul => ("mul_assign", lang.mul_assign_trait()),
672                 hir::BinOpKind::Div => ("div_assign", lang.div_assign_trait()),
673                 hir::BinOpKind::Rem => ("rem_assign", lang.rem_assign_trait()),
674                 hir::BinOpKind::BitXor => ("bitxor_assign", lang.bitxor_assign_trait()),
675                 hir::BinOpKind::BitAnd => ("bitand_assign", lang.bitand_assign_trait()),
676                 hir::BinOpKind::BitOr => ("bitor_assign", lang.bitor_assign_trait()),
677                 hir::BinOpKind::Shl => ("shl_assign", lang.shl_assign_trait()),
678                 hir::BinOpKind::Shr => ("shr_assign", lang.shr_assign_trait()),
679                 hir::BinOpKind::Lt | hir::BinOpKind::Le |
680                 hir::BinOpKind::Ge | hir::BinOpKind::Gt |
681                 hir::BinOpKind::Eq | hir::BinOpKind::Ne |
682                 hir::BinOpKind::And | hir::BinOpKind::Or => {
683                     span_bug!(span,
684                               "impossible assignment operation: {}=",
685                               op.node.as_str())
686                 }
687             }
688         } else if let Op::Binary(op, IsAssign::No) = op {
689             match op.node {
690                 hir::BinOpKind::Add => ("add", lang.add_trait()),
691                 hir::BinOpKind::Sub => ("sub", lang.sub_trait()),
692                 hir::BinOpKind::Mul => ("mul", lang.mul_trait()),
693                 hir::BinOpKind::Div => ("div", lang.div_trait()),
694                 hir::BinOpKind::Rem => ("rem", lang.rem_trait()),
695                 hir::BinOpKind::BitXor => ("bitxor", lang.bitxor_trait()),
696                 hir::BinOpKind::BitAnd => ("bitand", lang.bitand_trait()),
697                 hir::BinOpKind::BitOr => ("bitor", lang.bitor_trait()),
698                 hir::BinOpKind::Shl => ("shl", lang.shl_trait()),
699                 hir::BinOpKind::Shr => ("shr", lang.shr_trait()),
700                 hir::BinOpKind::Lt => ("lt", lang.partial_ord_trait()),
701                 hir::BinOpKind::Le => ("le", lang.partial_ord_trait()),
702                 hir::BinOpKind::Ge => ("ge", lang.partial_ord_trait()),
703                 hir::BinOpKind::Gt => ("gt", lang.partial_ord_trait()),
704                 hir::BinOpKind::Eq => ("eq", lang.eq_trait()),
705                 hir::BinOpKind::Ne => ("ne", lang.eq_trait()),
706                 hir::BinOpKind::And | hir::BinOpKind::Or => {
707                     span_bug!(span, "&& and || are not overloadable")
708                 }
709             }
710         } else if let Op::Unary(hir::UnNot, _) = op {
711             ("not", lang.not_trait())
712         } else if let Op::Unary(hir::UnNeg, _) = op {
713             ("neg", lang.neg_trait())
714         } else {
715             bug!("lookup_op_method: op not supported: {:?}", op)
716         };
717
718         debug!("lookup_op_method(lhs_ty={:?}, op={:?}, opname={:?}, trait_did={:?})",
719                lhs_ty,
720                op,
721                opname,
722                trait_did);
723
724         let method = trait_did.and_then(|trait_did| {
725             let opname = Ident::from_str(opname);
726             self.lookup_method_in_trait(span, opname, trait_did, lhs_ty, Some(other_tys))
727         });
728
729         match method {
730             Some(ok) => {
731                 let method = self.register_infer_ok_obligations(ok);
732                 self.select_obligations_where_possible(false);
733
734                 Ok(method)
735             }
736             None => {
737                 Err(())
738             }
739         }
740     }
741 }
742
743 // Binary operator categories. These categories summarize the behavior
744 // with respect to the builtin operationrs supported.
745 enum BinOpCategory {
746     /// &&, || -- cannot be overridden
747     Shortcircuit,
748
749     /// <<, >> -- when shifting a single integer, rhs can be any
750     /// integer type. For simd, types must match.
751     Shift,
752
753     /// +, -, etc -- takes equal types, produces same type as input,
754     /// applicable to ints/floats/simd
755     Math,
756
757     /// &, |, ^ -- takes equal types, produces same type as input,
758     /// applicable to ints/floats/simd/bool
759     Bitwise,
760
761     /// ==, !=, etc -- takes equal types, produces bools, except for simd,
762     /// which produce the input type
763     Comparison,
764 }
765
766 impl BinOpCategory {
767     fn from(op: hir::BinOp) -> BinOpCategory {
768         match op.node {
769             hir::BinOpKind::Shl | hir::BinOpKind::Shr =>
770                 BinOpCategory::Shift,
771
772             hir::BinOpKind::Add |
773             hir::BinOpKind::Sub |
774             hir::BinOpKind::Mul |
775             hir::BinOpKind::Div |
776             hir::BinOpKind::Rem =>
777                 BinOpCategory::Math,
778
779             hir::BinOpKind::BitXor |
780             hir::BinOpKind::BitAnd |
781             hir::BinOpKind::BitOr =>
782                 BinOpCategory::Bitwise,
783
784             hir::BinOpKind::Eq |
785             hir::BinOpKind::Ne |
786             hir::BinOpKind::Lt |
787             hir::BinOpKind::Le |
788             hir::BinOpKind::Ge |
789             hir::BinOpKind::Gt =>
790                 BinOpCategory::Comparison,
791
792             hir::BinOpKind::And |
793             hir::BinOpKind::Or =>
794                 BinOpCategory::Shortcircuit,
795         }
796     }
797 }
798
799 /// Whether the binary operation is an assignment (`a += b`), or not (`a + b`)
800 #[derive(Clone, Copy, Debug, PartialEq)]
801 enum IsAssign {
802     No,
803     Yes,
804 }
805
806 #[derive(Clone, Copy, Debug)]
807 enum Op {
808     Binary(hir::BinOp, IsAssign),
809     Unary(hir::UnOp, Span),
810 }
811
812 /// Returns `true` if this is a built-in arithmetic operation (e.g., u32
813 /// + u32, i16x4 == i16x4) and false if these types would have to be
814 /// overloaded to be legal. There are two reasons that we distinguish
815 /// builtin operations from overloaded ones (vs trying to drive
816 /// everything uniformly through the trait system and intrinsics or
817 /// something like that):
818 ///
819 /// 1. Builtin operations can trivially be evaluated in constants.
820 /// 2. For comparison operators applied to SIMD types the result is
821 ///    not of type `bool`. For example, `i16x4 == i16x4` yields a
822 ///    type like `i16x4`. This means that the overloaded trait
823 ///    `PartialEq` is not applicable.
824 ///
825 /// Reason #2 is the killer. I tried for a while to always use
826 /// overloaded logic and just check the types in constants/codegen after
827 /// the fact, and it worked fine, except for SIMD types. -nmatsakis
828 fn is_builtin_binop(lhs: Ty<'_>, rhs: Ty<'_>, op: hir::BinOp) -> bool {
829     match BinOpCategory::from(op) {
830         BinOpCategory::Shortcircuit => {
831             true
832         }
833
834         BinOpCategory::Shift => {
835             lhs.references_error() || rhs.references_error() ||
836                 lhs.is_integral() && rhs.is_integral()
837         }
838
839         BinOpCategory::Math => {
840             lhs.references_error() || rhs.references_error() ||
841                 lhs.is_integral() && rhs.is_integral() ||
842                 lhs.is_floating_point() && rhs.is_floating_point()
843         }
844
845         BinOpCategory::Bitwise => {
846             lhs.references_error() || rhs.references_error() ||
847                 lhs.is_integral() && rhs.is_integral() ||
848                 lhs.is_floating_point() && rhs.is_floating_point() ||
849                 lhs.is_bool() && rhs.is_bool()
850         }
851
852         BinOpCategory::Comparison => {
853             lhs.references_error() || rhs.references_error() ||
854                 lhs.is_scalar() && rhs.is_scalar()
855         }
856     }
857 }