Skip to content

Commit c3004a7

Browse files
committed
Treat closures as part of their parent
1 parent b549ba1 commit c3004a7

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

compiler/rustc_ty_utils/src/opaque_types.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ impl<'tcx> OpaqueTypeCollector<'tcx> {
121121
self.collector.opaques.extend(items);
122122
}
123123
}
124+
#[instrument(level = "trace", skip(self))]
125+
// Recurse into these, as they are type checked with their parent
126+
fn visit_nested_body(&mut self, id: rustc_hir::BodyId) {
127+
let body = self.collector.tcx.hir().body(id);
128+
self.visit_body(body);
129+
}
124130
}
125131
TaitInBodyFinder { collector: self }.visit_expr(body);
126132
}
@@ -280,11 +286,7 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
280286
collector.collect_body_and_predicate_taits();
281287
}
282288
// Walk over the type of the item to find opaques.
283-
DefKind::Static(_)
284-
| DefKind::Const
285-
| DefKind::AssocConst
286-
| DefKind::AnonConst
287-
| DefKind::InlineConst => {
289+
DefKind::Static(_) | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => {
288290
let span = match tcx.hir().get_by_def_id(item).ty() {
289291
Some(ty) => ty.span,
290292
_ => tcx.def_span(item),
@@ -321,11 +323,9 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
321323
| DefKind::LifetimeParam
322324
| DefKind::GlobalAsm
323325
| DefKind::Impl { .. } => {}
324-
DefKind::Closure | DefKind::Generator => {
325-
// All items in the signature of the parent are ok
326-
collector.opaques.extend(tcx.opaque_types_defined_by(tcx.local_parent(item)));
327-
// And items in the body of the closure itself
328-
collector.collect_taits_declared_in_body();
326+
// Closures and generators are type checked with their parent, so there is no difference here.
327+
DefKind::Closure | DefKind::Generator | DefKind::InlineConst => {
328+
return tcx.opaque_types_defined_by(tcx.local_parent(item));
329329
}
330330
}
331331
tcx.arena.alloc_from_iter(collector.opaques)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
#![feature(type_alias_impl_trait)]
2+
// check-pass
23

34
fn main() {
45
let x = || {
56
type Tait = impl Sized;
67
let y: Tait = ();
7-
//~^ ERROR: item constrains opaque type that is not in its signature
88
};
9+
10+
let y = || {
11+
type Tait = impl std::fmt::Debug;
12+
let y: Tait = ();
13+
y
14+
};
15+
let mut z = y();
16+
z = ();
917
}

tests/ui/type-alias-impl-trait/nested_in_closure.stderr

-15
This file was deleted.

0 commit comments

Comments
 (0)