]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/op.rs
Auto merge of #30036 - mitaa:doc_id, r=alexcrichton
[rust.git] / src / librustc_typeck / check / op.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Code related to processing overloaded binary and unary operators.
12
13 use super::{
14     check_expr,
15     check_expr_coercable_to_type,
16     check_expr_with_lvalue_pref,
17     demand,
18     method,
19     FnCtxt,
20 };
21 use middle::def_id::DefId;
22 use middle::ty::{Ty, HasTypeFlags, PreferMutLvalue};
23 use syntax::ast;
24 use syntax::parse::token;
25 use rustc_front::hir;
26 use rustc_front::util as hir_util;
27
28 /// Check a `a <op>= b`
29 pub fn check_binop_assign<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
30                                    expr: &'tcx hir::Expr,
31                                    op: hir::BinOp,
32                                    lhs_expr: &'tcx hir::Expr,
33                                    rhs_expr: &'tcx hir::Expr)
34 {
35     check_expr_with_lvalue_pref(fcx, lhs_expr, PreferMutLvalue);
36
37     let lhs_ty = fcx.resolve_type_vars_if_possible(fcx.expr_ty(lhs_expr));
38     let (rhs_ty, return_ty) =
39         check_overloaded_binop(fcx, expr, lhs_expr, lhs_ty, rhs_expr, op, IsAssign::Yes);
40     let rhs_ty = fcx.resolve_type_vars_if_possible(rhs_ty);
41
42     if !lhs_ty.is_ty_var() && !rhs_ty.is_ty_var() && is_builtin_binop(lhs_ty, rhs_ty, op) {
43         enforce_builtin_binop_types(fcx, lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
44         fcx.write_nil(expr.id);
45     } else {
46         fcx.write_ty(expr.id, return_ty);
47     }
48
49     let tcx = fcx.tcx();
50     if !tcx.expr_is_lval(lhs_expr) {
51         span_err!(tcx.sess, lhs_expr.span, E0067, "invalid left-hand side expression");
52     }
53 }
54
55 /// Check a potentially overloaded binary operator.
56 pub fn check_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
57                              expr: &'tcx hir::Expr,
58                              op: hir::BinOp,
59                              lhs_expr: &'tcx hir::Expr,
60                              rhs_expr: &'tcx hir::Expr)
61 {
62     let tcx = fcx.ccx.tcx;
63
64     debug!("check_binop(expr.id={}, expr={:?}, op={:?}, lhs_expr={:?}, rhs_expr={:?})",
65            expr.id,
66            expr,
67            op,
68            lhs_expr,
69            rhs_expr);
70
71     check_expr(fcx, lhs_expr);
72     let lhs_ty = fcx.resolve_type_vars_if_possible(fcx.expr_ty(lhs_expr));
73
74     match BinOpCategory::from(op) {
75         BinOpCategory::Shortcircuit => {
76             // && and || are a simple case.
77             demand::suptype(fcx, lhs_expr.span, tcx.mk_bool(), lhs_ty);
78             check_expr_coercable_to_type(fcx, rhs_expr, tcx.mk_bool());
79             fcx.write_ty(expr.id, tcx.mk_bool());
80         }
81         _ => {
82             // Otherwise, we always treat operators as if they are
83             // overloaded. This is the way to be most flexible w/r/t
84             // types that get inferred.
85             let (rhs_ty, return_ty) =
86                 check_overloaded_binop(fcx, expr, lhs_expr, lhs_ty, rhs_expr, op, IsAssign::No);
87
88             // Supply type inference hints if relevant. Probably these
89             // hints should be enforced during select as part of the
90             // `consider_unification_despite_ambiguity` routine, but this
91             // more convenient for now.
92             //
93             // The basic idea is to help type inference by taking
94             // advantage of things we know about how the impls for
95             // scalar types are arranged. This is important in a
96             // scenario like `1_u32 << 2`, because it lets us quickly
97             // deduce that the result type should be `u32`, even
98             // though we don't know yet what type 2 has and hence
99             // can't pin this down to a specific impl.
100             let rhs_ty = fcx.resolve_type_vars_if_possible(rhs_ty);
101             if
102                 !lhs_ty.is_ty_var() && !rhs_ty.is_ty_var() &&
103                 is_builtin_binop(lhs_ty, rhs_ty, op)
104             {
105                 let builtin_return_ty =
106                     enforce_builtin_binop_types(fcx, lhs_expr, lhs_ty, rhs_expr, rhs_ty, op);
107                 demand::suptype(fcx, expr.span, builtin_return_ty, return_ty);
108             }
109
110             fcx.write_ty(expr.id, return_ty);
111         }
112     }
113 }
114
115 fn enforce_builtin_binop_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
116                                          lhs_expr: &'tcx hir::Expr,
117                                          lhs_ty: Ty<'tcx>,
118                                          rhs_expr: &'tcx hir::Expr,
119                                          rhs_ty: Ty<'tcx>,
120                                          op: hir::BinOp)
121                                          -> Ty<'tcx>
122 {
123     debug_assert!(is_builtin_binop(lhs_ty, rhs_ty, op));
124
125     let tcx = fcx.tcx();
126     match BinOpCategory::from(op) {
127         BinOpCategory::Shortcircuit => {
128             demand::suptype(fcx, lhs_expr.span, tcx.mk_bool(), lhs_ty);
129             demand::suptype(fcx, rhs_expr.span, tcx.mk_bool(), rhs_ty);
130             tcx.mk_bool()
131         }
132
133         BinOpCategory::Shift => {
134             // result type is same as LHS always
135             lhs_ty
136         }
137
138         BinOpCategory::Math |
139         BinOpCategory::Bitwise => {
140             // both LHS and RHS and result will have the same type
141             demand::suptype(fcx, rhs_expr.span, lhs_ty, rhs_ty);
142             lhs_ty
143         }
144
145         BinOpCategory::Comparison => {
146             // both LHS and RHS and result will have the same type
147             demand::suptype(fcx, rhs_expr.span, lhs_ty, rhs_ty);
148             tcx.mk_bool()
149         }
150     }
151 }
152
153 fn check_overloaded_binop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
154                                     expr: &'tcx hir::Expr,
155                                     lhs_expr: &'tcx hir::Expr,
156                                     lhs_ty: Ty<'tcx>,
157                                     rhs_expr: &'tcx hir::Expr,
158                                     op: hir::BinOp,
159                                     is_assign: IsAssign)
160                                     -> (Ty<'tcx>, Ty<'tcx>)
161 {
162     debug!("check_overloaded_binop(expr.id={}, lhs_ty={:?}, is_assign={:?})",
163            expr.id,
164            lhs_ty,
165            is_assign);
166
167     let (name, trait_def_id) = name_and_trait_def_id(fcx, op, is_assign);
168
169     // NB: As we have not yet type-checked the RHS, we don't have the
170     // type at hand. Make a variable to represent it. The whole reason
171     // for this indirection is so that, below, we can check the expr
172     // using this variable as the expected type, which sometimes lets
173     // us do better coercions than we would be able to do otherwise,
174     // particularly for things like `String + &String`.
175     let rhs_ty_var = fcx.infcx().next_ty_var();
176
177     let return_ty = match lookup_op_method(fcx, expr, lhs_ty, vec![rhs_ty_var],
178                                            token::intern(name), trait_def_id,
179                                            lhs_expr) {
180         Ok(return_ty) => return_ty,
181         Err(()) => {
182             // error types are considered "builtin"
183             if !lhs_ty.references_error() {
184                 if let IsAssign::Yes = is_assign {
185                     span_err!(fcx.tcx().sess, lhs_expr.span, E0368,
186                               "binary assignment operation `{}=` cannot be applied to type `{}`",
187                               hir_util::binop_to_string(op.node),
188                               lhs_ty);
189                 } else {
190                     span_err!(fcx.tcx().sess, lhs_expr.span, E0369,
191                               "binary operation `{}` cannot be applied to type `{}`",
192                               hir_util::binop_to_string(op.node),
193                               lhs_ty);
194                     let missing_trait = match op.node {
195                         hir::BiAdd    => Some("std::ops::Add"),
196                         hir::BiSub    => Some("std::ops::Sub"),
197                         hir::BiMul    => Some("std::ops::Mul"),
198                         hir::BiDiv    => Some("std::ops::Div"),
199                         hir::BiRem    => Some("std::ops::Rem"),
200                         hir::BiBitAnd => Some("std::ops::BitAnd"),
201                         hir::BiBitOr  => Some("std::ops::BitOr"),
202                         hir::BiShl    => Some("std::ops::Shl"),
203                         hir::BiShr    => Some("std::ops::Shr"),
204                         hir::BiEq | hir::BiNe => Some("std::cmp::PartialEq"),
205                         hir::BiLt | hir::BiLe | hir::BiGt | hir::BiGe =>
206                             Some("std::cmp::PartialOrd"),
207                         _             => None
208                     };
209
210                     if let Some(missing_trait) = missing_trait {
211                         span_note!(fcx.tcx().sess, lhs_expr.span,
212                                    "an implementation of `{}` might be missing for `{}`",
213                                     missing_trait, lhs_ty);
214                     }
215                 }
216             }
217             fcx.tcx().types.err
218         }
219     };
220
221     // see `NB` above
222     check_expr_coercable_to_type(fcx, rhs_expr, rhs_ty_var);
223
224     (rhs_ty_var, return_ty)
225 }
226
227 pub fn check_user_unop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
228                                  op_str: &str,
229                                  mname: &str,
230                                  trait_did: Option<DefId>,
231                                  ex: &'tcx hir::Expr,
232                                  operand_expr: &'tcx hir::Expr,
233                                  operand_ty: Ty<'tcx>,
234                                  op: hir::UnOp)
235                                  -> Ty<'tcx>
236 {
237     assert!(hir_util::is_by_value_unop(op));
238     match lookup_op_method(fcx, ex, operand_ty, vec![],
239                            token::intern(mname), trait_did,
240                            operand_expr) {
241         Ok(t) => t,
242         Err(()) => {
243             fcx.type_error_message(ex.span, |actual| {
244                 format!("cannot apply unary operator `{}` to type `{}`",
245                         op_str, actual)
246             }, operand_ty, None);
247             fcx.tcx().types.err
248         }
249     }
250 }
251
252 fn name_and_trait_def_id(fcx: &FnCtxt,
253                          op: hir::BinOp,
254                          is_assign: IsAssign)
255                          -> (&'static str, Option<DefId>) {
256     let lang = &fcx.tcx().lang_items;
257
258     if let IsAssign::Yes = is_assign {
259         match op.node {
260             hir::BiAdd => ("add_assign", lang.add_assign_trait()),
261             hir::BiSub => ("sub_assign", lang.sub_assign_trait()),
262             hir::BiMul => ("mul_assign", lang.mul_assign_trait()),
263             hir::BiDiv => ("div_assign", lang.div_assign_trait()),
264             hir::BiRem => ("rem_assign", lang.rem_assign_trait()),
265             hir::BiBitXor => ("bitxor_assign", lang.bitxor_assign_trait()),
266             hir::BiBitAnd => ("bitand_assign", lang.bitand_assign_trait()),
267             hir::BiBitOr => ("bitor_assign", lang.bitor_assign_trait()),
268             hir::BiShl => ("shl_assign", lang.shl_assign_trait()),
269             hir::BiShr => ("shr_assign", lang.shr_assign_trait()),
270             hir::BiLt | hir::BiLe | hir::BiGe | hir::BiGt | hir::BiEq | hir::BiNe | hir::BiAnd |
271             hir::BiOr => {
272                 fcx.tcx().sess.span_bug(op.span, &format!("impossible assignment operation: {}=",
273                                         hir_util::binop_to_string(op.node)))
274             }
275         }
276     } else {
277         match op.node {
278             hir::BiAdd => ("add", lang.add_trait()),
279             hir::BiSub => ("sub", lang.sub_trait()),
280             hir::BiMul => ("mul", lang.mul_trait()),
281             hir::BiDiv => ("div", lang.div_trait()),
282             hir::BiRem => ("rem", lang.rem_trait()),
283             hir::BiBitXor => ("bitxor", lang.bitxor_trait()),
284             hir::BiBitAnd => ("bitand", lang.bitand_trait()),
285             hir::BiBitOr => ("bitor", lang.bitor_trait()),
286             hir::BiShl => ("shl", lang.shl_trait()),
287             hir::BiShr => ("shr", lang.shr_trait()),
288             hir::BiLt => ("lt", lang.ord_trait()),
289             hir::BiLe => ("le", lang.ord_trait()),
290             hir::BiGe => ("ge", lang.ord_trait()),
291             hir::BiGt => ("gt", lang.ord_trait()),
292             hir::BiEq => ("eq", lang.eq_trait()),
293             hir::BiNe => ("ne", lang.eq_trait()),
294             hir::BiAnd | hir::BiOr => {
295                 fcx.tcx().sess.span_bug(op.span, "&& and || are not overloadable")
296             }
297         }
298     }
299 }
300
301 fn lookup_op_method<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
302                               expr: &'tcx hir::Expr,
303                               lhs_ty: Ty<'tcx>,
304                               other_tys: Vec<Ty<'tcx>>,
305                               opname: ast::Name,
306                               trait_did: Option<DefId>,
307                               lhs_expr: &'a hir::Expr)
308                               -> Result<Ty<'tcx>,()>
309 {
310     debug!("lookup_op_method(expr={:?}, lhs_ty={:?}, opname={:?}, trait_did={:?}, lhs_expr={:?})",
311            expr,
312            lhs_ty,
313            opname,
314            trait_did,
315            lhs_expr);
316
317     let method = match trait_did {
318         Some(trait_did) => {
319             method::lookup_in_trait_adjusted(fcx,
320                                              expr.span,
321                                              Some(lhs_expr),
322                                              opname,
323                                              trait_did,
324                                              0,
325                                              false,
326                                              lhs_ty,
327                                              Some(other_tys))
328         }
329         None => None
330     };
331
332     match method {
333         Some(method) => {
334             let method_ty = method.ty;
335
336             // HACK(eddyb) Fully qualified path to work around a resolve bug.
337             let method_call = ::middle::ty::MethodCall::expr(expr.id);
338             fcx.inh.tables.borrow_mut().method_map.insert(method_call, method);
339
340             // extract return type for method; all late bound regions
341             // should have been instantiated by now
342             let ret_ty = method_ty.fn_ret();
343             Ok(fcx.tcx().no_late_bound_regions(&ret_ty).unwrap().unwrap())
344         }
345         None => {
346             Err(())
347         }
348     }
349 }
350
351 // Binary operator categories. These categories summarize the behavior
352 // with respect to the builtin operationrs supported.
353 enum BinOpCategory {
354     /// &&, || -- cannot be overridden
355     Shortcircuit,
356
357     /// <<, >> -- when shifting a single integer, rhs can be any
358     /// integer type. For simd, types must match.
359     Shift,
360
361     /// +, -, etc -- takes equal types, produces same type as input,
362     /// applicable to ints/floats/simd
363     Math,
364
365     /// &, |, ^ -- takes equal types, produces same type as input,
366     /// applicable to ints/floats/simd/bool
367     Bitwise,
368
369     /// ==, !=, etc -- takes equal types, produces bools, except for simd,
370     /// which produce the input type
371     Comparison,
372 }
373
374 impl BinOpCategory {
375     fn from(op: hir::BinOp) -> BinOpCategory {
376         match op.node {
377             hir::BiShl | hir::BiShr =>
378                 BinOpCategory::Shift,
379
380             hir::BiAdd |
381             hir::BiSub |
382             hir::BiMul |
383             hir::BiDiv |
384             hir::BiRem =>
385                 BinOpCategory::Math,
386
387             hir::BiBitXor |
388             hir::BiBitAnd |
389             hir::BiBitOr =>
390                 BinOpCategory::Bitwise,
391
392             hir::BiEq |
393             hir::BiNe |
394             hir::BiLt |
395             hir::BiLe |
396             hir::BiGe |
397             hir::BiGt =>
398                 BinOpCategory::Comparison,
399
400             hir::BiAnd |
401             hir::BiOr =>
402                 BinOpCategory::Shortcircuit,
403         }
404     }
405 }
406
407 /// Whether the binary operation is an assignment (`a += b`), or not (`a + b`)
408 #[derive(Clone, Copy, Debug)]
409 enum IsAssign {
410     No,
411     Yes,
412 }
413
414 /// Returns true if this is a built-in arithmetic operation (e.g. u32
415 /// + u32, i16x4 == i16x4) and false if these types would have to be
416 /// overloaded to be legal. There are two reasons that we distinguish
417 /// builtin operations from overloaded ones (vs trying to drive
418 /// everything uniformly through the trait system and intrinsics or
419 /// something like that):
420 ///
421 /// 1. Builtin operations can trivially be evaluated in constants.
422 /// 2. For comparison operators applied to SIMD types the result is
423 ///    not of type `bool`. For example, `i16x4==i16x4` yields a
424 ///    type like `i16x4`. This means that the overloaded trait
425 ///    `PartialEq` is not applicable.
426 ///
427 /// Reason #2 is the killer. I tried for a while to always use
428 /// overloaded logic and just check the types in constants/trans after
429 /// the fact, and it worked fine, except for SIMD types. -nmatsakis
430 fn is_builtin_binop<'tcx>(lhs: Ty<'tcx>,
431                           rhs: Ty<'tcx>,
432                           op: hir::BinOp)
433                           -> bool
434 {
435     match BinOpCategory::from(op) {
436         BinOpCategory::Shortcircuit => {
437             true
438         }
439
440         BinOpCategory::Shift => {
441             lhs.references_error() || rhs.references_error() ||
442                 lhs.is_integral() && rhs.is_integral()
443         }
444
445         BinOpCategory::Math => {
446             lhs.references_error() || rhs.references_error() ||
447                 lhs.is_integral() && rhs.is_integral() ||
448                 lhs.is_floating_point() && rhs.is_floating_point()
449         }
450
451         BinOpCategory::Bitwise => {
452             lhs.references_error() || rhs.references_error() ||
453                 lhs.is_integral() && rhs.is_integral() ||
454                 lhs.is_floating_point() && rhs.is_floating_point() ||
455                 lhs.is_bool() && rhs.is_bool()
456         }
457
458         BinOpCategory::Comparison => {
459             lhs.references_error() || rhs.references_error() ||
460                 lhs.is_scalar() && rhs.is_scalar()
461         }
462     }
463 }