Fix Double prop defaults losing precision in generated Android delegates - #58070
Open
dennytosp wants to merge 1 commit into
Open
Fix Double prop defaults losing precision in generated Android delegates#58070dennytosp wants to merge 1 commit into
dennytosp wants to merge 1 commit into
Conversation
GeneratePropsJavaDelegate emitted the default for an optional DoubleTypeAnnotation prop with a 'f' (float) suffix, while the setter it calls takes a 'double'. The float literal is widened back to double at the call site, so the default silently arrives rounded to float precision.
|
@fabriziocucci has imported this pull request. If you are a Meta employee, you can view this in D117082006. |
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:
GeneratePropsJavaDelegateemits the default value for an optionalDoubleTypeAnnotationprop with anf(float) suffix, but the setter it calls takes adouble:The
fliteral is parsed as afloat, then widened back todoubleat the call site, so the default silently arrives rounded to float precision. It compiles without a warning, which is why it has gone unnoticed.This is visible in the repo's own snapshots today. Same fixture, same prop, two platforms:
blurRadius3?: WithDefault<Double, 2.1>GeneratePropsH(C++)double blurRadius3{2.1};GeneratePropsJavaDelegatevalue == null ? 2.1f : ((Double) value).doubleValue()So a component using the C++ renderer gets
2.1and the same component on the Android delegate path gets2.0999999046325684.Compiling the generated shape confirms it:
The integer case is the clearest damage: any
Doubledefault above 2^24 is not representable as afloat, so123456789arrives as123456792. Fractional defaults are wrong from the first value that is not a dyadic rational —0.1,2.1, and the fixture's own0.001all shift.FloatTypeAnnotationon the line below is correct — it targets afloatsetter, sofis right there. Only theDoublebranch has the wrong suffix, which is consistent with it having been copied from theFloatbranch.Changed to a
dsuffix, mirroring the existingffor float rather than dropping the suffix entirely, so the literal's type is stated rather than left to numeric promotion.DoubleTypeAnnotationis never boxed by codegen (it is always a primitivedouble, defaulting toDouble.NaNwhen non-optional), so there is no nullable-double path to consider.Changelog:
[ANDROID] [FIXED] - Fix
Doubleprop defaults being rounded to float precision in generatedViewManagerdelegatesTest Plan:
The generator is snapshot-tested, so the snapshot is the test. Updating it changes only the
Doublecomponent and leaves theFloatcomponent untouched:public class DoublePropNativeComponentManagerDelegate<T extends View, U extends ... case "blurRadius2": - mViewManager.setBlurRadius2(view, value == null ? 0.001f : ((Double) value).doubleValue()); + mViewManager.setBlurRadius2(view, value == null ? 0.001d : ((Double) value).doubleValue()); case "blurRadius3": - mViewManager.setBlurRadius3(view, value == null ? 2.1f : ((Double) value).doubleValue()); + mViewManager.setBlurRadius3(view, value == null ? 2.1d : ((Double) value).doubleValue()); case "blurRadius4": - mViewManager.setBlurRadius4(view, value == null ? 0f : ((Double) value).doubleValue()); + mViewManager.setBlurRadius4(view, value == null ? 0d : ((Double) value).doubleValue()); case "blurRadius5": - mViewManager.setBlurRadius5(view, value == null ? 1f : ((Double) value).doubleValue()); + mViewManager.setBlurRadius5(view, value == null ? 1d : ((Double) value).doubleValue()); case "blurRadius6": - mViewManager.setBlurRadius6(view, value == null ? 0f : ((Double) value).doubleValue()); + mViewManager.setBlurRadius6(view, value == null ? 0d : ((Double) value).doubleValue());0d,1dand2.1dare valid Java double literals; the proof snippet above compiles and runs underjava Proof.java.No committed generated sources carry the old output —
grep -rn "f : ((Double) value).doubleValue()" --include='*.java' --include='*.kt'returns nothing outside the snapshot.