2 use rustc::infer::InferOk;
3 use rustc::traits::{ObligationCause, ObligationCauseCode};
6 use syntax::util::parser::PREC_POSTFIX;
9 use rustc::hir::def::Def;
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};
16 use super::method::probe;
18 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
19 // Requires that the two types unify, and prints an error message if
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());
25 pub fn demand_suptype_diag(&self,
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);
36 Some(self.report_mismatched_types(&cause, expected, actual, e))
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) {
47 pub fn demand_eqtype_diag(&self,
50 actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
51 self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
54 pub fn demand_eqtype_with_origin(&self,
55 cause: &ObligationCause<'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);
64 Some(self.report_mismatched_types(cause, expected, actual, e))
69 pub fn demand_eqtype_pat(
74 match_expr_span: Option<Span>,
76 let cause = if let Some(span) = match_expr_span {
79 ObligationCauseCode::MatchExpressionArmPattern { span, ty: expected },
84 self.demand_eqtype_with_origin(&cause, expected, actual).map(|mut err| err.emit());
88 pub fn demand_coerce(&self,
92 allow_two_phase: AllowTwoPhase)
94 let (ty, err) = self.demand_coerce_diag(expr, checked_ty, expected, allow_two_phase);
95 if let Some(mut err) = err {
101 // Checks that the type of `expr` can be coerced to `expected`.
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,
108 checked_ty: Ty<'tcx>,
110 allow_two_phase: AllowTwoPhase)
111 -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
112 let expected = self.resolve_type_vars_with_obligations(expected);
114 let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
115 Ok(ty) => return (ty, None),
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);
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
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())
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(
148 "try using a variant of the expected type",
150 Applicability::MaybeIncorrect,
156 self.suggest_ref_or_into(&mut err, expr, expected, expr_ty);
158 (expected, Some(err))
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,
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.
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).
179 // FIXME? Other potential candidate methods: `as_ref` and
181 .find(|a| a.check_name("rustc_conversion_suggestion")).is_some()
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 {
190 Def::Method(def_id) => {
191 self.tcx.fn_sig(def_id).inputs().skip_binder().len() == 1
197 /// Identify some cases where `as_ref()` would be appropriate and suggest it.
199 /// Given the following code:
202 /// fn takes_ref(_: &Foo) {}
203 /// let ref opt = Some(Foo);
205 /// opt.map(|arg| takes_ref(arg));
207 /// Suggest using `opt.as_ref().map(|arg| takes_ref(arg));` instead.
209 /// It only checks for `Option` and `Result` and won't work with
211 /// opt.map(|arg| { takes_ref(arg) });
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 {
219 node: hir::ExprKind::Closure(_, decl, ..),
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),
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");
237 return Some((span.shrink_to_lo(),
238 "consider using `as_ref` instead",
239 "as_ref().".into()));
248 /// This function is used to determine potential "simple" improvements or users' errors and
249 /// provide them useful help. For example:
252 /// fn some_fn(s: &str) {}
254 /// let x = "hey!".to_owned();
255 /// some_fn(x); // error
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!
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
264 pub fn check_ref(&self,
266 checked_ty: 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() {
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\"") {
284 "consider removing the leading `b`",
285 src[1..].to_string()));
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("\"") {
296 "consider adding a leading `b`",
297 format!("b{}", src)));
304 (&ty::Ref(_, _, mutability), _) => {
305 // Check if it can work when put into a ref. For example:
308 // fn bar(x: &mut i32) {}
311 // bar(&x); // error, expected &mut
313 let ref_ty = match mutability {
314 hir::Mutability::MutMutable => self.tcx.mk_mut_ref(
315 self.tcx.mk_region(ty::ReStatic),
317 hir::Mutability::MutImmutable => self.tcx.mk_imm_ref(
318 self.tcx.mk_region(ty::ReStatic),
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,
331 let sugg_expr = if needs_parens {
337 if let Some(sugg) = self.can_use_as_ref(expr) {
340 return Some(match mutability {
341 hir::Mutability::MutMutable => {
342 (sp, "consider mutably borrowing here", format!("&mut {}",
345 hir::Mutability::MutImmutable => {
346 (sp, "consider borrowing here", format!("&{}", sugg_expr))
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
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() {
364 hir::ExprKind::AddrOf(_, ref expr) => {
365 if !cm.span_to_filename(expr.span).is_real() {
368 if let Ok(code) = cm.span_to_snippet(expr.span) {
369 return Some((sp, "consider removing the borrow", code));
373 // Maybe add `*`? Only if `T: Copy`.
375 if self.infcx.type_is_copy_modulo_regions(self.param_env,
378 // do not suggest if the span comes from a macro (#52783)
380 true) = (cm.span_to_snippet(sp), sp == expr.span) {
383 "consider dereferencing the borrow",
384 format!("*{}", code),
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};
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());
407 if let (Some(root), Some(std_core), Some(ops), Some(range), None) =
408 (segs.next(), segs.next(), segs.next(), segs.next(), segs.next())
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")
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);
424 if let Ok(end_string) = source_map.span_to_snippet(end_point) {
425 !(end_string.ends_with("}") || end_string.ends_with(")"))
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);
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";
445 return is_range_path(&path) && span_is_range_literal(&expr.span)
457 pub fn check_for_cast(
459 err: &mut DiagnosticBuilder<'tcx>,
461 checked_ty: Ty<'tcx>,
462 expected_ty: Ty<'tcx>,
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
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 \
476 let depending_on_usize = "will truncate or zero-extend depending on the bit width of \
478 let will_sign_extend = "will sign-extend the source value";
479 let will_zero_extend = "will zero-extend the source value";
481 // If casting this expression to a given numeric type would be appropriate in case of a type
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.
488 // For now, don't suggest casting with `as`.
489 let can_cast = false;
491 let mut prefix = String::new();
492 if let Some(hir::Node::Expr(hir::Expr {
493 node: hir::ExprKind::Struct(_, fields, _),
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 {
499 // This is a field literal
500 prefix = format!("{}: ", field.ident);
505 // Likely a field was meant, but this field wasn't found. Do not suggest anything.
510 let needs_paren = expr.precedence().order() < (PREC_POSTFIX as i8);
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!(
517 if needs_paren { "(" } else { "" },
519 if needs_paren { ")" } else { "" },
522 let into_suggestion = format!(
525 if needs_paren { "(" } else { "" },
527 if needs_paren { ")" } else { "" },
529 let literal_is_ty_suffixed = |expr: &hir::Expr| {
530 if let hir::ExprKind::Lit(lit) = &expr.node {
531 lit.node.is_suffixed()
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) {
542 "change the type of the numeric literal from `{}` to `{}`",
548 Some(note) => format!("{}, which {}", msg, note),
549 _ => format!("{} in a lossless way", msg),
553 let suffix_suggestion = format!(
555 if needs_paren { "(" } else { "" },
556 src.trim_end_matches(&checked_ty.to_string()),
558 if needs_paren { ")" } else { "" },
561 err.span_suggestion_with_applicability(
564 if literal_is_ty_suffixed(expr) {
569 Applicability::MachineApplicable,
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 => {
578 err.span_suggestion_with_applicability(
580 &format!("{}, which {}", msg, will_truncate),
582 Applicability::MaybeIncorrect // lossy conversion
586 (None, _) | (_, None) => {
588 err.span_suggestion_with_applicability(
590 &format!("{}, which {}", msg, depending_on_isize),
592 Applicability::MaybeIncorrect // lossy conversion
597 suggest_to_change_suffix_or_into(
599 Some(will_sign_extend),
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 => {
609 err.span_suggestion_with_applicability(
611 &format!("{}, which {}", msg, will_truncate),
613 Applicability::MaybeIncorrect // lossy conversion
617 (None, _) | (_, None) => {
619 err.span_suggestion_with_applicability(
621 &format!("{}, which {}", msg, depending_on_usize),
623 Applicability::MaybeIncorrect // lossy conversion
628 suggest_to_change_suffix_or_into(
630 Some(will_zero_extend),
636 (&ty::Int(ref exp), &ty::Uint(ref found)) => {
638 match (found.bit_width(), exp.bit_width()) {
639 (Some(found), Some(exp)) if found > exp - 1 => {
640 err.span_suggestion_with_applicability(
642 &format!("{}, which {}", msg, will_truncate),
644 Applicability::MaybeIncorrect // lossy conversion
648 err.span_suggestion_with_applicability(
650 &format!("{}, which {}", msg, will_truncate),
652 Applicability::MaybeIncorrect // lossy conversion
656 err.span_suggestion_with_applicability(
658 &format!("{}, which {}", msg, depending_on_isize),
660 Applicability::MaybeIncorrect // lossy conversion
664 err.span_suggestion_with_applicability(
666 &format!("{}, which {}", msg, depending_on_usize),
668 Applicability::MaybeIncorrect // lossy conversion
672 err.span_suggestion_with_applicability(
674 &format!("{}, which {}", msg, will_zero_extend),
676 Applicability::MachineApplicable
683 (&ty::Uint(ref exp), &ty::Int(ref found)) => {
685 match (found.bit_width(), exp.bit_width()) {
686 (Some(found), Some(exp)) if found - 1 > exp => {
687 err.span_suggestion_with_applicability(
689 &format!("{}, which {}", msg, will_truncate),
691 Applicability::MaybeIncorrect // lossy conversion
695 err.span_suggestion_with_applicability(
697 &format!("{}, which {}", msg, will_sign_extend),
699 Applicability::MachineApplicable // lossy conversion
703 err.span_suggestion_with_applicability(
705 &format!("{}, which {}", msg, depending_on_usize),
707 Applicability::MaybeIncorrect // lossy conversion
711 err.span_suggestion_with_applicability(
713 &format!("{}, which {}", msg, depending_on_isize),
715 Applicability::MaybeIncorrect // lossy conversion
719 err.span_suggestion_with_applicability(
721 &format!("{}, which {}", msg, will_sign_extend),
723 Applicability::MachineApplicable
730 (&ty::Float(ref exp), &ty::Float(ref found)) => {
731 if found.bit_width() < exp.bit_width() {
732 suggest_to_change_suffix_or_into(
737 err.span_suggestion_with_applicability(
739 &format!("{}, producing the closest possible value", msg),
741 Applicability::MaybeIncorrect // lossy conversion
746 (&ty::Uint(_), &ty::Float(_)) | (&ty::Int(_), &ty::Float(_)) => {
748 err.span_suggestion_with_applicability(
750 &format!("{}, rounding the float towards zero", msg),
752 Applicability::MaybeIncorrect // lossy conversion
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)");
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(
765 &format!("{}, producing the floating point representation of the \
769 Applicability::MachineApplicable
772 err.span_suggestion_with_applicability(expr.span,
773 &format!("{}, producing the floating point representation of the \
774 integer, rounded if necessary",
777 Applicability::MaybeIncorrect // lossy conversion
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(
787 &format!("{}, producing the floating point representation of the \
791 Applicability::MachineApplicable
794 err.span_suggestion_with_applicability(
796 &format!("{}, producing the floating point representation of the \
797 integer, rounded if necessary",
800 Applicability::MaybeIncorrect // lossy conversion