Skip to content

Commit 1389069

Browse files
committed
Simplify
1 parent 6a852d7 commit 1389069

File tree

12 files changed

+120
-135
lines changed

12 files changed

+120
-135
lines changed

crates/base-db/src/input.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ pub struct CrateData {
285285
/// For purposes of analysis, crates are anonymous (only names in
286286
/// `Dependency` matters), this name should only be used for UI.
287287
pub display_name: Option<CrateDisplayName>,
288+
// FIXME: Arc this
288289
pub cfg_options: CfgOptions,
289290
/// The cfg options that could be used by the crate
290291
pub potential_cfg_options: Option<CfgOptions>,

crates/hir-def/src/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl<'a> AssocItemCollector<'a> {
737737
&AstIdWithPath::new(file_id, ast_id, Clone::clone(path)),
738738
ctxt,
739739
expand_to,
740-
self.expander.module.krate(),
740+
self.expander.krate(),
741741
resolver,
742742
) {
743743
Ok(Some(call_id)) => {

crates/hir-def/src/expander.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::{
2121
pub struct Expander {
2222
cfg_options: CfgOptions,
2323
span_map: OnceCell<SpanMap>,
24-
krate: CrateId,
2524
current_file_id: HirFileId,
2625
pub(crate) module: ModuleId,
2726
/// `recursion_depth == usize::MAX` indicates that the recursion limit has been reached.
@@ -45,10 +44,13 @@ impl Expander {
4544
recursion_limit,
4645
cfg_options: db.crate_graph()[module.krate].cfg_options.clone(),
4746
span_map: OnceCell::new(),
48-
krate: module.krate,
4947
}
5048
}
5149

50+
pub fn krate(&self) -> CrateId {
51+
self.module.krate
52+
}
53+
5254
pub fn enter_expand<T: ast::AstNode>(
5355
&mut self,
5456
db: &dyn DefDatabase,
@@ -112,7 +114,7 @@ impl Expander {
112114
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
113115
Attrs::filter(
114116
db,
115-
self.krate,
117+
self.krate(),
116118
RawAttrs::new(
117119
db.upcast(),
118120
owner,

crates/hir-ty/src/consteval.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,32 @@ pub(crate) fn path_to_const(
7777
resolver: &Resolver,
7878
path: &Path,
7979
mode: ParamLoweringMode,
80-
args_lazy: impl FnOnce() -> Generics,
80+
args: impl FnOnce() -> Option<Generics>,
8181
debruijn: DebruijnIndex,
8282
expected_ty: Ty,
8383
) -> Option<Const> {
8484
match resolver.resolve_path_in_value_ns_fully(db.upcast(), path) {
8585
Some(ValueNs::GenericParam(p)) => {
8686
let ty = db.const_param_ty(p);
87-
let args = args_lazy();
8887
let value = match mode {
8988
ParamLoweringMode::Placeholder => {
9089
ConstValue::Placeholder(to_placeholder_idx(db, p.into()))
9190
}
92-
ParamLoweringMode::Variable => match args.param_idx(p.into()) {
93-
Some(it) => ConstValue::BoundVar(BoundVar::new(debruijn, it)),
94-
None => {
95-
never!(
96-
"Generic list doesn't contain this param: {:?}, {:?}, {:?}",
97-
args,
98-
path,
99-
p
100-
);
101-
return None;
91+
ParamLoweringMode::Variable => {
92+
let args = args();
93+
match args.as_ref().and_then(|args| args.param_idx(p.into())) {
94+
Some(it) => ConstValue::BoundVar(BoundVar::new(debruijn, it)),
95+
None => {
96+
never!(
97+
"Generic list doesn't contain this param: {:?}, {:?}, {:?}",
98+
args,
99+
path,
100+
p
101+
);
102+
return None;
103+
}
102104
}
103-
},
105+
}
104106
};
105107
Some(ConstData { ty, value }.intern(Interner))
106108
}
@@ -285,7 +287,6 @@ pub(crate) fn eval_to_const(
285287
expr: ExprId,
286288
mode: ParamLoweringMode,
287289
ctx: &mut InferenceContext<'_>,
288-
args: impl FnOnce() -> Generics,
289290
debruijn: DebruijnIndex,
290291
) -> Const {
291292
let db = ctx.db;
@@ -304,7 +305,9 @@ pub(crate) fn eval_to_const(
304305
}
305306
if let Expr::Path(p) = &ctx.body.exprs[expr] {
306307
let resolver = &ctx.resolver;
307-
if let Some(c) = path_to_const(db, resolver, p, mode, args, debruijn, infer[expr].clone()) {
308+
if let Some(c) =
309+
path_to_const(db, resolver, p, mode, || ctx.generics(), debruijn, infer[expr].clone())
310+
{
308311
return c;
309312
}
310313
}

crates/hir-ty/src/infer.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use crate::{
6060
lower::ImplTraitLoweringMode,
6161
to_assoc_type_id,
6262
traits::FnTrait,
63-
utils::{InTypeConstIdMetadata, UnevaluatedConstEvaluatorFolder},
63+
utils::{Generics, InTypeConstIdMetadata, UnevaluatedConstEvaluatorFolder},
6464
AliasEq, AliasTy, Binders, ClosureId, Const, DomainGoal, GenericArg, Goal, ImplTraitId,
6565
ImplTraitIdx, InEnvironment, Interner, Lifetime, OpaqueTyId, ProjectionTy, Substitution,
6666
TraitEnvironment, Ty, TyBuilder, TyExt,
@@ -630,6 +630,10 @@ impl<'a> InferenceContext<'a> {
630630
}
631631
}
632632

633+
pub(crate) fn generics(&self) -> Option<Generics> {
634+
Some(crate::utils::generics(self.db.upcast(), self.resolver.generic_def()?))
635+
}
636+
633637
// FIXME: This function should be private in module. It is currently only used in the consteval, since we need
634638
// `InferenceResult` in the middle of inference. See the fixme comment in `consteval::eval_to_const`. If you
635639
// used this function for another workaround, mention it here. If you really need this function and believe that

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ impl CastCheck {
1919
let expr_ty = table.resolve_ty_shallow(&self.expr_ty);
2020
let cast_ty = table.resolve_ty_shallow(&self.cast_ty);
2121

22-
if expr_ty.contains_unknown() || cast_ty.contains_unknown() {
23-
return;
24-
}
25-
2622
if table.coerce(&expr_ty, &cast_ty).is_ok() {
2723
return;
2824
}

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::{
2626
mir::{BorrowKind, MirSpan, MutBorrowKind, ProjectionElem},
2727
to_chalk_trait_id,
2828
traits::FnTrait,
29-
utils::{self, elaborate_clause_supertraits, generics, Generics},
29+
utils::{self, elaborate_clause_supertraits, Generics},
3030
Adjust, Adjustment, AliasEq, AliasTy, Binders, BindingMode, ChalkTraitId, ClosureId, DynTy,
3131
DynTyExt, FnAbi, FnPointer, FnSig, Interner, OpaqueTy, ProjectionTyExt, Substitution, Ty,
3232
TyExt, WhereClause,
@@ -331,14 +331,10 @@ impl CapturedItemWithoutTy {
331331
place: self.place,
332332
kind: self.kind,
333333
span: self.span,
334-
ty: replace_placeholder_with_binder(ctx.db, ctx.owner, ty),
334+
ty: replace_placeholder_with_binder(ctx, ty),
335335
};
336336

337-
fn replace_placeholder_with_binder(
338-
db: &dyn HirDatabase,
339-
owner: DefWithBodyId,
340-
ty: Ty,
341-
) -> Binders<Ty> {
337+
fn replace_placeholder_with_binder(ctx: &mut InferenceContext<'_>, ty: Ty) -> Binders<Ty> {
342338
struct Filler<'a> {
343339
db: &'a dyn HirDatabase,
344340
generics: Generics,
@@ -379,12 +375,12 @@ impl CapturedItemWithoutTy {
379375
Ok(BoundVar::new(outer_binder, idx).to_ty(Interner))
380376
}
381377
}
382-
let Some(generic_def) = owner.as_generic_def_id() else {
378+
let Some(generics) = ctx.generics() else {
383379
return Binders::empty(Interner, ty);
384380
};
385-
let filler = &mut Filler { db, generics: generics(db.upcast(), generic_def) };
381+
let filler = &mut Filler { db: ctx.db, generics };
386382
let result = ty.clone().try_fold_with(filler, DebruijnIndex::INNERMOST).unwrap_or(ty);
387-
make_binders(db, &filler.generics, result)
383+
make_binders(ctx.db, &filler.generics, result)
388384
}
389385
}
390386
}

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,18 +1040,12 @@ impl InferenceContext<'_> {
10401040

10411041
(
10421042
elem_ty,
1043-
if let Some(g_def) = self.owner.as_generic_def_id() {
1044-
let generics = generics(self.db.upcast(), g_def);
1045-
consteval::eval_to_const(
1046-
repeat,
1047-
ParamLoweringMode::Placeholder,
1048-
self,
1049-
|| generics,
1050-
DebruijnIndex::INNERMOST,
1051-
)
1052-
} else {
1053-
consteval::usize_const(self.db, None, krate)
1054-
},
1043+
consteval::eval_to_const(
1044+
repeat,
1045+
ParamLoweringMode::Placeholder,
1046+
self,
1047+
DebruijnIndex::INNERMOST,
1048+
),
10551049
)
10561050
}
10571051
};
@@ -1852,7 +1846,7 @@ impl InferenceContext<'_> {
18521846
ty,
18531847
c,
18541848
ParamLoweringMode::Placeholder,
1855-
|| generics(this.db.upcast(), this.resolver.generic_def().unwrap()),
1849+
|| this.generics(),
18561850
DebruijnIndex::INNERMOST,
18571851
)
18581852
},

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

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
33
use std::iter::repeat_with;
44

5-
use chalk_ir::Mutability;
65
use hir_def::{
76
body::Body,
87
hir::{Binding, BindingAnnotation, BindingId, Expr, ExprId, ExprOrPatId, Literal, Pat, PatId},
98
path::Path,
109
};
1110
use hir_expand::name::Name;
11+
use stdx::TupleExt;
1212

1313
use crate::{
1414
consteval::{try_const_usize, usize_const},
1515
error_lifetime,
1616
infer::{BindingMode, Expectation, InferenceContext, TypeMismatch},
1717
lower::lower_to_chalk_mutability,
1818
primitive::UintTy,
19-
static_lifetime, InferenceDiagnostic, Interner, Scalar, Substitution, Ty, TyBuilder, TyExt,
20-
TyKind,
19+
static_lifetime, InferenceDiagnostic, Interner, Mutability, Scalar, Substitution, Ty,
20+
TyBuilder, TyExt, TyKind,
2121
};
2222

2323
/// Used to generalize patterns and assignee expressions.
@@ -90,9 +90,6 @@ impl InferenceContext<'_> {
9090

9191
self.unify(&ty, expected);
9292

93-
let substs =
94-
ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(Interner));
95-
9693
match def {
9794
_ if subs.is_empty() => {}
9895
Some(def) => {
@@ -109,26 +106,28 @@ impl InferenceContext<'_> {
109106
let pre_iter = pre.iter().enumerate();
110107
let post_iter = (post_idx_offset..).zip(post.iter());
111108

109+
let substs = ty.as_adt().map(TupleExt::tail);
110+
112111
for (i, &subpat) in pre_iter.chain(post_iter) {
113-
let field_def = {
112+
let expected_ty = {
114113
match variant_data.field(&Name::new_tuple_field(i)) {
115114
Some(local_id) => {
116115
if !visibilities[local_id]
117116
.is_visible_from(self.db.upcast(), self.resolver.module())
118117
{
119118
// FIXME(DIAGNOSE): private tuple field
120119
}
121-
Some(local_id)
120+
let f = field_types[local_id].clone();
121+
let expected_ty = match substs {
122+
Some(substs) => f.substitute(Interner, substs),
123+
None => f.substitute(Interner, &Substitution::empty(Interner)),
124+
};
125+
self.normalize_associated_types_in(expected_ty)
122126
}
123-
None => None,
127+
None => self.err_ty(),
124128
}
125129
};
126130

127-
let expected_ty = field_def.map_or(self.err_ty(), |f| {
128-
field_types[f].clone().substitute(Interner, &substs)
129-
});
130-
let expected_ty = self.normalize_associated_types_in(expected_ty);
131-
132131
T::infer(self, subpat, &expected_ty, default_bm);
133132
}
134133
}
@@ -159,18 +158,17 @@ impl InferenceContext<'_> {
159158

160159
self.unify(&ty, expected);
161160

162-
let substs =
163-
ty.as_adt().map(|(_, s)| s.clone()).unwrap_or_else(|| Substitution::empty(Interner));
164-
165161
match def {
166162
_ if subs.len() == 0 => {}
167163
Some(def) => {
168164
let field_types = self.db.field_types(def);
169165
let variant_data = def.variant_data(self.db.upcast());
170166
let visibilities = self.db.field_visibilities(def);
171167

168+
let substs = ty.as_adt().map(TupleExt::tail);
169+
172170
for (name, inner) in subs {
173-
let field_def = {
171+
let expected_ty = {
174172
match variant_data.field(&name) {
175173
Some(local_id) => {
176174
if !visibilities[local_id]
@@ -181,23 +179,23 @@ impl InferenceContext<'_> {
181179
private: true,
182180
});
183181
}
184-
Some(local_id)
182+
let f = field_types[local_id].clone();
183+
let expected_ty = match substs {
184+
Some(substs) => f.substitute(Interner, substs),
185+
None => f.substitute(Interner, &Substitution::empty(Interner)),
186+
};
187+
self.normalize_associated_types_in(expected_ty)
185188
}
186189
None => {
187190
self.push_diagnostic(InferenceDiagnostic::NoSuchField {
188191
field: inner.into(),
189192
private: false,
190193
});
191-
None
194+
self.err_ty()
192195
}
193196
}
194197
};
195198

196-
let expected_ty = field_def.map_or(self.err_ty(), |f| {
197-
field_types[f].clone().substitute(Interner, &substs)
198-
});
199-
let expected_ty = self.normalize_associated_types_in(expected_ty);
200-
201199
T::infer(self, inner, &expected_ty, default_bm);
202200
}
203201
}

0 commit comments

Comments
 (0)