1
Fork 0

Eliminate DefiningAnchor::Error, it is indistinguishable from DefiningAnchor::Bind with an empty list

This commit is contained in:
Oli Scherer 2024-03-07 09:08:20 +00:00
parent 40d5609548
commit 7348dd1950
5 changed files with 11 additions and 18 deletions

View file

@ -311,7 +311,7 @@ fn check_opaque_type_well_formed<'tcx>(
parent_def_id = tcx.local_parent(parent_def_id); 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 // and prepopulate this `InferCtxt` with known opaque values, rather than
// using the `Bind` anchor here. For now it's fine. // using the `Bind` anchor here. For now it's fine.
let infcx = tcx let infcx = tcx

View file

@ -242,7 +242,8 @@ pub struct InferCtxt<'tcx> {
/// short lived InferCtxt within queries. The opaque type obligations are forwarded /// 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. /// 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. /// might come up during inference or typeck.
pub defining_use_anchor: DefiningAnchor<'tcx>, pub defining_use_anchor: DefiningAnchor<'tcx>,
@ -620,7 +621,7 @@ impl<'tcx> TyCtxt<'tcx> {
fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> { fn infer_ctxt(self) -> InferCtxtBuilder<'tcx> {
InferCtxtBuilder { InferCtxtBuilder {
tcx: self, tcx: self,
defining_use_anchor: DefiningAnchor::Error, defining_use_anchor: DefiningAnchor::Bind(ty::List::empty()),
considering_regions: true, considering_regions: true,
skip_leak_check: false, skip_leak_check: false,
intercrate: false, intercrate: false,
@ -1208,13 +1209,11 @@ impl<'tcx> InferCtxt<'tcx> {
#[instrument(level = "debug", skip(self), ret)] #[instrument(level = "debug", skip(self), ret)]
pub fn take_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> { 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) std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
} }
#[instrument(level = "debug", skip(self), ret)] #[instrument(level = "debug", skip(self), ret)]
pub fn clone_opaque_types(&self) -> opaque_types::OpaqueTypeMap<'tcx> { 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() self.inner.borrow().opaque_type_storage.opaque_types.clone()
} }

View file

@ -150,9 +150,6 @@ impl<'tcx> InferCtxt<'tcx> {
} }
} }
DefiningAnchor::Bubble => {} DefiningAnchor::Bubble => {}
DefiningAnchor::Error => {
return None;
}
} }
if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, .. }) = *b.kind() { 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 // We could accept this, but there are various ways to handle this situation, and we don't
@ -379,7 +376,7 @@ impl<'tcx> InferCtxt<'tcx> {
#[instrument(skip(self), level = "trace", ret)] #[instrument(skip(self), level = "trace", ret)]
pub fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<OpaqueTyOrigin> { pub fn opaque_type_origin(&self, def_id: LocalDefId) -> Option<OpaqueTyOrigin> {
let defined_opaque_types = match self.defining_use_anchor { let defined_opaque_types = match self.defining_use_anchor {
DefiningAnchor::Bubble | DefiningAnchor::Error => return None, DefiningAnchor::Bubble => return None,
DefiningAnchor::Bind(bind) => bind, DefiningAnchor::Bind(bind) => bind,
}; };

View file

@ -1004,6 +1004,10 @@ pub enum CodegenObligationError {
pub enum DefiningAnchor<'tcx> { pub enum DefiningAnchor<'tcx> {
/// Define opaques which are in-scope of the current item being analyzed. /// 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`. /// 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>), Bind(&'tcx ty::List<LocalDefId>),
/// In contexts where we don't currently know what opaques are allowed to be /// 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 /// defined, such as (old solver) canonical queries, we will simply allow
@ -1013,10 +1017,6 @@ pub enum DefiningAnchor<'tcx> {
/// We do not eagerly replace opaque types in `replace_opaque_types_with_inference_vars`, /// 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. /// which may affect what predicates pass and fail in the old trait solver.
Bubble, 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> { impl<'tcx> DefiningAnchor<'tcx> {

View file

@ -16,7 +16,7 @@ use rustc_middle::traits::solve::{
CanonicalInput, CanonicalResponse, Certainty, IsNormalizesToHack, PredefinedOpaques, CanonicalInput, CanonicalResponse, Certainty, IsNormalizesToHack, PredefinedOpaques,
PredefinedOpaquesData, QueryResult, PredefinedOpaquesData, QueryResult,
}; };
use rustc_middle::traits::{specialization_graph, DefiningAnchor}; use rustc_middle::traits::specialization_graph;
use rustc_middle::ty::{ use rustc_middle::ty::{
self, InferCtxtLike, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, self, InferCtxtLike, OpaqueTypeKey, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable,
TypeVisitable, TypeVisitableExt, TypeVisitor, TypeVisitable, TypeVisitableExt, TypeVisitor,
@ -258,10 +258,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
// instead of taking them. This would cause an ICE here, since we have // instead of taking them. This would cause an ICE here, since we have
// assertions against dropping an `InferCtxt` without taking opaques. // assertions against dropping an `InferCtxt` without taking opaques.
// FIXME: Once we remove support for the old impl we can remove this. // 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 result
} }