Auto merge of #136073 - compiler-errors:recursive-coro-always, r= · rust-lang/rust@c104bc5 (original) (raw)
`@@ -776,7 +776,6 @@ impl<'tcx> TyCtxt<'tcx> {
`
776
776
`self,
`
777
777
`def_id: DefId,
`
778
778
`args: GenericArgsRef<'tcx>,
`
779
``
`-
inspect_coroutine_fields: InspectCoroutineFields,
`
780
779
`) -> Result<Ty<'tcx>, Ty<'tcx>> {
`
781
780
`let mut visitor = OpaqueTypeExpander {
`
782
781
`seen_opaque_tys: FxHashSet::default(),
`
`@@ -785,9 +784,7 @@ impl<'tcx> TyCtxt<'tcx> {
`
785
784
`found_recursion: false,
`
786
785
`found_any_recursion: false,
`
787
786
`check_recursion: true,
`
788
``
`-
expand_coroutines: true,
`
789
787
`tcx: self,
`
790
``
`-
inspect_coroutine_fields,
`
791
788
`};
`
792
789
``
793
790
`let expanded_type = visitor.expand_opaque_ty(def_id, args).unwrap();
`
`@@ -964,19 +961,11 @@ struct OpaqueTypeExpander<'tcx> {
`
964
961
`primary_def_id: Option,
`
965
962
`found_recursion: bool,
`
966
963
`found_any_recursion: bool,
`
967
``
`-
expand_coroutines: bool,
`
968
964
`/// Whether or not to check for recursive opaque types.
`
969
965
`` /// This is true
when we're explicitly checking for opaque type
``
970
966
`/// recursion, and 'false' otherwise to avoid unnecessary work.
`
971
967
`check_recursion: bool,
`
972
968
`tcx: TyCtxt<'tcx>,
`
973
``
`-
inspect_coroutine_fields: InspectCoroutineFields,
`
974
``
`-
}
`
975
``
-
976
``
`-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
`
977
``
`-
pub enum InspectCoroutineFields {
`
978
``
`-
No,
`
979
``
`-
Yes,
`
980
969
`}
`
981
970
``
982
971
`impl<'tcx> OpaqueTypeExpander<'tcx> {
`
`@@ -1008,41 +997,6 @@ impl<'tcx> OpaqueTypeExpander<'tcx> {
`
1008
997
`None
`
1009
998
`}
`
1010
999
`}
`
1011
``
-
1012
``
`-
fn expand_coroutine(&mut self, def_id: DefId, args: GenericArgsRef<'tcx>) -> Option<Ty<'tcx>> {
`
1013
``
`-
if self.found_any_recursion {
`
1014
``
`-
return None;
`
1015
``
`-
}
`
1016
``
`-
let args = args.fold_with(self);
`
1017
``
`-
if !self.check_recursion || self.seen_opaque_tys.insert(def_id) {
`
1018
``
`-
let expanded_ty = match self.expanded_cache.get(&(def_id, args)) {
`
1019
``
`-
Some(expanded_ty) => *expanded_ty,
`
1020
``
`-
None => {
`
1021
``
`-
if matches!(self.inspect_coroutine_fields, InspectCoroutineFields::Yes) {
`
1022
``
`-
for bty in self.tcx.bound_coroutine_hidden_types(def_id) {
`
1023
``
`-
let hidden_ty = self.tcx.instantiate_bound_regions_with_erased(
`
1024
``
`-
bty.instantiate(self.tcx, args),
`
1025
``
`-
);
`
1026
``
`-
self.fold_ty(hidden_ty);
`
1027
``
`-
}
`
1028
``
`-
}
`
1029
``
`-
let expanded_ty = Ty::new_coroutine_witness(self.tcx, def_id, args);
`
1030
``
`-
self.expanded_cache.insert((def_id, args), expanded_ty);
`
1031
``
`-
expanded_ty
`
1032
``
`-
}
`
1033
``
`-
};
`
1034
``
`-
if self.check_recursion {
`
1035
``
`-
self.seen_opaque_tys.remove(&def_id);
`
1036
``
`-
}
`
1037
``
`-
Some(expanded_ty)
`
1038
``
`-
} else {
`
1039
``
`-
// If another opaque type that we contain is recursive, then it
`
1040
``
`-
// will report the error, so we don't have to.
`
1041
``
`-
self.found_any_recursion = true;
`
1042
``
`-
self.found_recursion = def_id == *self.primary_def_id.as_ref().unwrap();
`
1043
``
`-
None
`
1044
``
`-
}
`
1045
``
`-
}
`
1046
1000
`}
`
1047
1001
``
1048
1002
`impl<'tcx> TypeFolder<TyCtxt<'tcx>> for OpaqueTypeExpander<'tcx> {
`
`@@ -1051,19 +1005,13 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for OpaqueTypeExpander<'tcx> {
`
1051
1005
`}
`
1052
1006
``
1053
1007
`fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
`
1054
``
`-
let mut t = if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) = *t.kind() {
`
``
1008
`+
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) = *t.kind() {
`
1055
1009
`self.expand_opaque_ty(def_id, args).unwrap_or(t)
`
1056
``
`-
} else if t.has_opaque_types() || t.has_coroutines() {
`
``
1010
`+
} else if t.has_opaque_types() {
`
1057
1011
` t.super_fold_with(self)
`
1058
1012
`} else {
`
1059
1013
` t
`
1060
``
`-
};
`
1061
``
`-
if self.expand_coroutines {
`
1062
``
`-
if let ty::CoroutineWitness(def_id, args) = *t.kind() {
`
1063
``
`-
t = self.expand_coroutine(def_id, args).unwrap_or(t);
`
1064
``
`-
}
`
1065
1014
`}
`
1066
``
`-
t
`
1067
1015
`}
`
1068
1016
``
1069
1017
`fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
`
`@@ -1752,9 +1700,7 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
`
1752
1700
`found_recursion: false,
`
1753
1701
`found_any_recursion: false,
`
1754
1702
`check_recursion: false,
`
1755
``
`-
expand_coroutines: false,
`
1756
1703
` tcx,
`
1757
``
`-
inspect_coroutine_fields: InspectCoroutineFields::No,
`
1758
1704
`};
`
1759
1705
` val.fold_with(&mut visitor)
`
1760
1706
`}
`