Skip to content

Commit bc8903b

Browse files
committed
refactor narrow and union type
1 parent bfc5295 commit bc8903b

38 files changed

Lines changed: 302 additions & 343 deletions

File tree

crates/emmylua_code_analysis/src/compilation/analyzer/flow/bind_analyze/stats.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ use emmylua_parser::{
77
use crate::{
88
compilation::analyzer::flow::{
99
bind_analyze::{
10-
bind_block, bind_each_child, bind_node, exprs::{bind_condition_expr, bind_expr}, finish_flow_label
10+
bind_block, bind_each_child, bind_node,
11+
exprs::{bind_condition_expr, bind_expr},
12+
finish_flow_label,
1113
},
1214
binder::FlowBinder,
1315
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn find_union_function_member(
330330
let result = find_function_type_by_member_key(
331331
db,
332332
cache,
333-
sub_type,
333+
&sub_type,
334334
index_expr.clone(),
335335
&mut InferGuard::new(),
336336
deep_guard,
@@ -692,7 +692,7 @@ fn find_member_by_index_union(
692692
let result = find_function_type_by_operator(
693693
db,
694694
cache,
695-
member,
695+
&member,
696696
index_expr.clone(),
697697
&mut InferGuard::new(),
698698
deep_guard,

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,12 @@ fn resolve_closure_member_type(
314314
multi_function_type.push(func.clone());
315315
}
316316
LuaType::Ref(ref_id) => {
317-
if infer_guard.check(ref_id).is_err() {
317+
if infer_guard.check(&ref_id).is_err() {
318318
continue;
319319
}
320320
let type_decl = db
321321
.get_type_index()
322-
.get_type_decl(ref_id)
322+
.get_type_decl(&ref_id)
323323
.ok_or(InferFailReason::None)?;
324324

325325
if let Some(origin) = type_decl.get_alias_origin(&db, None) {
@@ -492,17 +492,18 @@ fn resolve_doc_function(
492492
Ok(())
493493
}
494494

495-
fn filter_signature_type(typ: &LuaType) -> Option<Vec<&Arc<LuaFunctionType>>> {
496-
let mut result: Vec<&Arc<LuaFunctionType>> = Vec::new();
495+
fn filter_signature_type(typ: &LuaType) -> Option<Vec<Arc<LuaFunctionType>>> {
496+
let mut result: Vec<Arc<LuaFunctionType>> = Vec::new();
497497
let mut stack = Vec::new();
498-
stack.push(typ);
498+
stack.push(typ.clone());
499499
while let Some(typ) = stack.pop() {
500500
match typ {
501501
LuaType::DocFunction(func) => {
502-
result.push(func);
502+
result.push(func.clone());
503503
}
504504
LuaType::Union(union) => {
505-
for typ in union.get_types().iter().rev() {
505+
let types = union.get_types();
506+
for typ in types.into_iter().rev() {
506507
stack.push(typ);
507508
}
508509
}

crates/emmylua_code_analysis/src/compilation/test/and_or_test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ mod test {
5252
);
5353

5454
let a_ty = ws.expr_ty("a");
55+
println!("{:?}", a_ty);
5556
assert_eq!(
5657
format!("{:?}", a_ty).to_string(),
57-
"Union(LuaUnionType { types: [IntegerConst(2), Nil] })"
58+
"Union(Multi([IntegerConst(2), Nil]))"
5859
);
5960
}
6061

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ mod type_owner;
66
mod types;
77

88
use super::traits::LuaIndex;
9-
use crate::{FileId, InFiled};
9+
use crate::{DbIndex, FileId, InFiled};
1010
pub use humanize_type::{format_union_type, humanize_type, RenderLevel};
1111
use std::collections::{HashMap, HashSet};
1212
pub use type_decl::{
1313
LuaDeclLocation, LuaDeclTypeKind, LuaTypeAttribute, LuaTypeDecl, LuaTypeDeclId,
1414
};
15-
pub use type_ops::get_real_type;
1615
pub use type_ops::TypeOps;
1716
pub use type_owner::{LuaTypeCache, LuaTypeOwner};
1817
pub use types::*;
@@ -283,3 +282,30 @@ impl LuaIndex for LuaTypeIndex {
283282
self.in_filed_type_owner.clear();
284283
}
285284
}
285+
286+
pub fn get_real_type<'a>(db: &'a DbIndex, typ: &'a LuaType) -> Option<&'a LuaType> {
287+
get_real_type_with_depth(db, typ, 0)
288+
}
289+
290+
fn get_real_type_with_depth<'a>(
291+
db: &'a DbIndex,
292+
typ: &'a LuaType,
293+
depth: u32,
294+
) -> Option<&'a LuaType> {
295+
const MAX_RECURSION_DEPTH: u32 = 10;
296+
297+
if depth >= MAX_RECURSION_DEPTH {
298+
return Some(typ);
299+
}
300+
301+
match typ {
302+
LuaType::Ref(type_decl_id) => {
303+
let type_decl = db.get_type_index().get_type_decl(type_decl_id)?;
304+
if type_decl.is_alias() {
305+
return get_real_type_with_depth(db, type_decl.get_alias_ref()?, depth + 1);
306+
}
307+
Some(typ)
308+
}
309+
_ => Some(typ),
310+
}
311+
}

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

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
mod and_type;
2-
mod false_or_nil_type;
3-
mod narrow_type;
41
mod remove_type;
52
mod test;
63
mod union_type;
@@ -15,14 +12,6 @@ pub enum TypeOps {
1512
Union,
1613
/// Remove a type from the source type
1714
Remove,
18-
/// Remove a type from the source type, but keep the source type
19-
RemoveNilOrFalse,
20-
/// Force a type to the source type
21-
Narrow,
22-
/// Only keep the false or nil type
23-
NarrowFalseOrNil,
24-
// /// And operation
25-
// And,
2615
}
2716

2817
impl TypeOps {
@@ -32,45 +21,6 @@ impl TypeOps {
3221
TypeOps::Remove => {
3322
remove_type::remove_type(db, source.clone(), target.clone()).unwrap_or(LuaType::Any)
3423
}
35-
TypeOps::Narrow => narrow_type::narrow_down_type(db, source.clone(), target.clone())
36-
.unwrap_or(target.clone()),
37-
// TypeOps::And => and_type::and_type(source.clone(), target.clone()),
38-
_ => source.clone(),
3924
}
4025
}
41-
42-
pub fn apply_source(&self, db: &DbIndex, source: &LuaType) -> LuaType {
43-
match self {
44-
TypeOps::NarrowFalseOrNil => false_or_nil_type::narrow_false_or_nil(db, source.clone()),
45-
TypeOps::RemoveNilOrFalse => false_or_nil_type::remove_false_or_nil(source.clone()),
46-
_ => source.clone(),
47-
}
48-
}
49-
}
50-
51-
pub fn get_real_type<'a>(db: &'a DbIndex, typ: &'a LuaType) -> Option<&'a LuaType> {
52-
get_real_type_with_depth(db, typ, 0)
53-
}
54-
55-
fn get_real_type_with_depth<'a>(
56-
db: &'a DbIndex,
57-
typ: &'a LuaType,
58-
depth: u32,
59-
) -> Option<&'a LuaType> {
60-
const MAX_RECURSION_DEPTH: u32 = 100;
61-
62-
if depth >= MAX_RECURSION_DEPTH {
63-
return Some(typ);
64-
}
65-
66-
match typ {
67-
LuaType::Ref(type_decl_id) => {
68-
let type_decl = db.get_type_index().get_type_decl(type_decl_id)?;
69-
if type_decl.is_alias() {
70-
return get_real_type_with_depth(db, type_decl.get_alias_ref()?, depth + 1);
71-
}
72-
Some(typ)
73-
}
74-
_ => Some(typ),
75-
}
7626
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::{DbIndex, LuaType, LuaUnionType};
2-
3-
use super::get_real_type;
1+
use crate::{get_real_type, DbIndex, LuaType, LuaUnionType};
42

53
pub fn remove_type(db: &DbIndex, source: LuaType, removed_type: LuaType) -> Option<LuaType> {
64
if source == removed_type {

0 commit comments

Comments
 (0)