Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5390949

Browse files
committed
Auto merge of rust-lang#14448 - Veykril:infer-table, r=Veykril
internal: Don't expose InferenceTable outside of hir-ty
2 parents 02ea92f + fc840db commit 5390949

File tree

6 files changed

+49
-37
lines changed

6 files changed

+49
-37
lines changed

crates/hir-ty/src/autoderef.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
//! reference to a type with the field `bar`. This is an approximation of the
44
//! logic in rustc (which lives in rustc_hir_analysis/check/autoderef.rs).
55
6+
use std::sync::Arc;
7+
68
use chalk_ir::cast::Cast;
79
use hir_def::lang_item::LangItem;
810
use hir_expand::name::name;
911
use limit::Limit;
1012

11-
use crate::{infer::unify::InferenceTable, Goal, Interner, ProjectionTyExt, Ty, TyBuilder, TyKind};
13+
use crate::{
14+
db::HirDatabase, infer::unify::InferenceTable, Canonical, Goal, Interner, ProjectionTyExt,
15+
TraitEnvironment, Ty, TyBuilder, TyKind,
16+
};
1217

1318
static AUTODEREF_RECURSION_LIMIT: Limit = Limit::new(10);
1419

@@ -18,16 +23,31 @@ pub(crate) enum AutoderefKind {
1823
Overloaded,
1924
}
2025

26+
pub fn autoderef(
27+
db: &dyn HirDatabase,
28+
env: Arc<TraitEnvironment>,
29+
ty: Canonical<Ty>,
30+
) -> impl Iterator<Item = Canonical<Ty>> + '_ {
31+
let mut table = InferenceTable::new(db, env);
32+
let ty = table.instantiate_canonical(ty);
33+
let mut autoderef = Autoderef::new(&mut table, ty);
34+
let mut v = Vec::new();
35+
while let Some((ty, _steps)) = autoderef.next() {
36+
v.push(autoderef.table.canonicalize(ty).value);
37+
}
38+
v.into_iter()
39+
}
40+
2141
#[derive(Debug)]
22-
pub struct Autoderef<'a, 'db> {
23-
pub table: &'a mut InferenceTable<'db>,
42+
pub(crate) struct Autoderef<'a, 'db> {
43+
pub(crate) table: &'a mut InferenceTable<'db>,
2444
ty: Ty,
2545
at_start: bool,
2646
steps: Vec<(AutoderefKind, Ty)>,
2747
}
2848

2949
impl<'a, 'db> Autoderef<'a, 'db> {
30-
pub fn new(table: &'a mut InferenceTable<'db>, ty: Ty) -> Self {
50+
pub(crate) fn new(table: &'a mut InferenceTable<'db>, ty: Ty) -> Self {
3151
let ty = table.resolve_ty_shallow(&ty);
3252
Autoderef { table, ty, at_start: true, steps: Vec::new() }
3353
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use stdx::always;
2323
use syntax::ast::RangeOp;
2424

2525
use crate::{
26-
autoderef::{self, Autoderef},
26+
autoderef::{builtin_deref, deref_by_trait, Autoderef},
2727
consteval,
2828
infer::{
2929
coerce::CoerceMany, find_continuable, pat::contains_explicit_ref_binding, BreakableKind,
@@ -675,12 +675,10 @@ impl<'a> InferenceContext<'a> {
675675
);
676676
}
677677
}
678-
if let Some(derefed) =
679-
autoderef::builtin_deref(&mut self.table, &inner_ty, true)
680-
{
678+
if let Some(derefed) = builtin_deref(&mut self.table, &inner_ty, true) {
681679
self.resolve_ty_shallow(derefed)
682680
} else {
683-
autoderef::deref_by_trait(&mut self.table, inner_ty)
681+
deref_by_trait(&mut self.table, inner_ty)
684682
.unwrap_or_else(|| self.err_ty())
685683
}
686684
}
@@ -793,7 +791,7 @@ impl<'a> InferenceContext<'a> {
793791
let canonicalized = self.canonicalize(base_ty.clone());
794792
let receiver_adjustments = method_resolution::resolve_indexing_op(
795793
self.db,
796-
&mut self.table,
794+
self.table.trait_env.clone(),
797795
canonicalized.value,
798796
index_trait,
799797
);

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ impl<'a> InferenceContext<'a> {
3232
}
3333

3434
#[derive(Debug, Clone)]
35-
pub struct Canonicalized<T>
35+
pub(crate) struct Canonicalized<T>
3636
where
3737
T: HasInterner<Interner = Interner>,
3838
{
39-
pub value: Canonical<T>,
39+
pub(crate) value: Canonical<T>,
4040
free_vars: Vec<GenericArg>,
4141
}
4242

@@ -140,7 +140,7 @@ bitflags::bitflags! {
140140
type ChalkInferenceTable = chalk_solve::infer::InferenceTable<Interner>;
141141

142142
#[derive(Clone)]
143-
pub struct InferenceTable<'a> {
143+
pub(crate) struct InferenceTable<'a> {
144144
pub(crate) db: &'a dyn HirDatabase,
145145
pub(crate) trait_env: Arc<TraitEnvironment>,
146146
var_unification_table: ChalkInferenceTable,
@@ -155,7 +155,7 @@ pub(crate) struct InferenceTableSnapshot {
155155
}
156156

157157
impl<'a> InferenceTable<'a> {
158-
pub fn new(db: &'a dyn HirDatabase, trait_env: Arc<TraitEnvironment>) -> Self {
158+
pub(crate) fn new(db: &'a dyn HirDatabase, trait_env: Arc<TraitEnvironment>) -> Self {
159159
InferenceTable {
160160
db,
161161
trait_env,
@@ -204,7 +204,7 @@ impl<'a> InferenceTable<'a> {
204204
.intern(Interner)
205205
}
206206

207-
pub fn canonicalize<T: TypeFoldable<Interner> + HasInterner<Interner = Interner>>(
207+
pub(crate) fn canonicalize<T: TypeFoldable<Interner> + HasInterner<Interner = Interner>>(
208208
&mut self,
209209
t: T,
210210
) -> Canonicalized<T>
@@ -320,7 +320,7 @@ impl<'a> InferenceTable<'a> {
320320
)
321321
}
322322

323-
pub fn instantiate_canonical<T>(&mut self, canonical: Canonical<T>) -> T
323+
pub(crate) fn instantiate_canonical<T>(&mut self, canonical: Canonical<T>) -> T
324324
where
325325
T: HasInterner<Interner = Interner> + TypeFoldable<Interner> + std::fmt::Debug,
326326
{

crates/hir-ty/src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ use rustc_hash::FxHashSet;
5252
use traits::FnTrait;
5353
use utils::Generics;
5454

55-
use crate::{consteval::unknown_const, db::HirDatabase, utils::generics};
55+
use crate::{
56+
consteval::unknown_const, db::HirDatabase, infer::unify::InferenceTable, utils::generics,
57+
};
5658

57-
pub use autoderef::Autoderef;
59+
pub use autoderef::autoderef;
5860
pub use builder::{ParamKind, TyBuilder};
5961
pub use chalk_ext::*;
6062
pub use infer::{
61-
could_coerce, could_unify, unify::InferenceTable, Adjust, Adjustment, AutoBorrow, BindingMode,
62-
InferenceDiagnostic, InferenceResult, OverloadedDeref, PointerCast,
63+
could_coerce, could_unify, Adjust, Adjustment, AutoBorrow, BindingMode, InferenceDiagnostic,
64+
InferenceResult, OverloadedDeref, PointerCast,
6365
};
6466
pub use interner::Interner;
6567
pub use lower::{

crates/hir-ty/src/method_resolution.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,14 +1263,15 @@ fn iterate_inherent_methods(
12631263
}
12641264

12651265
/// Returns the receiver type for the index trait call.
1266-
pub fn resolve_indexing_op(
1266+
pub(crate) fn resolve_indexing_op(
12671267
db: &dyn HirDatabase,
1268-
table: &mut InferenceTable<'_>,
1268+
env: Arc<TraitEnvironment>,
12691269
ty: Canonical<Ty>,
12701270
index_trait: TraitId,
12711271
) -> Option<ReceiverAdjustments> {
1272+
let mut table = InferenceTable::new(db, env.clone());
12721273
let ty = table.instantiate_canonical(ty);
1273-
let deref_chain = autoderef_method_receiver(table, ty);
1274+
let deref_chain = autoderef_method_receiver(&mut table, ty);
12741275
for (ty, adj) in deref_chain {
12751276
let goal = generic_implements_goal(db, table.trait_env.clone(), index_trait, &ty);
12761277
if db

crates/hir/src/lib.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use hir_def::{
5757
};
5858
use hir_expand::{name::name, MacroCallKind};
5959
use hir_ty::{
60-
all_super_traits,
60+
all_super_traits, autoderef,
6161
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
6262
diagnostics::BodyValidationDiagnostic,
6363
display::HexifiedConst,
@@ -66,10 +66,9 @@ use hir_ty::{
6666
mir::{self, interpret_mir},
6767
primitive::UintTy,
6868
traits::FnTrait,
69-
AliasTy, Autoderef, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId,
70-
GenericArgData, InferenceTable, Interner, ParamKind, QuantifiedWhereClause, Scalar,
71-
Substitution, TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind,
72-
WhereClause,
69+
AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId,
70+
GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution,
71+
TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, WhereClause,
7372
};
7473
use itertools::Itertools;
7574
use nameres::diagnostics::DefDiagnosticKind;
@@ -3518,15 +3517,7 @@ impl Type {
35183517
fn autoderef_<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Ty> + 'a {
35193518
// There should be no inference vars in types passed here
35203519
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
3521-
3522-
let mut table = InferenceTable::new(db, self.env.clone());
3523-
let ty = table.instantiate_canonical(canonical);
3524-
let mut autoderef = Autoderef::new(&mut table, ty);
3525-
let mut v = Vec::new();
3526-
while let Some((ty, _steps)) = autoderef.next() {
3527-
v.push(autoderef.table.canonicalize(ty).value);
3528-
}
3529-
v.into_iter().map(|canonical| canonical.value)
3520+
autoderef(db, self.env.clone(), canonical).map(|canonical| canonical.value)
35303521
}
35313522

35323523
// This would be nicer if it just returned an iterator, but that runs into

0 commit comments

Comments
 (0)