about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorgaurikholkar <f2013002@goa.bits-pilani.ac.in>2017-09-11 19:59:57 +0530
committerNiko Matsakis <niko@alum.mit.edu>2017-09-26 11:55:52 -0400
commite58f528bb0664c33355ff6b0aa125b8a751fee2a (patch)
tree2bea64ce73bddceed3317e3d00ed1d715a3c0c2a /src
parent1c4510adc81bd7623bc2993b42ee7d87320f1f2b (diff)
downloadrust-e58f528bb0664c33355ff6b0aa125b8a751fee2a.tar.gz
rust-e58f528bb0664c33355ff6b0aa125b8a751fee2a.zip
merge fixes, addressing CR comments
Diffstat (limited to 'src')
-rw-r--r--src/librustc/infer/error_reporting/different_lifetimes.rs132
-rw-r--r--src/librustc/infer/error_reporting/named_anon_conflict.rs53
-rw-r--r--src/librustc/infer/error_reporting/util.rs11
-rw-r--r--src/test/ui/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr26
-rw-r--r--src/test/ui/lifetime-errors/ex1-return-one-existing-name-self-is-anon.stderr26
-rw-r--r--src/test/ui/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr24
-rw-r--r--src/test/ui/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr24
7 files changed, 138 insertions, 158 deletions
diff --git a/src/librustc/infer/error_reporting/different_lifetimes.rs b/src/librustc/infer/error_reporting/different_lifetimes.rs
index 6c57130a995..687b518ef6a 100644
--- a/src/librustc/infer/error_reporting/different_lifetimes.rs
+++ b/src/librustc/infer/error_reporting/different_lifetimes.rs
@@ -18,6 +18,7 @@ use infer::region_inference::RegionResolutionError;
 use hir::map as hir_map;
 use middle::resolve_lifetime as rl;
 use hir::intravisit::{self, Visitor, NestedVisitorMap};
+use infer::error_reporting::util::AnonymousArgInfo;
 
 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
     // This method prints the error message for lifetime errors when both the concerned regions
@@ -57,6 +58,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
         let ty_sup = or_false!(self.find_anon_type(sup, &bregion_sup));
 
         let ty_sub = or_false!(self.find_anon_type(sub, &bregion_sub));
+
         debug!("try_report_anon_anon_conflict: found_arg1={:?} sup={:?} br1={:?}",
                ty_sub,
                sup,
@@ -66,56 +68,78 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
                sub,
                bregion_sub);
 
-        let (main_label, label1, label2) = if let (Some(sup_arg), Some(sub_arg)) =
-            (self.find_arg_with_region(sup, sup), self.find_arg_with_region(sub, sub)) {
+        let (ty_sup, ty_fndecl_sup) = ty_sup;
+        let (ty_sub, ty_fndecl_sub) = ty_sub;
 
-            let (anon_arg_sup, is_first_sup, anon_arg_sub, is_first_sub) =
-                (sup_arg.arg, sup_arg.is_first, sub_arg.arg, sub_arg.is_first);
-            if self.is_self_anon(is_first_sup, scope_def_id_sup) ||
-               self.is_self_anon(is_first_sub, scope_def_id_sub) {
-                return false;
-            }
+        let AnonymousArgInfo { arg: anon_arg_sup, .. } =
+            or_false!(self.find_arg_with_region(sup, sup));
+        let AnonymousArgInfo { arg: anon_arg_sub, .. } =
+            or_false!(self.find_arg_with_region(sub, sub));
 
-            if self.is_return_type_anon(scope_def_id_sup, bregion_sup) ||
-               self.is_return_type_anon(scope_def_id_sub, bregion_sub) {
-                return false;
-            }
+        let sup_is_ret_type =
+            self.is_return_type_anon(scope_def_id_sup, bregion_sup, ty_fndecl_sup);
+        let sub_is_ret_type =
+            self.is_return_type_anon(scope_def_id_sub, bregion_sub, ty_fndecl_sub);
 
-            if anon_arg_sup == anon_arg_sub {
-                (format!("this type was declared with multiple lifetimes..."),
-                 format!(" with one lifetime"),
-                 format!(" into the other"))
-            } else {
-                let span_label_var1 = if let Some(simple_name) = anon_arg_sup.pat.simple_name() {
-                    format!(" from `{}`", simple_name)
-                } else {
-                    format!("")
-                };
+        let span_label_var1 = if let Some(simple_name) = anon_arg_sup.pat.simple_name() {
+            format!(" flows from `{}`", simple_name)
+        } else {
+            format!("")
+        };
+
+        let span_label_var2 = if let Some(simple_name) = anon_arg_sub.pat.simple_name() {
+            format!(" into `{}`", simple_name)
+        } else {
+            format!("")
+        };
 
-                let span_label_var2 = if let Some(simple_name) = anon_arg_sub.pat.simple_name() {
-                    format!(" into `{}`", simple_name)
-                } else {
-                    format!("")
-                };
 
-                let span_label =
-                    format!("these two types are declared with different lifetimes...",);
+        let (span_1, span_2, main_label, span_label) = match (sup_is_ret_type, sub_is_ret_type) {
+            (None, None) => {
+                let (main_label_1, span_label_1) = if ty_sup == ty_sub {
 
-                (span_label, span_label_var1, span_label_var2)
+                    (format!("this type was declared with multiple lifetimes..."),
+                     format!("...but data{} flows{} here",
+                             format!(" with one lifetime"),
+                             format!(" into the other")))
+                } else {
+                    (format!("these two types was declared with multiple lifetimes..."),
+                     format!("...but data{} flows{} here",
+                             span_label_var1,
+                             span_label_var2))
+                };
+                (ty_sup.span, ty_sub.span, main_label_1, span_label_1)
+            }
+            (Some(ret_span1), Some(ret_span2)) => {
+                (ret_span1,
+                 ret_span2,
+                 format!("the return type is declared with different lifetimes..."),
+                 format!("...but data{} flows{} here",
+                         format!(" with one lifetime"),
+                         format!(" into the other")))
+            }
+            
+            (Some(ret_span), _) => {
+                (ty_sub.span,
+                 ret_span,
+                 format!("this parameter and the return type are declared
+                  with different lifetimes...",),
+                 format!("...but data{} is returned here", span_label_var1))
+            }
+            (_, Some(ret_span)) => {
+                (ty_sup.span,
+                 ret_span,
+                 format!("this parameter and the return type are declared
+                  with different lifetimes...",),
+                 format!("...but data{} is returned here", span_label_var1))
             }
-        } else {
-            debug!("no arg with anon region found");
-            debug!("try_report_anon_anon_conflict: is_suitable(sub) = {:?}",
-                   self.is_suitable_region(sub));
-            debug!("try_report_anon_anon_conflict: is_suitable(sup) = {:?}",
-                   self.is_suitable_region(sup));
-            return false;
         };
 
+
         struct_span_err!(self.tcx.sess, span, E0623, "lifetime mismatch")
-            .span_label(ty_sup.span, main_label)
-            .span_label(ty_sub.span, format!(""))
-            .span_label(span, format!("...but data{} flows{} here", label1, label2))
+            .span_label(span_1, main_label)
+            .span_label(span_2, format!(""))
+            .span_label(span, span_label)
             .emit();
         return true;
     }
@@ -135,28 +159,32 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
     /// ```
     /// The function returns the nested type corresponding to the anonymous region
     /// for e.g. `&u8` and Vec<`&u8`.
-    pub fn find_anon_type(&self, region: Region<'tcx>, br: &ty::BoundRegion) -> Option<&hir::Ty> {
+    pub fn find_anon_type(&self,
+                          region: Region<'tcx>,
+                          br: &ty::BoundRegion)
+                          -> Option<(&hir::Ty, &hir::FnDecl)> {
         if let Some(anon_reg) = self.is_suitable_region(region) {
             let def_id = anon_reg.def_id;
             if let Some(node_id) = self.tcx.hir.as_local_node_id(def_id) {
-                let inputs: &[_] = match self.tcx.hir.get(node_id) {
+                let fndecl = match self.tcx.hir.get(node_id) {
                     hir_map::NodeItem(&hir::Item { node: hir::ItemFn(ref fndecl, ..), .. }) => {
-                        &fndecl.inputs
+                        &fndecl
                     }
                     hir_map::NodeTraitItem(&hir::TraitItem {
-                                               node: hir::TraitItemKind::Method(ref fndecl, ..), ..
-                                           }) => &fndecl.decl.inputs,
+                                               node: hir::TraitItemKind::Method(ref m, ..), ..
+                                           }) |
                     hir_map::NodeImplItem(&hir::ImplItem {
-                                              node: hir::ImplItemKind::Method(ref fndecl, ..), ..
-                                          }) => &fndecl.decl.inputs,
-
-                    _ => &[],
+                                              node: hir::ImplItemKind::Method(ref m, ..), ..
+                                          }) => &m.decl,
+                    _ => return None,
                 };
 
-                return inputs
+                return fndecl
+                           .inputs
                            .iter()
-                           .filter_map(|arg| self.find_component_for_bound_region(&**arg, br))
-                           .next();
+                           .filter_map(|arg| self.find_component_for_bound_region(arg, br))
+                           .next()
+                           .map(|ty| (ty, &**fndecl));
             }
         }
         None
diff --git a/src/librustc/infer/error_reporting/named_anon_conflict.rs b/src/librustc/infer/error_reporting/named_anon_conflict.rs
index a3bbdab497a..7027c868e6e 100644
--- a/src/librustc/infer/error_reporting/named_anon_conflict.rs
+++ b/src/librustc/infer/error_reporting/named_anon_conflict.rs
@@ -35,15 +35,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
         // only introduced anonymous regions in parameters) as well as a
         // version new_ty of its type where the anonymous region is replaced
         // with the named one.//scope_def_id
-        let (named, anon_arg_info, region_info) =
+        let (named, anon, anon_arg_info, region_info) =
             if self.is_named_region(sub) && self.is_suitable_region(sup).is_some() &&
                self.find_arg_with_region(sup, sub).is_some() {
                 (sub,
+                 sup,
                  self.find_arg_with_region(sup, sub).unwrap(),
                  self.is_suitable_region(sup).unwrap())
             } else if self.is_named_region(sup) && self.is_suitable_region(sub).is_some() &&
                       self.find_arg_with_region(sub, sup).is_some() {
                 (sup,
+                 sub,
                  self.find_arg_with_region(sub, sup).unwrap(),
                  self.is_suitable_region(sub).unwrap())
             } else {
@@ -76,33 +78,30 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
             return false;
         }
 
-        if self.is_return_type_anon(scope_def_id, br) {
-            debug!("try_report_named_anon_conflict: is_return_type_anon({:?}, {:?}) = true",
-                   scope_def_id,
-                   br);
-            return false;
-        } else if self.is_self_anon(is_first, scope_def_id) {
-            debug!("try_report_named_anon_conflict: is_self_anon({:?}, {:?}) = true",
-                   is_first,
-                   scope_def_id);
-            return false;
+        if let Some(anon_ty) = self.find_anon_type(anon, &br) {
+            let (_, fndecl) = anon_ty;
+            if self.is_return_type_anon(scope_def_id, br, fndecl).is_some() ||
+               self.is_self_anon(is_first, scope_def_id) {
+                return false;
+            }
+        }
+
+        let (error_var, span_label_var) = if let Some(simple_name) = arg.pat.simple_name() {
+            (format!("the type of `{}`", simple_name), format!("the type of `{}`", simple_name))
         } else {
-            let (error_var, span_label_var) = if let Some(simple_name) = arg.pat.simple_name() {
-                (format!("the type of `{}`", simple_name), format!("the type of `{}`", simple_name))
-            } else {
-                ("parameter type".to_owned(), "type".to_owned())
-            };
+            ("parameter type".to_owned(), "type".to_owned())
+        };
+
+        struct_span_err!(self.tcx.sess,
+                         span,
+                         E0621,
+                         "explicit lifetime required in {}",
+                         error_var)
+                .span_label(arg.pat.span,
+                            format!("consider changing {} to `{}`", span_label_var, new_ty))
+                .span_label(span, format!("lifetime `{}` required", named))
+                .emit();
+        return true;
 
-            struct_span_err!(self.tcx.sess,
-                             span,
-                             E0621,
-                             "explicit lifetime required in {}",
-                             error_var)
-                    .span_label(arg.pat.span,
-                                format!("consider changing {} to `{}`", span_label_var, new_ty))
-                    .span_label(span, format!("lifetime `{}` required", named))
-                    .emit();
-            return true;
-        }
     }
 }
diff --git a/src/librustc/infer/error_reporting/util.rs b/src/librustc/infer/error_reporting/util.rs
index 94faec464b2..47db3f1b792 100644
--- a/src/librustc/infer/error_reporting/util.rs
+++ b/src/librustc/infer/error_reporting/util.rs
@@ -15,6 +15,7 @@ use infer::InferCtxt;
 use ty::{self, Region, Ty};
 use hir::def_id::DefId;
 use hir::map as hir_map;
+use syntax_pos::Span;
 
 macro_rules! or_false {
      ($v:expr) => {
@@ -163,7 +164,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
     // Here, we check for the case where the anonymous region
     // is in the return type.
     // FIXME(#42703) - Need to handle certain cases here.
-    pub fn is_return_type_anon(&self, scope_def_id: DefId, br: ty::BoundRegion) -> bool {
+    pub fn is_return_type_anon(&self,
+                               scope_def_id: DefId,
+                               br: ty::BoundRegion,
+                               decl: &hir::FnDecl)
+                               -> Option<Span> {
         let ret_ty = self.tcx.type_of(scope_def_id);
         match ret_ty.sty {
             ty::TyFnDef(_, _) => {
@@ -171,12 +176,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
                 let late_bound_regions = self.tcx
                     .collect_referenced_late_bound_regions(&sig.output());
                 if late_bound_regions.iter().any(|r| *r == br) {
-                    return true;
+                    return Some(decl.output.span());
                 }
             }
             _ => {}
         }
-        false
+        None
     }
     // Here we check for the case where anonymous region
     // corresponds to self and if yes, we display E0312.
diff --git a/src/test/ui/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr
index e3fd0192053..4a1673a531d 100644
--- a/src/test/ui/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr
+++ b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr
@@ -1,27 +1,11 @@
-error[E0312]: lifetime of reference outlives lifetime of borrowed content...
+error[E0621]: explicit lifetime required in the type of `self`
   --> $DIR/ex1-return-one-existing-name-return-type-is-anon.rs:18:5
    |
+16 |   fn foo<'a>(&self, x: &'a i32) -> &i32 {
+   |              ----- consider changing the type of `self` to `&'a Foo`
+17 | 
 18 |     x
-   |     ^
-   |
-note: ...the reference is valid for the anonymous lifetime #1 defined on the method body at 16:3...
-  --> $DIR/ex1-return-one-existing-name-return-type-is-anon.rs:16:3
-   |
-16 | /   fn foo<'a>(&self, x: &'a i32) -> &i32 {
-17 | |
-18 | |     x
-19 | |
-20 | |   }
-   | |___^
-note: ...but the borrowed content is only valid for the lifetime 'a as defined on the method body at 16:3
-  --> $DIR/ex1-return-one-existing-name-return-type-is-anon.rs:16:3
-   |
-16 | /   fn foo<'a>(&self, x: &'a i32) -> &i32 {
-17 | |
-18 | |     x
-19 | |
-20 | |   }
-   | |___^
+   |     ^ lifetime `'a` required
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/lifetime-errors/ex1-return-one-existing-name-self-is-anon.stderr b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-self-is-anon.stderr
index 8551f015db5..973c5ed72f8 100644
--- a/src/test/ui/lifetime-errors/ex1-return-one-existing-name-self-is-anon.stderr
+++ b/src/test/ui/lifetime-errors/ex1-return-one-existing-name-self-is-anon.stderr
@@ -1,27 +1,11 @@
-error[E0312]: lifetime of reference outlives lifetime of borrowed content...
+error[E0621]: explicit lifetime required in the type of `self`
   --> $DIR/ex1-return-one-existing-name-self-is-anon.rs:18:30
    |
+16 |     fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
+   |                ----- consider changing the type of `self` to `&'a Foo`
+17 | 
 18 |         if true { x } else { self }
-   |                              ^^^^
-   |
-note: ...the reference is valid for the lifetime 'a as defined on the method body at 16:5...
-  --> $DIR/ex1-return-one-existing-name-self-is-anon.rs:16:5
-   |
-16 | /     fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
-17 | |
-18 | |         if true { x } else { self }
-19 | |
-20 | |     }
-   | |_____^
-note: ...but the borrowed content is only valid for the anonymous lifetime #1 defined on the method body at 16:5
-  --> $DIR/ex1-return-one-existing-name-self-is-anon.rs:16:5
-   |
-16 | /     fn foo<'a>(&self, x: &'a Foo) -> &'a Foo {
-17 | |
-18 | |         if true { x } else { self }
-19 | |
-20 | |     }
-   | |_____^
+   |                              ^^^^ lifetime `'a` required
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr b/src/test/ui/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr
index 890f9b311e7..c6b1280ca95 100644
--- a/src/test/ui/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr
+++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-return-type-is-anon.stderr
@@ -1,23 +1,13 @@
-error[E0312]: lifetime of reference outlives lifetime of borrowed content...
+error[E0621]: explicit lifetime required in the type of `self`
   --> $DIR/ex3-both-anon-regions-return-type-is-anon.rs:17:5
    |
+16 |   fn foo<'a>(&self, x: &i32) -> &i32 {
+   |                        ----     ----
+   |                        |
+   |                        this parameter and the return type are
+                            declared with different lifetimes...
 17 |     x
-   |     ^
-   |
-note: ...the reference is valid for the anonymous lifetime #1 defined on the method body at 16:3...
-  --> $DIR/ex3-both-anon-regions-return-type-is-anon.rs:16:3
-   |
-16 | /   fn foo<'a>(&self, x: &i32) -> &i32 {
-17 | |     x
-18 | |   }
-   | |___^
-note: ...but the borrowed content is only valid for the anonymous lifetime #2 defined on the method body at 16:3
-  --> $DIR/ex3-both-anon-regions-return-type-is-anon.rs:16:3
-   |
-16 | /   fn foo<'a>(&self, x: &i32) -> &i32 {
-17 | |     x
-18 | |   }
-   | |___^
+   |     ^ ...but data from `x` is returned here
 
 error: aborting due to previous error
 
diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr b/src/test/ui/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr
index 43f00c32c62..9b15de1b173 100644
--- a/src/test/ui/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr
+++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-self-is-anon.stderr
@@ -1,23 +1,13 @@
-error[E0312]: lifetime of reference outlives lifetime of borrowed content...
+error[E0623]: lifetime mismatch
   --> $DIR/ex3-both-anon-regions-self-is-anon.rs:17:19
    |
+16 |     fn foo<'a>(&self, x: &Foo) -> &Foo {
+   |                          ----     ----
+   |                          |
+   |                          this parameter and the return type are
+                            declared with different lifetimes...
 17 |         if true { x } else { self }
-   |                   ^
-   |
-note: ...the reference is valid for the anonymous lifetime #1 defined on the method body at 16:5...
-  --> $DIR/ex3-both-anon-regions-self-is-anon.rs:16:5
-   |
-16 | /     fn foo<'a>(&self, x: &Foo) -> &Foo {
-17 | |         if true { x } else { self }
-18 | |     }
-   | |_____^
-note: ...but the borrowed content is only valid for the anonymous lifetime #2 defined on the method body at 16:5
-  --> $DIR/ex3-both-anon-regions-self-is-anon.rs:16:5
-   |
-16 | /     fn foo<'a>(&self, x: &Foo) -> &Foo {
-17 | |         if true { x } else { self }
-18 | |     }
-   | |_____^
+   |                   ^ ...but data from `x` is returned here
 
 error: aborting due to previous error