#4118 Fix collections no longer copied for JSpecify @NonNull sources - #4119
Open
adityaanikam wants to merge 1 commit into
Open
adityaanikam wants to merge 1 commit into
adityaanikam wants to merge 1 commit into
Conversation
…sources CollectionAssignmentBuilder.setterWrapperNeedsSourceNullCheck() returned false whenever the source was JSpecify @nonnull, skipping the whole SetterWrapperForCollectionsAndMapsWithNullCheck wrapper. That wrapper is what generates the copy-constructor call (new ArrayList<>(...)) for a direct getter-to-setter assignment; skipping it did not just drop the redundant null check, it dropped the defensive copy along with it, so copy() started returning a target sharing the source's collection instance instead of a mutable copy. Decouple the two concerns: direct assignments still always route through SetterWrapperForCollectionsAndMapsWithNullCheck (so the copy constructor is always generated), and a new skipNullCheck flag on that wrapper suppresses only the redundant "if ( x != null )" runtime check for statically non-null sources. The local variable and copy-constructor wrapping happen either way. Updated the JSpecifyCollectionPropertyMapperImpl fixture (used by the existing JSpecifyCollectionPropertyTest) to the corrected generated code, and confirmed with a negative control that reverting only the source fix reproduces the exact original bug in that same fixture comparison. Ran the full nullcheck and collection test packages (334 tests) with no regressions.
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.
Fixes #4118.
CollectionAssignmentBuilder.setterWrapperNeedsSourceNullCheck()returnedfalsewhenever the source was JSpecify@NonNull, skipping the wholeSetterWrapperForCollectionsAndMapsWithNullCheckwrapper. That wrapper is what generates the copy-constructor call (new ArrayList<>(...)) for a direct getter-to-setter assignment, so skipping it did not just drop the redundant null check, it dropped the defensive copy along with it:copy()started returning a target sharing the source's collection instance instead of a mutable copy, contradicting the documented collection-copy behavior.This decouples the two concerns: direct assignments still always route through
SetterWrapperForCollectionsAndMapsWithNullCheck(so the copy constructor is always generated), and a newskipNullCheckflag on that wrapper suppresses only the redundantif ( x != null )runtime check for statically non-null sources. The local variable and copy-constructor wrapping happen either way.Testing
JSpecifyCollectionPropertyMapperImplfixture (used by the existingJSpecifyCollectionPropertyTest) to the corrected generated code.nullcheckandcollectiontest packages (334 tests) with no regressions.