1 use crate::check::FnCtxt;
2 use rustc::infer::InferOk;
3 use rustc::traits::{ObligationCause, ObligationCauseCode};
5 use syntax::util::parser::PREC_POSTFIX;
8 use rustc::hir::def::Def;
10 use rustc::hir::{Item, ItemKind, print};
11 use rustc::ty::{self, Ty, AssociatedItem};
12 use rustc::ty::adjustment::AllowTwoPhase;
13 use errors::{Applicability, DiagnosticBuilder, SourceMapper};
15 use super::method::probe;
17 impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
18 // Requires that the two types unify, and prints an error message if
20 pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
21 self.demand_suptype_diag(sp, expected, actual).map(|mut e| e.emit());
24 pub fn demand_suptype_diag(&self,
27 actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
28 let cause = &self.misc(sp);
29 match self.at(cause, self.param_env).sup(expected, actual) {
30 Ok(InferOk { obligations, value: () }) => {
31 self.register_predicates(obligations);
35 Some(self.report_mismatched_types(&cause, expected, actual, e))
40 pub fn demand_eqtype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
41 if let Some(mut err) = self.demand_eqtype_diag(sp, expected, actual) {
46 pub fn demand_eqtype_diag(&self,
49 actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
50 self.demand_eqtype_with_origin(&self.misc(sp), expected, actual)
53 pub fn demand_eqtype_with_origin(&self,
54 cause: &ObligationCause<'tcx>,
56 actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
57 match self.at(cause, self.param_env).eq(expected, actual) {
58 Ok(InferOk { obligations, value: () }) => {
59 self.register_predicates(obligations);
63 Some(self.report_mismatched_types(cause, expected, actual, e))
68 pub fn demand_eqtype_pat(
73 match_expr_span: Option<Span>,
75 let cause = if let Some(span) = match_expr_span {
78 ObligationCauseCode::MatchExpressionArmPattern { span, ty: expected },
83 self.demand_eqtype_with_origin(&cause, expected, actual).map(|mut err| err.emit());
87 pub fn demand_coerce(&self,
91 allow_two_phase: AllowTwoPhase)
93 let (ty, err) = self.demand_coerce_diag(expr, checked_ty, expected, allow_two_phase);
94 if let Some(mut err) = err {
100 // Checks that the type of `expr` can be coerced to `expected`.
102 // N.B., this code relies on `self.diverges` to be accurate. In
103 // particular, assignments to `!` will be permitted if the
104 // diverges flag is currently "always".
105 pub fn demand_coerce_diag(&self,
107 checked_ty: Ty<'tcx>,
109 allow_two_phase: AllowTwoPhase)
110 -> (Ty<'tcx>, Option<DiagnosticBuilder<'tcx>>) {
111 let expected = self.resolve_type_vars_with_obligations(expected);
113 let e = match self.try_coerce(expr, checked_ty, expected, allow_two_phase) {
114 Ok(ty) => return (ty, None),
118 let cause = self.misc(expr.span);
119 let expr_ty = self.resolve_type_vars_with_obligations(checked_ty);
120 let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
122 // If the expected type is an enum (Issue #55250) with any variants whose
123 // sole field is of the found type, suggest such variants. (Issue #42764)
124 if let ty::Adt(expected_adt, substs) = expected.sty {
125 if expected_adt.is_enum() {
126 let mut compatible_variants = expected_adt.variants
128 .filter(|variant| variant.fields.len() == 1)
129 .filter_map(|variant| {
130 let sole_field = &variant.fields[0];
131 let sole_field_ty = sole_field.ty(self.tcx, substs);
132 if self.can_coerce(expr_ty, sole_field_ty) {
133 let variant_path = self.tcx.def_path_str(variant.did);
134 // FIXME #56861: DRYer prelude filtering
135 Some(variant_path.trim_start_matches("std::prelude::v1::").to_string())
141 if compatible_variants.peek().is_some() {
142 let expr_text = print::to_string(print::NO_ANN, |s| s.print_expr(expr));
143 let suggestions = compatible_variants
144 .map(|v| format!("{}({})", v, expr_text));
145 err.span_suggestions(
147 "try using a variant of the expected type",
149 Applicability::MaybeIncorrect,
155 self.suggest_ref_or_into(&mut err, expr, expected, expr_ty);
157 (expected, Some(err))
160 pub fn get_conversion_methods(&self, span: Span, expected: Ty<'tcx>, checked_ty: Ty<'tcx>)
161 -> Vec<AssociatedItem> {
162 let mut methods = self.probe_for_return_type(span,
163 probe::Mode::MethodCall,
168 self.has_no_input_arg(m) &&
169 self.tcx.get_attrs(m.def_id).iter()
170 // This special internal attribute is used to whitelist
171 // "identity-like" conversion methods to be suggested here.
173 // FIXME (#46459 and #46460): ideally
174 // `std::convert::Into::into` and `std::borrow:ToOwned` would
175 // also be `#[rustc_conversion_suggestion]`, if not for
176 // method-probing false-positives and -negatives (respectively).
178 // FIXME? Other potential candidate methods: `as_ref` and
180 .find(|a| a.check_name("rustc_conversion_suggestion")).is_some()
186 // This function checks if the method isn't static and takes other arguments than `self`.
187 fn has_no_input_arg(&self, method: &AssociatedItem) -> bool {
189 Def::Method(def_id) => {
190 self.tcx.fn_sig(def_id).inputs().skip_binder().len() == 1
196 /// Identify some cases where `as_ref()` would be appropriate and suggest it.
198 /// Given the following code:
201 /// fn takes_ref(_: &Foo) {}
202 /// let ref opt = Some(Foo);
204 /// opt.map(|arg| takes_ref(arg));
206 /// Suggest using `opt.as_ref().map(|arg| takes_ref(arg));` instead.
208 /// It only checks for `Option` and `Result` and won't work with
210 /// opt.map(|arg| { takes_ref(arg) });
215 ) -> Option<(Span, &'static str, String)> {
216 if let hir::ExprKind::Path(hir::QPath::Resolved(_, ref path)) = expr.node {
217 if let hir::def::Def::Local(id) = path.def {
218 let parent = self.tcx.hir().get_parent_node(id);
219 if let Some(Node::Expr(hir::Expr {
221 node: hir::ExprKind::Closure(_, decl, ..),
223 })) = self.tcx.hir().find(parent) {
224 let parent = self.tcx.hir().get_parent_node_by_hir_id(*hir_id);
225 if let (Some(Node::Expr(hir::Expr {
226 node: hir::ExprKind::MethodCall(path, span, expr),
228 })), 1) = (self.tcx.hir().find_by_hir_id(parent), decl.inputs.len()) {
229 let self_ty = self.tables.borrow().node_type(expr[0].hir_id);
230 let self_ty = format!("{:?}", self_ty);
231 let name = path.ident.as_str();
232 let is_as_ref_able = (
233 self_ty.starts_with("&std::option::Option") ||
234 self_ty.starts_with("&std::result::Result") ||
235 self_ty.starts_with("std::option::Option") ||
236 self_ty.starts_with("std::result::Result")
237 ) && (name == "map" || name == "and_then");
238 match (is_as_ref_able, self.sess().source_map().span_to_snippet(*span)) {
240 return Some((*span, "consider using `as_ref` instead",
241 format!("as_ref().{}", src)));
252 /// This function is used to determine potential "simple" improvements or users' errors and
253 /// provide them useful help. For example:
256 /// fn some_fn(s: &str) {}
258 /// let x = "hey!".to_owned();
259 /// some_fn(x); // error
262 /// No need to find every potential function which could make a coercion to transform a
263 /// `String` into a `&str` since a `&` would do the trick!
265 /// In addition of this check, it also checks between references mutability state. If the
266 /// expected is mutable but the provided isn't, maybe we could just say "Hey, try with
268 pub fn check_ref(&self,
270 checked_ty: Ty<'tcx>,
272 -> Option<(Span, &'static str, String)> {
273 let cm = self.sess().source_map();
274 // Use the callsite's span if this is a macro call. #41858
275 let sp = cm.call_span_if_macro(expr.span);
276 if !cm.span_to_filename(sp).is_real() {
280 match (&expected.sty, &checked_ty.sty) {
281 (&ty::Ref(_, exp, _), &ty::Ref(_, check, _)) => match (&exp.sty, &check.sty) {
282 (&ty::Str, &ty::Array(arr, _)) |
283 (&ty::Str, &ty::Slice(arr)) if arr == self.tcx.types.u8 => {
284 if let hir::ExprKind::Lit(_) = expr.node {
285 if let Ok(src) = cm.span_to_snippet(sp) {
286 if src.starts_with("b\"") {
288 "consider removing the leading `b`",
289 src[1..].to_string()));
294 (&ty::Array(arr, _), &ty::Str) |
295 (&ty::Slice(arr), &ty::Str) if arr == self.tcx.types.u8 => {
296 if let hir::ExprKind::Lit(_) = expr.node {
297 if let Ok(src) = cm.span_to_snippet(sp) {
298 if src.starts_with("\"") {
300 "consider adding a leading `b`",
301 format!("b{}", src)));
308 (&ty::Ref(_, _, mutability), _) => {
309 // Check if it can work when put into a ref. For example:
312 // fn bar(x: &mut i32) {}
315 // bar(&x); // error, expected &mut
317 let ref_ty = match mutability {
318 hir::Mutability::MutMutable => self.tcx.mk_mut_ref(
319 self.tcx.mk_region(ty::ReStatic),
321 hir::Mutability::MutImmutable => self.tcx.mk_imm_ref(
322 self.tcx.mk_region(ty::ReStatic),
325 if self.can_coerce(ref_ty, expected) {
326 if let Ok(src) = cm.span_to_snippet(sp) {
327 let needs_parens = match expr.node {
328 // parenthesize if needed (Issue #46756)
329 hir::ExprKind::Cast(_, _) |
330 hir::ExprKind::Binary(_, _, _) => true,
331 // parenthesize borrows of range literals (Issue #54505)
332 _ if self.is_range_literal(expr) => true,
335 let sugg_expr = if needs_parens {
341 if let Some(sugg) = self.can_use_as_ref(expr) {
344 return Some(match mutability {
345 hir::Mutability::MutMutable => {
346 (sp, "consider mutably borrowing here", format!("&mut {}",
349 hir::Mutability::MutImmutable => {
350 (sp, "consider borrowing here", format!("&{}", sugg_expr))
356 (_, &ty::Ref(_, checked, _)) => {
357 // We have `&T`, check if what was expected was `T`. If so,
358 // we may want to suggest adding a `*`, or removing
361 // (But, also check the `expn_info()` to see if this is
362 // a macro; if so, it's hard to extract the text and make a good
363 // suggestion, so don't bother.)
364 if self.infcx.can_sub(self.param_env, checked, &expected).is_ok() &&
365 sp.ctxt().outer().expn_info().is_none() {
368 hir::ExprKind::AddrOf(_, ref expr) => {
369 if !cm.span_to_filename(expr.span).is_real() {
370 if let Ok(code) = cm.span_to_snippet(sp) {
371 if code.chars().next() == Some('&') {
374 "consider removing the borrow",
375 code[1..].to_string()),
381 if let Ok(code) = cm.span_to_snippet(expr.span) {
382 return Some((sp, "consider removing the borrow", code));
386 // Maybe add `*`? Only if `T: Copy`.
388 if self.infcx.type_is_copy_modulo_regions(self.param_env,
391 // do not suggest if the span comes from a macro (#52783)
393 true) = (cm.span_to_snippet(sp), sp == expr.span) {
396 "consider dereferencing the borrow",
397 format!("*{}", code),
410 /// This function checks if the specified expression is a built-in range literal.
411 /// (See: `LoweringContext::lower_expr()` in `src/librustc/hir/lowering.rs`).
412 fn is_range_literal(&self, expr: &hir::Expr) -> bool {
413 use hir::{Path, QPath, ExprKind, TyKind};
415 // We support `::std::ops::Range` and `::core::ops::Range` prefixes
416 let is_range_path = |path: &Path| {
417 let mut segs = path.segments.iter()
418 .map(|seg| seg.ident.as_str());
420 if let (Some(root), Some(std_core), Some(ops), Some(range), None) =
421 (segs.next(), segs.next(), segs.next(), segs.next(), segs.next())
423 // "{{root}}" is the equivalent of `::` prefix in Path
424 root == "{{root}}" && (std_core == "std" || std_core == "core")
425 && ops == "ops" && range.starts_with("Range")
431 let span_is_range_literal = |span: &Span| {
432 // Check whether a span corresponding to a range expression
433 // is a range literal, rather than an explicit struct or `new()` call.
434 let source_map = self.tcx.sess.source_map();
435 let end_point = source_map.end_point(*span);
437 if let Ok(end_string) = source_map.span_to_snippet(end_point) {
438 !(end_string.ends_with("}") || end_string.ends_with(")"))
445 // All built-in range literals but `..=` and `..` desugar to Structs
446 ExprKind::Struct(ref qpath, _, _) => {
447 if let QPath::Resolved(None, ref path) = **qpath {
448 return is_range_path(&path) && span_is_range_literal(&expr.span);
451 // `..` desugars to its struct path
452 ExprKind::Path(QPath::Resolved(None, ref path)) => {
453 return is_range_path(&path) && span_is_range_literal(&expr.span);
456 // `..=` desugars into `::std::ops::RangeInclusive::new(...)`
457 ExprKind::Call(ref func, _) => {
458 if let ExprKind::Path(QPath::TypeRelative(ref ty, ref segment)) = func.node {
459 if let TyKind::Path(QPath::Resolved(None, ref path)) = ty.node {
460 let call_to_new = segment.ident.as_str() == "new";
462 return is_range_path(&path) && span_is_range_literal(&expr.span)
474 pub fn check_for_cast(
476 err: &mut DiagnosticBuilder<'tcx>,
478 checked_ty: Ty<'tcx>,
479 expected_ty: Ty<'tcx>,
481 let parent_id = self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
482 if let Some(parent) = self.tcx.hir().find_by_hir_id(parent_id) {
483 // Shouldn't suggest `.into()` on `const`s.
484 if let Node::Item(Item { node: ItemKind::Const(_, _), .. }) = parent {
485 // FIXME(estebank): modify once we decide to suggest `as` casts
490 let will_truncate = "will truncate the source value";
491 let depending_on_isize = "will truncate or zero-extend depending on the bit width of \
493 let depending_on_usize = "will truncate or zero-extend depending on the bit width of \
495 let will_sign_extend = "will sign-extend the source value";
496 let will_zero_extend = "will zero-extend the source value";
498 // If casting this expression to a given numeric type would be appropriate in case of a type
501 // We want to minimize the amount of casting operations that are suggested, as it can be a
502 // lossy operation with potentially bad side effects, so we only suggest when encountering
503 // an expression that indicates that the original type couldn't be directly changed.
505 // For now, don't suggest casting with `as`.
506 let can_cast = false;
508 let mut prefix = String::new();
509 if let Some(hir::Node::Expr(hir::Expr {
510 node: hir::ExprKind::Struct(_, fields, _),
512 })) = self.tcx.hir().find_by_hir_id(self.tcx.hir().get_parent_node_by_hir_id(expr.hir_id)) {
513 // `expr` is a literal field for a struct, only suggest if appropriate
514 for field in fields {
515 if field.expr.hir_id == expr.hir_id && field.is_shorthand {
516 // This is a field literal
517 prefix = format!("{}: ", field.ident);
522 // Likely a field was meant, but this field wasn't found. Do not suggest anything.
527 let needs_paren = expr.precedence().order() < (PREC_POSTFIX as i8);
529 if let Ok(src) = self.tcx.sess.source_map().span_to_snippet(expr.span) {
530 let msg = format!("you can cast an `{}` to `{}`", checked_ty, expected_ty);
531 let cast_suggestion = format!(
534 if needs_paren { "(" } else { "" },
536 if needs_paren { ")" } else { "" },
539 let into_suggestion = format!(
542 if needs_paren { "(" } else { "" },
544 if needs_paren { ")" } else { "" },
546 let literal_is_ty_suffixed = |expr: &hir::Expr| {
547 if let hir::ExprKind::Lit(lit) = &expr.node {
548 lit.node.is_suffixed()
554 let into_sugg = into_suggestion.clone();
555 let suggest_to_change_suffix_or_into = |err: &mut DiagnosticBuilder<'_>,
556 note: Option<&str>| {
557 let suggest_msg = if literal_is_ty_suffixed(expr) {
559 "change the type of the numeric literal from `{}` to `{}`",
565 Some(note) => format!("{}, which {}", msg, note),
566 _ => format!("{} in a lossless way", msg),
570 let suffix_suggestion = format!(
572 if needs_paren { "(" } else { "" },
573 src.trim_end_matches(&checked_ty.to_string()),
575 if needs_paren { ")" } else { "" },
581 if literal_is_ty_suffixed(expr) {
586 Applicability::MachineApplicable,
590 match (&expected_ty.sty, &checked_ty.sty) {
591 (&ty::Int(ref exp), &ty::Int(ref found)) => {
592 match (found.bit_width(), exp.bit_width()) {
593 (Some(found), Some(exp)) if found > exp => {
597 &format!("{}, which {}", msg, will_truncate),
599 Applicability::MaybeIncorrect // lossy conversion
603 (None, _) | (_, None) => {
607 &format!("{}, which {}", msg, depending_on_isize),
609 Applicability::MaybeIncorrect // lossy conversion
614 suggest_to_change_suffix_or_into(
616 Some(will_sign_extend),
622 (&ty::Uint(ref exp), &ty::Uint(ref found)) => {
623 match (found.bit_width(), exp.bit_width()) {
624 (Some(found), Some(exp)) if found > exp => {
628 &format!("{}, which {}", msg, will_truncate),
630 Applicability::MaybeIncorrect // lossy conversion
634 (None, _) | (_, None) => {
638 &format!("{}, which {}", msg, depending_on_usize),
640 Applicability::MaybeIncorrect // lossy conversion
645 suggest_to_change_suffix_or_into(
647 Some(will_zero_extend),
653 (&ty::Int(ref exp), &ty::Uint(ref found)) => {
655 match (found.bit_width(), exp.bit_width()) {
656 (Some(found), Some(exp)) if found > exp - 1 => {
659 &format!("{}, which {}", msg, will_truncate),
661 Applicability::MaybeIncorrect // lossy conversion
667 &format!("{}, which {}", msg, will_truncate),
669 Applicability::MaybeIncorrect // lossy conversion
675 &format!("{}, which {}", msg, depending_on_isize),
677 Applicability::MaybeIncorrect // lossy conversion
683 &format!("{}, which {}", msg, depending_on_usize),
685 Applicability::MaybeIncorrect // lossy conversion
691 &format!("{}, which {}", msg, will_zero_extend),
693 Applicability::MachineApplicable
700 (&ty::Uint(ref exp), &ty::Int(ref found)) => {
702 match (found.bit_width(), exp.bit_width()) {
703 (Some(found), Some(exp)) if found - 1 > exp => {
706 &format!("{}, which {}", msg, will_truncate),
708 Applicability::MaybeIncorrect // lossy conversion
714 &format!("{}, which {}", msg, will_sign_extend),
716 Applicability::MachineApplicable // lossy conversion
722 &format!("{}, which {}", msg, depending_on_usize),
724 Applicability::MaybeIncorrect // lossy conversion
730 &format!("{}, which {}", msg, depending_on_isize),
732 Applicability::MaybeIncorrect // lossy conversion
738 &format!("{}, which {}", msg, will_sign_extend),
740 Applicability::MachineApplicable
747 (&ty::Float(ref exp), &ty::Float(ref found)) => {
748 if found.bit_width() < exp.bit_width() {
749 suggest_to_change_suffix_or_into(
756 &format!("{}, producing the closest possible value", msg),
758 Applicability::MaybeIncorrect // lossy conversion
763 (&ty::Uint(_), &ty::Float(_)) | (&ty::Int(_), &ty::Float(_)) => {
767 &format!("{}, rounding the float towards zero", msg),
769 Applicability::MaybeIncorrect // lossy conversion
771 err.warn("casting here will cause undefined behavior if the rounded value \
772 cannot be represented by the target integer type, including \
773 `Inf` and `NaN` (this is a bug and will be fixed)");
777 (&ty::Float(ref exp), &ty::Uint(ref found)) => {
778 // if `found` is `None` (meaning found is `usize`), don't suggest `.into()`
779 if exp.bit_width() > found.bit_width().unwrap_or(256) {
782 &format!("{}, producing the floating point representation of the \
786 Applicability::MachineApplicable
791 &format!("{}, producing the floating point representation of the \
792 integer, rounded if necessary",
795 Applicability::MaybeIncorrect // lossy conversion
800 (&ty::Float(ref exp), &ty::Int(ref found)) => {
801 // if `found` is `None` (meaning found is `isize`), don't suggest `.into()`
802 if exp.bit_width() > found.bit_width().unwrap_or(256) {
805 &format!("{}, producing the floating point representation of the \
809 Applicability::MachineApplicable
814 &format!("{}, producing the floating point representation of the \
815 integer, rounded if necessary",
818 Applicability::MaybeIncorrect // lossy conversion