From d3c2386434fa7a7881bbb123cb96c2da158edc4c Mon Sep 17 00:00:00 2001 From: gaurikholkar Date: Thu, 14 Sep 2017 01:04:47 +0530 Subject: extend E0623 for earlybound and latebound for structs --- .../infer/error_reporting/different_lifetimes.rs | 44 ++++++++++++++++------ 1 file changed, 33 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/librustc/infer/error_reporting/different_lifetimes.rs b/src/librustc/infer/error_reporting/different_lifetimes.rs index 051263dfb53..bf1428cabd6 100644 --- a/src/librustc/infer/error_reporting/different_lifetimes.rs +++ b/src/librustc/infer/error_reporting/different_lifetimes.rs @@ -321,24 +321,46 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> { } fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) { - let br_index = match self.bound_region { - ty::BrAnon(index) => index, - _ => return, - }; let hir_id = self.infcx.tcx.hir.node_to_hir_id(lifetime.id); - match self.infcx.tcx.named_region(hir_id) { + match (self.infcx.tcx.named_region(hir_id), self.bound_region) { // the lifetime of the TyPath! - Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)) => { + (Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => { if debruijn_index.depth == 1 && anon_index == br_index { self.found_it = true; + return; + } + } + + (Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => { + debug!("EarlyBound self.infcx.tcx.hir.local_def_id(id)={:?} \ + def_id={:?}", + self.infcx.tcx.hir.local_def_id(id), + def_id); + if self.infcx.tcx.hir.local_def_id(id) == def_id { + self.found_it = true; + return; // we can stop visiting now } } - Some(rl::Region::Static) | - Some(rl::Region::EarlyBound(_, _)) | - Some(rl::Region::LateBound(_, _)) | - Some(rl::Region::Free(_, _)) | - None => { + + (Some(rl::Region::LateBound(debruijn_index, id)), ty::BrNamed(def_id, _)) => { + debug!("FindNestedTypeVisitor::visit_ty: LateBound depth = {:?}", + debruijn_index.depth); + 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 && self.infcx.tcx.hir.local_def_id(id) == def_id { + self.found_it = true; + return; // we can stop visiting now + } + } + + (Some(rl::Region::Static), _) | + (Some(rl::Region::EarlyBound(_, _)), _) | + (Some(rl::Region::LateBound(_, _)), _) | + (Some(rl::Region::LateBoundAnon(_, _)), _) | + (Some(rl::Region::Free(_, _)), _) | + (None, _) => { debug!("no arg found"); } } -- cgit 1.4.1-3-g733a5 From f8df89a5cbf868677c342e3fa06dc6ca9a784e59 Mon Sep 17 00:00:00 2001 From: gaurikholkar Date: Thu, 14 Sep 2017 01:28:43 +0530 Subject: adding ui tests --- ...-anon-regions-both-are-structs-latebound-regions.rs | 18 ++++++++++++++++++ ...n-regions-both-are-structs-latebound-regions.stderr | 10 ++++++++++ 2 files changed, 28 insertions(+) create mode 100644 src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.rs create mode 100644 src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.stderr (limited to 'src') diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.rs b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.rs new file mode 100644 index 00000000000..a91d0b55dc7 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.rs @@ -0,0 +1,18 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +struct Ref<'a> { + x: &'a u32, +} + +fn foo<'a, 'b>(mut x: Vec>, y: Ref<'b>) { + x.push(y); +} + +fn main() {} diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.stderr b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.stderr new file mode 100644 index 00000000000..87835121068 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-latebound-regions.stderr @@ -0,0 +1,10 @@ +error[E0623]: lifetime mismatch + --> $DIR/ex3-both-anon-regions-both-are-structs-latebound-regions.rs:15:12 + | +14 | fn foo<'a, 'b>(mut x: Vec>, y: Ref<'b>) { + | ------- ------- these two types are declared with different lifetimes... +15 | x.push(y); + | ^ ...but data from `y` flows into `x` here + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5 From 696a26882278ac631e11dee457dfe8e6dfbd0891 Mon Sep 17 00:00:00 2001 From: gaurikholkar Date: Thu, 14 Sep 2017 10:01:12 +0530 Subject: fix depth for structs --- src/librustc/infer/error_reporting/different_lifetimes.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/librustc/infer/error_reporting/different_lifetimes.rs b/src/librustc/infer/error_reporting/different_lifetimes.rs index bf1428cabd6..536715ffadb 100644 --- a/src/librustc/infer/error_reporting/different_lifetimes.rs +++ b/src/librustc/infer/error_reporting/different_lifetimes.rs @@ -287,6 +287,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindNestedTypeVisitor<'a, 'gcx, 'tcx> { found_it: false, bound_region: self.bound_region, hir_map: self.hir_map, + depth: self.depth, }; intravisit::walk_ty(subvisitor, arg); // call walk_ty; as visit_ty is empty, // this will visit only outermost type @@ -313,6 +314,7 @@ struct TyPathVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> { hir_map: &'a hir::map::Map<'gcx>, found_it: bool, bound_region: ty::BoundRegion, + depth: u32, } impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> { @@ -326,7 +328,7 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'a, 'gcx, 'tcx> { match (self.infcx.tcx.named_region(hir_id), self.bound_region) { // the lifetime of the TyPath! (Some(rl::Region::LateBoundAnon(debruijn_index, anon_index)), ty::BrAnon(br_index)) => { - if debruijn_index.depth == 1 && anon_index == br_index { + if debruijn_index.depth == self.depth && anon_index == br_index { self.found_it = true; return; } @@ -349,7 +351,8 @@ impl<'a, 'gcx, 'tcx> Visitor<'gcx> for TyPathVisitor<'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 && self.infcx.tcx.hir.local_def_id(id) == def_id { + if debruijn_index.depth == self.depth && + self.infcx.tcx.hir.local_def_id(id) == def_id { self.found_it = true; return; // we can stop visiting now } -- cgit 1.4.1-3-g733a5 From 9240454a3f5c8677e82c8e8ba68333b3e10d6953 Mon Sep 17 00:00:00 2001 From: gaurikholkar Date: Thu, 14 Sep 2017 10:27:41 +0530 Subject: add ui tests for EBR --- ...n-regions-both-are-structs-earlybound-regions.rs | 21 +++++++++++++++++++++ ...gions-both-are-structs-earlybound-regions.stderr | 11 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.rs create mode 100644 src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.stderr (limited to 'src') diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.rs b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.rs new file mode 100644 index 00000000000..0fef709ae53 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.rs @@ -0,0 +1,21 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. +struct Ref<'a> { + x: &'a u32, +} + +fn foo<'a, 'b>(mut x: Vec>, y: Ref<'b>) + where &'a (): Sized, + &'b u32: Sized +{ + x.push(y); +} + +fn main() {} diff --git a/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.stderr b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.stderr new file mode 100644 index 00000000000..59bf5d17222 --- /dev/null +++ b/src/test/ui/lifetime-errors/ex3-both-anon-regions-both-are-structs-earlybound-regions.stderr @@ -0,0 +1,11 @@ +error[E0623]: lifetime mismatch + --> $DIR/ex3-both-anon-regions-both-are-structs-earlybound-regions.rs:18:12 + | +14 | fn foo<'a, 'b>(mut x: Vec>, y: Ref<'b>) + | ------- ------- these two types are declared with different lifetimes... +... +18 | x.push(y); + | ^ ...but data from `y` flows into `x` here + +error: aborting due to previous error + -- cgit 1.4.1-3-g733a5