Auto merge of #117703 - compiler-errors:recursive-async, r=lcnr · rust-lang/rust@dc64103 (original) (raw)

`@@ -702,6 +702,7 @@ impl<'tcx> TyCtxt<'tcx> {

`

702

702

`self,

`

703

703

`def_id: DefId,

`

704

704

`args: GenericArgsRef<'tcx>,

`

``

705

`+

inspect_coroutine_fields: InspectCoroutineFields,

`

705

706

`) -> Result<Ty<'tcx>, Ty<'tcx>> {

`

706

707

`let mut visitor = OpaqueTypeExpander {

`

707

708

`seen_opaque_tys: FxHashSet::default(),

`

`@@ -712,6 +713,7 @@ impl<'tcx> TyCtxt<'tcx> {

`

712

713

`check_recursion: true,

`

713

714

`expand_coroutines: true,

`

714

715

`tcx: self,

`

``

716

`+

inspect_coroutine_fields,

`

715

717

`};

`

716

718

``

717

719

`let expanded_type = visitor.expand_opaque_ty(def_id, args).unwrap();

`

`@@ -729,16 +731,43 @@ impl<'tcx> TyCtxt<'tcx> {

`

729

731

`DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "method",

`

730

732

`DefKind::Closure if let Some(coroutine_kind) = self.coroutine_kind(def_id) => {

`

731

733

`match coroutine_kind {

`

732

``

`-

hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _) => {

`

733

``

`-

"async closure"

`

734

``

`-

}

`

735

``

`-

hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _) => {

`

736

``

`-

"async gen closure"

`

737

``

`-

}

`

``

734

`+

hir::CoroutineKind::Desugared(

`

``

735

`+

hir::CoroutineDesugaring::Async,

`

``

736

`+

hir::CoroutineSource::Fn,

`

``

737

`+

) => "async fn",

`

``

738

`+

hir::CoroutineKind::Desugared(

`

``

739

`+

hir::CoroutineDesugaring::Async,

`

``

740

`+

hir::CoroutineSource::Block,

`

``

741

`+

) => "async block",

`

``

742

`+

hir::CoroutineKind::Desugared(

`

``

743

`+

hir::CoroutineDesugaring::Async,

`

``

744

`+

hir::CoroutineSource::Closure,

`

``

745

`+

) => "async closure",

`

``

746

`+

hir::CoroutineKind::Desugared(

`

``

747

`+

hir::CoroutineDesugaring::AsyncGen,

`

``

748

`+

hir::CoroutineSource::Fn,

`

``

749

`+

) => "async gen fn",

`

``

750

`+

hir::CoroutineKind::Desugared(

`

``

751

`+

hir::CoroutineDesugaring::AsyncGen,

`

``

752

`+

hir::CoroutineSource::Block,

`

``

753

`+

) => "async gen block",

`

``

754

`+

hir::CoroutineKind::Desugared(

`

``

755

`+

hir::CoroutineDesugaring::AsyncGen,

`

``

756

`+

hir::CoroutineSource::Closure,

`

``

757

`+

) => "async gen closure",

`

``

758

`+

hir::CoroutineKind::Desugared(

`

``

759

`+

hir::CoroutineDesugaring::Gen,

`

``

760

`+

hir::CoroutineSource::Fn,

`

``

761

`+

) => "gen fn",

`

``

762

`+

hir::CoroutineKind::Desugared(

`

``

763

`+

hir::CoroutineDesugaring::Gen,

`

``

764

`+

hir::CoroutineSource::Block,

`

``

765

`+

) => "gen block",

`

``

766

`+

hir::CoroutineKind::Desugared(

`

``

767

`+

hir::CoroutineDesugaring::Gen,

`

``

768

`+

hir::CoroutineSource::Closure,

`

``

769

`+

) => "gen closure",

`

738

770

` hir::CoroutineKind::Coroutine(_) => "coroutine",

`

739

``

`-

hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _) => {

`

740

``

`-

"gen closure"

`

741

``

`-

}

`

742

771

`}

`

743

772

`}

`

744

773

` _ => def_kind.descr(def_id),

`

`@@ -865,6 +894,13 @@ struct OpaqueTypeExpander<'tcx> {

`

865

894

`/// recursion, and 'false' otherwise to avoid unnecessary work.

`

866

895

`check_recursion: bool,

`

867

896

`tcx: TyCtxt<'tcx>,

`

``

897

`+

inspect_coroutine_fields: InspectCoroutineFields,

`

``

898

`+

}

`

``

899

+

``

900

`+

#[derive(Copy, Clone, PartialEq, Eq, Debug)]

`

``

901

`+

pub enum InspectCoroutineFields {

`

``

902

`+

No,

`

``

903

`+

Yes,

`

868

904

`}

`

869

905

``

870

906

`impl<'tcx> OpaqueTypeExpander<'tcx> {

`

`@@ -906,9 +942,11 @@ impl<'tcx> OpaqueTypeExpander<'tcx> {

`

906

942

`let expanded_ty = match self.expanded_cache.get(&(def_id, args)) {

`

907

943

`Some(expanded_ty) => *expanded_ty,

`

908

944

`None => {

`

909

``

`-

for bty in self.tcx.coroutine_hidden_types(def_id) {

`

910

``

`-

let hidden_ty = bty.instantiate(self.tcx, args);

`

911

``

`-

self.fold_ty(hidden_ty);

`

``

945

`+

if matches!(self.inspect_coroutine_fields, InspectCoroutineFields::Yes) {

`

``

946

`+

for bty in self.tcx.coroutine_hidden_types(def_id) {

`

``

947

`+

let hidden_ty = bty.instantiate(self.tcx, args);

`

``

948

`+

self.fold_ty(hidden_ty);

`

``

949

`+

}

`

912

950

`}

`

913

951

`let expanded_ty = Ty::new_coroutine_witness(self.tcx, def_id, args);

`

914

952

`self.expanded_cache.insert((def_id, args), expanded_ty);

`

`@@ -1486,6 +1524,7 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(

`

1486

1524

`check_recursion: false,

`

1487

1525

`expand_coroutines: false,

`

1488

1526

` tcx,

`

``

1527

`+

inspect_coroutine_fields: InspectCoroutineFields::No,

`

1489

1528

`};

`

1490

1529

` val.fold_with(&mut visitor)

`

1491

1530

`}

`