Fix dangling definitions after document deletion#710
Merged
Conversation
1a6553a to
e5e956a
Compare
vinistock
approved these changes
Apr 1, 2026
Member
vinistock
left a comment
There was a problem hiding this comment.
The overall PR looks good, but let's please avoid exposing mutable state in a generic way
Comment on lines
+969
to
+979
| // Build a set of definition IDs being removed so we can detach them from declarations. | ||
| let removed_def_ids: IdentityHashSet<DefinitionId> = document.definitions().iter().copied().collect(); | ||
|
|
||
| // Detach removed definitions from their declarations. We iterate all declarations | ||
| // rather than trying to map definitions → declarations (which is lossy for methods, | ||
| // instance variables, etc. that `definition_to_declaration_id` can't always resolve). | ||
| for declaration in self.declarations.values_mut() { | ||
| declaration | ||
| .definitions_mut() | ||
| .retain(|def_id| !removed_def_ids.contains(def_id)); | ||
| } |
Member
There was a problem hiding this comment.
I'm not a fan of exposing generic mutable APIs from our structs. Would it be possible to do this (especially since this API already exists)?
// May need to copy due to borrow checks
for def_id in document.definitions() {
declaration.remove_definition(def_id);
}363f9f0 to
ebbaf7f
Compare
When remove_document_data removes definitions from self.definitions, declarations still reference them via their definition_ids list. The resolver later panics when trying to access the removed definition. Add retain_definitions sweep in remove_document_data to detach definitions from ALL declarations before removing them from the definitions map.
ebbaf7f to
4473183
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Found by the incremental consistency testing tool.
remove_document_dataremoves definitions from the definitions map, declarations still reference them via theirdefinition_idslist. The resolver later panics when trying to access the removed definition.pending_detachments, scan for the remainder thatdefinition_to_declaration_idcan't resolve (e.g. methods insideclass << selfwhen the singleton name was unresolved, instance variables in class body owned by the singleton, or definitions whose enclosing namespace name chain is broken) and detach those by sweeping declarations.