]> git.lizzy.rs Git - rust.git/commitdiff
Only use the parent if it's an opaque type
authorAaron Hill <aa1ronham@gmail.com>
Tue, 11 Feb 2020 04:35:49 +0000 (23:35 -0500)
committerAaron Hill <aa1ronham@gmail.com>
Tue, 11 Feb 2020 04:41:16 +0000 (23:41 -0500)
src/librustc_typeck/collect.rs

index 49b1bfb72a3553e64cfb00ac319fe3fc7be6c8d2..242faebe53ec82262ceac4467c1537359abb822b 100644 (file)
@@ -1057,11 +1057,19 @@ fn generics_of(tcx: TyCtxt<'_>, def_id: DefId) -> &ty::Generics {
             ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn, .. }) => {
                 impl_trait_fn.or_else(|| {
                     let parent_id = tcx.hir().get_parent_item(hir_id);
-                    // This opaque type might occur inside another opaque type
-                    // (e.g. `impl Foo<MyType = impl Bar<A>>`)
                     if parent_id != hir_id && parent_id != CRATE_HIR_ID {
                         debug!("generics_of: parent of opaque ty {:?} is {:?}", def_id, parent_id);
-                        Some(tcx.hir().local_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
                     }