Skip to content

Commit 8ff43c8

Browse files
committed
refactor type_ops: 添加参数 db
1 parent 4eead02 commit 8ff43c8

File tree

24 files changed

+256
-156
lines changed

24 files changed

+256
-156
lines changed

crates/emmylua_code_analysis/src/compilation/analyzer/doc/field_or_operator_def_tags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ pub fn analyze_field(analyzer: &mut DocAnalyzer, tag: LuaDocTagField) -> Option<
5858
};
5959

6060
if nullable && !field_type.is_nullable() {
61-
field_type = TypeOps::Union.apply(&field_type, &LuaType::Nil);
61+
field_type = TypeOps::Union.apply(analyzer.db, &field_type, &LuaType::Nil);
6262
}
6363

6464
let description = if let Some(description) = tag.get_description() {

crates/emmylua_code_analysis/src/compilation/analyzer/doc/infer_type.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub fn infer_type(analyzer: &mut DocAnalyzer, node: LuaDocType) -> LuaType {
3535
}
3636

3737
if !t.is_nullable() {
38-
return TypeOps::Union.apply(&t, &LuaType::Nil);
38+
return TypeOps::Union.apply(analyzer.db, &t, &LuaType::Nil);
3939
}
4040

4141
return t;
@@ -408,7 +408,7 @@ fn infer_func_type(analyzer: &mut DocAnalyzer, func: &LuaDocFuncType) -> LuaType
408408
let type_ref = if let Some(type_ref) = param.get_type() {
409409
let mut typ = infer_type(analyzer, type_ref);
410410
if nullable && !typ.is_nullable() {
411-
typ = TypeOps::Union.apply(&typ, &LuaType::Nil);
411+
typ = TypeOps::Union.apply(analyzer.db, &typ, &LuaType::Nil);
412412
}
413413
Some(typ)
414414
} else {
@@ -505,7 +505,7 @@ fn infer_object_type(analyzer: &mut DocAnalyzer, object_type: &LuaDocObjectType)
505505
};
506506

507507
if field.is_nullable() {
508-
type_ref = TypeOps::Union.apply(&type_ref, &LuaType::Nil);
508+
type_ref = TypeOps::Union.apply(analyzer.db, &type_ref, &LuaType::Nil);
509509
}
510510

511511
fields.push((key, type_ref));

crates/emmylua_code_analysis/src/compilation/analyzer/doc/type_ref_tags.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub fn analyze_param(analyzer: &mut DocAnalyzer, tag: LuaDocTagParam) -> Option<
109109
};
110110

111111
if nullable && !type_ref.is_nullable() {
112-
type_ref = TypeOps::Union.apply(&type_ref, &LuaType::Nil);
112+
type_ref = TypeOps::Union.apply(analyzer.db, &type_ref, &LuaType::Nil);
113113
}
114114

115115
let description = if let Some(des) = tag.get_description() {

crates/emmylua_code_analysis/src/compilation/analyzer/lua/closure.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ pub fn analyze_return_point(
208208
match point {
209209
LuaReturnPoint::Expr(expr) => {
210210
let expr_type = infer_expr(db, cache, expr.clone())?;
211-
return_type = TypeOps::Union.apply(&return_type, &expr_type);
211+
return_type = TypeOps::Union.apply(db, &return_type, &expr_type);
212212
}
213213
LuaReturnPoint::MuliExpr(exprs) => {
214214
let mut multi_return = vec![];
@@ -217,10 +217,10 @@ pub fn analyze_return_point(
217217
multi_return.push(expr_type);
218218
}
219219
let typ = LuaType::Variadic(VariadicType::Multi(multi_return).into());
220-
return_type = TypeOps::Union.apply(&return_type, &typ);
220+
return_type = TypeOps::Union.apply(db, &return_type, &typ);
221221
}
222222
LuaReturnPoint::Nil => {
223-
return_type = TypeOps::Union.apply(&return_type, &LuaType::Nil);
223+
return_type = TypeOps::Union.apply(db, &return_type, &LuaType::Nil);
224224
}
225225
_ => {}
226226
}

crates/emmylua_code_analysis/src/compilation/analyzer/lua/for_range_stat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn analyze_for_range_stat(
3131
.get_type(idx)
3232
.cloned()
3333
.unwrap_or(LuaType::Unknown);
34-
let ret_type = TypeOps::Remove.apply(&ret_type, &LuaType::Nil);
34+
let ret_type = TypeOps::Remove.apply(analyzer.db, &ret_type, &LuaType::Nil);
3535
analyzer
3636
.db
3737
.get_type_index_mut()

crates/emmylua_code_analysis/src/compilation/analyzer/unresolve/resolve.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ pub fn try_resolve_iter_var(
286286
.get_type(iter_var.ret_idx)
287287
.cloned()
288288
.unwrap_or(LuaType::Unknown);
289-
iter_type = TypeOps::Remove.apply(&iter_type, &LuaType::Nil);
289+
iter_type = TypeOps::Remove.apply(db, &iter_type, &LuaType::Nil);
290290
let decl_id = iter_var.decl_id;
291291
bind_type(
292292
db,

crates/emmylua_code_analysis/src/compilation/analyzer/unresolve/resolve_closure.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ pub fn try_resolve_closure_parent_params(
333333
continue;
334334
}
335335
let new_type = TypeOps::Union.apply(
336+
db,
336337
final_param.1.as_ref().unwrap_or(&LuaType::Unknown),
337338
param.1.as_ref().unwrap_or(&LuaType::Unknown),
338339
);

crates/emmylua_code_analysis/src/db_index/member/lua_member_item.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ fn resolve_member_type(
7575
let mut typ = LuaType::Unknown;
7676
for member in members {
7777
typ = TypeOps::Union.apply(
78+
db,
7879
&typ,
7980
&db.get_type_index()
8081
.get_type_cache(&member.get_id().into())
@@ -90,6 +91,7 @@ fn resolve_member_type(
9091
let feature = member.get_feature();
9192
if feature.is_meta_decl() {
9293
typ = TypeOps::Union.apply(
94+
db,
9395
&typ,
9496
&db.get_type_index()
9597
.get_type_cache(&member.get_id().into())
@@ -106,6 +108,7 @@ fn resolve_member_type(
106108
let feature = member.get_feature();
107109
if feature.is_file_decl() {
108110
typ = TypeOps::Union.apply(
111+
db,
109112
&typ,
110113
&db.get_type_index()
111114
.get_type_cache(&member.get_id().into())

crates/emmylua_code_analysis/src/db_index/type/type_assert.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ impl TypeAssertion {
4848
source: LuaType,
4949
) -> Result<LuaType, InferFailReason> {
5050
match self {
51-
TypeAssertion::Exist => Ok(TypeOps::RemoveNilOrFalse.apply_source(&source)),
52-
TypeAssertion::NotExist => Ok(TypeOps::NarrowFalseOrNil.apply_source(&source)),
53-
TypeAssertion::Narrow(t) => Ok(TypeOps::Narrow.apply(&source, t)),
54-
TypeAssertion::Add(lua_type) => Ok(TypeOps::Union.apply(&source, lua_type)),
55-
TypeAssertion::Remove(lua_type) => Ok(TypeOps::Remove.apply(&source, lua_type)),
51+
TypeAssertion::Exist => Ok(TypeOps::RemoveNilOrFalse.apply_source(db, &source)),
52+
TypeAssertion::NotExist => Ok(TypeOps::NarrowFalseOrNil.apply_source(db, &source)),
53+
TypeAssertion::Narrow(t) => Ok(TypeOps::Narrow.apply(db, &source, t)),
54+
TypeAssertion::Add(lua_type) => Ok(TypeOps::Union.apply(db, &source, lua_type)),
55+
TypeAssertion::Remove(lua_type) => Ok(TypeOps::Remove.apply(db, &source, lua_type)),
5656
TypeAssertion::Force(t) => Ok(t.clone()),
5757
TypeAssertion::Reassign((syntax_id, idx)) => {
5858
let expr = LuaExpr::cast(
@@ -68,7 +68,7 @@ impl TypeAssertion {
6868
}
6969
t => t,
7070
};
71-
Ok(TypeOps::Narrow.apply(&source, &expr_type))
71+
Ok(TypeOps::Narrow.apply(db, &source, &expr_type))
7272
}
7373
TypeAssertion::And(a) => {
7474
let mut result = vec![];
@@ -82,7 +82,7 @@ impl TypeAssertion {
8282
_ => {
8383
let mut result_type = result.remove(0);
8484
for t in result {
85-
result_type = TypeOps::And.apply(&result_type, &t);
85+
result_type = TypeOps::And.apply(db, &result_type, &t);
8686
if result_type.is_nil() {
8787
return Ok(LuaType::Nil);
8888
}
@@ -104,7 +104,7 @@ impl TypeAssertion {
104104
_ => {
105105
let mut result_type = result.remove(0);
106106
for t in result {
107-
result_type = TypeOps::Union.apply(&result_type, &t);
107+
result_type = TypeOps::Union.apply(db, &result_type, &t);
108108
}
109109

110110
Ok(result_type)

crates/emmylua_code_analysis/src/db_index/type/type_ops/false_or_nil_type.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
use crate::{LuaType, LuaUnionType};
1+
use crate::{DbIndex, LuaType, LuaUnionType};
22

33
use super::TypeOps;
44

5-
pub fn narrow_false_or_nil(t: LuaType) -> LuaType {
5+
pub fn narrow_false_or_nil(db: &DbIndex, t: LuaType) -> LuaType {
66
if t.is_boolean() {
77
return LuaType::BooleanConst(false);
88
}
99

10-
return TypeOps::Narrow.apply(&t, &LuaType::Nil);
10+
return TypeOps::Narrow.apply(db, &t, &LuaType::Nil);
1111
}
1212

1313
pub fn remove_false_or_nil(t: LuaType) -> LuaType {

0 commit comments

Comments
 (0)