1 //! Error Reporting for Anonymous Region Lifetime Errors
2 //! where both the regions are anonymous.
4 use crate::infer::error_reporting::nice_region_error::util::AnonymousParamInfo;
5 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
6 use rustc::util::common::ErrorReported;
8 use rustc_errors::struct_span_err;
10 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
11 /// Print the error message for lifetime errors when both the concerned regions are anonymous.
13 /// Consider a case where we have
16 /// fn foo(x: &mut Vec<&u8>, y: &u8) {
24 /// fn foo(x: &mut Vec<&u8>, y: &u8) {
25 /// --- --- these references are declared with different lifetimes...
27 /// ^ ...but data from `y` flows into `x` here
30 /// It has been extended for the case of structs too.
32 /// Consider the example
35 /// struct Ref<'a> { x: &'a u32 }
39 /// fn foo(mut x: Vec<Ref>, y: Ref) {
40 /// --- --- these structs are declared with different lifetimes...
42 /// ^ ...but data from `y` flows into `x` here
46 /// It will later be extended to trait objects.
47 pub(super) fn try_report_anon_anon_conflict(&self) -> Option<ErrorReported> {
48 let (span, sub, sup) = self.regions()?;
50 // Determine whether the sub and sup consist of both anonymous (elided) regions.
51 let anon_reg_sup = self.tcx().is_suitable_region(sup)?;
53 let anon_reg_sub = self.tcx().is_suitable_region(sub)?;
54 let scope_def_id_sup = anon_reg_sup.def_id;
55 let bregion_sup = anon_reg_sup.boundregion;
56 let scope_def_id_sub = anon_reg_sub.def_id;
57 let bregion_sub = anon_reg_sub.boundregion;
59 let ty_sup = self.find_anon_type(sup, &bregion_sup)?;
61 let ty_sub = self.find_anon_type(sub, &bregion_sub)?;
64 "try_report_anon_anon_conflict: found_param1={:?} sup={:?} br1={:?}",
65 ty_sub, sup, bregion_sup
68 "try_report_anon_anon_conflict: found_param2={:?} sub={:?} br2={:?}",
69 ty_sup, sub, bregion_sub
72 let (ty_sup, ty_fndecl_sup) = ty_sup;
73 let (ty_sub, ty_fndecl_sub) = ty_sub;
75 let AnonymousParamInfo { param: anon_param_sup, .. } =
76 self.find_param_with_region(sup, sup)?;
77 let AnonymousParamInfo { param: anon_param_sub, .. } =
78 self.find_param_with_region(sub, sub)?;
81 self.is_return_type_anon(scope_def_id_sup, bregion_sup, ty_fndecl_sup);
83 self.is_return_type_anon(scope_def_id_sub, bregion_sub, ty_fndecl_sub);
85 let span_label_var1 = match anon_param_sup.pat.simple_ident() {
86 Some(simple_ident) => format!(" from `{}`", simple_ident),
87 None => String::new(),
90 let span_label_var2 = match anon_param_sub.pat.simple_ident() {
91 Some(simple_ident) => format!(" into `{}`", simple_ident),
92 None => String::new(),
95 let (span_1, span_2, main_label, span_label) = match (sup_is_ret_type, sub_is_ret_type) {
97 let (main_label_1, span_label_1) = if ty_sup.hir_id == ty_sub.hir_id {
99 "this type is declared with multiple lifetimes...".to_owned(),
100 "...but data with one lifetime flows into the other here".to_owned(),
104 "these two types are declared with different lifetimes...".to_owned(),
105 format!("...but data{} flows{} here", span_label_var1, span_label_var2),
108 (ty_sup.span, ty_sub.span, main_label_1, span_label_1)
111 (Some(ret_span), _) => (
114 "this parameter and the return type are declared \
115 with different lifetimes..."
117 format!("...but data{} is returned here", span_label_var1),
119 (_, Some(ret_span)) => (
122 "this parameter and the return type are declared \
123 with different lifetimes..."
125 format!("...but data{} is returned here", span_label_var1),
129 struct_span_err!(self.tcx().sess, span, E0623, "lifetime mismatch")
130 .span_label(span_1, main_label)
131 .span_label(span_2, String::new())
132 .span_label(span, span_label)
134 return Some(ErrorReported);