Skip to content

Commit 251b3a4

Browse files
committed
Simplify
1 parent e797479 commit 251b3a4

File tree

8 files changed

+98
-108
lines changed

8 files changed

+98
-108
lines changed

crates/hir-ty/src/autoderef.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
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-
86
use chalk_ir::cast::Cast;
97
use hir_def::{
108
lang_item::{LangItem, LangItemTarget},
@@ -13,10 +11,7 @@ use hir_def::{
1311
use hir_expand::name::name;
1412
use limit::Limit;
1513

16-
use crate::{
17-
db::HirDatabase, infer::unify::InferenceTable, Canonical, Goal, Interner, ProjectionTyExt,
18-
TraitEnvironment, Ty, TyBuilder, TyKind,
19-
};
14+
use crate::{infer::unify::InferenceTable, Goal, Interner, ProjectionTyExt, Ty, TyBuilder, TyKind};
2015

2116
static AUTODEREF_RECURSION_LIMIT: Limit = Limit::new(10);
2217

@@ -27,15 +22,15 @@ pub(crate) enum AutoderefKind {
2722
}
2823

2924
#[derive(Debug)]
30-
pub(crate) struct Autoderef<'a, 'db> {
31-
pub(crate) table: &'a mut InferenceTable<'db>,
25+
pub struct Autoderef<'a, 'db> {
26+
pub table: &'a mut InferenceTable<'db>,
3227
ty: Ty,
3328
at_start: bool,
3429
steps: Vec<(AutoderefKind, Ty)>,
3530
}
3631

3732
impl<'a, 'db> Autoderef<'a, 'db> {
38-
pub(crate) fn new(table: &'a mut InferenceTable<'db>, ty: Ty) -> Self {
33+
pub fn new(table: &'a mut InferenceTable<'db>, ty: Ty) -> Self {
3934
let ty = table.resolve_ty_shallow(&ty);
4035
Autoderef { table, ty, at_start: true, steps: Vec::new() }
4136
}
@@ -86,22 +81,6 @@ pub(crate) fn autoderef_step(
8681
}
8782
}
8883

89-
// FIXME: replace uses of this with Autoderef above
90-
pub fn autoderef(
91-
db: &dyn HirDatabase,
92-
env: Arc<TraitEnvironment>,
93-
ty: Canonical<Ty>,
94-
) -> impl Iterator<Item = Canonical<Ty>> + '_ {
95-
let mut table = InferenceTable::new(db, env);
96-
let ty = table.instantiate_canonical(ty);
97-
let mut autoderef = Autoderef::new(&mut table, ty);
98-
let mut v = Vec::new();
99-
while let Some((ty, _steps)) = autoderef.next() {
100-
v.push(autoderef.table.canonicalize(ty).value);
101-
}
102-
v.into_iter()
103-
}
104-
10584
pub(crate) fn builtin_deref<'ty>(
10685
table: &mut InferenceTable<'_>,
10786
ty: &'ty Ty,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ use crate::{
2828
infer::{
2929
coerce::CoerceMany, find_continuable, pat::contains_explicit_ref_binding, BreakableKind,
3030
},
31+
lang_items::lang_items_for_bin_op,
3132
lower::{
3233
const_or_path_to_chalk, generic_arg_to_chalk, lower_to_chalk_mutability, ParamLoweringMode,
3334
},
3435
mapping::{from_chalk, ToChalk},
35-
method_resolution::{self, lang_items_for_bin_op, VisibleFromModule},
36+
method_resolution::{self, VisibleFromModule},
3637
primitive::{self, UintTy},
3738
static_lifetime, to_chalk_trait_id,
3839
traits::FnTrait,
@@ -792,7 +793,7 @@ impl<'a> InferenceContext<'a> {
792793
let canonicalized = self.canonicalize(base_ty.clone());
793794
let receiver_adjustments = method_resolution::resolve_indexing_op(
794795
self.db,
795-
self.table.trait_env.clone(),
796+
&mut self.table,
796797
canonicalized.value,
797798
index_trait,
798799
);

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(crate) struct Canonicalized<T>
35+
pub struct Canonicalized<T>
3636
where
3737
T: HasInterner<Interner = Interner>,
3838
{
39-
pub(crate) value: Canonical<T>,
39+
pub 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(crate) struct InferenceTable<'a> {
143+
pub 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(crate) fn new(db: &'a dyn HirDatabase, trait_env: Arc<TraitEnvironment>) -> Self {
158+
pub 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(crate) fn canonicalize<T: TypeFoldable<Interner> + HasInterner<Interner = Interner>>(
207+
pub 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(crate) fn instantiate_canonical<T>(&mut self, canonical: Canonical<T>) -> T
323+
pub 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/lang_items.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Functions to detect special lang items
22
33
use hir_def::{lang_item::LangItem, AdtId, HasModule};
4+
use hir_expand::name::Name;
45

56
use crate::db::HirDatabase;
67

@@ -17,3 +18,52 @@ pub fn is_unsafe_cell(adt: AdtId, db: &dyn HirDatabase) -> bool {
1718
db.lang_item(krate, LangItem::UnsafeCell).and_then(|it| it.as_struct()).map(AdtId::from);
1819
Some(adt) == box_adt
1920
}
21+
22+
pub fn lang_items_for_bin_op(op: syntax::ast::BinaryOp) -> Option<(Name, LangItem)> {
23+
use hir_expand::name;
24+
use syntax::ast::{ArithOp, BinaryOp, CmpOp, Ordering};
25+
Some(match op {
26+
BinaryOp::LogicOp(_) => return None,
27+
BinaryOp::ArithOp(aop) => match aop {
28+
ArithOp::Add => (name![add], LangItem::Add),
29+
ArithOp::Mul => (name![mul], LangItem::Mul),
30+
ArithOp::Sub => (name![sub], LangItem::Sub),
31+
ArithOp::Div => (name![div], LangItem::Div),
32+
ArithOp::Rem => (name![rem], LangItem::Rem),
33+
ArithOp::Shl => (name![shl], LangItem::Shl),
34+
ArithOp::Shr => (name![shr], LangItem::Shr),
35+
ArithOp::BitXor => (name![bitxor], LangItem::BitXor),
36+
ArithOp::BitOr => (name![bitor], LangItem::BitOr),
37+
ArithOp::BitAnd => (name![bitand], LangItem::BitAnd),
38+
},
39+
BinaryOp::Assignment { op: Some(aop) } => match aop {
40+
ArithOp::Add => (name![add_assign], LangItem::AddAssign),
41+
ArithOp::Mul => (name![mul_assign], LangItem::MulAssign),
42+
ArithOp::Sub => (name![sub_assign], LangItem::SubAssign),
43+
ArithOp::Div => (name![div_assign], LangItem::DivAssign),
44+
ArithOp::Rem => (name![rem_assign], LangItem::RemAssign),
45+
ArithOp::Shl => (name![shl_assign], LangItem::ShlAssign),
46+
ArithOp::Shr => (name![shr_assign], LangItem::ShrAssign),
47+
ArithOp::BitXor => (name![bitxor_assign], LangItem::BitXorAssign),
48+
ArithOp::BitOr => (name![bitor_assign], LangItem::BitOrAssign),
49+
ArithOp::BitAnd => (name![bitand_assign], LangItem::BitAndAssign),
50+
},
51+
BinaryOp::CmpOp(cop) => match cop {
52+
CmpOp::Eq { negated: false } => (name![eq], LangItem::PartialEq),
53+
CmpOp::Eq { negated: true } => (name![ne], LangItem::PartialEq),
54+
CmpOp::Ord { ordering: Ordering::Less, strict: false } => {
55+
(name![le], LangItem::PartialOrd)
56+
}
57+
CmpOp::Ord { ordering: Ordering::Less, strict: true } => {
58+
(name![lt], LangItem::PartialOrd)
59+
}
60+
CmpOp::Ord { ordering: Ordering::Greater, strict: false } => {
61+
(name![ge], LangItem::PartialOrd)
62+
}
63+
CmpOp::Ord { ordering: Ordering::Greater, strict: true } => {
64+
(name![gt], LangItem::PartialOrd)
65+
}
66+
},
67+
BinaryOp::Assignment { op: None } => return None,
68+
})
69+
}

crates/hir-ty/src/lib.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,28 @@ macro_rules! eprintln {
77
($($tt:tt)*) => { stdx::eprintln!($($tt)*) };
88
}
99

10-
mod autoderef;
1110
mod builder;
1211
mod chalk_db;
1312
mod chalk_ext;
14-
pub mod consteval;
15-
pub mod mir;
1613
mod infer;
1714
mod inhabitedness;
1815
mod interner;
1916
mod lower;
2017
mod mapping;
2118
mod tls;
2219
mod utils;
20+
21+
pub mod autoderef;
22+
pub mod consteval;
2323
pub mod db;
2424
pub mod diagnostics;
2525
pub mod display;
26+
pub mod lang_items;
27+
pub mod layout;
2628
pub mod method_resolution;
29+
pub mod mir;
2730
pub mod primitive;
2831
pub mod traits;
29-
pub mod layout;
30-
pub mod lang_items;
3132

3233
#[cfg(test)]
3334
mod tests;
@@ -51,16 +52,14 @@ use rustc_hash::FxHashSet;
5152
use traits::FnTrait;
5253
use utils::Generics;
5354

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

58-
pub use autoderef::autoderef;
57+
pub use autoderef::Autoderef;
5958
pub use builder::{ParamKind, TyBuilder};
6059
pub use chalk_ext::*;
6160
pub use infer::{
62-
could_coerce, could_unify, Adjust, Adjustment, AutoBorrow, BindingMode, InferenceDiagnostic,
63-
InferenceResult, OverloadedDeref, PointerCast,
61+
could_coerce, could_unify, unify::InferenceTable, Adjust, Adjustment, AutoBorrow, BindingMode,
62+
InferenceDiagnostic, InferenceResult, OverloadedDeref, PointerCast,
6463
};
6564
pub use interner::Interner;
6665
pub use lower::{

crates/hir-ty/src/method_resolution.rs

Lines changed: 9 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ use std::{ops::ControlFlow, sync::Arc};
77
use base_db::{CrateId, Edition};
88
use chalk_ir::{cast::Cast, Mutability, TyKind, UniverseIndex, WhereClause};
99
use hir_def::{
10-
data::ImplData, item_scope::ItemScope, lang_item::LangItem, nameres::DefMap, AssocItemId,
11-
BlockId, ConstId, FunctionId, HasModule, ImplId, ItemContainerId, Lookup, ModuleDefId,
12-
ModuleId, TraitId,
10+
data::ImplData, item_scope::ItemScope, nameres::DefMap, AssocItemId, BlockId, ConstId,
11+
FunctionId, HasModule, ImplId, ItemContainerId, Lookup, ModuleDefId, ModuleId, TraitId,
1312
};
1413
use hir_expand::name::Name;
1514
use rustc_hash::{FxHashMap, FxHashSet};
@@ -451,55 +450,6 @@ pub fn def_crates(
451450
}
452451
}
453452

454-
pub fn lang_items_for_bin_op(op: syntax::ast::BinaryOp) -> Option<(Name, LangItem)> {
455-
use hir_expand::name;
456-
use syntax::ast::{ArithOp, BinaryOp, CmpOp, Ordering};
457-
Some(match op {
458-
BinaryOp::LogicOp(_) => return None,
459-
BinaryOp::ArithOp(aop) => match aop {
460-
ArithOp::Add => (name![add], LangItem::Add),
461-
ArithOp::Mul => (name![mul], LangItem::Mul),
462-
ArithOp::Sub => (name![sub], LangItem::Sub),
463-
ArithOp::Div => (name![div], LangItem::Div),
464-
ArithOp::Rem => (name![rem], LangItem::Rem),
465-
ArithOp::Shl => (name![shl], LangItem::Shl),
466-
ArithOp::Shr => (name![shr], LangItem::Shr),
467-
ArithOp::BitXor => (name![bitxor], LangItem::BitXor),
468-
ArithOp::BitOr => (name![bitor], LangItem::BitOr),
469-
ArithOp::BitAnd => (name![bitand], LangItem::BitAnd),
470-
},
471-
BinaryOp::Assignment { op: Some(aop) } => match aop {
472-
ArithOp::Add => (name![add_assign], LangItem::AddAssign),
473-
ArithOp::Mul => (name![mul_assign], LangItem::MulAssign),
474-
ArithOp::Sub => (name![sub_assign], LangItem::SubAssign),
475-
ArithOp::Div => (name![div_assign], LangItem::DivAssign),
476-
ArithOp::Rem => (name![rem_assign], LangItem::RemAssign),
477-
ArithOp::Shl => (name![shl_assign], LangItem::ShlAssign),
478-
ArithOp::Shr => (name![shr_assign], LangItem::ShrAssign),
479-
ArithOp::BitXor => (name![bitxor_assign], LangItem::BitXorAssign),
480-
ArithOp::BitOr => (name![bitor_assign], LangItem::BitOrAssign),
481-
ArithOp::BitAnd => (name![bitand_assign], LangItem::BitAndAssign),
482-
},
483-
BinaryOp::CmpOp(cop) => match cop {
484-
CmpOp::Eq { negated: false } => (name![eq], LangItem::PartialEq),
485-
CmpOp::Eq { negated: true } => (name![ne], LangItem::PartialEq),
486-
CmpOp::Ord { ordering: Ordering::Less, strict: false } => {
487-
(name![le], LangItem::PartialOrd)
488-
}
489-
CmpOp::Ord { ordering: Ordering::Less, strict: true } => {
490-
(name![lt], LangItem::PartialOrd)
491-
}
492-
CmpOp::Ord { ordering: Ordering::Greater, strict: false } => {
493-
(name![ge], LangItem::PartialOrd)
494-
}
495-
CmpOp::Ord { ordering: Ordering::Greater, strict: true } => {
496-
(name![gt], LangItem::PartialOrd)
497-
}
498-
},
499-
BinaryOp::Assignment { op: None } => return None,
500-
})
501-
}
502-
503453
/// Look up the method with the given name.
504454
pub(crate) fn lookup_method(
505455
db: &dyn HirDatabase,
@@ -1310,16 +1260,18 @@ fn iterate_inherent_methods(
13101260
/// Returns the receiver type for the index trait call.
13111261
pub fn resolve_indexing_op(
13121262
db: &dyn HirDatabase,
1313-
env: Arc<TraitEnvironment>,
1263+
table: &mut InferenceTable<'_>,
13141264
ty: Canonical<Ty>,
13151265
index_trait: TraitId,
13161266
) -> Option<ReceiverAdjustments> {
1317-
let mut table = InferenceTable::new(db, env.clone());
13181267
let ty = table.instantiate_canonical(ty);
1319-
let deref_chain = autoderef_method_receiver(&mut table, ty);
1268+
let deref_chain = autoderef_method_receiver(table, ty);
13201269
for (ty, adj) in deref_chain {
1321-
let goal = generic_implements_goal(db, env.clone(), index_trait, &ty);
1322-
if db.trait_solve(env.krate, env.block, goal.cast(Interner)).is_some() {
1270+
let goal = generic_implements_goal(db, table.trait_env.clone(), index_trait, &ty);
1271+
if db
1272+
.trait_solve(table.trait_env.krate, table.trait_env.block, goal.cast(Interner))
1273+
.is_some()
1274+
{
13231275
return Some(adj);
13241276
}
13251277
}

crates/hir/src/lib.rs

Lines changed: 14 additions & 6 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, autoderef,
60+
all_super_traits,
6161
consteval::{try_const_usize, unknown_const_as_generic, ConstEvalError, ConstExt},
6262
diagnostics::BodyValidationDiagnostic,
6363
display::HexifiedConst,
@@ -66,9 +66,10 @@ use hir_ty::{
6666
mir::{self, interpret_mir},
6767
primitive::UintTy,
6868
traits::FnTrait,
69-
AliasTy, CallableDefId, CallableSig, Canonical, CanonicalVarKinds, Cast, ClosureId,
70-
GenericArgData, Interner, ParamKind, QuantifiedWhereClause, Scalar, Substitution,
71-
TraitEnvironment, TraitRefExt, Ty, TyBuilder, TyDefId, TyExt, TyKind, WhereClause,
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,
7273
};
7374
use itertools::Itertools;
7475
use nameres::diagnostics::DefDiagnosticKind;
@@ -3517,8 +3518,15 @@ impl Type {
35173518
fn autoderef_<'a>(&'a self, db: &'a dyn HirDatabase) -> impl Iterator<Item = Ty> + 'a {
35183519
// There should be no inference vars in types passed here
35193520
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
3520-
let environment = self.env.clone();
3521-
autoderef(db, environment, canonical).map(|canonical| canonical.value)
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)
35223530
}
35233531

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

crates/hir/src/source_analyzer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ use hir_ty::{
3939
record_literal_missing_fields, record_pattern_missing_fields, unsafe_expressions,
4040
UnsafeExpr,
4141
},
42-
method_resolution::{self, lang_items_for_bin_op},
42+
lang_items::lang_items_for_bin_op,
43+
method_resolution::{self},
4344
Adjustment, InferenceResult, Interner, Substitution, Ty, TyExt, TyKind, TyLoweringContext,
4445
};
4546
use itertools::Itertools;

0 commit comments

Comments
 (0)