Skip to content

Commit 99785b2

Browse files
committed
improve type check
1 parent 50fe9de commit 99785b2

File tree

10 files changed

+36
-26
lines changed

10 files changed

+36
-26
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
`FIX` Fix inference issue by resolving iteration order problem.
2929

30+
`FIX` Improve type check
31+
3032
# 0.4.6
3133

3234
`FIX` Fix issue with executable file directory hierarchy being too deep.

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/emmylua_code_analysis/src/diagnostic/checker/param_type_check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn check_call_expr(
7474
expr_type = LuaType::Any;
7575
}
7676

77-
if !semantic_model.check_type_compact(&param_type, &expr_type) {
77+
if !semantic_model.type_check(&param_type, &expr_type) {
7878
let db = semantic_model.get_db();
7979
context.add_diagnostic(
8080
DiagnosticCode::ParamTypeNotMatch,
@@ -103,7 +103,7 @@ fn check_call_expr(
103103
expr_type = LuaType::Any;
104104
}
105105

106-
if !semantic_model.check_type_compact(&param_type, &expr_type) {
106+
if !semantic_model.type_check(&param_type, &expr_type) {
107107
let db = semantic_model.get_db();
108108
context.add_diagnostic(
109109
DiagnosticCode::ParamTypeNotMatch,

crates/emmylua_code_analysis/src/semantic/infer/infer_index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
semantic::{
1515
instantiate::{instantiate_type, TypeSubstitutor},
1616
member::{get_buildin_type_map_type_id, without_index_operator, without_members},
17-
type_compact::check_type_compact,
17+
type_check::check_type_compact,
1818
InferGuard,
1919
},
2020
InFiled, LuaInstanceType, TypeOps,

crates/emmylua_code_analysis/src/semantic/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ mod overload_resolve;
66
mod reference;
77
mod semantic_info;
88
mod type_calc;
9-
mod type_compact;
9+
mod type_check;
1010
mod visibility;
1111

1212
use std::cell::RefCell;
@@ -24,7 +24,7 @@ use semantic_info::{
2424
infer_node_property_owner, infer_node_semantic_info, infer_token_property_owner,
2525
infer_token_semantic_info,
2626
};
27-
use type_compact::{check_type_compact, is_sub_type_of};
27+
use type_check::{check_type_compact, is_sub_type_of};
2828
use visibility::check_visibility;
2929

3030
use crate::LuaFunctionType;
@@ -100,7 +100,7 @@ impl<'a> SemanticModel<'a> {
100100
infer_members(self.db, prefix_type)
101101
}
102102

103-
pub fn check_type_compact(&self, source: &LuaType, compact_type: &LuaType) -> bool {
103+
pub fn type_check(&self, source: &LuaType, compact_type: &LuaType) -> bool {
104104
check_type_compact(
105105
self.db,
106106
&mut self.infer_config.borrow_mut(),

crates/emmylua_code_analysis/src/semantic/overload_resolve/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use emmylua_parser::LuaCallExpr;
55
use crate::db_index::{DbIndex, LuaFunctionType, LuaType};
66

77
use super::{
8-
infer_expr, instantiate::instantiate_func, type_compact::check_type_compact, LuaInferConfig,
8+
infer_expr, instantiate::instantiate_func, type_check::check_type_compact, LuaInferConfig,
99
};
1010

1111
pub fn resolve_signature(

crates/emmylua_code_analysis/src/semantic/type_compact/func_type.rs renamed to crates/emmylua_code_analysis/src/semantic/type_check/func_type.rs

File renamed without changes.

crates/emmylua_code_analysis/src/semantic/type_compact/mod.rs renamed to crates/emmylua_code_analysis/src/semantic/type_check/mod.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,13 @@ fn infer_type_compact(
6161
(_, LuaType::MemberPathExist(right)) => {
6262
infer_type_compact(db, config, source, &right.get_origin(), infer_guard)
6363
}
64-
(LuaType::BooleanConst(_), _) => compact_type.is_boolean(),
65-
(LuaType::IntegerConst(_), _) => compact_type.is_number(),
66-
(LuaType::StringConst(_), _) => compact_type.is_string(),
67-
(LuaType::Number, _) => compact_type.is_number(),
68-
(LuaType::Integer, _) => compact_type.is_integer(),
69-
(LuaType::String, _) => compact_type.is_string(),
70-
(LuaType::Boolean, _) => compact_type.is_boolean(),
64+
(LuaType::BooleanConst(_), right) if right.is_boolean() => true,
65+
(LuaType::IntegerConst(_), right) if right.is_number() => true,
66+
(LuaType::StringConst(_), right) if right.is_string() => true,
67+
(LuaType::Number, right) if right.is_number() => true,
68+
(LuaType::Integer, right) if right.is_integer() => true,
69+
(LuaType::String, right) if right.is_string() => true,
70+
(LuaType::Boolean, right) if right.is_boolean() => true,
7171
(LuaType::DocIntegerConst(i), _) => match compact_type {
7272
LuaType::IntegerConst(j) => *i == j,
7373
LuaType::DocIntegerConst(j) => *i == j,
@@ -111,7 +111,9 @@ fn infer_type_compact(
111111
.unwrap_or(false)
112112
}
113113
(LuaType::Array(a), LuaType::Array(b)) => infer_type_compact(db, config, a, b, infer_guard),
114-
(LuaType::Array(a), _) => infer_array_type_compact_by_element_type(db, config, a, &compact_type, infer_guard),
114+
(LuaType::Array(a), _) => {
115+
infer_array_type_compact_by_element_type(db, config, a, &compact_type, infer_guard)
116+
}
115117
(LuaType::Tuple(a), LuaType::Tuple(b)) => {
116118
infer_tuple_type_compact(db, config, a, b, infer_guard).unwrap_or(false)
117119
}
@@ -124,6 +126,11 @@ fn infer_type_compact(
124126
.get_types()
125127
.iter()
126128
.any(|t| infer_type_compact(db, config, t, &compact_type, infer_guard)),
129+
(left, LuaType::Union(b)) if !left.is_union() => b
130+
.get_types()
131+
.iter()
132+
.all(|t| infer_type_compact(db, config, left, t, infer_guard)),
133+
127134
(LuaType::Intersection(a), _) => a
128135
.get_types()
129136
.iter()
@@ -366,13 +373,14 @@ fn infer_array_type_compact_by_element_type(
366373
infer_guard: &mut InferGuard,
367374
) -> bool {
368375
match compact_type {
369-
LuaType::Array(element_type) => infer_type_compact(db, config, source_element_type, element_type, infer_guard),
370-
LuaType::Tuple(tuple_type) =>
371-
tuple_type.get_types()
372-
.into_iter()
373-
.all(|element_type| infer_type_compact(db, config, source_element_type, element_type, infer_guard)),
376+
LuaType::Array(element_type) => {
377+
infer_type_compact(db, config, source_element_type, element_type, infer_guard)
378+
}
379+
LuaType::Tuple(tuple_type) => tuple_type.get_types().into_iter().all(|element_type| {
380+
infer_type_compact(db, config, source_element_type, element_type, infer_guard)
381+
}),
374382
// TODO implement the check for table
375-
_ => compact_type.is_table()
383+
_ => compact_type.is_table(),
376384
}
377385
}
378386

crates/emmylua_code_analysis/src/semantic/type_compact/sub_type.rs renamed to crates/emmylua_code_analysis/src/semantic/type_check/sub_type.rs

File renamed without changes.

crates/emmylua_code_analysis/src/semantic/visibility/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use emmylua_parser::{
55

66
use crate::{DbIndex, Emmyrc, FileId, LuaMemberOwner, LuaPropertyOwnerId, LuaType};
77

8-
use super::{infer_expr, type_compact::is_sub_type_of, LuaInferConfig};
8+
use super::{infer_expr, type_check::is_sub_type_of, LuaInferConfig};
99

1010
pub fn check_visibility(
1111
db: &DbIndex,

0 commit comments

Comments
 (0)