fix: scope field extraction to target class to prevent cross-class injection#1953
Open
mashraf-222 wants to merge 3 commits intomainfrom
Open
fix: scope field extraction to target class to prevent cross-class injection#1953mashraf-222 wants to merge 3 commits intomainfrom
mashraf-222 wants to merge 3 commits intomainfrom
Conversation
…jection find_fields() was called without a class_name filter, causing fields from inner/anonymous classes to be injected into the outer target class. Now scoped to target_method.class_name using the existing filter parameter. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Contributor
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
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.
Problem
When the LLM generates optimization code containing inner/anonymous classes with fields, those fields are incorrectly injected into the target method's enclosing class. This produces uncompilable code — e.g., type parameters like
Iterator<? extends F>injected whereFis not in scope.Observed in Guava PR #12 (
Iterables.mergeSorted) where inner class fields were injected into the outerIterablesclass.Root Cause
_parse_optimization_source()inreplacement.py(line 72) calledanalyzer.find_fields(new_source)without aclass_namefilter, extracting ALL fields from ALL classes in the generated code — including inner classes, static nested classes, and anonymous classes.Fix
Pass
class_name=target_method.class_nametofind_fields()so only fields belonging to the target method's class are extracted. Thefind_fields()API already supported this filter via its_walk_tree_for_fields()implementation — it just wasn't being used.The field extraction was also moved after the target method lookup (previously it ran before we knew which class the target was in).
Validation
badField→ it was injected into outer classbadFieldno longer injected, onlyOFFSET(same class as target) is addedTest Coverage
tests/test_languages/test_java/test_replacement.py—TestFieldInjectionClassFiltering::test_inner_class_fields_not_injected_into_outerCloses CF-1087