Skip to content

Commit

Permalink
Unconditionally encode hidden types in typeck results
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Oct 7, 2022
1 parent 43c22af commit 5d15beb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
44 changes: 20 additions & 24 deletions compiler/rustc_hir_analysis/src/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,33 +536,29 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
let opaque_types =
self.fcx.infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
for (opaque_type_key, decl) in opaque_types {
let hidden_type = match decl.origin {
hir::OpaqueTyOrigin::FnReturn(_) | hir::OpaqueTyOrigin::AsyncFn(_) => {
let ty = self.resolve(decl.hidden_type.ty, &decl.hidden_type.span);
struct RecursionChecker {
def_id: LocalDefId,
}
impl<'tcx> ty::TypeVisitor<'tcx> for RecursionChecker {
type BreakTy = ();
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
if let ty::Opaque(def_id, _) = *t.kind() {
if def_id == self.def_id.to_def_id() {
return ControlFlow::Break(());
}
}
t.super_visit_with(self)
let hidden_type = self.resolve(decl.hidden_type.ty, &decl.hidden_type.span);

struct RecursionChecker {
def_id: LocalDefId,
}
impl<'tcx> ty::TypeVisitor<'tcx> for RecursionChecker {
type BreakTy = ();
fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
if let ty::Opaque(def_id, _) = *t.kind() {
if def_id == self.def_id.to_def_id() {
return ControlFlow::Break(());
}
}
if ty
.visit_with(&mut RecursionChecker { def_id: opaque_type_key.def_id })
.is_break()
{
return;
}
Some(ty)
t.super_visit_with(self)
}
hir::OpaqueTyOrigin::TyAlias => None,
};
}
if hidden_type
.visit_with(&mut RecursionChecker { def_id: opaque_type_key.def_id })
.is_break()
{
continue;
}

self.typeck_results.concrete_opaque_types.insert(opaque_type_key.def_id, hidden_type);
}
}
Expand Down
23 changes: 9 additions & 14 deletions compiler/rustc_hir_analysis/src/collect/type_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -788,20 +788,15 @@ fn find_opaque_ty_constraints_for_rpit(
// the `concrete_opaque_types` table.
tcx.ty_error()
} else {
table
.concrete_opaque_types
.get(&def_id)
.copied()
.unwrap_or_else(|| {
// We failed to resolve the opaque type or it
// resolves to itself. We interpret this as the
// no values of the hidden type ever being constructed,
// so we can just make the hidden type be `!`.
// For backwards compatibility reasons, we fall back to
// `()` until we the diverging default is changed.
Some(tcx.mk_diverging_default())
})
.expect("RPIT always have a hidden type from typeck")
table.concrete_opaque_types.get(&def_id).copied().unwrap_or_else(|| {
// We failed to resolve the opaque type or it
// resolves to itself. We interpret this as the
// no values of the hidden type ever being constructed,
// so we can just make the hidden type be `!`.
// For backwards compatibility reasons, we fall back to
// `()` until we the diverging default is changed.
tcx.mk_diverging_default()
})
}
})
}
Expand Down
8 changes: 3 additions & 5 deletions compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,10 @@ pub struct TypeckResults<'tcx> {
pub tainted_by_errors: Option<ErrorGuaranteed>,

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

/// Tracks the minimum captures required for a closure;
/// see `MinCaptureInformationMap` for more details.
Expand Down
15 changes: 11 additions & 4 deletions src/test/ui/impl-trait/issues/issue-86800.stderr
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
error: unconstrained opaque type
--> $DIR/issue-86800.rs:33:34
|
LL | type TransactionFuture<'__, O> = impl '__ + Future<Output = TransactionResult<O>>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
=


stack backtrace:

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


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

0 comments on commit 5d15beb

Please sign in to comment.