Skip to content

Commit

Permalink
Treat closures as part of their parent
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jul 7, 2023
1 parent b549ba1 commit c3004a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
20 changes: 10 additions & 10 deletions compiler/rustc_ty_utils/src/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/type-alias-impl-trait/nested_in_closure.rs
Original file line number Diff line number Diff line change
@@ -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 = ();
}
15 changes: 0 additions & 15 deletions tests/ui/type-alias-impl-trait/nested_in_closure.stderr

This file was deleted.

0 comments on commit c3004a7

Please sign in to comment.