Skip to content

Commit bd04a39

Browse files
committed
fix: instantiate StrTplRef
1 parent 1b588b7 commit bd04a39

3 files changed

Lines changed: 34 additions & 29 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl LuaTypeDeclId {
271271
}) as _
272272
}
273273

274-
pub fn collect_super_types(&self, db: &DbIndex, collected_types: &mut Vec<LuaType>) {
274+
fn collect_super_types(&self, db: &DbIndex, collected_types: &mut Vec<LuaType>) {
275275
// 必须广度优先
276276
let mut queue = Vec::new();
277277
queue.push(self.clone());

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,13 +391,12 @@ fn instantiate_function_generic_params(
391391
.filter_map(|generic_tpl| {
392392
let tpl_id = generic_tpl.get_tpl_id();
393393
let param = generic_tpl.get_param();
394-
// A pending entry means this generic belongs to the current instantiation boundary
395-
// and has been finalized into the function params/return. Foreign nested generics
396-
// are absent from the substitutor and remain owned by the nested function.
394+
// substitutor 中存在该泛型时, 说明它有实际类型, 无需保留.
397395
if context.substitutor.get(tpl_id).is_some() {
398396
return None;
399397
}
400398

399+
// 对约束与默认值做一次实例化尝试以传递给后续.
401400
let constraint = param
402401
.constraint
403402
.as_ref()

crates/emmylua_code_analysis/src/semantic/generic/tpl_pattern/mod.rs

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,6 @@ pub fn multi_param_tpl_pattern_match_multi_return(
126126
Ok(())
127127
}
128128

129-
fn get_str_tpl_infer_type(name: &str) -> LuaType {
130-
match name {
131-
"unknown" => LuaType::Unknown,
132-
"never" => LuaType::Never,
133-
"nil" | "void" => LuaType::Nil,
134-
"any" => LuaType::Any,
135-
"userdata" => LuaType::Userdata,
136-
"thread" => LuaType::Thread,
137-
"boolean" | "bool" => LuaType::Boolean,
138-
"string" => LuaType::String,
139-
"integer" | "int" => LuaType::Integer,
140-
"number" => LuaType::Number,
141-
"io" => LuaType::Io,
142-
"self" => LuaType::SelfInfer,
143-
"global" => LuaType::Global,
144-
"function" => LuaType::Function,
145-
_ => LuaType::Ref(LuaTypeDeclId::global(&name)),
146-
}
147-
}
148-
149129
pub fn tpl_pattern_match(
150130
context: &mut TplContext,
151131
pattern: &LuaType,
@@ -169,11 +149,37 @@ pub fn tpl_pattern_match(
169149
let prefix = str_tpl.get_prefix();
170150
let suffix = str_tpl.get_suffix();
171151
let type_name = SmolStr::new(format!("{}{}{}", prefix, s, suffix));
172-
context.substitutor.infer_type(
173-
str_tpl.get_tpl_id(),
174-
get_str_tpl_infer_type(&type_name),
175-
true,
176-
);
152+
let file_id = context.cache.get_file_id();
153+
let inferred_type = match type_name.as_str() {
154+
"unknown" => LuaType::Unknown,
155+
"never" => LuaType::Never,
156+
"nil" | "void" => LuaType::Nil,
157+
"any" => LuaType::Any,
158+
"userdata" => LuaType::Userdata,
159+
"thread" => LuaType::Thread,
160+
"boolean" | "bool" => LuaType::Boolean,
161+
"string" => LuaType::String,
162+
"integer" | "int" => LuaType::Integer,
163+
"number" => LuaType::Number,
164+
"io" => LuaType::Io,
165+
"self" => LuaType::SelfInfer,
166+
"global" => LuaType::Global,
167+
"function" => LuaType::Function,
168+
_ => context
169+
.db
170+
.get_type_index()
171+
.find_type_decl(
172+
file_id,
173+
&type_name,
174+
context.db.resolve_workspace_id(file_id),
175+
)
176+
.map(|decl| LuaType::Ref(decl.get_id()))
177+
.unwrap_or(LuaType::Ref(LuaTypeDeclId::global(&type_name))),
178+
};
179+
180+
context
181+
.substitutor
182+
.infer_type(str_tpl.get_tpl_id(), inferred_type, true);
177183
}
178184
}
179185
LuaType::Array(array_type) => {

0 commit comments

Comments
 (0)