1 //! Code related to processing overloaded binary and unary operators.
3 use super::{FnCtxt, Needs};
4 use super::method::MethodCallee;
5 use rustc::ty::{self, Ty, TypeFoldable};
6 use rustc::ty::TyKind::{Ref, Adt, Str, Uint, Never, Tuple, Char, Array};
7 use rustc::ty::adjustment::{Adjustment, Adjust, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
8 use rustc::infer::type_variable::TypeVariableOrigin;
9 use errors::{self,Applicability};
11 use syntax::ast::Ident;
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,
19 lhs_expr: &'gcx hir::Expr,
20 rhs_expr: &'gcx hir::Expr) -> Ty<'tcx>
22 let (lhs_ty, rhs_ty, return_ty) =
23 self.check_overloaded_binop(expr, lhs_expr, rhs_expr, op, IsAssign::Yes);
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);
33 if !lhs_expr.is_place_expr() {
35 self.tcx.sess, lhs_expr.span,
36 E0067, "invalid left-hand side expression")
39 "invalid expression for left-hand side")
45 /// Checks a potentially overloaded binary operator.
46 pub fn check_binop(&self,
47 expr: &'gcx hir::Expr,
49 lhs_expr: &'gcx hir::Expr,
50 rhs_expr: &'gcx hir::Expr) -> Ty<'tcx>
54 debug!("check_binop(expr.id={}, expr={:?}, op={:?}, lhs_expr={:?}, rhs_expr={:?})",
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);
68 // Depending on the LHS' value, the RHS can never execute.
69 self.diverges.set(lhs_diverges);
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);
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.
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.
94 !lhs_ty.is_ty_var() && !rhs_ty.is_ty_var() &&
95 is_builtin_binop(lhs_ty, rhs_ty, op)
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);
107 fn enforce_builtin_binop_types(&self,
108 lhs_expr: &'gcx hir::Expr,
110 rhs_expr: &'gcx hir::Expr,
115 debug_assert!(is_builtin_binop(lhs_ty, rhs_ty, op));
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);
125 BinOpCategory::Shift => {
126 // result type is same as LHS always
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);
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);
145 fn check_overloaded_binop(&self,
146 expr: &'gcx hir::Expr,
147 lhs_expr: &'gcx hir::Expr,
148 rhs_expr: &'gcx hir::Expr,
151 -> (Ty<'tcx>, Ty<'tcx>, Ty<'tcx>)
153 debug!("check_overloaded_binop(expr.id={}, op={:?}, is_assign={:?})",
158 let lhs_ty = match is_assign {
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::MiscVariable(lhs_expr.span));
167 self.demand_coerce(lhs_expr, lhs_ty, fresh_var, AllowTwoPhase::No)
170 // rust-lang/rust#52126: We have to use strict
171 // equivalence on the LHS of an assign-op like `+=`;
172 // overwritten or mutably-borrowed places cannot be
173 // coerced to a supertype.
174 self.check_expr_with_needs(lhs_expr, Needs::MutPlace)
177 let lhs_ty = self.resolve_type_vars_with_obligations(lhs_ty);
179 // N.B., as we have not yet type-checked the RHS, we don't have the
180 // type at hand. Make a variable to represent it. The whole reason
181 // for this indirection is so that, below, we can check the expr
182 // using this variable as the expected type, which sometimes lets
183 // us do better coercions than we would be able to do otherwise,
184 // particularly for things like `String + &String`.
185 let rhs_ty_var = self.next_ty_var(TypeVariableOrigin::MiscVariable(rhs_expr.span));
187 let result = self.lookup_op_method(lhs_ty, &[rhs_ty_var], Op::Binary(op, is_assign));
190 let rhs_ty = self.check_expr_coercable_to_type(rhs_expr, rhs_ty_var);
191 let rhs_ty = self.resolve_type_vars_with_obligations(rhs_ty);
193 let return_ty = match result {
195 let by_ref_binop = !op.node.is_by_value();
196 if is_assign == IsAssign::Yes || by_ref_binop {
197 if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].sty {
198 let mutbl = match mutbl {
199 hir::MutImmutable => AutoBorrowMutability::Immutable,
200 hir::MutMutable => AutoBorrowMutability::Mutable {
201 // Allow two-phase borrows for binops in initial deployment
202 // since they desugar to methods
203 allow_two_phase_borrow: AllowTwoPhase::Yes,
206 let autoref = Adjustment {
207 kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
208 target: method.sig.inputs()[0]
210 self.apply_adjustments(lhs_expr, vec![autoref]);
214 if let ty::Ref(region, _, mutbl) = method.sig.inputs()[1].sty {
215 let mutbl = match mutbl {
216 hir::MutImmutable => AutoBorrowMutability::Immutable,
217 hir::MutMutable => AutoBorrowMutability::Mutable {
218 // Allow two-phase borrows for binops in initial deployment
219 // since they desugar to methods
220 allow_two_phase_borrow: AllowTwoPhase::Yes,
223 let autoref = Adjustment {
224 kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
225 target: method.sig.inputs()[1]
227 // HACK(eddyb) Bypass checks due to reborrows being in
228 // some cases applied on the RHS, on top of which we need
229 // to autoref, which is not allowed by apply_adjustments.
230 // self.apply_adjustments(rhs_expr, vec![autoref]);
234 .entry(rhs_expr.hir_id)
239 self.write_method_call(expr.hir_id, method);
244 // error types are considered "builtin"
245 if !lhs_ty.references_error() {
246 let source_map = self.tcx.sess.source_map();
249 let mut err = struct_span_err!(
253 "binary assignment operation `{}=` cannot be applied to type `{}`",
259 format!("cannot use `{}=` on type `{}`",
260 op.node.as_str(), lhs_ty),
262 let mut suggested_deref = false;
263 if let Ref(_, mut rty, _) = lhs_ty.sty {
265 self.infcx.type_is_copy_modulo_regions(self.param_env,
268 self.lookup_op_method(rty,
270 Op::Binary(op, is_assign))
273 if let Ok(lstring) = source_map.span_to_snippet(lhs_expr.span) {
274 while let Ref(_, rty_inner, _) = rty.sty {
278 "`{}=` can be used on '{}', you can dereference `{}`",
286 format!("*{}", lstring),
287 errors::Applicability::MachineApplicable,
289 suggested_deref = true;
293 let missing_trait = match op.node {
294 hir::BinOpKind::Add => Some("std::ops::AddAssign"),
295 hir::BinOpKind::Sub => Some("std::ops::SubAssign"),
296 hir::BinOpKind::Mul => Some("std::ops::MulAssign"),
297 hir::BinOpKind::Div => Some("std::ops::DivAssign"),
298 hir::BinOpKind::Rem => Some("std::ops::RemAssign"),
299 hir::BinOpKind::BitAnd => Some("std::ops::BitAndAssign"),
300 hir::BinOpKind::BitXor => Some("std::ops::BitXorAssign"),
301 hir::BinOpKind::BitOr => Some("std::ops::BitOrAssign"),
302 hir::BinOpKind::Shl => Some("std::ops::ShlAssign"),
303 hir::BinOpKind::Shr => Some("std::ops::ShrAssign"),
306 if let Some(missing_trait) = missing_trait {
307 if op.node == hir::BinOpKind::Add &&
308 self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
309 rhs_ty, &mut err, true) {
310 // This has nothing here because it means we did string
311 // concatenation (e.g., "Hello " += "World!"). This means
312 // we don't want the note in the else clause to be emitted
313 } else if let ty::Param(_) = lhs_ty.sty {
314 // FIXME: point to span of param
316 "`{}` might need a bound for `{}`",
317 lhs_ty, missing_trait
319 } else if !suggested_deref {
321 "an implementation of `{}` might \
322 be missing for `{}`",
323 missing_trait, lhs_ty
330 let mut err = struct_span_err!(self.tcx.sess, expr.span, E0369,
331 "binary operation `{}` cannot be applied to type `{}`",
334 let mut suggested_deref = false;
335 if let Ref(_, mut rty, _) = lhs_ty.sty {
337 self.infcx.type_is_copy_modulo_regions(self.param_env,
340 self.lookup_op_method(rty,
342 Op::Binary(op, is_assign))
345 if let Ok(lstring) = source_map.span_to_snippet(lhs_expr.span) {
346 while let Ref(_, rty_inner, _) = rty.sty {
350 "`{}` can be used on '{}', you can \
351 dereference `{2}`: `*{2}`",
357 suggested_deref = true;
361 let missing_trait = match op.node {
362 hir::BinOpKind::Add => Some("std::ops::Add"),
363 hir::BinOpKind::Sub => Some("std::ops::Sub"),
364 hir::BinOpKind::Mul => Some("std::ops::Mul"),
365 hir::BinOpKind::Div => Some("std::ops::Div"),
366 hir::BinOpKind::Rem => Some("std::ops::Rem"),
367 hir::BinOpKind::BitAnd => Some("std::ops::BitAnd"),
368 hir::BinOpKind::BitXor => Some("std::ops::BitXor"),
369 hir::BinOpKind::BitOr => Some("std::ops::BitOr"),
370 hir::BinOpKind::Shl => Some("std::ops::Shl"),
371 hir::BinOpKind::Shr => Some("std::ops::Shr"),
373 hir::BinOpKind::Ne => Some("std::cmp::PartialEq"),
377 hir::BinOpKind::Ge => Some("std::cmp::PartialOrd"),
380 if let Some(missing_trait) = missing_trait {
381 if op.node == hir::BinOpKind::Add &&
382 self.check_str_addition(expr, lhs_expr, rhs_expr, lhs_ty,
383 rhs_ty, &mut err, false) {
384 // This has nothing here because it means we did string
385 // concatenation (e.g., "Hello " + "World!"). This means
386 // we don't want the note in the else clause to be emitted
387 } else if let ty::Param(_) = lhs_ty.sty {
388 // FIXME: point to span of param
390 "`{}` might need a bound for `{}`",
391 lhs_ty, missing_trait
393 } else if !suggested_deref {
395 "an implementation of `{}` might \
396 be missing for `{}`",
397 missing_trait, lhs_ty
409 (lhs_ty, rhs_ty, return_ty)
412 fn check_str_addition(
414 expr: &'gcx hir::Expr,
415 lhs_expr: &'gcx hir::Expr,
416 rhs_expr: &'gcx hir::Expr,
419 err: &mut errors::DiagnosticBuilder,
422 let source_map = self.tcx.sess.source_map();
423 let msg = "`to_owned()` can be used to create an owned `String` \
424 from a string reference. String concatenation \
425 appends the string on the right to the string \
426 on the left and may require reallocation. This \
427 requires ownership of the string on the left";
428 // If this function returns true it means a note was printed, so we don't need
429 // to print the normal "implementation of `std::ops::Add` might be missing" note
430 match (&lhs_ty.sty, &rhs_ty.sty) {
431 (&Ref(_, l_ty, _), &Ref(_, r_ty, _))
432 if l_ty.sty == Str && r_ty.sty == Str => {
434 err.span_label(expr.span,
435 "`+` can't be used to concatenate two `&str` strings");
436 match source_map.span_to_snippet(lhs_expr.span) {
437 Ok(lstring) => err.span_suggestion(
440 format!("{}.to_owned()", lstring),
441 Applicability::MachineApplicable,
448 (&Ref(_, l_ty, _), &Adt(..))
449 if l_ty.sty == Str && &format!("{:?}", rhs_ty) == "std::string::String" => {
450 err.span_label(expr.span,
451 "`+` can't be used to concatenate a `&str` with a `String`");
453 source_map.span_to_snippet(lhs_expr.span),
454 source_map.span_to_snippet(rhs_expr.span),
457 (Ok(l), Ok(r), false) => {
458 err.multipart_suggestion(
461 (lhs_expr.span, format!("{}.to_owned()", l)),
462 (rhs_expr.span, format!("&{}", r)),
464 Applicability::MachineApplicable,
477 pub fn check_user_unop(&self,
479 operand_ty: Ty<'tcx>,
483 assert!(op.is_by_value());
484 match self.lookup_op_method(operand_ty, &[], Op::Unary(op, ex.span)) {
486 self.write_method_call(ex.hir_id, method);
490 let actual = self.resolve_type_vars_if_possible(&operand_ty);
491 if !actual.references_error() {
492 let mut err = struct_span_err!(self.tcx.sess, ex.span, E0600,
493 "cannot apply unary operator `{}` to type `{}`",
494 op.as_str(), actual);
495 err.span_label(ex.span, format!("cannot apply unary \
496 operator `{}`", op.as_str()));
498 Uint(_) if op == hir::UnNeg => {
499 err.note("unsigned values cannot be negated");
501 Str | Never | Char | Tuple(_) | Array(_,_) => {},
502 Ref(_, ref lty, _) if lty.sty == Str => {},
504 let missing_trait = match op {
505 hir::UnNeg => "std::ops::Neg",
506 hir::UnNot => "std::ops::Not",
507 hir::UnDeref => "std::ops::UnDerf"
509 err.note(&format!("an implementation of `{}` might \
510 be missing for `{}`",
511 missing_trait, operand_ty));
521 fn lookup_op_method(&self, lhs_ty: Ty<'tcx>, other_tys: &[Ty<'tcx>], op: Op)
522 -> Result<MethodCallee<'tcx>, ()>
524 let lang = self.tcx.lang_items();
526 let span = match op {
527 Op::Binary(op, _) => op.span,
528 Op::Unary(_, span) => span
530 let (opname, trait_did) = if let Op::Binary(op, IsAssign::Yes) = op {
532 hir::BinOpKind::Add => ("add_assign", lang.add_assign_trait()),
533 hir::BinOpKind::Sub => ("sub_assign", lang.sub_assign_trait()),
534 hir::BinOpKind::Mul => ("mul_assign", lang.mul_assign_trait()),
535 hir::BinOpKind::Div => ("div_assign", lang.div_assign_trait()),
536 hir::BinOpKind::Rem => ("rem_assign", lang.rem_assign_trait()),
537 hir::BinOpKind::BitXor => ("bitxor_assign", lang.bitxor_assign_trait()),
538 hir::BinOpKind::BitAnd => ("bitand_assign", lang.bitand_assign_trait()),
539 hir::BinOpKind::BitOr => ("bitor_assign", lang.bitor_assign_trait()),
540 hir::BinOpKind::Shl => ("shl_assign", lang.shl_assign_trait()),
541 hir::BinOpKind::Shr => ("shr_assign", lang.shr_assign_trait()),
542 hir::BinOpKind::Lt | hir::BinOpKind::Le |
543 hir::BinOpKind::Ge | hir::BinOpKind::Gt |
544 hir::BinOpKind::Eq | hir::BinOpKind::Ne |
545 hir::BinOpKind::And | hir::BinOpKind::Or => {
547 "impossible assignment operation: {}=",
551 } else if let Op::Binary(op, IsAssign::No) = op {
553 hir::BinOpKind::Add => ("add", lang.add_trait()),
554 hir::BinOpKind::Sub => ("sub", lang.sub_trait()),
555 hir::BinOpKind::Mul => ("mul", lang.mul_trait()),
556 hir::BinOpKind::Div => ("div", lang.div_trait()),
557 hir::BinOpKind::Rem => ("rem", lang.rem_trait()),
558 hir::BinOpKind::BitXor => ("bitxor", lang.bitxor_trait()),
559 hir::BinOpKind::BitAnd => ("bitand", lang.bitand_trait()),
560 hir::BinOpKind::BitOr => ("bitor", lang.bitor_trait()),
561 hir::BinOpKind::Shl => ("shl", lang.shl_trait()),
562 hir::BinOpKind::Shr => ("shr", lang.shr_trait()),
563 hir::BinOpKind::Lt => ("lt", lang.partial_ord_trait()),
564 hir::BinOpKind::Le => ("le", lang.partial_ord_trait()),
565 hir::BinOpKind::Ge => ("ge", lang.partial_ord_trait()),
566 hir::BinOpKind::Gt => ("gt", lang.partial_ord_trait()),
567 hir::BinOpKind::Eq => ("eq", lang.eq_trait()),
568 hir::BinOpKind::Ne => ("ne", lang.eq_trait()),
569 hir::BinOpKind::And | hir::BinOpKind::Or => {
570 span_bug!(span, "&& and || are not overloadable")
573 } else if let Op::Unary(hir::UnNot, _) = op {
574 ("not", lang.not_trait())
575 } else if let Op::Unary(hir::UnNeg, _) = op {
576 ("neg", lang.neg_trait())
578 bug!("lookup_op_method: op not supported: {:?}", op)
581 debug!("lookup_op_method(lhs_ty={:?}, op={:?}, opname={:?}, trait_did={:?})",
587 let method = trait_did.and_then(|trait_did| {
588 let opname = Ident::from_str(opname);
589 self.lookup_method_in_trait(span, opname, trait_did, lhs_ty, Some(other_tys))
594 let method = self.register_infer_ok_obligations(ok);
595 self.select_obligations_where_possible(false);
606 // Binary operator categories. These categories summarize the behavior
607 // with respect to the builtin operationrs supported.
609 /// &&, || -- cannot be overridden
612 /// <<, >> -- when shifting a single integer, rhs can be any
613 /// integer type. For simd, types must match.
616 /// +, -, etc -- takes equal types, produces same type as input,
617 /// applicable to ints/floats/simd
620 /// &, |, ^ -- takes equal types, produces same type as input,
621 /// applicable to ints/floats/simd/bool
624 /// ==, !=, etc -- takes equal types, produces bools, except for simd,
625 /// which produce the input type
630 fn from(op: hir::BinOp) -> BinOpCategory {
632 hir::BinOpKind::Shl | hir::BinOpKind::Shr =>
633 BinOpCategory::Shift,
635 hir::BinOpKind::Add |
636 hir::BinOpKind::Sub |
637 hir::BinOpKind::Mul |
638 hir::BinOpKind::Div |
639 hir::BinOpKind::Rem =>
642 hir::BinOpKind::BitXor |
643 hir::BinOpKind::BitAnd |
644 hir::BinOpKind::BitOr =>
645 BinOpCategory::Bitwise,
652 hir::BinOpKind::Gt =>
653 BinOpCategory::Comparison,
655 hir::BinOpKind::And |
656 hir::BinOpKind::Or =>
657 BinOpCategory::Shortcircuit,
662 /// Whether the binary operation is an assignment (`a += b`), or not (`a + b`)
663 #[derive(Clone, Copy, Debug, PartialEq)]
669 #[derive(Clone, Copy, Debug)]
671 Binary(hir::BinOp, IsAssign),
672 Unary(hir::UnOp, Span),
675 /// Returns `true` if this is a built-in arithmetic operation (e.g., u32
676 /// + u32, i16x4 == i16x4) and false if these types would have to be
677 /// overloaded to be legal. There are two reasons that we distinguish
678 /// builtin operations from overloaded ones (vs trying to drive
679 /// everything uniformly through the trait system and intrinsics or
680 /// something like that):
682 /// 1. Builtin operations can trivially be evaluated in constants.
683 /// 2. For comparison operators applied to SIMD types the result is
684 /// not of type `bool`. For example, `i16x4 == i16x4` yields a
685 /// type like `i16x4`. This means that the overloaded trait
686 /// `PartialEq` is not applicable.
688 /// Reason #2 is the killer. I tried for a while to always use
689 /// overloaded logic and just check the types in constants/codegen after
690 /// the fact, and it worked fine, except for SIMD types. -nmatsakis
691 fn is_builtin_binop(lhs: Ty, rhs: Ty, op: hir::BinOp) -> bool {
692 match BinOpCategory::from(op) {
693 BinOpCategory::Shortcircuit => {
697 BinOpCategory::Shift => {
698 lhs.references_error() || rhs.references_error() ||
699 lhs.is_integral() && rhs.is_integral()
702 BinOpCategory::Math => {
703 lhs.references_error() || rhs.references_error() ||
704 lhs.is_integral() && rhs.is_integral() ||
705 lhs.is_floating_point() && rhs.is_floating_point()
708 BinOpCategory::Bitwise => {
709 lhs.references_error() || rhs.references_error() ||
710 lhs.is_integral() && rhs.is_integral() ||
711 lhs.is_floating_point() && rhs.is_floating_point() ||
712 lhs.is_bool() && rhs.is_bool()
715 BinOpCategory::Comparison => {
716 lhs.references_error() || rhs.references_error() ||
717 lhs.is_scalar() && rhs.is_scalar()