]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/demand.rs
d985bdae491d09d6d2dd40f47a648b632cce65cb
[rust.git] / src / librustc_typeck / check / demand.rs
1 use check::FnCtxt;
2 use rustc::infer::InferOk;
3 use rustc::traits::{ObligationCause, ObligationCauseCode};
4
5 use syntax::ast;
6 use syntax::util::parser::PREC_POSTFIX;
7 use syntax_pos::Span;
8 use rustc::hir;
9 use rustc::hir::def::Def;
10 use rustc::hir::Node;
11 use rustc::hir::{Item, ItemKind, print};
12 use rustc::ty::{self, Ty, AssociatedItem};
13 use rustc::ty::adjustment::AllowTwoPhase;
14 use errors::{Applicability, DiagnosticBuilder, SourceMapper};
15
16 use super::method::probe;
17
18 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
19     // Requires that the two types unify, and prints an error message if
20     // they don't.
21     pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
22         self.demand_suptype_diag(sp, expected, actual).map(|mut e| e.emit());
23     }
24
25     pub fn demand_suptype_diag(&self,
26                                sp: Span,
27                                expected: Ty<'tcx>,
28                                actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
29         let cause = &self.misc(sp);
30         match self.at(cause, self.param_env).sup(expected, actual) {
31             Ok(InferOk { obligations, value: () }) => {
32                 self.register_predicates(obligations);
33                 None
34             },
35             Err(e) => {
36                 Some(self.report_mismatched_types(&cause, expected, actual, e))
37             }
38         }
39     }
40
41     pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
42         if let Some(mut err) = self.demand_eqtype_diag(sp, expected, actual) {
43             err.emit();
44         }
45     }
46
47     pub fn demand_eqtype_diag(&self,
48                              sp: Span,
49                              expected: Ty<'tcx>,
50                              actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
51         self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
52     }
53
54     pub fn demand_eqtype_with_origin(&self,
55                                      cause: &ObligationCause<'tcx>,
56                                      expected: Ty<'tcx>,
57                                      actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
58         match self.at(cause, self.param_env).eq(expected, actual) {
59             Ok(InferOk { obligations, value: () }) => {
60                 self.register_predicates(obligations);
61                 None
62             }
63             Err(e) => {
64                 Some(self.report_mismatched_types(cause, expected, actual, e))
65             }
66         }
67     }
68
69     pub fn demand_eqtype_pat(
70         &self,
71         cause_span: Span,
72         expected: Ty<'tcx>,
73         actual: Ty<'tcx>,
74         match_expr_span: Option<Span>,
75     ) {
76         let cause = if let Some(span) = match_expr_span {
77             self.cause(
78                 cause_span,
79                 ObligationCauseCode::MatchExpressionArmPattern { span, ty: expected },
80             )
81         } else {
82             self.misc(cause_span)
83         };
84         self.demand_eqtype_with_origin(&cause, expected, actual).map(|mut err| err.emit());
85     }
86
87
88     pub fn demand_coerce(&self,
89                          expr: &hir::Expr,
90                          checked_ty: Ty<'tcx>,
91                          expected: Ty<'tcx>,
92                          allow_two_phase: AllowTwoPhase)
93                          -> Ty<'tcx> {
94         let (ty, err) = self.demand_coerce_diag(expr, checked_ty, expected, allow_two_phase);
95         if let Some(mut err) = err {
96             err.emit();
97         }
98         ty
99     }
100
101     // Checks that the type of `expr` can be coerced to `expected`.
102     //
103     // N.B., this code relies on `self.diverges` to be accurate. In
104     // particular, assignments to `!` will be permitted if the
105     // diverges flag is currently "always".
106     pub fn demand_coerce_diag(&self,
107                               expr: &hir::Expr,
108                               checked_ty: Ty<'tcx>,
109                               expected: Ty<'tcx>,
110                               allow_two_phase: AllowTwoPhase)
111                               -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
112         let expected = self.resolve_type_vars_with_obligations(expected);
113
114         let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
115             Ok(ty) => return (ty, None),
116             Err(e) => e
117         };
118
119         let cause = self.misc(expr.span);
120         let expr_ty = self.resolve_type_vars_with_obligations(checked_ty);
121         let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
122
123         // If the expected type is an enum (Issue #55250) with any variants whose
124         // sole field is of the found type, suggest such variants. (Issue #42764)
125         if let ty::Adt(expected_adt, substs) = expected.sty {
126             if expected_adt.is_enum() {
127                 let mut compatible_variants = expected_adt.variants
128                     .iter()
129                     .filter(|variant| variant.fields.len() == 1)
130                     .filter_map(|variant| {
131                         let sole_field = &variant.fields[0];
132                         let sole_field_ty = sole_field.ty(self.tcx, substs);
133                         if self.can_coerce(expr_ty, sole_field_ty) {
134                             let variant_path = self.tcx.item_path_str(variant.did);
135                             // FIXME #56861: DRYer prelude filtering
136                             Some(variant_path.trim_start_matches("std::prelude::v1::").to_string())
137                         } else {
138                             None
139                         }
140                     }).peekable();
141
142                 if compatible_variants.peek().is_some() {
143                     let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
144                     let suggestions = compatible_variants
145                         .map(|v| format!("{}({})", v, expr_text));
146                     err.span_suggestions_with_applicability(
147                         expr.span,
148                         "try using a variant of the expected type",
149                         suggestions,
150                         Applicability::MaybeIncorrect,
151                     );
152                 }
153             }
154         }
155
156         self.suggest_ref_or_into(&mut err, expr, expected, expr_ty);
157
158         (expected, Some(err))
159     }
160
161     pub fn get_conversion_methods(&self, span: Span, expected: Ty<'tcx>, checked_ty: Ty<'tcx>)
162                               -> Vec<AssociatedItem> {
163         let mut methods = self.probe_for_return_type(span,
164                                                      probe::Mode::MethodCall,
165                                                      expected,
166                                                      checked_ty,
167                                                      ast::DUMMY_NODE_ID);
168         methods.retain(|m| {
169             self.has_no_input_arg(m) &&
170                 self.tcx.get_attrs(m.def_id).iter()
171                 // This special internal attribute is used to whitelist
172                 // "identity-like" conversion methods to be suggested here.
173                 //
174                 // FIXME (#46459 and #46460): ideally
175                 // `std::convert::Into::into` and `std::borrow:ToOwned` would
176                 // also be `#[rustc_conversion_suggestion]`, if not for
177                 // method-probing false-positives and -negatives (respectively).
178                 //
179                 // FIXME? Other potential candidate methods: `as_ref` and
180                 // `as_mut`?
181                 .find(|a| a.check_name("rustc_conversion_suggestion")).is_some()
182         });
183
184         methods
185     }
186
187     // This function checks if the method isn't static and takes other arguments than `self`.
188     fn has_no_input_arg(&self, method: &AssociatedItem) -> bool {
189         match method.def() {
190             Def::Method(def_id) => {
191                 self.tcx.fn_sig(def_id).inputs().skip_binder().len() == 1
192             }
193             _ => false,
194         }
195     }
196
197     /// Identify some cases where `as_ref()` would be appropriate and suggest it.
198     ///
199     /// Given the following code:
200     /// ```
201     /// struct Foo;
202     /// fn takes_ref(_: &Foo) {}
203     /// let ref opt = Some(Foo);
204     ///
205     /// opt.map(|arg| takes_ref(arg));
206     /// ```
207     /// Suggest using `opt.as_ref().map(|arg| takes_ref(arg));` instead.
208     ///
209     /// It only checks for `Option` and `Result` and won't work with
210     /// ```
211     /// opt.map(|arg| { takes_ref(arg) });
212     /// ```
213     fn can_use_as_ref(&self, expr: &hir::Expr) -> Option<(Span, &'static str, String)> {
214         if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
215             if let hir::def::Def::Local(id) = path.def {
216                 let parent = self.tcx.hir().get_parent_node(id);
217                 if let Some(Node::Expr(hir::Expr {
218                     id,
219                     node: hir::ExprKind::Closure(_, decl, ..),
220                     ..
221                 })) = self.tcx.hir().find(parent) {
222                     let parent = self.tcx.hir().get_parent_node(*id);
223                     if let (Some(Node::Expr(hir::Expr {
224                         node: hir::ExprKind::MethodCall(path, span, expr),
225                         ..
226                     })), 1) = (self.tcx.hir().find(parent), decl.inputs.len()) {
227                         let self_ty = self.tables.borrow().node_id_to_type(expr[0].hir_id);
228                         let self_ty = format!("{:?}", self_ty);
229                         let name = path.ident.as_str();
230                         let is_as_ref_able = (
231                             self_ty.starts_with("&std::option::Option") ||
232                             self_ty.starts_with("&std::result::Result") ||
233                             self_ty.starts_with("std::option::Option") ||
234                             self_ty.starts_with("std::result::Result")
235                         ) && (name == "map" || name == "and_then");
236                         if is_as_ref_able {
237                             return Some((span.shrink_to_lo(),
238                                          "consider using `as_ref` instead",
239                                          "as_ref().".into()));
240                         }
241                     }
242                 }
243             }
244         }
245         None
246     }
247
248     /// This function is used to determine potential "simple" improvements or users' errors and
249     /// provide them useful help. For example:
250     ///
251     /// ```
252     /// fn some_fn(s: &str) {}
253     ///
254     /// let x = "hey!".to_owned();
255     /// some_fn(x); // error
256     /// ```
257     ///
258     /// No need to find every potential function which could make a coercion to transform a
259     /// `String` into a `&str` since a `&` would do the trick!
260     ///
261     /// In addition of this check, it also checks between references mutability state. If the
262     /// expected is mutable but the provided isn't, maybe we could just say "Hey, try with
263     /// `&mut`!".
264     pub fn check_ref(&self,
265                  expr: &hir::Expr,
266                  checked_ty: Ty<'tcx>,
267                  expected: Ty<'tcx>)
268                  -> Option<(Span, &'static str, String)> {
269         let cm = self.sess().source_map();
270         // Use the callsite's span if this is a macro call. #41858
271         let sp = cm.call_span_if_macro(expr.span);
272         if !cm.span_to_filename(sp).is_real() {
273             return None;
274         }
275
276         match (&expected.sty, &checked_ty.sty) {
277             (&ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.sty, &check.sty) {
278                 (&ty::Str, &ty::Array(arr, _)) |
279                 (&ty::Str, &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
280                     if let hir::ExprKind::Lit(_) = expr.node {
281                         if let Ok(src) = cm.span_to_snippet(sp) {
282                             if src.starts_with("b\"") {
283                                 return Some((sp,
284                                              "consider removing the leading `b`",
285                                              src[1..].to_string()));
286                             }
287                         }
288                     }
289                 },
290                 (&ty::Array(arr, _), &ty::Str) |
291                 (&ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
292                     if let hir::ExprKind::Lit(_) = expr.node {
293                         if let Ok(src) = cm.span_to_snippet(sp) {
294                             if src.starts_with("\"") {
295                                 return Some((sp,
296                                              "consider adding a leading `b`",
297                                              format!("b{}", src)));
298                             }
299                         }
300                     }
301                 }
302                 _ => {}
303             },
304             (&ty::Ref(_, _, mutability), _) => {
305                 // Check if it can work when put into a ref. For example:
306                 //
307                 // ```
308                 // fn bar(x: &mut i32) {}
309                 //
310                 // let x = 0u32;
311                 // bar(&x); // error, expected &mut
312                 // ```
313                 let ref_ty = match mutability {
314                     hir::Mutability::MutMutable => self.tcx.mk_mut_ref(
315                                                        self.tcx.mk_region(ty::ReStatic),
316                                                        checked_ty),
317                     hir::Mutability::MutImmutable => self.tcx.mk_imm_ref(
318                                                        self.tcx.mk_region(ty::ReStatic),
319                                                        checked_ty),
320                 };
321                 if self.can_coerce(ref_ty, expected) {
322                     if let Ok(src) = cm.span_to_snippet(sp) {
323                         let needs_parens = match expr.node {
324                             // parenthesize if needed (Issue #46756)
325                             hir::ExprKind::Cast(_, _) |
326                             hir::ExprKind::Binary(_, _, _) => true,
327                             // parenthesize borrows of range literals (Issue #54505)
328                             _ if self.is_range_literal(expr) => true,
329                             _ => false,
330                         };
331                         let sugg_expr = if needs_parens {
332                             format!("({})", src)
333                         } else {
334                             src
335                         };
336
337                         if let Some(sugg) = self.can_use_as_ref(expr) {
338                             return Some(sugg);
339                         }
340                         return Some(match mutability {
341                             hir::Mutability::MutMutable => {
342                                 (sp, "consider mutably borrowing here", format!("&mut {}",
343                                                                                 sugg_expr))
344                             }
345                             hir::Mutability::MutImmutable => {
346                                 (sp, "consider borrowing here", format!("&{}", sugg_expr))
347                             }
348                         });
349                     }
350                 }
351             }
352             (_, &ty::Ref(_, checked, _)) => {
353                 // We have `&T`, check if what was expected was `T`. If so,
354                 // we may want to suggest adding a `*`, or removing
355                 // a `&`.
356                 //
357                 // (But, also check the `expn_info()` to see if this is
358                 // a macro; if so, it's hard to extract the text and make a good
359                 // suggestion, so don't bother.)
360                 if self.infcx.can_sub(self.param_env, checked, &expected).is_ok() &&
361                    sp.ctxt().outer().expn_info().is_none() {
362                     match expr.node {
363                         // Maybe remove `&`?
364                         hir::ExprKind::AddrOf(_, ref expr) => {
365                             if !cm.span_to_filename(expr.span).is_real() {
366                                 return None;
367                             }
368                             if let Ok(code) = cm.span_to_snippet(expr.span) {
369                                 return Some((sp, "consider removing the borrow", code));
370                             }
371                         }
372
373                         // Maybe add `*`? Only if `T: Copy`.
374                         _ => {
375                             if self.infcx.type_is_copy_modulo_regions(self.param_env,
376                                                                       checked,
377                                                                       sp) {
378                                 // do not suggest if the span comes from a macro (#52783)
379                                 if let (Ok(code),
380                                         true) = (cm.span_to_snippet(sp), sp == expr.span) {
381                                     return Some((
382                                         sp,
383                                         "consider dereferencing the borrow",
384                                         format!("*{}", code),
385                                     ));
386                                 }
387                             }
388                         }
389                     }
390                 }
391             }
392             _ => {}
393         }
394         None
395     }
396
397     /// This function checks if the specified expression is a built-in range literal.
398     /// (See: `LoweringContext::lower_expr()` in `src/librustc/hir/lowering.rs`).
399     fn is_range_literal(&self, expr: &hir::Expr) -> bool {
400         use hir::{Path, QPath, ExprKind, TyKind};
401
402         // We support `::std::ops::Range` and `::core::ops::Range` prefixes
403         let is_range_path = |path: &Path| {
404             let mut segs = path.segments.iter()
405                 .map(|seg| seg.ident.as_str());
406
407             if let (Some(root), Some(std_core), Some(ops), Some(range), None) =
408                 (segs.next(), segs.next(), segs.next(), segs.next(), segs.next())
409             {
410                 // "{{root}}" is the equivalent of `::` prefix in Path
411                 root == "{{root}}" && (std_core == "std" || std_core == "core")
412                     && ops == "ops" && range.starts_with("Range")
413             } else {
414                 false
415             }
416         };
417
418         let span_is_range_literal = |span: &Span| {
419             // Check whether a span corresponding to a range expression
420             // is a range literal, rather than an explicit struct or `new()` call.
421             let source_map = self.tcx.sess.source_map();
422             let end_point = source_map.end_point(*span);
423
424             if let Ok(end_string) = source_map.span_to_snippet(end_point) {
425                 !(end_string.ends_with("}") || end_string.ends_with(")"))
426             } else {
427                 false
428             }
429         };
430
431         match expr.node {
432             // All built-in range literals but `..=` and `..` desugar to Structs
433             ExprKind::Struct(QPath::Resolved(None, ref path), _, _) |
434             // `..` desugars to its struct path
435             ExprKind::Path(QPath::Resolved(None, ref path)) => {
436                 return is_range_path(&path) && span_is_range_literal(&expr.span);
437             }
438
439             // `..=` desugars into `::std::ops::RangeInclusive::new(...)`
440             ExprKind::Call(ref func, _) => {
441                 if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
442                     if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node {
443                         let call_to_new = segment.ident.as_str() == "new";
444
445                         return is_range_path(&path) && span_is_range_literal(&expr.span)
446                             && call_to_new;
447                     }
448                 }
449             }
450
451             _ => {}
452         }
453
454         false
455     }
456
457     pub fn check_for_cast(
458         &self,
459         err: &mut DiagnosticBuilder<'tcx>,
460         expr: &hir::Expr,
461         checked_ty: Ty<'tcx>,
462         expected_ty: Ty<'tcx>,
463     ) -> bool {
464         let parent_id = self.tcx.hir().get_parent_node(expr.id);
465         if let Some(parent) = self.tcx.hir().find(parent_id) {
466             // Shouldn't suggest `.into()` on `const`s.
467             if let Node::Item(Item { node: ItemKind::Const(_, _), .. }) = parent {
468                 // FIXME(estebank): modify once we decide to suggest `as` casts
469                 return false;
470             }
471         };
472
473         let will_truncate = "will truncate the source value";
474         let depending_on_isize = "will truncate or zero-extend depending on the bit width of \
475                                   `isize`";
476         let depending_on_usize = "will truncate or zero-extend depending on the bit width of \
477                                   `usize`";
478         let will_sign_extend = "will sign-extend the source value";
479         let will_zero_extend = "will zero-extend the source value";
480
481         // If casting this expression to a given numeric type would be appropriate in case of a type
482         // mismatch.
483         //
484         // We want to minimize the amount of casting operations that are suggested, as it can be a
485         // lossy operation with potentially bad side effects, so we only suggest when encountering
486         // an expression that indicates that the original type couldn't be directly changed.
487         //
488         // For now, don't suggest casting with `as`.
489         let can_cast = false;
490
491         let mut prefix = String::new();
492         if let Some(hir::Node::Expr(hir::Expr {
493             node: hir::ExprKind::Struct(_, fields, _),
494             ..
495         })) = self.tcx.hir().find(self.tcx.hir().get_parent_node(expr.id)) {
496             // `expr` is a literal field for a struct, only suggest if appropriate
497             for field in fields {
498                 if field.expr.id == expr.id && field.is_shorthand {
499                     // This is a field literal
500                     prefix = format!("{}: ", field.ident);
501                     break;
502                 }
503             }
504             if &prefix == "" {
505                 // Likely a field was meant, but this field wasn't found. Do not suggest anything.
506                 return false;
507             }
508         }
509
510         let needs_paren = expr.precedence().order() < (PREC_POSTFIX as i8);
511
512         if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
513             let msg = format!("you can cast an `{}` to `{}`", checked_ty, expected_ty);
514             let cast_suggestion = format!(
515                 "{}{}{}{} as {}",
516                 prefix,
517                 if needs_paren { "(" } else { "" },
518                 src,
519                 if needs_paren { ")" } else { "" },
520                 expected_ty,
521             );
522             let into_suggestion = format!(
523                 "{}{}{}{}.into()",
524                 prefix,
525                 if needs_paren { "(" } else { "" },
526                 src,
527                 if needs_paren { ")" } else { "" },
528             );
529             let literal_is_ty_suffixed = |expr: &hir::Expr| {
530                 if let hir::ExprKind::Lit(lit) = &expr.node {
531                     lit.node.is_suffixed()
532                 } else {
533                     false
534                 }
535             };
536
537             let into_sugg = into_suggestion.clone();
538             let suggest_to_change_suffix_or_into = |err: &mut DiagnosticBuilder,
539                                                     note: Option<&str>| {
540                 let suggest_msg = if literal_is_ty_suffixed(expr) {
541                     format!(
542                         "change the type of the numeric literal from `{}` to `{}`",
543                         checked_ty,
544                         expected_ty,
545                     )
546                 } else {
547                     match note {
548                         Some(note) => format!("{}, which {}", msg, note),
549                         _ => format!("{} in a lossless way", msg),
550                     }
551                 };
552
553                 let suffix_suggestion = format!(
554                     "{}{}{}{}",
555                     if needs_paren { "(" } else { "" },
556                     src.trim_end_matches(&checked_ty.to_string()),
557                     expected_ty,
558                     if needs_paren { ")" } else { "" },
559                 );
560
561                 err.span_suggestion_with_applicability(
562                     expr.span,
563                     &suggest_msg,
564                     if literal_is_ty_suffixed(expr) {
565                         suffix_suggestion
566                     } else {
567                         into_sugg
568                     },
569                     Applicability::MachineApplicable,
570                 );
571             };
572
573             match (&expected_ty.sty, &checked_ty.sty) {
574                 (&ty::Int(ref exp), &ty::Int(ref found)) => {
575                     match (found.bit_width(), exp.bit_width()) {
576                         (Some(found), Some(exp)) if found > exp => {
577                             if can_cast {
578                                 err.span_suggestion_with_applicability(
579                                     expr.span,
580                                     &format!("{}, which {}", msg, will_truncate),
581                                     cast_suggestion,
582                                     Applicability::MaybeIncorrect // lossy conversion
583                                 );
584                             }
585                         }
586                         (None, _) | (_, None) => {
587                             if can_cast {
588                                 err.span_suggestion_with_applicability(
589                                     expr.span,
590                                     &format!("{}, which {}", msg, depending_on_isize),
591                                     cast_suggestion,
592                                     Applicability::MaybeIncorrect // lossy conversion
593                                 );
594                             }
595                         }
596                         _ => {
597                             suggest_to_change_suffix_or_into(
598                                 err,
599                                 Some(will_sign_extend),
600                             );
601                         }
602                     }
603                     true
604                 }
605                 (&ty::Uint(ref exp), &ty::Uint(ref found)) => {
606                     match (found.bit_width(), exp.bit_width()) {
607                         (Some(found), Some(exp)) if found > exp => {
608                             if can_cast {
609                                 err.span_suggestion_with_applicability(
610                                     expr.span,
611                                     &format!("{}, which {}", msg, will_truncate),
612                                     cast_suggestion,
613                                     Applicability::MaybeIncorrect  // lossy conversion
614                                 );
615                             }
616                         }
617                         (None, _) | (_, None) => {
618                             if can_cast {
619                                 err.span_suggestion_with_applicability(
620                                     expr.span,
621                                     &format!("{}, which {}", msg, depending_on_usize),
622                                     cast_suggestion,
623                                     Applicability::MaybeIncorrect  // lossy conversion
624                                 );
625                             }
626                         }
627                         _ => {
628                            suggest_to_change_suffix_or_into(
629                                err,
630                                Some(will_zero_extend),
631                            );
632                         }
633                     }
634                     true
635                 }
636                 (&ty::Int(ref exp), &ty::Uint(ref found)) => {
637                     if can_cast {
638                         match (found.bit_width(), exp.bit_width()) {
639                             (Some(found), Some(exp)) if found > exp - 1 => {
640                                 err.span_suggestion_with_applicability(
641                                     expr.span,
642                                     &format!("{}, which {}", msg, will_truncate),
643                                     cast_suggestion,
644                                     Applicability::MaybeIncorrect  // lossy conversion
645                                 );
646                             }
647                             (None, None) => {
648                                 err.span_suggestion_with_applicability(
649                                     expr.span,
650                                     &format!("{}, which {}", msg, will_truncate),
651                                     cast_suggestion,
652                                     Applicability::MaybeIncorrect  // lossy conversion
653                                 );
654                             }
655                             (None, _) => {
656                                 err.span_suggestion_with_applicability(
657                                     expr.span,
658                                     &format!("{}, which {}", msg, depending_on_isize),
659                                     cast_suggestion,
660                                     Applicability::MaybeIncorrect  // lossy conversion
661                                 );
662                             }
663                             (_, None) => {
664                                 err.span_suggestion_with_applicability(
665                                     expr.span,
666                                     &format!("{}, which {}", msg, depending_on_usize),
667                                     cast_suggestion,
668                                     Applicability::MaybeIncorrect  // lossy conversion
669                                 );
670                             }
671                             _ => {
672                                 err.span_suggestion_with_applicability(
673                                     expr.span,
674                                     &format!("{}, which {}", msg, will_zero_extend),
675                                     cast_suggestion,
676                                     Applicability::MachineApplicable
677                                 );
678                             }
679                         }
680                     }
681                     true
682                 }
683                 (&ty::Uint(ref exp), &ty::Int(ref found)) => {
684                     if can_cast {
685                         match (found.bit_width(), exp.bit_width()) {
686                             (Some(found), Some(exp)) if found - 1 > exp => {
687                                 err.span_suggestion_with_applicability(
688                                     expr.span,
689                                     &format!("{}, which {}", msg, will_truncate),
690                                     cast_suggestion,
691                                     Applicability::MaybeIncorrect  // lossy conversion
692                                 );
693                             }
694                             (None, None) => {
695                                 err.span_suggestion_with_applicability(
696                                     expr.span,
697                                     &format!("{}, which {}", msg, will_sign_extend),
698                                     cast_suggestion,
699                                     Applicability::MachineApplicable  // lossy conversion
700                                 );
701                             }
702                             (None, _) => {
703                                 err.span_suggestion_with_applicability(
704                                     expr.span,
705                                     &format!("{}, which {}", msg, depending_on_usize),
706                                     cast_suggestion,
707                                     Applicability::MaybeIncorrect  // lossy conversion
708                                 );
709                             }
710                             (_, None) => {
711                                 err.span_suggestion_with_applicability(
712                                     expr.span,
713                                     &format!("{}, which {}", msg, depending_on_isize),
714                                     cast_suggestion,
715                                     Applicability::MaybeIncorrect  // lossy conversion
716                                 );
717                             }
718                             _ => {
719                                 err.span_suggestion_with_applicability(
720                                     expr.span,
721                                     &format!("{}, which {}", msg, will_sign_extend),
722                                     cast_suggestion,
723                                     Applicability::MachineApplicable
724                                 );
725                             }
726                         }
727                     }
728                     true
729                 }
730                 (&ty::Float(ref exp), &ty::Float(ref found)) => {
731                     if found.bit_width() < exp.bit_width() {
732                        suggest_to_change_suffix_or_into(
733                            err,
734                            None,
735                        );
736                     } else if can_cast {
737                         err.span_suggestion_with_applicability(
738                             expr.span,
739                             &format!("{}, producing the closest possible value", msg),
740                             cast_suggestion,
741                             Applicability::MaybeIncorrect  // lossy conversion
742                         );
743                     }
744                     true
745                 }
746                 (&ty::Uint(_), &ty::Float(_)) | (&ty::Int(_), &ty::Float(_)) => {
747                     if can_cast {
748                         err.span_suggestion_with_applicability(
749                             expr.span,
750                             &format!("{}, rounding the float towards zero", msg),
751                             cast_suggestion,
752                             Applicability::MaybeIncorrect  // lossy conversion
753                         );
754                         err.warn("casting here will cause undefined behavior if the rounded value \
755                                   cannot be represented by the target integer type, including \
756                                   `Inf` and `NaN` (this is a bug and will be fixed)");
757                     }
758                     true
759                 }
760                 (&ty::Float(ref exp), &ty::Uint(ref found)) => {
761                     // if `found` is `None` (meaning found is `usize`), don't suggest `.into()`
762                     if exp.bit_width() > found.bit_width().unwrap_or(256) {
763                         err.span_suggestion_with_applicability(
764                             expr.span,
765                             &format!("{}, producing the floating point representation of the \
766                                       integer",
767                                      msg),
768                             into_suggestion,
769                             Applicability::MachineApplicable
770                         );
771                     } else if can_cast {
772                         err.span_suggestion_with_applicability(expr.span,
773                             &format!("{}, producing the floating point representation of the \
774                                       integer, rounded if necessary",
775                                      msg),
776                             cast_suggestion,
777                             Applicability::MaybeIncorrect  // lossy conversion
778                         );
779                     }
780                     true
781                 }
782                 (&ty::Float(ref exp), &ty::Int(ref found)) => {
783                     // if `found` is `None` (meaning found is `isize`), don't suggest `.into()`
784                     if exp.bit_width() > found.bit_width().unwrap_or(256) {
785                         err.span_suggestion_with_applicability(
786                             expr.span,
787                             &format!("{}, producing the floating point representation of the \
788                                       integer",
789                                      msg),
790                             into_suggestion,
791                             Applicability::MachineApplicable
792                         );
793                     } else if can_cast {
794                         err.span_suggestion_with_applicability(
795                             expr.span,
796                             &format!("{}, producing the floating point representation of the \
797                                       integer, rounded if necessary",
798                                      msg),
799                             cast_suggestion,
800                             Applicability::MaybeIncorrect  // lossy conversion
801                         );
802                     }
803                     true
804                 }
805                 _ => false,
806             }
807         } else {
808             false
809         }
810     }
811 }