From 3690b3a064739644f864582fd6bc101423b2e06d Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Sat, 24 Oct 2026 02:53:15 +0200 Subject: [PATCH 1/3] test(check): `Partial` literals accept the fields of `T` (#1318) ```lua ---@type Partial local b = { cmd = 'x' } -- was: Field `cmd` cannot be injected ``` Upstream main (1b6df859) accepts these literals without a code change here (the test passes on it; `expand_mapped` keeps the `?` of mapped fields since #1316). This commit only pins both shapes, `Partial` itself and a class inheriting it (`---@class Sub: Partial`), for inject-field and missing-fields. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01923KEUKXBrPZHHVvqPVpA1 Claude-Session: https://claude.ai/code/session_018NxUURS1MVzowfNDjH6GGy --- .../src/check/test/inject_field_test.rs | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs b/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs index 9fe0756d1..71b9f847f 100644 --- a/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs @@ -1045,3 +1045,33 @@ mod dictionary_constructor { } } } +#[cfg(test)] +mod test_issue_1318 { + use crate::{DiagnosticCode, VirtualWorkspace}; + + const LINTER: &str = r#" + ---@class Linter + ---@field cmd string the command to run + ---@field stdin boolean pass content via stdin + "#; + + /// #1318: a literal typed `Partial` (or a class inheriting it) may set any field of `T`. + #[test] + fn test_partial_literal_accepts_fields_of_t() { + let mut ws = VirtualWorkspace::new_with_init_std_lib(); + ws.def_file("linter.lua", LINTER); + for code in [ + "---@type Partial\nlocal b = { cmd = 'x' }", + "---@class SubPartial: Partial\n---@field extra? integer\n\n---@type SubPartial\nlocal a = { cmd = 'x' }", + ] { + assert!( + ws.has_no_diagnostic(DiagnosticCode::InjectField, code), + "{code}" + ); + assert!( + ws.has_no_diagnostic(DiagnosticCode::MissingFields, code), + "{code}" + ); + } + } +} From 2da86c115eb07421fd4174f5598a36ab9a47aa2f Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Sat, 24 Oct 2026 02:57:29 +0200 Subject: [PATCH 2/3] feat(semantic): `Partial` fields keep `T`'s declaration and docs (#1318) Hover and completion on a `Partial` field showed only the type (`string?`): the mapped alias expands to an anonymous `Object`, so the field had no member declaration. For a mapped alias over `keyof X` (`{ [P in keyof T]?: T[P] }`), member lookup now attaches `X`'s member as the field's declaration while the field keeps its mapped type. Hover shows `(field) cmd: string?` with `T`'s description; completion resolve shows the description too. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01923KEUKXBrPZHHVvqPVpA1 --- .../src/semantic_model/access.rs | 10 ++++ .../src/semantic_model/member.rs | 48 ++++++++++++++++++- .../handlers/test/completion_resolve_test.rs | 25 ++++++++++ .../src/handlers/test/hover_test.rs | 21 ++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) diff --git a/crates/emmylua_code_analysis/src/semantic_model/access.rs b/crates/emmylua_code_analysis/src/semantic_model/access.rs index 1d03df6c4..1feb9f255 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/access.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/access.rs @@ -368,6 +368,16 @@ impl<'db> SemanticModel<'db> { let LuaType::Generic(generic) = &prefix_ty else { return None; }; + // A field of an alias instance (`Partial`) has the alias's own (mapped) type, + // `string?`, not the source member's `string`. + if self + .type_def_of(generic.get_base_type_id_ref()) + .is_some_and(|def| def.kind == TypeDefKind::Alias) + { + let name = index_expr.get_index_name_token()?; + let key = LuaMemberKey::Name(name.text().into()); + return self.member_info(&prefix_ty, &key).map(|info| info.typ); + } let member_type = self.type_of_member(member_id)?; let names = self .type_def_of(generic.get_base_type_id_ref()) diff --git a/crates/emmylua_code_analysis/src/semantic_model/member.rs b/crates/emmylua_code_analysis/src/semantic_model/member.rs index 516af6715..7ebd99574 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/member.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/member.rs @@ -228,10 +228,17 @@ fn direct_member_info( LuaType::Generic(generic) => { let def = model.type_def_of(&generic.get_base_type_id())?; if def.kind == TypeDefKind::Alias { - return with_expanded_alias(model, prefix_type, |expanded| { + let mut info = with_expanded_alias(model, prefix_type, |expanded| { direct_member_info(model, expanded, key) }) - .flatten(); + .flatten()?; + if info.id.is_none() + && let Some(source) = mapped_alias_source_member(model, &def, generic, key) + { + info.id = source.id; + info.file_id = source.file_id; + } + return Some(info); } let bindings: TplBindings = generic .get_params() @@ -296,6 +303,33 @@ fn direct_member_info( } } +/// For a mapped alias over `keyof X` (`Partial` is `{ [P in keyof T]?: T[P] }`), the member of +/// `X` that a produced field comes from. The field keeps its own (mapped) type, but takes the +/// source declaration, so hover and completion show `X`'s field docs. +fn mapped_alias_source_member( + model: &SemanticModel, + def: &TypeDef, + generic: &crate::LuaGenericType, + key: &LuaMemberKey, +) -> Option { + let LuaType::Mapped(mapped) = model.alias_target(def)? else { + return None; + }; + let LuaType::Call(call) = mapped.param.1.constraint.as_ref()? else { + return None; + }; + if call.get_call_kind() != crate::LuaAliasCallKind::KeyOf { + return None; + } + let names: Vec = def.generic_params.iter().map(|p| p.name.clone()).collect(); + let source = model.substitute_generic_params_named( + call.get_operands().first()?, + generic.get_params(), + &names, + ); + member_info(model, &source, key).filter(|info| info.id.is_some()) +} + pub(crate) fn member_info_impl( model: &SemanticModel, prefix_type: &LuaType, @@ -641,9 +675,19 @@ fn collect_members( if let Some(def) = model.type_def_of(&generic.get_base_type_id()) && def.kind == TypeDefKind::Alias { + let start = out.len(); with_expanded_alias(model, prefix_type, |expanded| { collect_members(model, expanded, None, None, visited, out, filter); }); + for info in &mut out[start..] { + if info.id.is_none() + && let Some(source) = + mapped_alias_source_member(model, &def, generic, &info.key) + { + info.id = source.id; + info.file_id = source.file_id; + } + } return; } let def = model.type_def_of(&generic.get_base_type_id()); diff --git a/crates/emmylua_ls/src/handlers/test/completion_resolve_test.rs b/crates/emmylua_ls/src/handlers/test/completion_resolve_test.rs index fe4a445b0..a18f56e36 100644 --- a/crates/emmylua_ls/src/handlers/test/completion_resolve_test.rs +++ b/crates/emmylua_ls/src/handlers/test/completion_resolve_test.rs @@ -158,4 +158,29 @@ mod tests { )); Ok(()) } + + /// #1318: completing a `Partial` field shows `T`'s field docs. Resolve renders the + /// declaration site, so the detail is `T`'s declared field type. + #[gtest] + fn test_partial_field_keeps_source_docs() -> Result<()> { + let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib(); + ws.def( + r#" + ---@class Linter + ---@field cmd string the command to run + "#, + ); + check!(ws.check_completion_resolve( + r#" + ---@type Partial + local p = {} + p. + "#, + VirtualCompletionResolveItem { + detail: "(field) cmd: string".to_string(), + documentation: Some("\nthe command to run".to_string()), + }, + )); + Ok(()) + } } diff --git a/crates/emmylua_ls/src/handlers/test/hover_test.rs b/crates/emmylua_ls/src/handlers/test/hover_test.rs index 28727db27..a0bc1a433 100644 --- a/crates/emmylua_ls/src/handlers/test/hover_test.rs +++ b/crates/emmylua_ls/src/handlers/test/hover_test.rs @@ -955,4 +955,25 @@ mod tests { }; assert!(content.value.contains("a: integer"), "{}", content.value); } + + /// #1318: a field read through `Partial` hovers with `T`'s declaration and docs. + #[gtest] + fn test_hover_partial_field_keeps_source_docs() -> Result<()> { + let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib(); + check!(ws.check_hover( + r#" + ---@class Linter + ---@field cmd string the command to run + ---@field stdin boolean pass content via stdin + + ---@type Partial + local p = {} + print(p.cmd) + "#, + VirtualHoverResult { + value: "```lua\n(field) cmd: string?\n```\n\n---\n\nthe command to run".to_string(), + }, + )); + Ok(()) + } } From 9b307e9a8165b439292f39583de813cfea981888 Mon Sep 17 00:00:00 2001 From: Marek Langiewicz Date: Sat, 24 Oct 2026 11:00:00 +0200 Subject: [PATCH 3/3] refactor(semantic): fall through for dynamic `Partial` index; tidy imports `p[k]` on an alias instance has no name token; keep the generic projection path for it instead of returning `None`. Import `LuaGenericType` and `LuaAliasCallKind` like the rest of member.rs. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014NAm41TqAcVsDEzkrkkEDG --- .../src/check/test/inject_field_test.rs | 1 + crates/emmylua_code_analysis/src/semantic_model/access.rs | 2 +- crates/emmylua_code_analysis/src/semantic_model/member.rs | 8 +++++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs b/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs index 71b9f847f..185da1712 100644 --- a/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs +++ b/crates/emmylua_code_analysis/src/check/test/inject_field_test.rs @@ -1045,6 +1045,7 @@ mod dictionary_constructor { } } } + #[cfg(test)] mod test_issue_1318 { use crate::{DiagnosticCode, VirtualWorkspace}; diff --git a/crates/emmylua_code_analysis/src/semantic_model/access.rs b/crates/emmylua_code_analysis/src/semantic_model/access.rs index 1feb9f255..528b7f9d2 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/access.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/access.rs @@ -373,8 +373,8 @@ impl<'db> SemanticModel<'db> { if self .type_def_of(generic.get_base_type_id_ref()) .is_some_and(|def| def.kind == TypeDefKind::Alias) + && let Some(name) = index_expr.get_index_name_token() { - let name = index_expr.get_index_name_token()?; let key = LuaMemberKey::Name(name.text().into()); return self.member_info(&prefix_ty, &key).map(|info| info.typ); } diff --git a/crates/emmylua_code_analysis/src/semantic_model/member.rs b/crates/emmylua_code_analysis/src/semantic_model/member.rs index 7ebd99574..75dd85f2d 100644 --- a/crates/emmylua_code_analysis/src/semantic_model/member.rs +++ b/crates/emmylua_code_analysis/src/semantic_model/member.rs @@ -6,7 +6,9 @@ use hashbrown::HashMap; use crate::semantic_db::def::{DeclKind, MemberRef, SemanticId, TypeDef, TypeDefKind}; use crate::semantic_model::infer::unify::{self, TplBindings}; use crate::semantic_model::type_eval::{map_type, substitute_named_refs, with_expanded_alias}; -use crate::{FileId, GenericTplId, InFiled, LuaMemberKey, LuaType}; +use crate::{ + FileId, GenericTplId, InFiled, LuaAliasCallKind, LuaGenericType, LuaMemberKey, LuaType, +}; use crate::{Member, semantic_db::facts::FileFacts}; use smol_str::SmolStr; @@ -309,7 +311,7 @@ fn direct_member_info( fn mapped_alias_source_member( model: &SemanticModel, def: &TypeDef, - generic: &crate::LuaGenericType, + generic: &LuaGenericType, key: &LuaMemberKey, ) -> Option { let LuaType::Mapped(mapped) = model.alias_target(def)? else { @@ -318,7 +320,7 @@ fn mapped_alias_source_member( let LuaType::Call(call) = mapped.param.1.constraint.as_ref()? else { return None; }; - if call.get_call_kind() != crate::LuaAliasCallKind::KeyOf { + if call.get_call_kind() != LuaAliasCallKind::KeyOf { return None; } let names: Vec = def.generic_params.iter().map(|p| p.name.clone()).collect();