Treat closures as part of their parent

This commit is contained in:
Oli Scherer 2023-06-29 13:32:37 +00:00
parent b549ba1bd4
commit c3004a7b65
3 changed files with 19 additions and 26 deletions

View File

@ -121,6 +121,12 @@ fn visit_nested_item(&mut self, id: rustc_hir::ItemId) {
self.collector.opaques.extend(items); 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); 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(); collector.collect_body_and_predicate_taits();
} }
// Walk over the type of the item to find opaques. // Walk over the type of the item to find opaques.
DefKind::Static(_) DefKind::Static(_) | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => {
| DefKind::Const
| DefKind::AssocConst
| DefKind::AnonConst
| DefKind::InlineConst => {
let span = match tcx.hir().get_by_def_id(item).ty() { let span = match tcx.hir().get_by_def_id(item).ty() {
Some(ty) => ty.span, Some(ty) => ty.span,
_ => tcx.def_span(item), _ => tcx.def_span(item),
@ -321,11 +323,9 @@ fn opaque_types_defined_by<'tcx>(tcx: TyCtxt<'tcx>, item: LocalDefId) -> &'tcx [
| DefKind::LifetimeParam | DefKind::LifetimeParam
| DefKind::GlobalAsm | DefKind::GlobalAsm
| DefKind::Impl { .. } => {} | DefKind::Impl { .. } => {}
DefKind::Closure | DefKind::Generator => { // Closures and generators are type checked with their parent, so there is no difference here.
// All items in the signature of the parent are ok DefKind::Closure | DefKind::Generator | DefKind::InlineConst => {
collector.opaques.extend(tcx.opaque_types_defined_by(tcx.local_parent(item))); return tcx.opaque_types_defined_by(tcx.local_parent(item));
// And items in the body of the closure itself
collector.collect_taits_declared_in_body();
} }
} }
tcx.arena.alloc_from_iter(collector.opaques) tcx.arena.alloc_from_iter(collector.opaques)

View File

@ -1,9 +1,17 @@
#![feature(type_alias_impl_trait)] #![feature(type_alias_impl_trait)]
// check-pass
fn main() { fn main() {
let x = || { let x = || {
type Tait = impl Sized; type Tait = impl Sized;
let y: Tait = (); 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 = ();
} }

View File

@ -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