Skip to content

Commit 918a911

Browse files
committed
refactor type check
1 parent c45102d commit 918a911

File tree

10 files changed

+261
-177
lines changed

10 files changed

+261
-177
lines changed

crates/emmylua_code_analysis/src/semantic/type_check/complex_type/array_type_check.rs

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
use crate::{
2-
DbIndex, LuaMemberKey, LuaMemberOwner, LuaType, TypeCheckFailReason, TypeCheckResult, TypeOps,
2+
LuaMemberKey, LuaMemberOwner, LuaType, TypeCheckFailReason, TypeCheckResult, TypeOps,
33
find_index_operations,
4-
semantic::type_check::{check_general_type_compact, type_check_guard::TypeCheckGuard},
4+
semantic::type_check::{
5+
check_general_type_compact, type_check_context::TypeCheckContext,
6+
type_check_guard::TypeCheckGuard,
7+
},
58
};
69

710
pub fn check_array_type_compact(
8-
db: &DbIndex,
11+
context: &TypeCheckContext,
912
source_base: &LuaType,
1013
compact_type: &LuaType,
1114
check_guard: TypeCheckGuard,
1215
) -> TypeCheckResult {
13-
let source_base = TypeOps::Union.apply(db, source_base, &LuaType::Nil);
16+
let source_base = TypeOps::Union.apply(context.db, source_base, &LuaType::Nil);
1417

1518
match compact_type {
1619
LuaType::Array(compact_array_type) => {
1720
return check_general_type_compact(
18-
db,
21+
context,
1922
&source_base,
2023
compact_array_type.get_base(),
2124
check_guard.next_level()?,
@@ -24,7 +27,7 @@ pub fn check_array_type_compact(
2427
LuaType::Tuple(tuple_type) => {
2528
for element_type in tuple_type.get_types() {
2629
check_general_type_compact(
27-
db,
30+
context,
2831
&source_base,
2932
element_type,
3033
check_guard.next_level()?,
@@ -36,18 +39,18 @@ pub fn check_array_type_compact(
3639
LuaType::TableConst(inst) => {
3740
let table_member_owner = LuaMemberOwner::Element(inst.clone());
3841
return check_array_type_compact_table(
39-
db,
42+
context,
4043
&source_base,
4144
table_member_owner,
4245
check_guard.next_level()?,
4346
);
4447
}
4548
LuaType::Object(compact_object) => {
4649
let compact_base = compact_object
47-
.cast_down_array_base(db)
50+
.cast_down_array_base(context.db)
4851
.ok_or(TypeCheckFailReason::TypeNotMatch)?;
4952
return check_general_type_compact(
50-
db,
53+
context,
5154
&source_base,
5255
&compact_base,
5356
check_guard.next_level()?,
@@ -57,7 +60,12 @@ pub fn check_array_type_compact(
5760
LuaType::TableGeneric(compact_types) => {
5861
if compact_types.len() == 2 {
5962
for typ in compact_types.iter() {
60-
check_general_type_compact(db, &source_base, typ, check_guard.next_level()?)?;
63+
check_general_type_compact(
64+
context,
65+
&source_base,
66+
typ,
67+
check_guard.next_level()?,
68+
)?;
6169
}
6270

6371
return Ok(());
@@ -66,7 +74,7 @@ pub fn check_array_type_compact(
6674
LuaType::Any => return Ok(()),
6775
LuaType::Ref(_) | LuaType::Def(_) => {
6876
return check_array_type_compact_ref_def(
69-
db,
77+
context,
7078
&source_base,
7179
compact_type,
7280
check_guard.next_level()?,
@@ -79,20 +87,21 @@ pub fn check_array_type_compact(
7987
}
8088

8189
fn check_array_type_compact_ref_def(
82-
db: &DbIndex,
90+
context: &TypeCheckContext,
8391
source_base: &LuaType,
8492
compact_type: &LuaType,
8593
check_guard: TypeCheckGuard,
8694
) -> TypeCheckResult {
87-
let Some(members) = find_index_operations(db, compact_type) else {
95+
let Some(members) = find_index_operations(context.db, compact_type) else {
8896
return Err(TypeCheckFailReason::TypeNotMatch);
8997
};
9098

9199
for member in &members {
92100
match &member.key {
93101
LuaMemberKey::ExprType(key_type) => {
94102
if key_type.is_integer() {
95-
match check_general_type_compact(db, source_base, &member.typ, check_guard) {
103+
match check_general_type_compact(context, source_base, &member.typ, check_guard)
104+
{
96105
Ok(()) => return Ok(()),
97106
_ => {}
98107
}
@@ -106,21 +115,26 @@ fn check_array_type_compact_ref_def(
106115
}
107116

108117
fn check_array_type_compact_table(
109-
db: &DbIndex,
118+
context: &TypeCheckContext,
110119
source_base: &LuaType,
111120
table_owner: LuaMemberOwner,
112121
check_guard: TypeCheckGuard,
113122
) -> TypeCheckResult {
114-
let member_index = db.get_member_index();
123+
let member_index = context.db.get_member_index();
115124

116125
let member_len = member_index.get_member_len(&table_owner);
117126
for i in 0..member_len {
118127
let key = LuaMemberKey::Integer((i + 1) as i64);
119128
if let Some(member_item) = member_index.get_member_item(&table_owner, &key) {
120129
let member_type = member_item
121-
.resolve_type(db)
130+
.resolve_type(context.db)
122131
.map_err(|_| TypeCheckFailReason::TypeNotMatch)?;
123-
check_general_type_compact(db, source_base, &member_type, check_guard.next_level()?)?;
132+
check_general_type_compact(
133+
context,
134+
source_base,
135+
&member_type,
136+
check_guard.next_level()?,
137+
)?;
124138
} else {
125139
return Err(TypeCheckFailReason::TypeNotMatch);
126140
}

crates/emmylua_code_analysis/src/semantic/type_check/complex_type/intersection_type_check.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
use crate::{
2-
DbIndex, LuaIntersectionType, LuaMemberOwner, LuaType, TypeCheckFailReason, TypeCheckResult,
3-
semantic::type_check::{check_general_type_compact, type_check_guard::TypeCheckGuard},
2+
LuaIntersectionType, LuaMemberOwner, LuaType, TypeCheckFailReason, TypeCheckResult,
3+
semantic::type_check::{
4+
check_general_type_compact, type_check_context::TypeCheckContext,
5+
type_check_guard::TypeCheckGuard,
6+
},
47
};
58

69
pub fn check_intersection_type_compact(
7-
db: &DbIndex,
10+
context: &TypeCheckContext,
811
source_intersection: &LuaIntersectionType,
912
compact_type: &LuaType,
1013
check_guard: TypeCheckGuard,
1114
) -> TypeCheckResult {
1215
match compact_type {
1316
LuaType::TableConst(range) => check_intersection_type_compact_table(
14-
db,
17+
context,
1518
source_intersection,
1619
LuaMemberOwner::Element(range.clone()),
1720
check_guard.next_level()?,
@@ -20,7 +23,7 @@ pub fn check_intersection_type_compact(
2023
// 检查对象是否满足交叉类型的所有组成部分
2124
for intersection_component in source_intersection.get_types() {
2225
check_general_type_compact(
23-
db,
26+
context,
2427
intersection_component,
2528
compact_type,
2629
check_guard.next_level()?,
@@ -31,7 +34,7 @@ pub fn check_intersection_type_compact(
3134
LuaType::Intersection(compact_intersection) => {
3235
// 交叉类型对交叉类型:检查所有组成部分
3336
check_intersection_type_compact_intersection(
34-
db,
37+
context,
3538
source_intersection,
3639
compact_intersection,
3740
check_guard.next_level()?,
@@ -42,7 +45,7 @@ pub fn check_intersection_type_compact(
4245
// 对于其他类型,检查是否至少满足一个组成部分
4346
for intersection_component in source_intersection.get_types() {
4447
if check_general_type_compact(
45-
db,
48+
context,
4649
intersection_component,
4750
compact_type,
4851
check_guard.next_level()?,
@@ -58,15 +61,15 @@ pub fn check_intersection_type_compact(
5861
}
5962

6063
fn check_intersection_type_compact_table(
61-
db: &DbIndex,
64+
context: &TypeCheckContext,
6265
source_intersection: &LuaIntersectionType,
6366
table_owner: LuaMemberOwner,
6467
check_guard: TypeCheckGuard,
6568
) -> TypeCheckResult {
6669
// 交叉类型要求 TableConst 必须满足所有组成部分
6770
for intersection_component in source_intersection.get_types() {
6871
check_general_type_compact(
69-
db,
72+
context,
7073
intersection_component,
7174
&LuaType::TableConst(
7275
table_owner
@@ -82,7 +85,7 @@ fn check_intersection_type_compact_table(
8285
}
8386

8487
fn check_intersection_type_compact_intersection(
85-
db: &DbIndex,
88+
context: &TypeCheckContext,
8689
source_intersection: &LuaIntersectionType,
8790
compact_intersection: &LuaIntersectionType,
8891
check_guard: TypeCheckGuard,
@@ -93,7 +96,7 @@ fn check_intersection_type_compact_intersection(
9396

9497
for compact_component in compact_intersection.get_types() {
9598
if check_general_type_compact(
96-
db,
99+
context,
97100
source_component,
98101
compact_component,
99102
check_guard.next_level()?,

crates/emmylua_code_analysis/src/semantic/type_check/complex_type/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use object_type_check::check_object_type_compact;
1010
use table_generic_check::check_table_generic_type_compact;
1111
use tuple_type_check::check_tuple_type_compact;
1212

13-
use crate::{DbIndex, LuaType, LuaUnionType};
13+
use crate::{semantic::type_check::type_check_context::TypeCheckContext, LuaType, LuaUnionType};
1414

1515
use super::{
1616
TypeCheckResult, check_general_type_compact, type_check_fail_reason::TypeCheckFailReason,
@@ -19,15 +19,15 @@ use super::{
1919

2020
// all is duck typing
2121
pub fn check_complex_type_compact(
22-
db: &DbIndex,
22+
context: &TypeCheckContext,
2323
source: &LuaType,
2424
compact_type: &LuaType,
2525
check_guard: TypeCheckGuard,
2626
) -> TypeCheckResult {
2727
match source {
2828
LuaType::Array(source_array_type) => {
2929
match check_array_type_compact(
30-
db,
30+
context,
3131
source_array_type.get_base(),
3232
compact_type,
3333
check_guard,
@@ -37,20 +37,20 @@ pub fn check_complex_type_compact(
3737
}
3838
}
3939
LuaType::Tuple(tuple) => {
40-
match check_tuple_type_compact(db, &tuple, compact_type, check_guard) {
40+
match check_tuple_type_compact(context, &tuple, compact_type, check_guard) {
4141
Err(TypeCheckFailReason::DonotCheck) => {}
4242
result => return result,
4343
}
4444
}
4545
LuaType::Object(source_object) => {
46-
match check_object_type_compact(db, source_object, compact_type, check_guard) {
46+
match check_object_type_compact(context, source_object, compact_type, check_guard) {
4747
Err(TypeCheckFailReason::DonotCheck) => {}
4848
result => return result,
4949
}
5050
}
5151
LuaType::TableGeneric(source_generic_param) => {
5252
match check_table_generic_type_compact(
53-
db,
53+
context,
5454
source_generic_param,
5555
compact_type,
5656
check_guard,
@@ -61,7 +61,7 @@ pub fn check_complex_type_compact(
6161
}
6262
LuaType::Intersection(source_intersection) => {
6363
match check_intersection_type_compact(
64-
db,
64+
context,
6565
source_intersection,
6666
compact_type,
6767
check_guard,
@@ -74,7 +74,7 @@ pub fn check_complex_type_compact(
7474
match compact_type {
7575
LuaType::Union(compact_union) => {
7676
return check_union_type_compact_union(
77-
db,
77+
context,
7878
source,
7979
compact_union,
8080
check_guard.next_level()?,
@@ -84,7 +84,7 @@ pub fn check_complex_type_compact(
8484
}
8585
for sub_type in union_type.into_vec() {
8686
match check_general_type_compact(
87-
db,
87+
context,
8888
&sub_type,
8989
compact_type,
9090
check_guard.next_level()?,
@@ -104,7 +104,7 @@ pub fn check_complex_type_compact(
104104
LuaType::MultiLineUnion(multi_union) => {
105105
let union = multi_union.to_union();
106106
return check_complex_type_compact(
107-
db,
107+
context,
108108
&union,
109109
&compact_type,
110110
check_guard.next_level()?,
@@ -115,7 +115,7 @@ pub fn check_complex_type_compact(
115115
// Do I need to check union types?
116116
if let LuaType::Union(union) = compact_type {
117117
for sub_compact in union.into_vec() {
118-
match check_complex_type_compact(db, source, &sub_compact, check_guard.next_level()?) {
118+
match check_complex_type_compact(context, source, &sub_compact, check_guard.next_level()?) {
119119
Ok(_) => {}
120120
Err(e) => return Err(e),
121121
}
@@ -129,14 +129,14 @@ pub fn check_complex_type_compact(
129129

130130
// too complex
131131
fn check_union_type_compact_union(
132-
db: &DbIndex,
132+
context: &TypeCheckContext,
133133
source: &LuaType,
134134
compact_union: &LuaUnionType,
135135
check_guard: TypeCheckGuard,
136136
) -> TypeCheckResult {
137137
let compact_types = compact_union.into_vec();
138138
for compact_sub_type in compact_types {
139-
check_general_type_compact(db, source, &compact_sub_type, check_guard.next_level()?)?;
139+
check_general_type_compact(context, source, &compact_sub_type, check_guard.next_level()?)?;
140140
}
141141

142142
Ok(())

0 commit comments

Comments
 (0)