Skip to content

rustc_infer cleanups #131226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Oct 7, 2024
12 changes: 0 additions & 12 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,18 +925,6 @@ impl<'a> DiagCtxtHandle<'a> {
self.inner.borrow_mut().emit_stashed_diagnostics()
}

/// This excludes lint errors, and delayed bugs.
#[inline]
pub fn err_count_excluding_lint_errs(&self) -> usize {
let inner = self.inner.borrow();
inner.err_guars.len()
+ inner
.stashed_diagnostics
.values()
.filter(|(diag, guar)| guar.is_some() && diag.is_lint.is_none())
.count()
}

/// This excludes delayed bugs.
#[inline]
pub fn err_count(&self) -> usize {
Expand Down
17 changes: 1 addition & 16 deletions compiler/rustc_infer/src/infer/at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ impl<'tcx> InferCtxt<'tcx> {
reported_trait_errors: self.reported_trait_errors.clone(),
reported_signature_mismatch: self.reported_signature_mismatch.clone(),
tainted_by_errors: self.tainted_by_errors.clone(),
err_count_on_creation: self.err_count_on_creation,
universe: self.universe.clone(),
intercrate,
next_trait_solver: self.next_trait_solver,
Expand Down Expand Up @@ -382,21 +381,7 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
(GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
}

(
GenericArgKind::Lifetime(_),
GenericArgKind::Type(_) | GenericArgKind::Const(_),
)
| (
GenericArgKind::Type(_),
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_),
)
| (
GenericArgKind::Const(_),
GenericArgKind::Lifetime(_) | GenericArgKind::Type(_),
) => {
bug!("relating different kinds: {a:?} {b:?}")
}
_ => bug!("relating different kinds: {a:?} {b:?}"),
},
}
}
Expand Down
113 changes: 37 additions & 76 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use region_constraints::{
GenericKind, RegionConstraintCollector, RegionConstraintStorage, VarInfos, VerifyBound,
};
pub use relate::StructurallyRelateAliases;
pub use relate::combine::{CombineFields, PredicateEmittingRelation};
use relate::combine::CombineFields;
pub use relate::combine::PredicateEmittingRelation;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
use rustc_data_structures::sync::Lrc;
Expand Down Expand Up @@ -50,23 +51,22 @@ use snapshot::undo_log::InferCtxtUndoLogs;
use tracing::{debug, instrument};
use type_variable::TypeVariableOrigin;

use crate::infer::relate::RelateResult;
use crate::traits::{self, ObligationCause, ObligationInspector, PredicateObligation, TraitEngine};

pub mod at;
pub mod canonical;
mod context;
pub mod free_regions;
mod free_regions;
mod freshen;
mod lexical_region_resolve;
pub mod opaque_types;
mod opaque_types;
pub mod outlives;
mod projection;
pub mod region_constraints;
pub mod relate;
pub mod resolve;
pub(crate) mod snapshot;
pub mod type_variable;
mod type_variable;

#[must_use]
#[derive(Debug)]
Expand All @@ -76,8 +76,7 @@ pub struct InferOk<'tcx, T> {
}
pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;

pub type UnitResult<'tcx> = RelateResult<'tcx, ()>; // "unify result"
pub type FixupResult<T> = Result<T, FixupError>; // "fixup result"
pub(crate) type FixupResult<T> = Result<T, FixupError>; // "fixup result"

pub(crate) type UnificationTable<'a, 'tcx, T> = ut::UnificationTable<
ut::InPlace<T, &'a mut ut::UnificationStorage<T>, &'a mut InferCtxtUndoLogs<'tcx>>,
Expand Down Expand Up @@ -202,7 +201,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
}

#[inline]
pub fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
self.opaque_type_storage.with_log(&mut self.undo_log)
}

Expand Down Expand Up @@ -280,27 +279,14 @@ pub struct InferCtxt<'tcx> {
pub reported_signature_mismatch: RefCell<FxHashSet<(Span, Option<Span>)>>,

/// When an error occurs, we want to avoid reporting "derived"
/// errors that are due to this original failure. Normally, we
/// handle this with the `err_count_on_creation` count, which
/// basically just tracks how many errors were reported when we
/// started type-checking a fn and checks to see if any new errors
/// have been reported since then. Not great, but it works.
///
/// However, when errors originated in other passes -- notably
/// resolve -- this heuristic breaks down. Therefore, we have this
/// auxiliary flag that one can set whenever one creates a
/// type-error that is due to an error in a prior pass.
/// errors that are due to this original failure. We have this
/// flag that one can set whenever one creates a type-error that
/// is due to an error in a prior pass.
///
/// Don't read this flag directly, call `is_tainted_by_errors()`
/// and `set_tainted_by_errors()`.
tainted_by_errors: Cell<Option<ErrorGuaranteed>>,

/// Track how many errors were reported when this infcx is created.
/// If the number of errors increases, that's also a sign (like
/// `tainted_by_errors`) to avoid reporting certain kinds of errors.
// FIXME(matthewjasper) Merge into `tainted_by_errors`
err_count_on_creation: usize,

/// What is the innermost universe we have created? Starts out as
/// `UniverseIndex::root()` but grows from there as we enter
/// universal quantifiers.
Expand Down Expand Up @@ -509,46 +495,41 @@ pub enum NllRegionVariableOrigin {
},
}

// FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
#[derive(Copy, Clone, Debug)]
pub enum FixupError {
UnresolvedIntTy(IntVid),
UnresolvedFloatTy(FloatVid),
UnresolvedTy(TyVid),
UnresolvedConst(ConstVid),
UnresolvedEffect(EffectVid),
}

/// See the `region_obligations` field for more information.
#[derive(Clone, Debug)]
pub struct RegionObligation<'tcx> {
pub sub_region: ty::Region<'tcx>,
pub sup_type: Ty<'tcx>,
pub origin: SubregionOrigin<'tcx>,
pub struct FixupError {
unresolved: TyOrConstInferVar,
}

impl fmt::Display for FixupError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use self::FixupError::*;
use TyOrConstInferVar::*;

match *self {
UnresolvedIntTy(_) => write!(
match self.unresolved {
TyInt(_) => write!(
f,
"cannot determine the type of this integer; \
add a suffix to specify the type explicitly"
),
UnresolvedFloatTy(_) => write!(
TyFloat(_) => write!(
f,
"cannot determine the type of this number; \
add a suffix to specify the type explicitly"
),
UnresolvedTy(_) => write!(f, "unconstrained type"),
UnresolvedConst(_) => write!(f, "unconstrained const value"),
UnresolvedEffect(_) => write!(f, "unconstrained effect value"),
Ty(_) => write!(f, "unconstrained type"),
Const(_) => write!(f, "unconstrained const value"),
Effect(_) => write!(f, "unconstrained effect value"),
}
}
}

/// See the `region_obligations` field for more information.
#[derive(Clone, Debug)]
pub struct RegionObligation<'tcx> {
pub sub_region: ty::Region<'tcx>,
pub sup_type: Ty<'tcx>,
pub origin: SubregionOrigin<'tcx>,
}

/// Used to configure inference contexts before their creation.
pub struct InferCtxtBuilder<'tcx> {
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -588,14 +569,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
self
}

pub fn with_defining_opaque_types(
mut self,
defining_opaque_types: &'tcx ty::List<LocalDefId>,
) -> Self {
self.defining_opaque_types = defining_opaque_types;
self
}

pub fn with_next_trait_solver(mut self, next_trait_solver: bool) -> Self {
self.next_trait_solver = next_trait_solver;
self
Expand Down Expand Up @@ -624,14 +597,15 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
/// the bound values in `C` to their instantiated values in `V`
/// (in other words, `S(C) = V`).
pub fn build_with_canonical<T>(
self,
mut self,
span: Span,
canonical: &Canonical<'tcx, T>,
) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)
where
T: TypeFoldable<TyCtxt<'tcx>>,
{
let infcx = self.with_defining_opaque_types(canonical.defining_opaque_types).build();
self.defining_opaque_types = canonical.defining_opaque_types;
let infcx = self.build();
let (value, args) = infcx.instantiate_canonical(span, canonical);
(infcx, value, args)
}
Expand All @@ -657,7 +631,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
reported_trait_errors: Default::default(),
reported_signature_mismatch: Default::default(),
tainted_by_errors: Cell::new(None),
err_count_on_creation: tcx.dcx().err_count_excluding_lint_errs(),
universe: Cell::new(ty::UniverseIndex::ROOT),
intercrate,
next_trait_solver,
Expand Down Expand Up @@ -919,28 +892,16 @@ impl<'tcx> InferCtxt<'tcx> {
ty::Const::new_var(self.tcx, vid)
}

pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid {
self.inner
.borrow_mut()
.const_unification_table()
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
.vid
}

fn next_int_var_id(&self) -> IntVid {
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown)
}

pub fn next_int_var(&self) -> Ty<'tcx> {
Ty::new_int_var(self.tcx, self.next_int_var_id())
}

fn next_float_var_id(&self) -> FloatVid {
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown)
let next_int_var_id =
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown);
Ty::new_int_var(self.tcx, next_int_var_id)
}

pub fn next_float_var(&self) -> Ty<'tcx> {
Ty::new_float_var(self.tcx, self.next_float_var_id())
let next_float_var_id =
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown);
Ty::new_float_var(self.tcx, next_float_var_id)
}

/// Creates a fresh region variable with the next available index.
Expand Down Expand Up @@ -1353,7 +1314,7 @@ impl<'tcx> InferCtxt<'tcx> {
}

/// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
pub fn verify_generic_bound(
pub(crate) fn verify_generic_bound(
&self,
origin: SubregionOrigin<'tcx>,
kind: GenericKind<'tcx>,
Expand Down
27 changes: 6 additions & 21 deletions compiler/rustc_infer/src/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ use rustc_middle::ty::{
use rustc_span::Span;
use tracing::{debug, instrument};

use super::DefineOpaqueTypes;
use crate::errors::OpaqueHiddenTypeDiag;
use crate::infer::{InferCtxt, InferOk};
use crate::traits::{self, Obligation};

mod table;

pub type OpaqueTypeMap<'tcx> = FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>;
pub use table::{OpaqueTypeStorage, OpaqueTypeTable};

use super::DefineOpaqueTypes;
pub(crate) type OpaqueTypeMap<'tcx> = FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>;
pub(crate) use table::{OpaqueTypeStorage, OpaqueTypeTable};

/// Information about the opaque types whose values we
/// are inferring in this function (these are the `impl Trait` that
Expand Down Expand Up @@ -377,9 +376,9 @@ impl<'tcx> InferCtxt<'tcx> {
///
/// We ignore any type parameters because impl trait values are assumed to
/// capture all the in-scope type parameters.
pub struct ConstrainOpaqueTypeRegionVisitor<'tcx, OP: FnMut(ty::Region<'tcx>)> {
pub tcx: TyCtxt<'tcx>,
pub op: OP,
struct ConstrainOpaqueTypeRegionVisitor<'tcx, OP: FnMut(ty::Region<'tcx>)> {
tcx: TyCtxt<'tcx>,
op: OP,
}

impl<'tcx, OP> TypeVisitor<TyCtxt<'tcx>> for ConstrainOpaqueTypeRegionVisitor<'tcx, OP>
Expand Down Expand Up @@ -455,20 +454,6 @@ where
}
}

pub enum UseKind {
DefiningUse,
OpaqueUse,
}

impl UseKind {
pub fn is_defining(self) -> bool {
match self {
UseKind::DefiningUse => true,
UseKind::OpaqueUse => false,
}
}
}

impl<'tcx> InferCtxt<'tcx> {
#[instrument(skip(self), level = "debug")]
fn register_hidden_type(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/opaque_types/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{OpaqueTypeDecl, OpaqueTypeMap};
use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, UndoLog};

#[derive(Default, Debug, Clone)]
pub struct OpaqueTypeStorage<'tcx> {
pub(crate) struct OpaqueTypeStorage<'tcx> {
/// Opaque types found in explicit return types and their
/// associated fresh inference variable. Writeback resolves these
/// variables to get the concrete type, which can be used to
Expand Down Expand Up @@ -46,7 +46,7 @@ impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
}
}

pub struct OpaqueTypeTable<'a, 'tcx> {
pub(crate) struct OpaqueTypeTable<'a, 'tcx> {
storage: &'a mut OpaqueTypeStorage<'tcx>,

undo_log: &'a mut InferCtxtUndoLogs<'tcx>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/infer/outlives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub mod env;
pub mod for_liveness;
pub mod obligations;
pub mod test_type_match;
pub mod verify;
pub(crate) mod verify;

#[instrument(level = "debug", skip(param_env), ret)]
pub fn explicit_outlives_bounds<'tcx>(
Expand Down
Loading
Loading