Skip to content

Commit a8ec1af

Browse files
committed
Merge remote-tracking branch 'EmmyLuaLs/main' into attribute
2 parents fbd05b1 + 5e04abf commit a8ec1af

File tree

21 files changed

+989
-935
lines changed

21 files changed

+989
-935
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use emmylua_parser::{LuaAstNode, LuaAstToken, LuaExpr, LuaForRangeStat};
1+
use emmylua_parser::{LuaAstToken, LuaExpr, LuaForRangeStat};
22

33
use crate::{
44
DbIndex, InferFailReason, LuaDeclId, LuaInferCache, LuaOperatorMetaMethod, LuaType,
@@ -79,7 +79,6 @@ pub fn infer_for_range_iter_expr_func(
7979
}
8080

8181
let iter_func_expr = iter_exprs[0].clone();
82-
let root = iter_func_expr.get_root();
8382
let first_expr_type = infer_expr(db, cache, iter_func_expr)?;
8483
let doc_function = match first_expr_type {
8584
LuaType::DocFunction(func) => func,
@@ -150,7 +149,6 @@ pub fn infer_for_range_iter_expr_func(
150149
db,
151150
cache,
152151
substitutor: &mut substitutor,
153-
root,
154152
call_expr: None,
155153
};
156154
let params = doc_function

crates/emmylua_code_analysis/src/db_index/signature/signature.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::{
1111
FileId,
1212
db_index::{LuaFunctionType, LuaType},
1313
};
14-
use crate::{LuaAttributeUse, SemanticModel, VariadicType};
14+
use crate::{LuaAttributeUse, SemanticModel, VariadicType, first_param_may_not_self};
1515

1616
#[derive(Debug)]
1717
pub struct LuaSignature {
@@ -137,11 +137,8 @@ impl LuaSignature {
137137
match owner_type {
138138
Some(owner_type) => {
139139
// 一些类型不应该被视为 method
140-
if let (LuaType::Ref(_) | LuaType::Def(_), _) = (owner_type, param_type)
141-
&& (param_type.is_any()
142-
|| param_type.is_table()
143-
|| param_type.is_class_tpl()
144-
|| param_type.is_str_tpl_ref())
140+
if matches!(owner_type, LuaType::Ref(_) | LuaType::Def(_))
141+
&& first_param_may_not_self(param_type)
145142
{
146143
return false;
147144
}
@@ -195,7 +192,7 @@ impl LuaDocParamInfo {
195192
}
196193
}
197194

198-
#[derive(Debug)]
195+
#[derive(Debug, Clone)]
199196
pub struct LuaDocReturnInfo {
200197
pub name: Option<String>,
201198
pub type_ref: LuaType,

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,15 @@ fn get_real_type_with_depth<'a>(
299299
_ => Some(typ),
300300
}
301301
}
302+
303+
// 第一个参数是否不应该视为 self
304+
pub fn first_param_may_not_self(typ: &LuaType) -> bool {
305+
if typ.is_table() || matches!(typ, LuaType::Ref(_) | LuaType::Def(_) | LuaType::Any) {
306+
return false;
307+
}
308+
if let LuaType::Union(u) = typ {
309+
return u.into_vec().iter().any(first_param_may_not_self);
310+
}
311+
312+
true
313+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use smol_str::SmolStr;
1212
use crate::{
1313
AsyncState, DbIndex, FileId, InFiled, SemanticModel,
1414
db_index::{LuaMemberKey, LuaSignatureId, r#type::type_visit_trait::TypeVisitTrait},
15+
first_param_may_not_self,
1516
};
1617

1718
use super::{TypeOps, type_decl::LuaTypeDeclId};
@@ -689,8 +690,8 @@ impl LuaFunctionType {
689690
match owner_type {
690691
Some(owner_type) => {
691692
// 一些类型不应该被视为 method
692-
if let (LuaType::Ref(_) | LuaType::Def(_), _) = (owner_type, t)
693-
&& (t.is_any() || t.is_table() || t.is_class_tpl())
693+
if matches!(owner_type, LuaType::Ref(_) | LuaType::Def(_))
694+
&& first_param_may_not_self(t)
694695
{
695696
return false;
696697
}

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

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ impl Checker for UnusedChecker {
5555
).to_string(),
5656
None)
5757
}
58+
UnusedCheckResult::UnusedSelf(range) => {
59+
context.add_diagnostic(
60+
DiagnosticCode::Unused,
61+
range,
62+
t!(
63+
"Implicit self is never used, if this is intentional, please use '.' instead of ':' to define the method",
64+
).to_string(),
65+
None,
66+
);
67+
}
5868
}
5969
}
6070
}
@@ -64,6 +74,7 @@ impl Checker for UnusedChecker {
6474
enum UnusedCheckResult {
6575
Unused(TextRange),
6676
AssignedButNotRead(TextRange),
77+
UnusedSelf(TextRange),
6778
}
6879

6980
fn get_unused_check_result(
@@ -73,9 +84,15 @@ fn get_unused_check_result(
7384
) -> Result<(), UnusedCheckResult> {
7485
let decl_range = decl.get_range();
7586
let file_id = decl.get_file_id();
76-
let decl_ref = ref_index
77-
.get_decl_references(&file_id, &decl.get_id())
78-
.ok_or(UnusedCheckResult::Unused(decl_range))?;
87+
let decl_ref = match ref_index.get_decl_references(&file_id, &decl.get_id()) {
88+
Some(decl_ref) => decl_ref,
89+
None => {
90+
if decl.is_implicit_self() {
91+
return Err(UnusedCheckResult::UnusedSelf(decl_range));
92+
}
93+
return Err(UnusedCheckResult::Unused(decl_range));
94+
}
95+
};
7996

8097
if decl_ref.cells.is_empty() {
8198
return Err(UnusedCheckResult::Unused(decl_range));

crates/emmylua_code_analysis/src/semantic/generic/instantiate_type/instantiate_func_generic.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{collections::HashSet, ops::Deref, sync::Arc};
22

3-
use emmylua_parser::{LuaAstNode, LuaCallExpr, LuaExpr};
3+
use emmylua_parser::{LuaCallExpr, LuaExpr};
44
use internment::ArcIntern;
55

66
use crate::{
@@ -63,7 +63,6 @@ pub fn instantiate_func_generic(
6363
db,
6464
cache,
6565
substitutor: &mut substitutor,
66-
root: call_expr.get_root(),
6766
call_expr: Some(call_expr.clone()),
6867
};
6968
if !generic_tpls.is_empty() {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use emmylua_parser::{LuaCallExpr, LuaSyntaxNode};
1+
use emmylua_parser::LuaCallExpr;
22

33
use crate::{DbIndex, LuaInferCache, TypeSubstitutor};
44

@@ -7,6 +7,5 @@ pub struct TplContext<'a> {
77
pub db: &'a DbIndex,
88
pub cache: &'a mut LuaInferCache,
99
pub substitutor: &'a mut TypeSubstitutor,
10-
pub root: LuaSyntaxNode,
1110
pub call_expr: Option<LuaCallExpr>,
1211
}

crates/emmylua_code_analysis/src/semantic/infer/infer_call/mod.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ use super::{
1313
};
1414
use crate::{
1515
CacheEntry, DbIndex, InFiled, LuaFunctionType, LuaGenericType, LuaInstanceType,
16-
LuaOperatorMetaMethod, LuaOperatorOwner, LuaSignatureId, LuaType, LuaTypeDeclId, LuaUnionType,
16+
LuaOperatorMetaMethod, LuaOperatorOwner, LuaSignature, LuaSignatureId, LuaType, LuaTypeDeclId,
17+
LuaUnionType,
1718
};
1819
use crate::{
1920
InferGuardRef,
@@ -179,6 +180,7 @@ fn infer_signature_doc_function(
179180
if !signature.is_resolve_return() {
180181
return Err(InferFailReason::UnResolveSignatureReturn(signature_id));
181182
}
183+
let is_generic = signature_is_generic(db, cache, &signature, &call_expr).unwrap_or(false);
182184
let overloads = &signature.overloads;
183185
if overloads.is_empty() {
184186
let mut fake_doc_function = LuaFunctionType::new(
@@ -187,7 +189,7 @@ fn infer_signature_doc_function(
187189
signature.get_type_params(),
188190
signature.get_return_type(),
189191
);
190-
if signature.is_generic() {
192+
if is_generic {
191193
fake_doc_function = instantiate_func_generic(db, cache, &fake_doc_function, call_expr)?;
192194
}
193195

@@ -207,7 +209,7 @@ fn infer_signature_doc_function(
207209
cache,
208210
new_overloads,
209211
call_expr.clone(),
210-
signature.is_generic(),
212+
is_generic,
211213
args_count,
212214
)
213215
}
@@ -654,3 +656,23 @@ fn check_can_infer(
654656

655657
Ok(())
656658
}
659+
660+
fn signature_is_generic(
661+
db: &DbIndex,
662+
cache: &mut LuaInferCache,
663+
signature: &LuaSignature,
664+
call_expr: &LuaCallExpr,
665+
) -> Option<bool> {
666+
if signature.is_generic() {
667+
return Some(true);
668+
}
669+
let LuaExpr::IndexExpr(index_expr) = call_expr.get_prefix_expr()? else {
670+
return None;
671+
};
672+
let prefix_type = infer_expr(db, cache, index_expr.get_prefix_expr()?).ok()?;
673+
match prefix_type {
674+
// 对于 Generic 直接认为是泛型
675+
LuaType::Generic(_) => Some(true),
676+
_ => Some(prefix_type.contain_tpl()),
677+
}
678+
}

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,6 @@ fn infer_generic_member(
738738
let generic_params = generic_type.get_params();
739739
let substitutor = TypeSubstitutor::from_type_array(generic_params.clone());
740740

741-
// TODO: this is just a hack to support inheritance from the generic objects
742-
// like `---@class box<T>: T`. Should be rewritten: generic types should
743-
// be passed to the called instantiate_type_generic() in some kind of a
744-
// context.
745741
if let LuaType::Ref(base_type_decl_id) = &base_type {
746742
let result = infer_generic_members_from_super_generics(
747743
db,

crates/emmylua_code_analysis/src/semantic/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ use crate::{LuaFunctionType, LuaMemberId, LuaMemberKey, LuaTypeOwner};
5151
pub use generic::*;
5252
pub use guard::{InferGuard, InferGuardRef};
5353
pub use infer::InferFailReason;
54+
pub use infer::infer_call_expr_func;
55+
pub(crate) use infer::infer_expr;
5456
pub use infer::infer_param;
55-
pub(crate) use infer::{infer_call_expr_func, infer_expr};
5657
use overload_resolve::resolve_signature;
5758
pub use semantic_info::SemanticDeclLevel;
5859
pub use type_check::{TypeCheckFailReason, TypeCheckResult};

0 commit comments

Comments
 (0)