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