Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shrinker-test examples with never-referenced constructors #2478

Merged

Conversation

sfreilich
Copy link
Contributor

R8 can do some optimizations that assume values of types whose constructors are never referenced are null. (A bit confused why the rules here are sufficient to avoid that optimization in the case of a class whose sole constructor has arguments and is unreferenced, since the keep rule only refers to <init>() instead of <init>(...). Does R8 have special behavior for <init>() where attempting to keep that makes it assume a class is constructable even if it doesn't have a no-args constructor?)

Also rename some test classes/names to refer to NoArgs instead of Default constructors. The examples here care about whether a no-argument constructor is present. While a no-argument constructor (explicitly defined or not) is used by default in some circumstances, the Java documentation consistently uses "default constructor" to refer only to constructors whose implementation is implicitly provided by the complier (all default constructors are no-argument constructors, but not vice versa).

R8 can do some optimizations that assume values of types whose
constructors are never referenced are `null`. (A bit confused why
the rules here are sufficient to avoid that optimization in the
case of a class whose sole constructor has arguments and is
unreferenced, since the keep rule only refers to `<init>()` instead
of `<init>(...)`. Does R8 have special behavior for `<init>()` where
attempting to keep that makes it assume a class is constructable even
if it doesn't have a no-args constructor?)

Also rename some test classes/names to refer to `NoArgs` instead of
`Default` constructors. The examples here care about whether a
no-argument constructor is present. While a no-argument
constructor (explicitly defined or not) is used by default in some
circumstances, the Java documentation consistently uses "default
constructor" to refer only to constructors whose implementation is
implicitly provided by the complier (all default constructors are
no-argument constructors, but not vice versa).
Copy link
Collaborator

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot! The changes look good, just a few small things (see comments below).

R8 can do some optimizations that assume values of types whose constructors are never referenced are null

I think by accident I triggered this for ProGuard while initially creating the shrinker-tests, respectively I triggered the cryptic NullPointerException this is causing (with JVM's helpful NullPointerException messages it says something like 'Cannot throw exception because "null" is null'). Though I am not sure if I still have the code to cause this, and if it would still cause this with the latest gson.pro file.

A bit confused why the rules here are sufficient to avoid that optimization...

I am not sure either, but I am also not very familiar with the details of R8.

Also rename some test classes/names to refer to NoArgs instead of Default constructors

Good point! You are right, the JLS seems to use the term "default constructor" only for the implicit one. I was mixing the terms a bit inconsistently.

Copy link
Collaborator

@Marcono1234 Marcono1234 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the changes, they look good to me!

@eamonnmcmanus eamonnmcmanus merged commit ad5cd31 into google:main Sep 5, 2023
9 checks passed
@Marcono1234 Marcono1234 added the proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation label Mar 29, 2024
tibor-universe pushed a commit to getuniverse/gson that referenced this pull request Sep 14, 2024
…#2478)

* Add shrinker-test examples with never-referenced constructors

R8 can do some optimizations that assume values of types whose
constructors are never referenced are `null`. (A bit confused why
the rules here are sufficient to avoid that optimization in the
case of a class whose sole constructor has arguments and is
unreferenced, since the keep rule only refers to `<init>()` instead
of `<init>(...)`. Does R8 have special behavior for `<init>()` where
attempting to keep that makes it assume a class is constructable even
if it doesn't have a no-args constructor?)

Also rename some test classes/names to refer to `NoArgs` instead of
`Default` constructors. The examples here care about whether a
no-argument constructor is present. While a no-argument
constructor (explicitly defined or not) is used by default in some
circumstances, the Java documentation consistently uses "default
constructor" to refer only to constructors whose implementation is
implicitly provided by the complier (all default constructors are
no-argument constructors, but not vice versa).

* Update shrinker-test/src/main/java/com/example/ClassWithNoArgsConstructor.java

Accept review suggestion

Co-authored-by: Marcono1234 <[email protected]>

* Update shrinker-test/src/main/java/com/example/NoSerializedNameMain.java

Accept review suggestion

Co-authored-by: Marcono1234 <[email protected]>

* Further adjust test class/method names for consistency

* Update shrinker-test/src/main/java/com/example/NoSerializedNameMain.java

Co-authored-by: Marcono1234 <[email protected]>

---------

Co-authored-by: Marcono1234 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants