Skip to content

Commit

Permalink
Eliminate DefiningAnchor::Error, it is indistinguishable from `Defi…
Browse files Browse the repository at this point in the history
…ningAnchor::Bind` with an empty list
  • Loading branch information
oli-obk committed Mar 11, 2024
1 parent 40d5609 commit 7348dd1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/opaque_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ fn check_opaque_type_well_formed<'tcx>(
parent_def_id = tcx.local_parent(parent_def_id);
}

// FIXME(-Znext-solver): We probably should use `DefiningAnchor::Error`
// FIXME(-Znext-solver): We probably should use `DefiningAnchor::Bind(&[])`
// and prepopulate this `InferCtxt` with known opaque values, rather than
// using the `Bind` anchor here. For now it's fine.
let infcx = tcx
Expand Down
7 changes: 3 additions & 4 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ pub struct InferCtxt<'tcx> {
/// short lived InferCtxt within queries. The opaque type obligations are forwarded
/// to the outside until the end up in an `InferCtxt` for typeck or borrowck.
///
/// Its default value is `DefiningAnchor::Error`, this way it is easier to catch errors that
/// Its default value is `DefiningAnchor::Bind(&[])`, which means no opaque types may be defined.
/// This way it is easier to catch errors that
/// might come up during inference or typeck.
pub defining_use_anchor: DefiningAnchor<'tcx>,

Expand Down Expand Up @@ -620,7 +621,7 @@ impl<'tcx> TyCtxt<'tcx> {
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
InferCtxtBuilder {
tcx: self,
defining_use_anchor: DefiningAnchor::Error,
defining_use_anchor: DefiningAnchor::Bind(ty::List::empty()),
considering_regions: true,
skip_leak_check: false,
intercrate: false,
Expand Down Expand Up @@ -1208,13 +1209,11 @@ impl<'tcx> InferCtxt<'tcx> {

#[instrument(level = "debug", skip(self), ret)]
pub fn take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
debug_assert_ne!(self.defining_use_anchor, DefiningAnchor::Error);
std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
}

#[instrument(level = "debug", skip(self), ret)]
pub fn clone_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> {
debug_assert_ne!(self.defining_use_anchor, DefiningAnchor::Error);
self.inner.borrow().opaque_type_storage.opaque_types.clone()
}

Expand Down
5 changes: 1 addition & 4 deletions compiler/rustc_infer/src/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ impl<'tcx> InferCtxt<'tcx> {
}
}
DefiningAnchor::Bubble => {}
DefiningAnchor::Error => {
return None;
}
}
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() {
// We could accept this, but there are various ways to handle this situation, and we don't
Expand Down Expand Up @@ -379,7 +376,7 @@ impl<'tcx> InferCtxt<'tcx> {
#[instrument(skip(self), level = "trace", ret)]
pub fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<OpaqueTyOrigin> {
let defined_opaque_types = match self.defining_use_anchor {
DefiningAnchor::Bubble | DefiningAnchor::Error => return None,
DefiningAnchor::Bubble => return None,
DefiningAnchor::Bind(bind) => bind,
};

Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,10 @@ pub enum CodegenObligationError {
pub enum DefiningAnchor<'tcx> {
/// Define opaques which are in-scope of the current item being analyzed.
/// Also, eagerly replace these opaque types in `replace_opaque_types_with_inference_vars`.
///
/// If the list is empty, do not allow any opaques to be defined. This is used to catch type mismatch
/// errors when handling opaque types, and also should be used when we would
/// otherwise reveal opaques (such as [`Reveal::All`] reveal mode).
Bind(&'tcx ty::List<LocalDefId>),
/// In contexts where we don't currently know what opaques are allowed to be
/// defined, such as (old solver) canonical queries, we will simply allow
Expand All @@ -1013,10 +1017,6 @@ pub enum DefiningAnchor<'tcx> {
/// We do not eagerly replace opaque types in `replace_opaque_types_with_inference_vars`,
/// which may affect what predicates pass and fail in the old trait solver.
Bubble,
/// Do not allow any opaques to be defined. This is used to catch type mismatch
/// errors when handling opaque types, and also should be used when we would
/// otherwise reveal opaques (such as [`Reveal::All`] reveal mode).
Error,
}

impl<'tcx> DefiningAnchor<'tcx> {
Expand Down
7 changes: 2 additions & 5 deletions compiler/rustc_trait_selection/src/solve/eval_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_middle::traits::solve::{
CanonicalInput, CanonicalResponse, Certainty, IsNormalizesToHack, PredefinedOpaques,
PredefinedOpaquesData, QueryResult,
};
use rustc_middle::traits::{specialization_graph, DefiningAnchor};
use rustc_middle::traits::specialization_graph;
use rustc_middle::ty::{
self, InferCtxtLike, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
TypeVisitable, TypeVisitableExt, TypeVisitor,
Expand Down Expand Up @@ -258,10 +258,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
// instead of taking them. This would cause an ICE here, since we have
// assertions against dropping an `InferCtxt` without taking opaques.
// FIXME: Once we remove support for the old impl we can remove this.
if input.anchor != DefiningAnchor::Error {
// This seems ok, but fragile.
let _ = infcx.take_opaque_types();
}
let _ = infcx.take_opaque_types();

result
}
Expand Down

0 comments on commit 7348dd1

Please sign in to comment.