Skip to content

Commit dc52db6

Browse files
committed
Remove InternedStandardTypes
There is no reason to have this duplicated across all inference results
1 parent 6feb662 commit dc52db6

File tree

6 files changed

+59
-97
lines changed

6 files changed

+59
-97
lines changed

crates/hir-ty/src/infer.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ mod pat;
2323
mod path;
2424
pub(crate) mod unify;
2525

26-
use std::{cell::OnceCell, convert::identity, iter, ops::Index};
26+
use std::{cell::OnceCell, convert::identity, iter, ops::Index, sync::LazyLock};
2727

2828
use chalk_ir::{
29-
DebruijnIndex, Mutability, Safety, Scalar, TyKind, TypeFlags, Variance,
29+
DebruijnIndex, Mutability, Safety, TyKind, TypeFlags, Variance,
3030
cast::Cast,
3131
fold::TypeFoldable,
3232
interner::HasInterner,
@@ -195,6 +195,10 @@ pub enum InferenceTyDiagnosticSource {
195195
Signature,
196196
}
197197

198+
pub(crate) static ERROR_TY: LazyLock<Ty> = LazyLock::new(|| TyKind::Error.intern(Interner));
199+
pub(crate) static UNIT_TY: LazyLock<Ty> =
200+
LazyLock::new(|| TyKind::Tuple(0, Substitution::empty(Interner)).intern(Interner));
201+
198202
#[derive(Debug)]
199203
pub(crate) struct TypeError;
200204
pub(crate) type InferResult<T> = Result<InferOk<T>, TypeError>;
@@ -299,24 +303,6 @@ pub struct TypeMismatch {
299303
pub actual: Ty,
300304
}
301305

302-
#[derive(Clone, PartialEq, Eq, Debug)]
303-
struct InternedStandardTypes {
304-
unknown: Ty,
305-
bool_: Ty,
306-
unit: Ty,
307-
never: Ty,
308-
}
309-
310-
impl Default for InternedStandardTypes {
311-
fn default() -> Self {
312-
InternedStandardTypes {
313-
unknown: TyKind::Error.intern(Interner),
314-
bool_: TyKind::Scalar(Scalar::Bool).intern(Interner),
315-
unit: TyKind::Tuple(0, Substitution::empty(Interner)).intern(Interner),
316-
never: TyKind::Never.intern(Interner),
317-
}
318-
}
319-
}
320306
/// Represents coercing a value to a different type of value.
321307
///
322308
/// We transform values by following a number of `Adjust` steps in order.
@@ -475,8 +461,6 @@ pub struct InferenceResult {
475461
// Which will then mark this field.
476462
pub(crate) has_errors: bool,
477463
/// Interned common types to return references to.
478-
// FIXME: Move this into `InferenceContext`
479-
standard_types: InternedStandardTypes,
480464
/// Stores the types which were implicitly dereferenced in pattern binding modes.
481465
pub pat_adjustments: FxHashMap<PatId, Vec<Ty>>,
482466
/// Stores the binding mode (`ref` in `let ref x = 2`) of bindings.
@@ -564,31 +548,31 @@ impl Index<ExprId> for InferenceResult {
564548
type Output = Ty;
565549

566550
fn index(&self, expr: ExprId) -> &Ty {
567-
self.type_of_expr.get(expr).unwrap_or(&self.standard_types.unknown)
551+
self.type_of_expr.get(expr).unwrap_or_else(|| &ERROR_TY)
568552
}
569553
}
570554

571555
impl Index<PatId> for InferenceResult {
572556
type Output = Ty;
573557

574558
fn index(&self, pat: PatId) -> &Ty {
575-
self.type_of_pat.get(pat).unwrap_or(&self.standard_types.unknown)
559+
self.type_of_pat.get(pat).unwrap_or_else(|| &ERROR_TY)
576560
}
577561
}
578562

579563
impl Index<ExprOrPatId> for InferenceResult {
580564
type Output = Ty;
581565

582566
fn index(&self, id: ExprOrPatId) -> &Ty {
583-
self.type_of_expr_or_pat(id).unwrap_or(&self.standard_types.unknown)
567+
self.type_of_expr_or_pat(id).unwrap_or_else(|| &ERROR_TY)
584568
}
585569
}
586570

587571
impl Index<BindingId> for InferenceResult {
588572
type Output = Ty;
589573

590574
fn index(&self, b: BindingId) -> &Ty {
591-
self.type_of_binding.get(b).unwrap_or(&self.standard_types.unknown)
575+
self.type_of_binding.get(b).unwrap_or_else(|| &ERROR_TY)
592576
}
593577
}
594578

@@ -767,7 +751,6 @@ impl<'db> InferenceContext<'db> {
767751
type_of_for_iterator,
768752
type_mismatches,
769753
has_errors,
770-
standard_types: _,
771754
pat_adjustments,
772755
binding_modes: _,
773756
expr_adjustments,
@@ -1022,7 +1005,7 @@ impl<'db> InferenceContext<'db> {
10221005
return_ty
10231006
}
10241007
}
1025-
None => self.result.standard_types.unit.clone(),
1008+
None => UNIT_TY.clone(),
10261009
};
10271010

10281011
self.return_ty = self.normalize_associated_types_in(return_ty);
@@ -1400,7 +1383,7 @@ impl<'db> InferenceContext<'db> {
14001383
}
14011384

14021385
fn err_ty(&self) -> Ty {
1403-
self.result.standard_types.unknown.clone()
1386+
TyKind::Error.intern(Interner)
14041387
}
14051388

14061389
fn make_body_lifetime(&mut self, lifetime_ref: LifetimeRefId) -> Lifetime {

crates/hir-ty/src/infer/closure.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::{
3535
db::{HirDatabase, InternedClosure, InternedCoroutine},
3636
error_lifetime, from_assoc_type_id, from_chalk_trait_id, from_placeholder_idx,
3737
generics::Generics,
38-
infer::{BreakableKind, CoerceMany, Diverges, coerce::CoerceNever},
38+
infer::{BreakableKind, CoerceMany, Diverges, ERROR_TY, UNIT_TY, coerce::CoerceNever},
3939
make_binders,
4040
mir::{BorrowKind, MirSpan, MutBorrowKind, ProjectionElem},
4141
to_assoc_type_id, to_chalk_trait_id,
@@ -82,7 +82,7 @@ impl InferenceContext<'_> {
8282
// When `sig_tys.len() == 1` the first type is the return type, not the
8383
// first parameter type.
8484
Some(ty) if sig_tys.len() > 1 => ty.assert_ty_ref(Interner).clone(),
85-
_ => self.result.standard_types.unit.clone(),
85+
_ => UNIT_TY.clone(),
8686
};
8787
let yield_ty = self.table.new_type_var();
8888

@@ -337,7 +337,7 @@ impl InferenceContext<'_> {
337337
.map(to_chalk_trait_id)
338338
.collect();
339339

340-
let self_ty = self.result.standard_types.unknown.clone();
340+
let self_ty = ERROR_TY.clone();
341341
let bounds = dyn_ty.bounds.clone().substitute(Interner, &[self_ty.cast(Interner)]);
342342
for bound in bounds.iter(Interner) {
343343
// NOTE(skip_binders): the extracted types are rebound by the returned `FnPointer`

crates/hir-ty/src/infer/coerce.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
db::HirDatabase,
2020
infer::{
2121
Adjust, Adjustment, AutoBorrow, InferOk, InferenceContext, OverloadedDeref, PointerCast,
22-
TypeError, TypeMismatch,
22+
TypeError, TypeMismatch, UNIT_TY,
2323
},
2424
utils::ClosureSubst,
2525
};
@@ -85,20 +85,16 @@ impl CoerceMany {
8585
self.final_ty.clone().unwrap_or_else(|| self.expected_ty.clone())
8686
}
8787

88-
pub(super) fn complete(self, ctx: &mut InferenceContext<'_>) -> Ty {
89-
if let Some(final_ty) = self.final_ty {
90-
final_ty
91-
} else {
92-
ctx.result.standard_types.never.clone()
93-
}
88+
pub(super) fn complete(self) -> Ty {
89+
if let Some(final_ty) = self.final_ty { final_ty } else { TyKind::Never.intern(Interner) }
9490
}
9591

9692
pub(super) fn coerce_forced_unit(
9793
&mut self,
9894
ctx: &mut InferenceContext<'_>,
9995
cause: CoercionCause,
10096
) {
101-
self.coerce(ctx, None, &ctx.result.standard_types.unit.clone(), cause)
97+
self.coerce(ctx, None, &UNIT_TY.clone(), cause)
10298
}
10399

104100
/// Merge two types from different branches, with possible coercion.

0 commit comments

Comments
 (0)