Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly check the defining scope of existential types #63093

Merged
merged 3 commits into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/librustc/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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
}
23 changes: 20 additions & 3 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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);
Expand All @@ -1703,9 +1706,23 @@ 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:
Aaron1011 marked this conversation as resolved.
Show resolved Hide resolved
// ```
// fn foo() {
// existential type Blah: Debug;
// let my_closure = || -> Blah { true };
// }
Aaron1011 marked this conversation as resolved.
Show resolved Hide resolved
// ```
//
// 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
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/existential-type/issue-52843-closure-constrain.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Checks to ensure that we properly detect when a closure constrains an existential type
#![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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moderately confusing to have the first item (textually) talk about "previous". Maybe we should reword "previous" to "other"

let null = || -> Existential { 0 };
println!("{:?}", null());
}
20 changes: 20 additions & 0 deletions src/test/ui/existential-type/issue-52843-closure-constrain.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: concrete type differs from previous defining existential type use
--> $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-closure-constrain.rs:6: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