Skip to content

Commit

Permalink
Preserve ErrorGuaranteed when comparing two opaque types that are b…
Browse files Browse the repository at this point in the history
…oth in their defining scope
  • Loading branch information
oli-obk committed Apr 11, 2024
1 parent bb78dba commit 6df1ffe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
30 changes: 25 additions & 5 deletions compiler/rustc_infer/src/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,40 @@ impl<'tcx> InferCtxt<'tcx> {
return None;
}

if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() {
if let ty::Alias(ty::Opaque, b) = *b.kind() {
// We could accept this, but there are various ways to handle this situation, and we don't
// want to make a decision on it right now. Likely this case is so super rare anyway, that
// no one encounters it in practice.
// It does occur however in `fn fut() -> impl Future<Output = i32> { async { 42 } }`,
// where it is of no concern, so we only check for TAITs.
if self.can_define_opaque_ty(b_def_id)
&& self.tcx.is_type_alias_impl_trait(b_def_id)
if self.can_define_opaque_ty(b.def_id)
&& self.tcx.is_type_alias_impl_trait(b.def_id)
{
self.tcx.dcx().emit_err(OpaqueHiddenTypeDiag {
let guar = self.tcx.dcx().emit_err(OpaqueHiddenTypeDiag {
span: cause.span,
hidden_type: self.tcx.def_span(b_def_id),
hidden_type: self.tcx.def_span(b.def_id),
opaque_type: self.tcx.def_span(def_id),
});
let mut obligations = vec![];
return Some(
try {
self.insert_hidden_type(
OpaqueTypeKey { def_id, args },
&cause,
param_env,
Ty::new_error(self.tcx, guar),
&mut obligations,
)?;
self.insert_hidden_type(
OpaqueTypeKey { def_id: b.def_id.expect_local(), args: b.args },
&cause,
param_env,
Ty::new_error(self.tcx, guar),
&mut obligations,
)?;
InferOk { value: (), obligations }
},
);
}
}
Some(self.register_hidden_type(
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_infer/src/infer/opaque_types/table.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_data_structures::undo_log::UndoLogs;
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty};
use rustc_middle::ty::{self, OpaqueHiddenType, OpaqueTypeKey, Ty, TypeVisitableExt};

use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, UndoLog};

Expand Down Expand Up @@ -59,6 +59,9 @@ impl<'a, 'tcx> OpaqueTypeTable<'a, 'tcx> {
hidden_type: OpaqueHiddenType<'tcx>,
) -> Option<Ty<'tcx>> {
if let Some(decl) = self.storage.opaque_types.get_mut(&key) {
if decl.hidden_type.ty.references_error() {
return Some(decl.hidden_type.ty);
}
let prev = std::mem::replace(&mut decl.hidden_type, hidden_type);
self.undo_log.push(UndoLog::OpaqueTypes(key, Some(prev)));
return Some(prev.ty);
Expand Down

0 comments on commit 6df1ffe

Please sign in to comment.