From c3004a7b6523bfe939df0f73b49f63510557e735 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Thu, 29 Jun 2023 13:32:37 +0000 Subject: [PATCH] Treat closures as part of their parent --- compiler/rustc_ty_utils/src/opaque_types.rs | 20 +++++++++---------- .../nested_in_closure.rs | 10 +++++++++- .../nested_in_closure.stderr | 15 -------------- 3 files changed, 19 insertions(+), 26 deletions(-) delete mode 100644 tests/ui/type-alias-impl-trait/nested_in_closure.stderr diff --git a/compiler/rustc_ty_utils/src/opaque_types.rs b/compiler/rustc_ty_utils/src/opaque_types.rs index 40d61f47adb..570c3b245cd 100644 --- a/compiler/rustc_ty_utils/src/opaque_types.rs +++ b/compiler/rustc_ty_utils/src/opaque_types.rs @@ -121,6 +121,12 @@ impl<'tcx> OpaqueTypeCollector<'tcx> { self.collector.opaques.extend(items); } } + #[instrument(level = "trace", skip(self))] + // Recurse into these, as they are type checked with their parent + fn visit_nested_body(&mut self, id: rustc_hir::BodyId) { + let body = self.collector.tcx.hir().body(id); + self.visit_body(body); + } } TaitInBodyFinder { collector: self }.visit_expr(body); } @@ -280,11 +286,7 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [ collector.collect_body_and_predicate_taits(); } // Walk over the type of the item to find opaques. - DefKind::Static(_) - | DefKind::Const - | DefKind::AssocConst - | DefKind::AnonConst - | DefKind::InlineConst => { + DefKind::Static(_) | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => { let span = match tcx.hir().get_by_def_id(item).ty() { Some(ty) => ty.span, _ => tcx.def_span(item), @@ -321,11 +323,9 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [ | DefKind::LifetimeParam | DefKind::GlobalAsm | DefKind::Impl { .. } => {} - DefKind::Closure | DefKind::Generator => { - // All items in the signature of the parent are ok - collector.opaques.extend(tcx.opaque_types_defined_by(tcx.local_parent(item))); - // And items in the body of the closure itself - collector.collect_taits_declared_in_body(); + // Closures and generators are type checked with their parent, so there is no difference here. + DefKind::Closure | DefKind::Generator | DefKind::InlineConst => { + return tcx.opaque_types_defined_by(tcx.local_parent(item)); } } tcx.arena.alloc_from_iter(collector.opaques) diff --git a/tests/ui/type-alias-impl-trait/nested_in_closure.rs b/tests/ui/type-alias-impl-trait/nested_in_closure.rs index 3c15b0708a3..362f3d53e88 100644 --- a/tests/ui/type-alias-impl-trait/nested_in_closure.rs +++ b/tests/ui/type-alias-impl-trait/nested_in_closure.rs @@ -1,9 +1,17 @@ #![feature(type_alias_impl_trait)] +// check-pass fn main() { let x = || { type Tait = impl Sized; let y: Tait = (); - //~^ ERROR: item constrains opaque type that is not in its signature }; + + let y = || { + type Tait = impl std::fmt::Debug; + let y: Tait = (); + y + }; + let mut z = y(); + z = (); } diff --git a/tests/ui/type-alias-impl-trait/nested_in_closure.stderr b/tests/ui/type-alias-impl-trait/nested_in_closure.stderr deleted file mode 100644 index beb670db171..00000000000 --- a/tests/ui/type-alias-impl-trait/nested_in_closure.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error: item constrains opaque type that is not in its signature - --> $DIR/nested_in_closure.rs:6:23 - | -LL | let y: Tait = (); - | ^^ - | - = note: this item must have the opaque type in its signature in order to be able to register hidden types -note: this item must have the opaque type in its signature in order to be able to register hidden types - --> $DIR/nested_in_closure.rs:3:4 - | -LL | fn main() { - | ^^^^ - -error: aborting due to previous error -