Skip to content

Commit 5d15beb

Browse files
committed
Unconditionally encode hidden types in typeck results
1 parent 43c22af commit 5d15beb

File tree

4 files changed

+43
-47
lines changed

4 files changed

+43
-47
lines changed

compiler/rustc_hir_analysis/src/check/writeback.rs

+20-24
Original file line numberDiff line numberDiff line change
@@ -536,33 +536,29 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
536536
let opaque_types =
537537
self.fcx.infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
538538
for (opaque_type_key, decl) in opaque_types {
539-
let hidden_type = match decl.origin {
540-
hir::OpaqueTyOrigin::FnReturn(_) | hir::OpaqueTyOrigin::AsyncFn(_) => {
541-
let ty = self.resolve(decl.hidden_type.ty, &decl.hidden_type.span);
542-
struct RecursionChecker {
543-
def_id: LocalDefId,
544-
}
545-
impl<'tcx> ty::TypeVisitor<'tcx> for RecursionChecker {
546-
type BreakTy = ();
547-
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
548-
if let ty::Opaque(def_id, _) = *t.kind() {
549-
if def_id == self.def_id.to_def_id() {
550-
return ControlFlow::Break(());
551-
}
552-
}
553-
t.super_visit_with(self)
539+
let hidden_type = self.resolve(decl.hidden_type.ty, &decl.hidden_type.span);
540+
541+
struct RecursionChecker {
542+
def_id: LocalDefId,
543+
}
544+
impl<'tcx> ty::TypeVisitor<'tcx> for RecursionChecker {
545+
type BreakTy = ();
546+
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
547+
if let ty::Opaque(def_id, _) = *t.kind() {
548+
if def_id == self.def_id.to_def_id() {
549+
return ControlFlow::Break(());
554550
}
555551
}
556-
if ty
557-
.visit_with(&mut RecursionChecker { def_id: opaque_type_key.def_id })
558-
.is_break()
559-
{
560-
return;
561-
}
562-
Some(ty)
552+
t.super_visit_with(self)
563553
}
564-
hir::OpaqueTyOrigin::TyAlias => None,
565-
};
554+
}
555+
if hidden_type
556+
.visit_with(&mut RecursionChecker { def_id: opaque_type_key.def_id })
557+
.is_break()
558+
{
559+
continue;
560+
}
561+
566562
self.typeck_results.concrete_opaque_types.insert(opaque_type_key.def_id, hidden_type);
567563
}
568564
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -788,20 +788,15 @@ fn find_opaque_ty_constraints_for_rpit(
788788
// the `concrete_opaque_types` table.
789789
tcx.ty_error()
790790
} else {
791-
table
792-
.concrete_opaque_types
793-
.get(&def_id)
794-
.copied()
795-
.unwrap_or_else(|| {
796-
// We failed to resolve the opaque type or it
797-
// resolves to itself. We interpret this as the
798-
// no values of the hidden type ever being constructed,
799-
// so we can just make the hidden type be `!`.
800-
// For backwards compatibility reasons, we fall back to
801-
// `()` until we the diverging default is changed.
802-
Some(tcx.mk_diverging_default())
803-
})
804-
.expect("RPIT always have a hidden type from typeck")
791+
table.concrete_opaque_types.get(&def_id).copied().unwrap_or_else(|| {
792+
// We failed to resolve the opaque type or it
793+
// resolves to itself. We interpret this as the
794+
// no values of the hidden type ever being constructed,
795+
// so we can just make the hidden type be `!`.
796+
// For backwards compatibility reasons, we fall back to
797+
// `()` until we the diverging default is changed.
798+
tcx.mk_diverging_default()
799+
})
805800
}
806801
})
807802
}

compiler/rustc_middle/src/ty/context.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,10 @@ pub struct TypeckResults<'tcx> {
539539
pub tainted_by_errors: Option<ErrorGuaranteed>,
540540

541541
/// All the opaque types that have hidden types set
542-
/// by this function. For return-position-impl-trait we also store the
543-
/// type here, so that mir-borrowck can figure out hidden types,
542+
/// by this function. We also store the
543+
/// type here, so that mir-borrowck can use it as a hint for figuring out hidden types,
544544
/// even if they are only set in dead code (which doesn't show up in MIR).
545-
/// For type-alias-impl-trait, this map is only used to prevent query cycles,
546-
/// so the hidden types are all `None`.
547-
pub concrete_opaque_types: VecMap<LocalDefId, Option<Ty<'tcx>>>,
545+
pub concrete_opaque_types: VecMap<LocalDefId, Ty<'tcx>>,
548546

549547
/// Tracks the minimum captures required for a closure;
550548
/// see `MinCaptureInformationMap` for more details.

src/test/ui/impl-trait/issues/issue-86800.stderr

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
error: unconstrained opaque type
2+
--> $DIR/issue-86800.rs:33:34
3+
|
4+
LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResult<O>>;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
=
8+
19

210
stack backtrace:
311

@@ -12,8 +20,7 @@ error: internal compiler error: unexpected panic
1220

1321

1422
query stack during panic:
15-
#0 [mir_borrowck] borrow-checking `execute_transaction_fut`
16-
#1 [type_of] computing type of `TransactionFuture::{opaque#0}`
17-
#2 [check_mod_item_types] checking item types in top-level module
18-
#3 [analysis] running analysis passes on this crate
23+
#0 [type_of] computing type of `TransactionFuture::{opaque#0}`
24+
#1 [check_mod_item_types] checking item types in top-level module
25+
#2 [analysis] running analysis passes on this crate
1926
end of query stack

0 commit comments

Comments
 (0)