1 // Copyright 2012-2013 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.
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.
11 //! Error Reporting Code for the inference engine
13 //! Because of the way inference, and in particular region inference,
14 //! works, it often happens that errors are not detected until far after
15 //! the relevant line of code has been type-checked. Therefore, there is
16 //! an elaborate system to track why a particular constraint in the
17 //! inference graph arose so that we can explain to the user what gave
18 //! rise to a particular error.
20 //! The basis of the system are the "origin" types. An "origin" is the
21 //! reason that a constraint or inference variable arose. There are
22 //! different "origin" enums for different kinds of constraints/variables
23 //! (e.g., `TypeOrigin`, `RegionVariableOrigin`). An origin always has
24 //! a span, but also more information so that we can generate a meaningful
27 //! Having a catalog of all the different reasons an error can arise is
28 //! also useful for other reasons, like cross-referencing FAQs etc, though
29 //! we are not really taking advantage of this yet.
31 //! # Region Inference
33 //! Region inference is particularly tricky because it always succeeds "in
34 //! the moment" and simply registers a constraint. Then, at the end, we
35 //! can compute the full graph and report errors, so we need to be able to
36 //! store and later report what gave rise to the conflicting constraints.
40 //! Determining whether `T1 <: T2` often involves a number of subtypes and
41 //! subconstraints along the way. A "TypeTrace" is an extended version
42 //! of an origin that traces the types and other values that were being
43 //! compared. It is not necessarily comprehensive (in fact, at the time of
44 //! this writing it only tracks the root values being compared) but I'd
45 //! like to extend it to include significant "waypoints". For example, if
46 //! you are comparing `(T1, T2) <: (T3, T4)`, and the problem is that `T2
47 //! <: T4` fails, I'd like the trace to include enough information to say
48 //! "in the 2nd element of the tuple". Similarly, failures when comparing
49 //! arguments or return types in fn types should be able to cite the
50 //! specific position, etc.
54 //! Of course, there is still a LOT of code in typeck that has yet to be
55 //! ported to this system, and which relies on string concatenation at the
56 //! time of error detection.
59 use super::{InferCtxt, TypeTrace, SubregionOrigin, RegionVariableOrigin, ValuePairs};
60 use super::region_inference::{RegionResolutionError, ConcreteFailure, SubSupConflict,
61 GenericBoundFailure, GenericKind};
65 use hir::map as hir_map;
66 use hir::def_id::DefId;
68 use traits::{ObligationCause, ObligationCauseCode};
69 use ty::{self, Region, Ty, TyCtxt, TypeFoldable, TypeVariants};
70 use ty::error::TypeError;
71 use syntax::ast::DUMMY_NODE_ID;
72 use syntax_pos::{Pos, Span};
73 use errors::{DiagnosticBuilder, DiagnosticStyledString};
75 use rustc_data_structures::indexed_vec::Idx;
81 mod named_anon_conflict;
84 mod different_lifetimes;
86 impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
87 pub fn note_and_explain_region(self,
88 region_scope_tree: ®ion::ScopeTree,
89 err: &mut DiagnosticBuilder,
91 region: ty::Region<'tcx>,
93 fn item_scope_tag(item: &hir::Item) -> &'static str {
95 hir::ItemImpl(..) => "impl",
96 hir::ItemStruct(..) => "struct",
97 hir::ItemUnion(..) => "union",
98 hir::ItemEnum(..) => "enum",
99 hir::ItemTrait(..) => "trait",
100 hir::ItemFn(..) => "function body",
105 fn trait_item_scope_tag(item: &hir::TraitItem) -> &'static str {
107 hir::TraitItemKind::Method(..) => "method body",
108 hir::TraitItemKind::Const(..) |
109 hir::TraitItemKind::Type(..) => "associated item"
113 fn impl_item_scope_tag(item: &hir::ImplItem) -> &'static str {
115 hir::ImplItemKind::Method(..) => "method body",
116 hir::ImplItemKind::Const(..) |
117 hir::ImplItemKind::Type(_) => "associated item"
121 fn explain_span<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>,
122 heading: &str, span: Span)
123 -> (String, Option<Span>) {
124 let lo = tcx.sess.codemap().lookup_char_pos_adj(span.lo());
125 (format!("the {} at {}:{}", heading, lo.line, lo.col.to_usize() + 1),
129 let (description, span) = match *region {
130 ty::ReScope(scope) => {
132 let unknown_scope = || {
133 format!("{}unknown scope: {:?}{}. Please report a bug.",
134 prefix, scope, suffix)
136 let span = scope.span(self, region_scope_tree);
137 let tag = match self.hir.find(scope.node_id(self, region_scope_tree)) {
138 Some(hir_map::NodeBlock(_)) => "block",
139 Some(hir_map::NodeExpr(expr)) => match expr.node {
140 hir::ExprCall(..) => "call",
141 hir::ExprMethodCall(..) => "method call",
142 hir::ExprMatch(.., hir::MatchSource::IfLetDesugar { .. }) => "if let",
143 hir::ExprMatch(.., hir::MatchSource::WhileLetDesugar) => "while let",
144 hir::ExprMatch(.., hir::MatchSource::ForLoopDesugar) => "for",
145 hir::ExprMatch(..) => "match",
148 Some(hir_map::NodeStmt(_)) => "statement",
149 Some(hir_map::NodeItem(it)) => item_scope_tag(&it),
150 Some(hir_map::NodeTraitItem(it)) => trait_item_scope_tag(&it),
151 Some(hir_map::NodeImplItem(it)) => impl_item_scope_tag(&it),
153 err.span_note(span, &unknown_scope());
157 let scope_decorated_tag = match scope.data() {
158 region::ScopeData::Node(_) => tag,
159 region::ScopeData::CallSite(_) => {
160 "scope of call-site for function"
162 region::ScopeData::Arguments(_) => {
163 "scope of function body"
165 region::ScopeData::Destruction(_) => {
166 new_string = format!("destruction scope surrounding {}", tag);
169 region::ScopeData::Remainder(r) => {
170 new_string = format!("block suffix following statement {}",
171 r.first_statement_index.index());
175 explain_span(self, scope_decorated_tag, span)
178 ty::ReEarlyBound(_) |
180 let scope = match *region {
181 ty::ReEarlyBound(ref br) => {
182 self.parent_def_id(br.def_id).unwrap()
184 ty::ReFree(ref fr) => fr.scope,
187 let prefix = match *region {
188 ty::ReEarlyBound(ref br) => {
189 format!("the lifetime {} as defined on", br.name)
191 ty::ReFree(ref fr) => {
192 match fr.bound_region {
194 format!("the anonymous lifetime #{} defined on", idx + 1)
196 ty::BrFresh(_) => "an anonymous lifetime defined on".to_owned(),
198 format!("the lifetime {} as defined on",
206 let node = self.hir.as_local_node_id(scope)
207 .unwrap_or(DUMMY_NODE_ID);
209 let tag = match self.hir.find(node) {
210 Some(hir_map::NodeBlock(_)) |
211 Some(hir_map::NodeExpr(_)) => "body",
212 Some(hir_map::NodeItem(it)) => item_scope_tag(&it),
213 Some(hir_map::NodeTraitItem(it)) => trait_item_scope_tag(&it),
214 Some(hir_map::NodeImplItem(it)) => impl_item_scope_tag(&it),
216 // this really should not happen, but it does:
219 unknown = format!("unexpected node ({}) for scope {:?}. \
220 Please report a bug.",
221 self.hir.node_to_string(node), scope);
225 unknown = format!("unknown node for scope {:?}. \
226 Please report a bug.", scope);
230 let (msg, opt_span) = explain_span(self, tag, self.hir.span(node));
231 (format!("{} {}", prefix, msg), opt_span)
234 ty::ReStatic => ("the static lifetime".to_owned(), None),
236 ty::ReEmpty => ("the empty lifetime".to_owned(), None),
238 // FIXME(#13998) ReSkolemized should probably print like
239 // ReFree rather than dumping Debug output on the user.
241 // We shouldn't really be having unification failures with ReVar
242 // and ReLateBound though.
243 ty::ReSkolemized(..) |
245 ty::ReLateBound(..) |
247 (format!("lifetime {:?}", region), None)
250 let message = format!("{}{}{}", prefix, description, suffix);
251 if let Some(span) = span {
252 err.span_note(span, &message);
259 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
260 pub fn report_region_errors(&self,
261 region_scope_tree: ®ion::ScopeTree,
262 errors: &Vec<RegionResolutionError<'tcx>>) {
263 debug!("report_region_errors(): {} errors to start", errors.len());
265 if self.tcx.sess.opts.debugging_opts.nll {
266 for error in errors {
268 RegionResolutionError::ConcreteFailure(ref origin, ..) |
269 RegionResolutionError::GenericBoundFailure(ref origin, ..) => {
270 self.tcx.sess.span_warn(
272 "not reporting region error due to -Znll");
275 RegionResolutionError::SubSupConflict(ref rvo, ..) => {
276 self.tcx.sess.span_warn(
278 "not reporting region error due to -Znll");
286 // try to pre-process the errors, which will group some of them
287 // together into a `ProcessedErrors` group:
288 let errors = self.process_errors(errors);
290 debug!("report_region_errors: {} errors after preprocessing", errors.len());
292 for error in errors {
293 debug!("report_region_errors: error = {:?}", error);
295 if !self.try_report_named_anon_conflict(&error) &&
296 !self.try_report_anon_anon_conflict(&error) {
298 match error.clone() {
299 // These errors could indicate all manner of different
300 // problems with many different solutions. Rather
301 // than generate a "one size fits all" error, what we
302 // attempt to do is go through a number of specific
303 // scenarios and try to find the best way to present
304 // the error. If all of these fails, we fall back to a rather
305 // general bit of code that displays the error information
306 ConcreteFailure(origin, sub, sup) => {
307 self.report_concrete_failure(region_scope_tree, origin, sub, sup).emit();
310 GenericBoundFailure(kind, param_ty, sub) => {
311 self.report_generic_bound_failure(region_scope_tree, kind, param_ty, sub);
314 SubSupConflict(var_origin, sub_origin, sub_r, sup_origin, sup_r) => {
315 self.report_sub_sup_conflict(region_scope_tree,
327 // This method goes through all the errors and try to group certain types
328 // of error together, for the purpose of suggesting explicit lifetime
329 // parameters to the user. This is done so that we can have a more
330 // complete view of what lifetimes should be the same.
331 // If the return value is an empty vector, it means that processing
332 // failed (so the return value of this method should not be used).
334 // The method also attempts to weed out messages that seem like
335 // duplicates that will be unhelpful to the end-user. But
336 // obviously it never weeds out ALL errors.
337 fn process_errors(&self, errors: &Vec<RegionResolutionError<'tcx>>)
338 -> Vec<RegionResolutionError<'tcx>> {
339 debug!("process_errors()");
341 // We want to avoid reporting generic-bound failures if we can
342 // avoid it: these have a very high rate of being unhelpful in
343 // practice. This is because they are basically secondary
344 // checks that test the state of the region graph after the
345 // rest of inference is done, and the other kinds of errors
346 // indicate that the region constraint graph is internally
347 // inconsistent, so these test results are likely to be
350 // Therefore, we filter them out of the list unless they are
351 // the only thing in the list.
353 let is_bound_failure = |e: &RegionResolutionError<'tcx>| match *e {
354 ConcreteFailure(..) => false,
355 SubSupConflict(..) => false,
356 GenericBoundFailure(..) => true,
360 let mut errors = if errors.iter().all(|e| is_bound_failure(e)) {
363 errors.iter().filter(|&e| !is_bound_failure(e)).cloned().collect()
366 // sort the errors by span, for better error message stability.
367 errors.sort_by_key(|u| match *u {
368 ConcreteFailure(ref sro, _, _) => sro.span(),
369 GenericBoundFailure(ref sro, _, _) => sro.span(),
370 SubSupConflict(ref rvo, _, _, _, _) => rvo.span(),
375 /// Adds a note if the types come from similarly named crates
376 fn check_and_note_conflicting_crates(&self,
377 err: &mut DiagnosticBuilder,
378 terr: &TypeError<'tcx>,
380 let report_path_match = |err: &mut DiagnosticBuilder, did1: DefId, did2: DefId| {
381 // Only external crates, if either is from a local
382 // module we could have false positives
383 if !(did1.is_local() || did2.is_local()) && did1.krate != did2.krate {
384 let exp_path = self.tcx.item_path_str(did1);
385 let found_path = self.tcx.item_path_str(did2);
386 let exp_abs_path = self.tcx.absolute_item_path_str(did1);
387 let found_abs_path = self.tcx.absolute_item_path_str(did2);
388 // We compare strings because DefPath can be different
389 // for imported and non-imported crates
390 if exp_path == found_path
391 || exp_abs_path == found_abs_path {
392 let crate_name = self.tcx.crate_name(did1.krate);
393 err.span_note(sp, &format!("Perhaps two different versions \
394 of crate `{}` are being used?",
400 TypeError::Sorts(ref exp_found) => {
401 // if they are both "path types", there's a chance of ambiguity
402 // due to different versions of the same crate
403 match (&exp_found.expected.sty, &exp_found.found.sty) {
404 (&ty::TyAdt(exp_adt, _), &ty::TyAdt(found_adt, _)) => {
405 report_path_match(err, exp_adt.did, found_adt.did);
410 TypeError::Traits(ref exp_found) => {
411 report_path_match(err, exp_found.expected, exp_found.found);
413 _ => () // FIXME(#22750) handle traits and stuff
417 fn note_error_origin(&self,
418 err: &mut DiagnosticBuilder<'tcx>,
419 cause: &ObligationCause<'tcx>)
422 ObligationCauseCode::MatchExpressionArm { arm_span, source } => match source {
423 hir::MatchSource::IfLetDesugar {..} => {
424 err.span_note(arm_span, "`if let` arm with an incompatible type");
427 err.span_note(arm_span, "match arm with an incompatible type");
434 /// Given that `other_ty` is the same as a type argument for `name` in `sub`, populate `value`
435 /// highlighting `name` and every type argument that isn't at `pos` (which is `other_ty`), and
436 /// populate `other_value` with `other_ty`.
440 /// ^^^^--------^ this is highlighted
442 /// | this type argument is exactly the same as the other type, not highlighted
443 /// this is highlighted
445 /// -------- this type is the same as a type argument in the other type, not highlighted
447 fn highlight_outer(&self,
448 value: &mut DiagnosticStyledString,
449 other_value: &mut DiagnosticStyledString,
451 sub: &ty::subst::Substs<'tcx>,
453 other_ty: &Ty<'tcx>) {
454 // `value` and `other_value` hold two incomplete type representation for display.
455 // `name` is the path of both types being compared. `sub`
456 value.push_highlighted(name);
459 value.push_highlighted("<");
462 // Output the lifetimes fot the first type
463 let lifetimes = sub.regions().map(|lifetime| {
464 let s = format!("{}", lifetime);
470 }).collect::<Vec<_>>().join(", ");
471 if !lifetimes.is_empty() {
472 if sub.regions().count() < len {
473 value.push_normal(lifetimes + &", ");
475 value.push_normal(lifetimes);
479 // Highlight all the type arguments that aren't at `pos` and compare the type argument at
480 // `pos` and `other_ty`.
481 for (i, type_arg) in sub.types().enumerate() {
483 let values = self.cmp(type_arg, other_ty);
484 value.0.extend((values.0).0);
485 other_value.0.extend((values.1).0);
487 value.push_highlighted(format!("{}", type_arg));
490 if len > 0 && i != len - 1 {
491 value.push_normal(", ");
493 //self.push_comma(&mut value, &mut other_value, len, i);
496 value.push_highlighted(">");
500 /// If `other_ty` is the same as a type argument present in `sub`, highlight `path` in `t1_out`,
501 /// as that is the difference to the other type.
503 /// For the following code:
506 /// let x: Foo<Bar<Qux>> = foo::<Bar<Qux>>();
509 /// The type error output will behave in the following way:
513 /// ^^^^--------^ this is highlighted
515 /// | this type argument is exactly the same as the other type, not highlighted
516 /// this is highlighted
518 /// -------- this type is the same as a type argument in the other type, not highlighted
520 fn cmp_type_arg(&self,
521 mut t1_out: &mut DiagnosticStyledString,
522 mut t2_out: &mut DiagnosticStyledString,
524 sub: &ty::subst::Substs<'tcx>,
526 other_ty: &Ty<'tcx>) -> Option<()> {
527 for (i, ta) in sub.types().enumerate() {
529 self.highlight_outer(&mut t1_out, &mut t2_out, path, sub, i, &other_ty);
532 if let &ty::TyAdt(def, _) = &ta.sty {
533 let path_ = self.tcx.item_path_str(def.did.clone());
534 if path_ == other_path {
535 self.highlight_outer(&mut t1_out, &mut t2_out, path, sub, i, &other_ty);
543 /// Add a `,` to the type representation only if it is appropriate.
545 value: &mut DiagnosticStyledString,
546 other_value: &mut DiagnosticStyledString,
549 if len > 0 && pos != len - 1 {
550 value.push_normal(", ");
551 other_value.push_normal(", ");
555 /// Compare two given types, eliding parts that are the same between them and highlighting
556 /// relevant differences, and return two representation of those types for highlighted printing.
557 fn cmp(&self, t1: Ty<'tcx>, t2: Ty<'tcx>)
558 -> (DiagnosticStyledString, DiagnosticStyledString)
560 match (&t1.sty, &t2.sty) {
561 (&ty::TyAdt(def1, sub1), &ty::TyAdt(def2, sub2)) => {
562 let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
563 let path1 = self.tcx.item_path_str(def1.did.clone());
564 let path2 = self.tcx.item_path_str(def2.did.clone());
565 if def1.did == def2.did {
566 // Easy case. Replace same types with `_` to shorten the output and highlight
567 // the differing ones.
568 // let x: Foo<Bar, Qux> = y::<Foo<Quz, Qux>>();
571 // --- ^ type argument elided
573 // highlighted in output
574 values.0.push_normal(path1);
575 values.1.push_normal(path2);
577 // Only draw `<...>` if there're lifetime/type arguments.
578 let len = sub1.len();
580 values.0.push_normal("<");
581 values.1.push_normal("<");
584 fn lifetime_display(lifetime: Region) -> String {
585 let s = format!("{}", lifetime);
592 // At one point we'd like to elide all lifetimes here, they are irrelevant for
593 // all diagnostics that use this output
597 // ^^ ^^ --- type arguments are not elided
599 // | elided as they were the same
600 // not elided, they were different, but irrelevant
601 let lifetimes = sub1.regions().zip(sub2.regions());
602 for (i, lifetimes) in lifetimes.enumerate() {
603 let l1 = lifetime_display(lifetimes.0);
604 let l2 = lifetime_display(lifetimes.1);
606 values.0.push_normal("'_");
607 values.1.push_normal("'_");
609 values.0.push_highlighted(l1);
610 values.1.push_highlighted(l2);
612 self.push_comma(&mut values.0, &mut values.1, len, i);
615 // We're comparing two types with the same path, so we compare the type
616 // arguments for both. If they are the same, do not highlight and elide from the
620 // ^ elided type as this type argument was the same in both sides
621 let type_arguments = sub1.types().zip(sub2.types());
622 let regions_len = sub1.regions().collect::<Vec<_>>().len();
623 for (i, (ta1, ta2)) in type_arguments.enumerate() {
624 let i = i + regions_len;
626 values.0.push_normal("_");
627 values.1.push_normal("_");
629 let (x1, x2) = self.cmp(ta1, ta2);
630 (values.0).0.extend(x1.0);
631 (values.1).0.extend(x2.0);
633 self.push_comma(&mut values.0, &mut values.1, len, i);
636 // Close the type argument bracket.
637 // Only draw `<...>` if there're lifetime/type arguments.
639 values.0.push_normal(">");
640 values.1.push_normal(">");
645 // let x: Foo<Bar<Qux> = foo::<Bar<Qux>>();
647 // ------- this type argument is exactly the same as the other type
649 if self.cmp_type_arg(&mut values.0,
658 // let x: Bar<Qux> = y:<Foo<Bar<Qux>>>();
661 // ------- this type argument is exactly the same as the other type
662 if self.cmp_type_arg(&mut values.1,
671 // We couldn't find anything in common, highlight everything.
672 // let x: Bar<Qux> = y::<Foo<Zar>>();
673 (DiagnosticStyledString::highlighted(format!("{}", t1)),
674 DiagnosticStyledString::highlighted(format!("{}", t2)))
679 // The two types are the same, elide and don't highlight.
680 (DiagnosticStyledString::normal("_"), DiagnosticStyledString::normal("_"))
682 // We couldn't find anything in common, highlight everything.
683 (DiagnosticStyledString::highlighted(format!("{}", t1)),
684 DiagnosticStyledString::highlighted(format!("{}", t2)))
690 pub fn note_type_err(&self,
691 diag: &mut DiagnosticBuilder<'tcx>,
692 cause: &ObligationCause<'tcx>,
693 secondary_span: Option<(Span, String)>,
694 values: Option<ValuePairs<'tcx>>,
695 terr: &TypeError<'tcx>)
697 let (expected_found, exp_found, is_simple_error) = match values {
698 None => (None, None, false),
700 let (is_simple_error, exp_found) = match values {
701 ValuePairs::Types(exp_found) => {
702 let is_simple_err = exp_found.expected.is_primitive()
703 && exp_found.found.is_primitive();
705 (is_simple_err, Some(exp_found))
709 let vals = match self.values_str(&values) {
710 Some((expected, found)) => Some((expected, found)),
712 // Derived error. Cancel the emitter.
713 self.tcx.sess.diagnostic().cancel(diag);
717 (vals, exp_found, is_simple_error)
721 let span = cause.span;
723 diag.span_label(span, terr.to_string());
724 if let Some((sp, msg)) = secondary_span {
725 diag.span_label(sp, msg);
728 if let Some((expected, found)) = expected_found {
729 match (terr, is_simple_error, expected == found) {
730 (&TypeError::Sorts(ref values), false, true) => {
731 diag.note_expected_found_extra(
732 &"type", expected, found,
733 &format!(" ({})", values.expected.sort_string(self.tcx)),
734 &format!(" ({})", values.found.sort_string(self.tcx)));
737 if let Some(exp_found) = exp_found {
738 let (def_id, ret_ty) = match exp_found.found.sty {
739 TypeVariants::TyFnDef(def, _) => {
740 (Some(def), Some(self.tcx.fn_sig(def).output()))
745 let exp_is_struct = match exp_found.expected.sty {
746 TypeVariants::TyAdt(def, _) => def.is_struct(),
750 if let (Some(def_id), Some(ret_ty)) = (def_id, ret_ty) {
751 if exp_is_struct && exp_found.expected == ret_ty.0 {
752 let message = format!(
753 "did you mean `{}(/* fields */)`?",
754 self.tcx.item_path_str(def_id)
756 diag.span_label(cause.span, message);
761 diag.note_expected_found(&"type", expected, found);
767 self.note_error_origin(diag, &cause);
768 self.check_and_note_conflicting_crates(diag, terr, span);
769 self.tcx.note_and_explain_type_err(diag, terr, span);
772 pub fn report_and_explain_type_error(&self,
773 trace: TypeTrace<'tcx>,
774 terr: &TypeError<'tcx>)
775 -> DiagnosticBuilder<'tcx>
777 let span = trace.cause.span;
778 let failure_str = trace.cause.as_failure_str();
779 let mut diag = match trace.cause.code {
780 ObligationCauseCode::IfExpressionWithNoElse => {
781 struct_span_err!(self.tcx.sess, span, E0317, "{}", failure_str)
783 ObligationCauseCode::MainFunctionType => {
784 struct_span_err!(self.tcx.sess, span, E0580, "{}", failure_str)
787 struct_span_err!(self.tcx.sess, span, E0308, "{}", failure_str)
790 self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr);
794 fn values_str(&self, values: &ValuePairs<'tcx>)
795 -> Option<(DiagnosticStyledString, DiagnosticStyledString)>
798 infer::Types(ref exp_found) => self.expected_found_str_ty(exp_found),
799 infer::TraitRefs(ref exp_found) => self.expected_found_str(exp_found),
800 infer::PolyTraitRefs(ref exp_found) => self.expected_found_str(exp_found),
804 fn expected_found_str_ty(&self,
805 exp_found: &ty::error::ExpectedFound<Ty<'tcx>>)
806 -> Option<(DiagnosticStyledString, DiagnosticStyledString)> {
807 let exp_found = self.resolve_type_vars_if_possible(exp_found);
808 if exp_found.references_error() {
812 Some(self.cmp(exp_found.expected, exp_found.found))
815 /// Returns a string of the form "expected `{}`, found `{}`".
816 fn expected_found_str<T: fmt::Display + TypeFoldable<'tcx>>(
818 exp_found: &ty::error::ExpectedFound<T>)
819 -> Option<(DiagnosticStyledString, DiagnosticStyledString)>
821 let exp_found = self.resolve_type_vars_if_possible(exp_found);
822 if exp_found.references_error() {
826 Some((DiagnosticStyledString::highlighted(format!("{}", exp_found.expected)),
827 DiagnosticStyledString::highlighted(format!("{}", exp_found.found))))
830 fn report_generic_bound_failure(&self,
831 region_scope_tree: ®ion::ScopeTree,
832 origin: SubregionOrigin<'tcx>,
833 bound_kind: GenericKind<'tcx>,
836 // Attempt to obtain the span of the parameter so we can
837 // suggest adding an explicit lifetime bound to it.
838 let type_param_span = match (self.in_progress_tables, bound_kind) {
839 (Some(ref table), GenericKind::Param(ref param)) => {
840 let table = table.borrow();
841 table.local_id_root.and_then(|did| {
842 let generics = self.tcx.generics_of(did);
843 // Account for the case where `did` corresponds to `Self`, which doesn't have
844 // the expected type argument.
845 if !param.is_self() {
846 let type_param = generics.type_param(param, self.tcx);
847 let hir = &self.tcx.hir;
848 hir.as_local_node_id(type_param.def_id).map(|id| {
849 // Get the `hir::TyParam` to verify wether it already has any bounds.
850 // We do this to avoid suggesting code that ends up as `T: 'a'b`,
851 // instead we suggest `T: 'a + 'b` in that case.
852 let has_lifetimes = if let hir_map::NodeTyParam(ref p) = hir.get(id) {
857 let sp = hir.span(id);
858 // `sp` only covers `T`, change it so that it covers
859 // `T:` when appropriate
860 let sp = if has_lifetimes {
861 sp.to(sp.next_point().next_point())
875 let labeled_user_string = match bound_kind {
876 GenericKind::Param(ref p) =>
877 format!("the parameter type `{}`", p),
878 GenericKind::Projection(ref p) =>
879 format!("the associated type `{}`", p),
882 if let SubregionOrigin::CompareImplMethodObligation {
883 span, item_name, impl_item_def_id, trait_item_def_id, lint_id
885 self.report_extra_impl_obligation(span,
889 &format!("`{}: {}`", bound_kind, sub),
895 fn binding_suggestion<'tcx, S: fmt::Display>(err: &mut DiagnosticBuilder<'tcx>,
896 type_param_span: Option<(Span, bool)>,
897 bound_kind: GenericKind<'tcx>,
899 let consider = &format!("consider adding an explicit lifetime bound `{}: {}`...",
902 if let Some((sp, has_lifetimes)) = type_param_span {
903 let tail = if has_lifetimes {
908 let suggestion = format!("{}: {}{}", bound_kind, sub, tail);
909 err.span_suggestion_short(sp, consider, suggestion);
915 let mut err = match *sub {
916 ty::ReEarlyBound(_) |
917 ty::ReFree(ty::FreeRegion {bound_region: ty::BrNamed(..), ..}) => {
918 // Does the required lifetime have a nice name we can print?
919 let mut err = struct_span_err!(self.tcx.sess,
922 "{} may not live long enough",
923 labeled_user_string);
924 binding_suggestion(&mut err, type_param_span, bound_kind, sub);
929 // Does the required lifetime have a nice name we can print?
930 let mut err = struct_span_err!(self.tcx.sess,
933 "{} may not live long enough",
934 labeled_user_string);
935 binding_suggestion(&mut err, type_param_span, bound_kind, "'static");
940 // If not, be less specific.
941 let mut err = struct_span_err!(self.tcx.sess,
944 "{} may not live long enough",
945 labeled_user_string);
946 err.help(&format!("consider adding an explicit lifetime bound for `{}`",
948 self.tcx.note_and_explain_region(
951 &format!("{} must be valid for ", labeled_user_string),
958 self.note_region_origin(&mut err, &origin);
962 fn report_sub_sup_conflict(&self,
963 region_scope_tree: ®ion::ScopeTree,
964 var_origin: RegionVariableOrigin,
965 sub_origin: SubregionOrigin<'tcx>,
966 sub_region: Region<'tcx>,
967 sup_origin: SubregionOrigin<'tcx>,
968 sup_region: Region<'tcx>) {
969 let mut err = self.report_inference_failure(var_origin);
971 self.tcx.note_and_explain_region(region_scope_tree, &mut err,
972 "first, the lifetime cannot outlive ",
976 self.note_region_origin(&mut err, &sup_origin);
978 self.tcx.note_and_explain_region(region_scope_tree, &mut err,
979 "but, the lifetime must be valid for ",
983 self.note_region_origin(&mut err, &sub_origin);
988 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
989 fn report_inference_failure(&self,
990 var_origin: RegionVariableOrigin)
991 -> DiagnosticBuilder<'tcx> {
992 let br_string = |br: ty::BoundRegion| {
993 let mut s = br.to_string();
999 let var_description = match var_origin {
1000 infer::MiscVariable(_) => "".to_string(),
1001 infer::PatternRegion(_) => " for pattern".to_string(),
1002 infer::AddrOfRegion(_) => " for borrow expression".to_string(),
1003 infer::Autoref(_) => " for autoref".to_string(),
1004 infer::Coercion(_) => " for automatic coercion".to_string(),
1005 infer::LateBoundRegion(_, br, infer::FnCall) => {
1006 format!(" for lifetime parameter {}in function call",
1009 infer::LateBoundRegion(_, br, infer::HigherRankedType) => {
1010 format!(" for lifetime parameter {}in generic type", br_string(br))
1012 infer::LateBoundRegion(_, br, infer::AssocTypeProjection(def_id)) => {
1013 format!(" for lifetime parameter {}in trait containing associated type `{}`",
1014 br_string(br), self.tcx.associated_item(def_id).name)
1016 infer::EarlyBoundRegion(_, name) => {
1017 format!(" for lifetime parameter `{}`",
1020 infer::BoundRegionInCoherence(name) => {
1021 format!(" for lifetime parameter `{}` in coherence check",
1024 infer::UpvarRegion(ref upvar_id, _) => {
1025 let var_node_id = self.tcx.hir.hir_to_node_id(upvar_id.var_id);
1026 let var_name = self.tcx.hir.name(var_node_id);
1027 format!(" for capture of `{}` by closure", var_name)
1031 struct_span_err!(self.tcx.sess, var_origin.span(), E0495,
1032 "cannot infer an appropriate lifetime{} \
1033 due to conflicting requirements",
1038 impl<'tcx> ObligationCause<'tcx> {
1039 fn as_failure_str(&self) -> &'static str {
1040 use traits::ObligationCauseCode::*;
1042 CompareImplMethodObligation { .. } => "method not compatible with trait",
1043 MatchExpressionArm { source, .. } => match source {
1044 hir::MatchSource::IfLetDesugar{..} => "`if let` arms have incompatible types",
1045 _ => "match arms have incompatible types",
1047 IfExpression => "if and else have incompatible types",
1048 IfExpressionWithNoElse => "if may be missing an else clause",
1049 EquatePredicate => "equality predicate not satisfied",
1050 MainFunctionType => "main function has wrong type",
1051 StartFunctionType => "start function has wrong type",
1052 IntrinsicType => "intrinsic has wrong type",
1053 MethodReceiver => "mismatched method receiver",
1054 _ => "mismatched types",
1058 fn as_requirement_str(&self) -> &'static str {
1059 use traits::ObligationCauseCode::*;
1061 CompareImplMethodObligation { .. } => "method type is compatible with trait",
1062 ExprAssignable => "expression is assignable",
1063 MatchExpressionArm { source, .. } => match source {
1064 hir::MatchSource::IfLetDesugar{..} => "`if let` arms have compatible types",
1065 _ => "match arms have compatible types",
1067 IfExpression => "if and else have compatible types",
1068 IfExpressionWithNoElse => "if missing an else returns ()",
1069 EquatePredicate => "equality where clause is satisfied",
1070 MainFunctionType => "`main` function has the correct type",
1071 StartFunctionType => "`start` function has the correct type",
1072 IntrinsicType => "intrinsic has the correct type",
1073 MethodReceiver => "method receiver has the correct type",
1074 _ => "types are compatible",