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 use parent generics for opaque types #69008

Merged
merged 2 commits into from
Feb 13, 2020
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
22 changes: 21 additions & 1 deletion src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,27 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics {
Some(tcx.closure_base_def_id(def_id))
}
Node::Item(item) => match item.kind {
ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn, .. }) => impl_trait_fn,
ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn, .. }) => {
impl_trait_fn.or_else(|| {
let parent_id = tcx.hir().get_parent_item(hir_id);
if parent_id != hir_id && parent_id != CRATE_HIR_ID {
debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent_id);
// If this 'impl Trait' is nested inside another 'impl Trait'
// (e.g. `impl Foo<MyType = impl Bar<A>>`), we need to use the 'parent'
// 'impl Trait' for its generic parameters, since we can reference them
// from the 'child' 'impl Trait'
if let Node::Item(hir::Item { kind: ItemKind::OpaqueTy(..), .. }) =
tcx.hir().get(parent_id)
{
Some(tcx.hir().local_def_id(parent_id))
} else {
None
}
} else {
None
}
})
}
_ => None,
},
_ => None,
Expand Down
32 changes: 32 additions & 0 deletions src/test/ui/type-alias-impl-trait/issue-67844-nested-opaque.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// check-pass
// Regression test for issue #67844
// Ensures that we properly handle nested TAIT occurences
// with generic parameters

#![feature(type_alias_impl_trait)]

trait WithAssoc { type AssocType; }

trait WithParam<A> {}

type Return<A> = impl WithAssoc<AssocType = impl WithParam<A>>;

struct MyParam;
impl<A> WithParam<A> for MyParam {}

struct MyStruct;

impl WithAssoc for MyStruct {
type AssocType = MyParam;
}


fn my_fun<A>() -> Return<A> {
MyStruct
}

fn my_other_fn<A>() -> impl WithAssoc<AssocType = impl WithParam<A>> {
MyStruct
}

fn main() {}