Skip to content

Commit 47f81a0

Browse files
committed
fix type check for union
1 parent 43b7286 commit 47f81a0

File tree

2 files changed

+72
-16
lines changed

2 files changed

+72
-16
lines changed

crates/code_analysis/src/lib.rs

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ mod compilation;
22
mod config;
33
mod db_index;
44
mod diagnostic;
5+
mod profile;
56
mod semantic;
67
mod vfs;
7-
mod profile;
88

99
pub use compilation::*;
1010
pub use config::*;
1111
pub use db_index::*;
1212
pub use diagnostic::*;
1313
use log::{error, info};
1414
use lsp_types::Uri;
15+
pub use profile::Profile;
1516
pub use semantic::*;
1617
use std::{collections::HashSet, env, path::PathBuf, sync::Arc};
1718
use tokio_util::sync::CancellationToken;
1819
pub use vfs::*;
19-
pub use profile::Profile;
2020

2121
#[macro_use]
2222
extern crate rust_i18n;
@@ -131,26 +131,44 @@ impl EmmyLuaAnalysis {
131131
pub fn update_files_by_uri(&mut self, files: Vec<(Uri, Option<String>)>) -> Vec<FileId> {
132132
let mut removed_files = HashSet::new();
133133
let mut updated_files = HashSet::new();
134-
for (uri, text) in files {
135-
let is_new_text = text.is_some();
136-
let file_id = self
137-
.compilation
138-
.get_db_mut()
139-
.get_vfs_mut()
140-
.set_file_content(&uri, text);
141-
removed_files.insert(file_id);
142-
if is_new_text {
143-
updated_files.insert(file_id);
134+
{
135+
let _p = Profile::new("update files");
136+
for (uri, text) in files {
137+
let is_new_text = text.is_some();
138+
let file_id = self
139+
.compilation
140+
.get_db_mut()
141+
.get_vfs_mut()
142+
.set_file_content(&uri, text);
143+
removed_files.insert(file_id);
144+
if is_new_text {
145+
updated_files.insert(file_id);
146+
}
144147
}
145148
}
146-
147149
self.compilation
148150
.remove_index(removed_files.into_iter().collect());
149151
let updated_files: Vec<FileId> = updated_files.into_iter().collect();
150152
self.compilation.update_index(updated_files.clone());
151153
updated_files
152154
}
153155

156+
// pub fn parrallel_update_files_by_uri(
157+
// &mut self,
158+
// files: Vec<(Uri, Option<String>)>,
159+
// ) -> Vec<FileId> {
160+
// let mut removed_files = HashSet::new();
161+
// let mut updated_files = HashSet::new();
162+
// {
163+
// let _p = Profile::new("parrallel update files");
164+
// }
165+
// self.compilation
166+
// .remove_index(removed_files.into_iter().collect());
167+
// let updated_files: Vec<FileId> = updated_files.into_iter().collect();
168+
// self.compilation.update_index(updated_files.clone());
169+
// updated_files
170+
// }
171+
154172
pub fn update_files_by_path(&mut self, files: Vec<(PathBuf, Option<String>)>) -> Vec<FileId> {
155173
let files = files
156174
.into_iter()

crates/code_analysis/src/semantic/type_compact/mod.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ use std::ops::Deref;
55

66
use func_type::infer_doc_func_type_compact;
77

8-
use crate::db_index::{
9-
DbIndex, LuaGenericType, LuaMemberKey, LuaMemberOwner, LuaObjectType, LuaTupleType, LuaType,
10-
LuaTypeDeclId,
8+
use crate::{
9+
db_index::{
10+
DbIndex, LuaGenericType, LuaMemberKey, LuaMemberOwner, LuaObjectType, LuaTupleType,
11+
LuaType, LuaTypeDeclId,
12+
},
13+
LuaUnionType,
1114
};
1215

1316
use super::{InferGuard, LuaInferConfig};
@@ -115,6 +118,9 @@ fn infer_type_compact(
115118
}
116119
// TODO implement the check for table
117120
(LuaType::Tuple(_), _) => compact_type.is_table(),
121+
(LuaType::Union(a), LuaType::Union(b)) => {
122+
infer_union_union_type_compact(db, config, a, b, infer_guard).unwrap_or(false)
123+
}
118124
(LuaType::Union(a), _) => a
119125
.get_types()
120126
.iter()
@@ -409,3 +415,35 @@ fn infer_generic_type_compact(
409415

410416
Some(true)
411417
}
418+
419+
// a diffcult compare
420+
fn infer_union_union_type_compact(
421+
db: &DbIndex,
422+
config: &mut LuaInferConfig,
423+
source: &LuaUnionType,
424+
compact_type: &LuaUnionType,
425+
infer_guard: &mut InferGuard,
426+
) -> Option<bool> {
427+
let a_types = source.get_types();
428+
let b_types = compact_type.get_types();
429+
430+
for a_type in a_types {
431+
if !b_types
432+
.iter()
433+
.any(|b_type| infer_type_compact(db, config, a_type, b_type, infer_guard))
434+
{
435+
return Some(false);
436+
}
437+
}
438+
439+
for b_type in b_types {
440+
if !a_types
441+
.iter()
442+
.any(|a_type| infer_type_compact(db, config, a_type, b_type, infer_guard))
443+
{
444+
return Some(false);
445+
}
446+
}
447+
448+
Some(false)
449+
}

0 commit comments

Comments
 (0)