From 18bf9dd4b7e9834bc6981ee413f025f3b2f7e386 Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 28 Jul 2019 17:57:04 -0400 Subject: [PATCH 1/3] Properly check the defining scope of existential types Fixes #52632 Existential types (soon to be 'impl trait' aliases) can either be delcared at a top-level crate/module scope, or within another item such as an fn. Previously, we were handling the second case incorrectly when recursively searching for defining usages - we would check children of the item, but not the item itself. This lead to us missing closures that consituted a defining use of the existential type, as their opaque type instantiations are stored in the TypeckTables of their parent function. This commit ensures that we explicitly visit the defining item itself, not just its children. --- src/librustc/infer/opaque_types/mod.rs | 15 +++++++------ src/librustc_typeck/collect.rs | 21 ++++++++++++++++--- src/test/ui/existential-type/issue-52843.rs | 11 ++++++++++ .../ui/existential-type/issue-52843.stderr | 20 ++++++++++++++++++ 4 files changed, 58 insertions(+), 9 deletions(-) create mode 100644 src/test/ui/existential-type/issue-52843.rs create mode 100644 src/test/ui/existential-type/issue-52843.stderr diff --git a/src/librustc/infer/opaque_types/mod.rs b/src/librustc/infer/opaque_types/mod.rs index 5acc3fd2fbcfd..73a76ebcb74f4 100644 --- a/src/librustc/infer/opaque_types/mod.rs +++ b/src/librustc/infer/opaque_types/mod.rs @@ -1189,11 +1189,7 @@ pub fn may_define_existential_type( opaque_hir_id: hir::HirId, ) -> bool { let mut hir_id = tcx.hir().as_local_hir_id(def_id).unwrap(); - trace!( - "may_define_existential_type(def={:?}, opaque_node={:?})", - tcx.hir().get(hir_id), - tcx.hir().get(opaque_hir_id) - ); + // Named existential types can be defined by any siblings or children of siblings. let scope = tcx.hir().get_defining_scope(opaque_hir_id).expect("could not get defining scope"); @@ -1202,5 +1198,12 @@ pub fn may_define_existential_type( hir_id = tcx.hir().get_parent_item(hir_id); } // Syntactically, we are allowed to define the concrete type if: - hir_id == scope + let res = hir_id == scope; + trace!( + "may_define_existential_type(def={:?}, opaque_node={:?}) = {}", + tcx.hir().get(hir_id), + tcx.hir().get(opaque_hir_id), + res + ); + res } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 053ef1f8f8297..0f0cbd5d76b4b 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1664,6 +1664,7 @@ fn find_existential_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { intravisit::NestedVisitorMap::All(&self.tcx.hir()) } fn visit_item(&mut self, it: &'tcx Item) { + debug!("find_existential_constraints: visiting {:?}", it); let def_id = self.tcx.hir().local_def_id(it.hir_id); // The existential type itself or its children are not within its reveal scope. if def_id != self.def_id { @@ -1672,6 +1673,7 @@ fn find_existential_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } } fn visit_impl_item(&mut self, it: &'tcx ImplItem) { + debug!("find_existential_constraints: visiting {:?}", it); let def_id = self.tcx.hir().local_def_id(it.hir_id); // The existential type itself or its children are not within its reveal scope. if def_id != self.def_id { @@ -1680,6 +1682,7 @@ fn find_existential_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } } fn visit_trait_item(&mut self, it: &'tcx TraitItem) { + debug!("find_existential_constraints: visiting {:?}", it); let def_id = self.tcx.hir().local_def_id(it.hir_id); self.check(def_id); intravisit::walk_trait_item(self, it); @@ -1703,9 +1706,21 @@ fn find_existential_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } else { debug!("find_existential_constraints: scope={:?}", tcx.hir().get(scope)); match tcx.hir().get(scope) { - Node::Item(ref it) => intravisit::walk_item(&mut locator, it), - Node::ImplItem(ref it) => intravisit::walk_impl_item(&mut locator, it), - Node::TraitItem(ref it) => intravisit::walk_trait_item(&mut locator, it), + // We explicitly call 'visit_*' methods, instead of using intravisit::walk_* methods + // This allows our visitor to process the defining item itself, causing + // it to pick up any 'sibling' defining uses. + // + // For example, this code: + // fn foo() { + // existential type Blah: Debug; + // let my_closure = || -> Blah { true }; + // } + // + // requires us to explicitly process 'foo()' in order + // to notice the defining usage of 'Blah' + Node::Item(ref it) => locator.visit_item(it), + Node::ImplItem(ref it) => locator.visit_impl_item(it), + Node::TraitItem(ref it) => locator.visit_trait_item(it), other => bug!( "{:?} is not a valid scope for an existential type item", other diff --git a/src/test/ui/existential-type/issue-52843.rs b/src/test/ui/existential-type/issue-52843.rs new file mode 100644 index 0000000000000..c625909e47831 --- /dev/null +++ b/src/test/ui/existential-type/issue-52843.rs @@ -0,0 +1,11 @@ +#![feature(existential_type)] + +use std::fmt::Debug; + +fn main() { + existential type Existential: Debug; + fn _unused() -> Existential { String::new() } + //~^ ERROR: concrete type differs from previous defining existential type use + let null = || -> Existential { 0 }; + println!("{:?}", null()); +} diff --git a/src/test/ui/existential-type/issue-52843.stderr b/src/test/ui/existential-type/issue-52843.stderr new file mode 100644 index 0000000000000..337e84bb8f50a --- /dev/null +++ b/src/test/ui/existential-type/issue-52843.stderr @@ -0,0 +1,20 @@ +error: concrete type differs from previous defining existential type use + --> $DIR/issue-52843.rs:7:5 + | +LL | fn _unused() -> Existential { String::new() } + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, got `std::string::String` + | +note: previous use here + --> $DIR/issue-52843.rs:5:1 + | +LL | / fn main() { +LL | | existential type Existential: Debug; +LL | | fn _unused() -> Existential { String::new() } +LL | | +LL | | let null = || -> Existential { 0 }; +LL | | println!("{:?}", null()); +LL | | } + | |_^ + +error: aborting due to previous error + From 8811b9ce9ffafdbe483cc82ea8a3bcb4ab7cb9af Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 28 Jul 2019 18:33:32 -0400 Subject: [PATCH 2/3] Fix formatting Co-Authored-By: Mazdak Farrokhzad --- src/librustc_typeck/collect.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 0f0cbd5d76b4b..395e266ae46aa 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -1706,18 +1706,20 @@ fn find_existential_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> { } else { debug!("find_existential_constraints: scope={:?}", tcx.hir().get(scope)); match tcx.hir().get(scope) { - // We explicitly call 'visit_*' methods, instead of using intravisit::walk_* methods + // We explicitly call `visit_*` methods, instead of using `intravisit::walk_*` methods // This allows our visitor to process the defining item itself, causing // it to pick up any 'sibling' defining uses. // // For example, this code: + // ``` // fn foo() { // existential type Blah: Debug; // let my_closure = || -> Blah { true }; // } + // ``` // - // requires us to explicitly process 'foo()' in order - // to notice the defining usage of 'Blah' + // requires us to explicitly process `foo()` in order + // to notice the defining usage of `Blah`. Node::Item(ref it) => locator.visit_item(it), Node::ImplItem(ref it) => locator.visit_impl_item(it), Node::TraitItem(ref it) => locator.visit_trait_item(it), From 3e98c3acf5c598ae577428fa8fbf1c29e270739b Mon Sep 17 00:00:00 2001 From: Aaron Hill Date: Sun, 28 Jul 2019 19:12:04 -0400 Subject: [PATCH 3/3] Rename test and add comment --- .../{issue-52843.rs => issue-52843-closure-constrain.rs} | 1 + ...ssue-52843.stderr => issue-52843-closure-constrain.stderr} | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) rename src/test/ui/existential-type/{issue-52843.rs => issue-52843-closure-constrain.rs} (77%) rename src/test/ui/existential-type/{issue-52843.stderr => issue-52843-closure-constrain.stderr} (84%) diff --git a/src/test/ui/existential-type/issue-52843.rs b/src/test/ui/existential-type/issue-52843-closure-constrain.rs similarity index 77% rename from src/test/ui/existential-type/issue-52843.rs rename to src/test/ui/existential-type/issue-52843-closure-constrain.rs index c625909e47831..b2bbc1f154998 100644 --- a/src/test/ui/existential-type/issue-52843.rs +++ b/src/test/ui/existential-type/issue-52843-closure-constrain.rs @@ -1,3 +1,4 @@ +// Checks to ensure that we properly detect when a closure constrains an existential type #![feature(existential_type)] use std::fmt::Debug; diff --git a/src/test/ui/existential-type/issue-52843.stderr b/src/test/ui/existential-type/issue-52843-closure-constrain.stderr similarity index 84% rename from src/test/ui/existential-type/issue-52843.stderr rename to src/test/ui/existential-type/issue-52843-closure-constrain.stderr index 337e84bb8f50a..424d65a193c92 100644 --- a/src/test/ui/existential-type/issue-52843.stderr +++ b/src/test/ui/existential-type/issue-52843-closure-constrain.stderr @@ -1,11 +1,11 @@ error: concrete type differs from previous defining existential type use - --> $DIR/issue-52843.rs:7:5 + --> $DIR/issue-52843-closure-constrain.rs:8:5 | LL | fn _unused() -> Existential { String::new() } | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, got `std::string::String` | note: previous use here - --> $DIR/issue-52843.rs:5:1 + --> $DIR/issue-52843-closure-constrain.rs:6:1 | LL | / fn main() { LL | | existential type Existential: Debug;