about summary refs log tree commit diff
diff options
context:
space:
mode:
authorgaurikholkar <f2013002@goa.bits-pilani.ac.in>2017-09-12 18:22:22 +0530
committergaurikholkar <f2013002@goa.bits-pilani.ac.in>2017-09-12 18:22:22 +0530
commit6e3cdcea4c600b89dd067d8af8b7e1738f7df6a5 (patch)
treefe1af59fdce356ff517a681c9e6c4e8c9652b013
parent93529b40ca9471ebe3f79817d4f71a5245bec50e (diff)
downloadrust-6e3cdcea4c600b89dd067d8af8b7e1738f7df6a5.tar.gz
rust-6e3cdcea4c600b89dd067d8af8b7e1738f7df6a5.zip
Adding changes for trait objects
-rw-r--r--src/librustc/infer/error_reporting/different_lifetimes.rs14
-rw-r--r--src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.rs14
-rw-r--r--src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.stderr10
3 files changed, 36 insertions, 2 deletions
diff --git a/src/librustc/infer/error_reporting/different_lifetimes.rs b/src/librustc/infer/error_reporting/different_lifetimes.rs
index a54e75857dd..051263dfb53 100644
--- a/src/librustc/infer/error_reporting/different_lifetimes.rs
+++ b/src/librustc/infer/error_reporting/different_lifetimes.rs
@@ -173,6 +173,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
             hir_map: &self.tcx.hir,
             bound_region: *br,
             found_type: None,
+            depth: 1,
         };
         nested_visitor.visit_ty(arg);
         nested_visitor.found_type
@@ -195,6 +196,7 @@ struct FindNestedTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
     // The type where the anonymous lifetime appears
     // for e.g. Vec<`&u8`> and <`&u8`>
     found_type: Option<&'gcx hir::Ty>,
+    depth: u32,
 }
 
 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
@@ -211,6 +213,14 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
                 return;
             }
 
+            hir::TyTraitObject(ref bounds, _) => {
+                for bound in bounds {
+                    self.depth += 1;
+                    self.visit_poly_trait_ref(bound, hir::TraitBoundModifier::None);
+                    self.depth -= 1;
+                }
+            }
+
             hir::TyRptr(ref lifetime, _) => {
                 // the lifetime of the TyRptr
                 let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id);
@@ -224,7 +234,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
                                debruijn_index.depth,
                                anon_index,
                                br_index);
-                        if debruijn_index.depth == 1 && anon_index == br_index {
+                        if debruijn_index.depth == self.depth && anon_index == br_index {
                             self.found_type = Some(arg);
                             return; // we can stop visiting now
                         }
@@ -253,7 +263,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> {
                         debug!("self.infcx.tcx.hir.local_def_id(id)={:?}",
                                self.infcx.tcx.hir.local_def_id(id));
                         debug!("def_id={:?}", def_id);
-                        if debruijn_index.depth == 1 &&
+                        if debruijn_index.depth == self.depth &&
                            self.infcx.tcx.hir.local_def_id(id) == def_id {
                             self.found_type = Some(arg);
                             return; // we can stop visiting now
diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.rs b/src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.rs
new file mode 100644
index 00000000000..78a6ad54eae
--- /dev/null
+++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.rs
@@ -0,0 +1,14 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+fn foo(x:Box<Fn(&u8, &u8)> , y: Vec<&u8>, z: &u8) {
+  y.push(z);
+}
+
+fn main() { }
diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.stderr b/src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.stderr
new file mode 100644
index 00000000000..ce766b2e406
--- /dev/null
+++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-using-trait-objects.stderr
@@ -0,0 +1,10 @@
+error[E0623]: lifetime mismatch
+  --> $DIR/ex3-both-anon-regions-using-trait-objects.rs:11:10
+   |
+10 | fn foo(x:Box<Fn(&u8, &u8)> , y: Vec<&u8>, z: &u8) {
+   |                 ---  --- these two types are declared with different lifetimes...
+11 |   y.push(z);
+   |          ^ ...but data from `z` flows into `y` here
+
+error: aborting due to previous error
+