Skip to content

Commit 8841a3d

Browse files
committed
Auto merge of rust-lang#131226 - nnethercote:rustc_infer-cleanups, r=lcnr
`rustc_infer` cleanups Various small improvements I found while reading over this code. r? `@lcnr`
2 parents a964a92 + 3fdcde7 commit 8841a3d

File tree

17 files changed

+90
-195
lines changed

17 files changed

+90
-195
lines changed

compiler/rustc_errors/src/lib.rs

-12
Original file line numberDiff line numberDiff line change
@@ -925,18 +925,6 @@ impl<'a> DiagCtxtHandle<'a> {
925925
self.inner.borrow_mut().emit_stashed_diagnostics()
926926
}
927927

928-
/// This excludes lint errors, and delayed bugs.
929-
#[inline]
930-
pub fn err_count_excluding_lint_errs(&self) -> usize {
931-
let inner = self.inner.borrow();
932-
inner.err_guars.len()
933-
+ inner
934-
.stashed_diagnostics
935-
.values()
936-
.filter(|(diag, guar)| guar.is_some() && diag.is_lint.is_none())
937-
.count()
938-
}
939-
940928
/// This excludes delayed bugs.
941929
#[inline]
942930
pub fn err_count(&self) -> usize {
File renamed without changes.

compiler/rustc_infer/src/infer/at.rs

+1-16
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ impl<'tcx> InferCtxt<'tcx> {
8282
reported_trait_errors: self.reported_trait_errors.clone(),
8383
reported_signature_mismatch: self.reported_signature_mismatch.clone(),
8484
tainted_by_errors: self.tainted_by_errors.clone(),
85-
err_count_on_creation: self.err_count_on_creation,
8685
universe: self.universe.clone(),
8786
intercrate,
8887
next_trait_solver: self.next_trait_solver,
@@ -382,21 +381,7 @@ impl<'tcx> ToTrace<'tcx> for ty::GenericArg<'tcx> {
382381
(GenericArgKind::Const(a), GenericArgKind::Const(b)) => {
383382
ValuePairs::Terms(ExpectedFound::new(true, a.into(), b.into()))
384383
}
385-
386-
(
387-
GenericArgKind::Lifetime(_),
388-
GenericArgKind::Type(_) | GenericArgKind::Const(_),
389-
)
390-
| (
391-
GenericArgKind::Type(_),
392-
GenericArgKind::Lifetime(_) | GenericArgKind::Const(_),
393-
)
394-
| (
395-
GenericArgKind::Const(_),
396-
GenericArgKind::Lifetime(_) | GenericArgKind::Type(_),
397-
) => {
398-
bug!("relating different kinds: {a:?} {b:?}")
399-
}
384+
_ => bug!("relating different kinds: {a:?} {b:?}"),
400385
},
401386
}
402387
}

compiler/rustc_infer/src/infer/mod.rs

+37-76
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use region_constraints::{
1414
GenericKind, RegionConstraintCollector, RegionConstraintStorage, VarInfos, VerifyBound,
1515
};
1616
pub use relate::StructurallyRelateAliases;
17-
pub use relate::combine::{CombineFields, PredicateEmittingRelation};
17+
use relate::combine::CombineFields;
18+
pub use relate::combine::PredicateEmittingRelation;
1819
use rustc_data_structures::captures::Captures;
1920
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2021
use rustc_data_structures::sync::Lrc;
@@ -50,23 +51,22 @@ use snapshot::undo_log::InferCtxtUndoLogs;
5051
use tracing::{debug, instrument};
5152
use type_variable::TypeVariableOrigin;
5253

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

5656
pub mod at;
5757
pub mod canonical;
5858
mod context;
59-
pub mod free_regions;
59+
mod free_regions;
6060
mod freshen;
6161
mod lexical_region_resolve;
62-
pub mod opaque_types;
62+
mod opaque_types;
6363
pub mod outlives;
6464
mod projection;
6565
pub mod region_constraints;
6666
pub mod relate;
6767
pub mod resolve;
6868
pub(crate) mod snapshot;
69-
pub mod type_variable;
69+
mod type_variable;
7070

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

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

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

204203
#[inline]
205-
pub fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
204+
fn opaque_types(&mut self) -> opaque_types::OpaqueTypeTable<'_, 'tcx> {
206205
self.opaque_type_storage.with_log(&mut self.undo_log)
207206
}
208207

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

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

298-
/// Track how many errors were reported when this infcx is created.
299-
/// If the number of errors increases, that's also a sign (like
300-
/// `tainted_by_errors`) to avoid reporting certain kinds of errors.
301-
// FIXME(matthewjasper) Merge into `tainted_by_errors`
302-
err_count_on_creation: usize,
303-
304290
/// What is the innermost universe we have created? Starts out as
305291
/// `UniverseIndex::root()` but grows from there as we enter
306292
/// universal quantifiers.
@@ -509,46 +495,41 @@ pub enum NllRegionVariableOrigin {
509495
},
510496
}
511497

512-
// FIXME(eddyb) investigate overlap between this and `TyOrConstInferVar`.
513498
#[derive(Copy, Clone, Debug)]
514-
pub enum FixupError {
515-
UnresolvedIntTy(IntVid),
516-
UnresolvedFloatTy(FloatVid),
517-
UnresolvedTy(TyVid),
518-
UnresolvedConst(ConstVid),
519-
UnresolvedEffect(EffectVid),
520-
}
521-
522-
/// See the `region_obligations` field for more information.
523-
#[derive(Clone, Debug)]
524-
pub struct RegionObligation<'tcx> {
525-
pub sub_region: ty::Region<'tcx>,
526-
pub sup_type: Ty<'tcx>,
527-
pub origin: SubregionOrigin<'tcx>,
499+
pub struct FixupError {
500+
unresolved: TyOrConstInferVar,
528501
}
529502

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

534-
match *self {
535-
UnresolvedIntTy(_) => write!(
507+
match self.unresolved {
508+
TyInt(_) => write!(
536509
f,
537510
"cannot determine the type of this integer; \
538511
add a suffix to specify the type explicitly"
539512
),
540-
UnresolvedFloatTy(_) => write!(
513+
TyFloat(_) => write!(
541514
f,
542515
"cannot determine the type of this number; \
543516
add a suffix to specify the type explicitly"
544517
),
545-
UnresolvedTy(_) => write!(f, "unconstrained type"),
546-
UnresolvedConst(_) => write!(f, "unconstrained const value"),
547-
UnresolvedEffect(_) => write!(f, "unconstrained effect value"),
518+
Ty(_) => write!(f, "unconstrained type"),
519+
Const(_) => write!(f, "unconstrained const value"),
520+
Effect(_) => write!(f, "unconstrained effect value"),
548521
}
549522
}
550523
}
551524

525+
/// See the `region_obligations` field for more information.
526+
#[derive(Clone, Debug)]
527+
pub struct RegionObligation<'tcx> {
528+
pub sub_region: ty::Region<'tcx>,
529+
pub sup_type: Ty<'tcx>,
530+
pub origin: SubregionOrigin<'tcx>,
531+
}
532+
552533
/// Used to configure inference contexts before their creation.
553534
pub struct InferCtxtBuilder<'tcx> {
554535
tcx: TyCtxt<'tcx>,
@@ -588,14 +569,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
588569
self
589570
}
590571

591-
pub fn with_defining_opaque_types(
592-
mut self,
593-
defining_opaque_types: &'tcx ty::List<LocalDefId>,
594-
) -> Self {
595-
self.defining_opaque_types = defining_opaque_types;
596-
self
597-
}
598-
599572
pub fn with_next_trait_solver(mut self, next_trait_solver: bool) -> Self {
600573
self.next_trait_solver = next_trait_solver;
601574
self
@@ -624,14 +597,15 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
624597
/// the bound values in `C` to their instantiated values in `V`
625598
/// (in other words, `S(C) = V`).
626599
pub fn build_with_canonical<T>(
627-
self,
600+
mut self,
628601
span: Span,
629602
canonical: &Canonical<'tcx, T>,
630603
) -> (InferCtxt<'tcx>, T, CanonicalVarValues<'tcx>)
631604
where
632605
T: TypeFoldable<TyCtxt<'tcx>>,
633606
{
634-
let infcx = self.with_defining_opaque_types(canonical.defining_opaque_types).build();
607+
self.defining_opaque_types = canonical.defining_opaque_types;
608+
let infcx = self.build();
635609
let (value, args) = infcx.instantiate_canonical(span, canonical);
636610
(infcx, value, args)
637611
}
@@ -657,7 +631,6 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
657631
reported_trait_errors: Default::default(),
658632
reported_signature_mismatch: Default::default(),
659633
tainted_by_errors: Cell::new(None),
660-
err_count_on_creation: tcx.dcx().err_count_excluding_lint_errs(),
661634
universe: Cell::new(ty::UniverseIndex::ROOT),
662635
intercrate,
663636
next_trait_solver,
@@ -919,28 +892,16 @@ impl<'tcx> InferCtxt<'tcx> {
919892
ty::Const::new_var(self.tcx, vid)
920893
}
921894

922-
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid {
923-
self.inner
924-
.borrow_mut()
925-
.const_unification_table()
926-
.new_key(ConstVariableValue::Unknown { origin, universe: self.universe() })
927-
.vid
928-
}
929-
930-
fn next_int_var_id(&self) -> IntVid {
931-
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown)
932-
}
933-
934895
pub fn next_int_var(&self) -> Ty<'tcx> {
935-
Ty::new_int_var(self.tcx, self.next_int_var_id())
936-
}
937-
938-
fn next_float_var_id(&self) -> FloatVid {
939-
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown)
896+
let next_int_var_id =
897+
self.inner.borrow_mut().int_unification_table().new_key(ty::IntVarValue::Unknown);
898+
Ty::new_int_var(self.tcx, next_int_var_id)
940899
}
941900

942901
pub fn next_float_var(&self) -> Ty<'tcx> {
943-
Ty::new_float_var(self.tcx, self.next_float_var_id())
902+
let next_float_var_id =
903+
self.inner.borrow_mut().float_unification_table().new_key(ty::FloatVarValue::Unknown);
904+
Ty::new_float_var(self.tcx, next_float_var_id)
944905
}
945906

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

13551316
/// See the [`region_constraints::RegionConstraintCollector::verify_generic_bound`] method.
1356-
pub fn verify_generic_bound(
1317+
pub(crate) fn verify_generic_bound(
13571318
&self,
13581319
origin: SubregionOrigin<'tcx>,
13591320
kind: GenericKind<'tcx>,

compiler/rustc_infer/src/infer/opaque_types/mod.rs

+6-21
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@ use rustc_middle::ty::{
1313
use rustc_span::Span;
1414
use tracing::{debug, instrument};
1515

16+
use super::DefineOpaqueTypes;
1617
use crate::errors::OpaqueHiddenTypeDiag;
1718
use crate::infer::{InferCtxt, InferOk};
1819
use crate::traits::{self, Obligation};
1920

2021
mod table;
2122

22-
pub type OpaqueTypeMap<'tcx> = FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>;
23-
pub use table::{OpaqueTypeStorage, OpaqueTypeTable};
24-
25-
use super::DefineOpaqueTypes;
23+
pub(crate) type OpaqueTypeMap<'tcx> = FxIndexMap<OpaqueTypeKey<'tcx>, OpaqueTypeDecl<'tcx>>;
24+
pub(crate) use table::{OpaqueTypeStorage, OpaqueTypeTable};
2625

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

385384
impl<'tcx, OP> TypeVisitor<TyCtxt<'tcx>> for ConstrainOpaqueTypeRegionVisitor<'tcx, OP>
@@ -455,20 +454,6 @@ where
455454
}
456455
}
457456

458-
pub enum UseKind {
459-
DefiningUse,
460-
OpaqueUse,
461-
}
462-
463-
impl UseKind {
464-
pub fn is_defining(self) -> bool {
465-
match self {
466-
UseKind::DefiningUse => true,
467-
UseKind::OpaqueUse => false,
468-
}
469-
}
470-
}
471-
472457
impl<'tcx> InferCtxt<'tcx> {
473458
#[instrument(skip(self), level = "debug")]
474459
fn register_hidden_type(

compiler/rustc_infer/src/infer/opaque_types/table.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use super::{OpaqueTypeDecl, OpaqueTypeMap};
77
use crate::infer::snapshot::undo_log::{InferCtxtUndoLogs, UndoLog};
88

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

49-
pub struct OpaqueTypeTable<'a, 'tcx> {
49+
pub(crate) struct OpaqueTypeTable<'a, 'tcx> {
5050
storage: &'a mut OpaqueTypeStorage<'tcx>,
5151

5252
undo_log: &'a mut InferCtxtUndoLogs<'tcx>,

compiler/rustc_infer/src/infer/outlives/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub mod env;
1414
pub mod for_liveness;
1515
pub mod obligations;
1616
pub mod test_type_match;
17-
pub mod verify;
17+
pub(crate) mod verify;
1818

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

0 commit comments

Comments
 (0)