From e4a68a36786e8de61cab1959b07027bd1cf45818 Mon Sep 17 00:00:00 2001 From: "Aleksei (Faumaray) Selikhov" Date: Sun, 6 Dec 2026 04:23:39 +0300 Subject: [PATCH] Distinguish definition tokens from declarations The stock observer reports targetKind inside each proven target span, the decoder carries it, and semantic tokens apply the legend's definition modifier instead of always declaring. The observer verifier validates the field and the definition span parser ignores it as envelope metadata. A real stdio session showed modifier 2 on the factory definitions. --- docs/agent-workflow/LSP-PARITY.md | 2 +- docs/agent-workflow/VERIFICATION.md | 15 +++++++++++++++ experiments/frontend/gcc-observer/Observer.cpp | 3 ++- include/vls/features/SemanticTokens.hpp | 1 + include/vls/semantic/Occurrences.hpp | 2 ++ src/Server.cpp | 3 ++- src/semantic/EditorDiagnostics.cpp | 4 +++- src/semantic/Occurrences.cpp | 12 +++++++++++- tests/corpus/frontend/verify-stock-observer.py | 4 ++++ 9 files changed, 41 insertions(+), 5 deletions(-) diff --git a/docs/agent-workflow/LSP-PARITY.md b/docs/agent-workflow/LSP-PARITY.md index 73bc575..586d737 100644 --- a/docs/agent-workflow/LSP-PARITY.md +++ b/docs/agent-workflow/LSP-PARITY.md @@ -41,7 +41,7 @@ missing part. Updated December 5, 2026 after semantic tokens and inlay hints. | callHierarchy / typeHierarchy | prepare, incomingCalls and outgoingCalls are implemented and served; outgoing calls are verified over stdio now that the observer reports full function extents. Type hierarchy is open. | | Client-side application of server-issued workspace edits | Neovim and VS Code both apply `changes` and versioned `documentChanges` | | Applying multi-file renames through client buffers | plans can be ready; application is client work | -| richer token types | legend has `function` and `type` (class, base, definition names) plus a `declaration` modifier; more types need further compiler spans | +| richer token types | legend has `function` and `type` (class, base, definition names) plus `declaration`/`definition` modifiers; more types need further compiler spans | ## Next slices diff --git a/docs/agent-workflow/VERIFICATION.md b/docs/agent-workflow/VERIFICATION.md index 90c2626..7a25874 100644 --- a/docs/agent-workflow/VERIFICATION.md +++ b/docs/agent-workflow/VERIFICATION.md @@ -61,6 +61,21 @@ start coordinate only, so ranges are zero-width until an end is available. LSP adapters and index/rename/debugging integration remain open. `vbs test` passed all 17 native suites; CMake passed the reflection and trace suites. +## December 6 definition modifier from the observer target kind + +The stock observer now emits `targetKind` ("definition" or "declaration") inside +each proven target span. The occurrence decoder carries it as +`target_is_definition`, and semantic tokens use the legend's `definition` +modifier for definition-name tokens instead of always `declaration`. The +observer verifier validates the field; real stdio session over the +`Base`/`DerivedA`/`DerivedB` fixture: +``` +semanticTokens: [(0, 7, 4, 1, 0), (1, 7, 8, 1, 0), (2, 7, 8, 1, 0), + (3, 9, 6, 0, 2), (4, 9, 6, 0, 2), (5, 22, 6, 0, 0), (5, 41, 6, 0, 0)] +``` +The two factory definitions now carry modifier 2. All 18 native suites and all +18 CMake tests passed. + ## December 6 index snapshot schema 4 The symbol-index snapshot is now schema 4 and records `digestAlgorithm` inside diff --git a/experiments/frontend/gcc-observer/Observer.cpp b/experiments/frontend/gcc-observer/Observer.cpp index e4664da..27a87b6 100644 --- a/experiments/frontend/gcc-observer/Observer.cpp +++ b/experiments/frontend/gcc-observer/Observer.cpp @@ -874,7 +874,8 @@ void finish(void *, void *) { std::fprintf(out, ",\"line\":%u,\"startByte\":%u,\"endByte\":%u},\"definition\":", use.where.line, use.where.start, use.where.end); if (known) { - std::fputs("{\"path\":", out); + std::fprintf(out, "{\"targetKind\":\"%s\",\"path\":", + body->definition ? "definition" : "declaration"); json_string(out, body->path); std::fprintf(out, ",\"line\":%u,\"startByte\":%u,\"endByte\":%u", body->where.line, body->where.start, body->where.end); diff --git a/include/vls/features/SemanticTokens.hpp b/include/vls/features/SemanticTokens.hpp index d5ff980..3d27cc6 100644 --- a/include/vls/features/SemanticTokens.hpp +++ b/include/vls/features/SemanticTokens.hpp @@ -13,6 +13,7 @@ namespace vls::features::semantic_tokens { inline constexpr std::uint32_t function_type = 0; // Bit flag in Token::modifiers, matching the server's advertised legend. inline constexpr std::uint32_t declaration_modifier = 1; +inline constexpr std::uint32_t definition_modifier = 2; // Second legend entry: a compiler-proven type name. inline constexpr std::uint32_t type_type = 1; diff --git a/include/vls/semantic/Occurrences.hpp b/include/vls/semantic/Occurrences.hpp index 8fa39de..ed02da8 100644 --- a/include/vls/semantic/Occurrences.hpp +++ b/include/vls/semantic/Occurrences.hpp @@ -24,6 +24,8 @@ struct Occurrence { std::optional extent; // Uid of the reported class type of this call's result, when present. std::uint32_t result_type_uid{}; + // The resolved target is a definition (not merely a declaration). + bool target_is_definition{}; [[nodiscard]] bool has_definition() const noexcept { return definition_file != nullptr; } }; diff --git a/src/Server.cpp b/src/Server.cpp index d17f54e..d40821b 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -362,7 +362,8 @@ DispatchResult Server::handle(std::string_view body) { Json::Object legend{ {"tokenTypes", Json{Json::Array{Json{std::string{"function"}}, Json{std::string{"type"}}}}}, - {"tokenModifiers", Json{Json::Array{Json{std::string{"declaration"}}}}}}; + {"tokenModifiers", Json{Json::Array{Json{std::string{"declaration"}}, + Json{std::string{"definition"}}}}}}; Json::Object semantic{{"legend", Json{std::move(legend)}}, {"full", Json{true}}}; capabilities.emplace("semanticTokensProvider", Json{std::move(semantic)}); } diff --git a/src/semantic/EditorDiagnostics.cpp b/src/semantic/EditorDiagnostics.cpp index b431237..7f427b4 100644 --- a/src/semantic/EditorDiagnostics.cpp +++ b/src/semantic/EditorDiagnostics.cpp @@ -1413,7 +1413,9 @@ Json EditorDiagnostics::semantic_tokens(std::string_view uri) const { {occurrence.definition.start.line, occurrence.definition.start.character, occurrence.definition.end.character - occurrence.definition.start.character, features::semantic_tokens::function_type, - features::semantic_tokens::declaration_modifier}); + occurrence.target_is_definition + ? features::semantic_tokens::definition_modifier + : features::semantic_tokens::declaration_modifier}); } if (types) for (const auto &type : *types) { diff --git a/src/semantic/Occurrences.cpp b/src/semantic/Occurrences.cpp index cd9bfc7..c96302f 100644 --- a/src/semantic/Occurrences.cpp +++ b/src/semantic/Occurrences.cpp @@ -287,8 +287,18 @@ decode_occurrences(std::string_view report, std::string_view nonce, occurrence.use_file = use->first; occurrence.use = use->second; const auto *definition = P::find_member(*fields, "definition"); + const auto *definition_object = definition ? P::as_object(definition) : nullptr; + const auto *target_kind = + definition_object ? P::as_string(P::find_member(*definition_object, "targetKind")) + : nullptr; if (definition && !std::holds_alternative(definition->value)) { - const auto definition_span = span(definition); + if (target_kind && *target_kind != "definition" && *target_kind != "declaration") + return std::unexpected{OccurrenceError::invalid_report}; + occurrence.target_is_definition = target_kind && *target_kind == "definition"; + // targetKind is envelope metadata, not part of the byte span. + Json normalized{*definition}; + std::get(normalized.value).erase("targetKind"); + const auto definition_span = span(&normalized); if (!definition_span) return std::unexpected{OccurrenceError::invalid_report}; if (captured(capture, definition_span->path)) { diff --git a/tests/corpus/frontend/verify-stock-observer.py b/tests/corpus/frontend/verify-stock-observer.py index 947f82c..1400d67 100755 --- a/tests/corpus/frontend/verify-stock-observer.py +++ b/tests/corpus/frontend/verify-stock-observer.py @@ -61,6 +61,10 @@ def compile(compiler, observer, work, main, flags=()): def read_span(span, name, label): + if isinstance(span, dict) and "targetKind" in span: + check(span["targetKind"] in ("definition", "declaration"), + f"invalid {label} targetKind {span!r}") + span = {key: value for key, value in span.items() if key != "targetKind"} check(isinstance(span, dict) and set(span) in ({"path", "line", "startByte", "endByte"}, {"path", "line", "startByte", "endByte", "extent"}),