Skip to content

Introduce optimistic default#1386

Open
aosen-xiong wants to merge 13 commits intoeisop:masterfrom
aosen-xiong:optimistic-default
Open

Introduce optimistic default#1386
aosen-xiong wants to merge 13 commits intoeisop:masterfrom
aosen-xiong:optimistic-default

Conversation

@aosen-xiong
Copy link
Copy Markdown
Collaborator

Fixes #1359.

I disable the JSpecify reference checker CI because the method name are changing. I will enable and make the change at there (if necessary) and enable the CI.

Copy link
Copy Markdown
Member

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

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

@aosen-xiong @thisisalexandercook We had discussed this PR in the past. Let's go through this and related PRs next week and decide which direction to go.

* @param annotationScope the element that the conservative default might apply to
* @return whether the conservative default applies to the given element
*/
public boolean applyOptimisticDefaults(Element annotationScope) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Most of this code is just copy-and-paste of applyConservativeDefaults. Can you find a way to share more of the common logic?

if (applyConservativeDefaults(annotationScope)) {
for (Default def : uncheckedCodeDefaults) {
for (Default def : conservativeUncheckedCodeDefaults) {
if (!typeVarUseDef || def.location != TypeUseLocation.TYPE_VARIABLE_USE) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This also duplicates the logic in the else branch.
Can you first set which defaults to use and then iterate over that set once?

}
}

for (TypeUseLocation loc : CONSERVATIVE_UNCHECKED_DEFAULTS_TOP) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why iterate over all these sets? Won't most of these conflict with defaults that were already set above?
I think this logic needs some cleaning up.

*/
protected void addUncheckedStandardDefaults(QualifierDefaults defs) {
defs.addUncheckedStandardDefaults();
protected void addUncheckedDefaults(QualifierDefaults defs) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why rename this method? It will still set the standard defaults. What changed?

* @param kindOfCode source or bytecode
* @return whether optimistic defaults should be used
*/
public boolean useOptimisticDefault(String kindOfCode) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Lots of duplicated code.

"checkEnclosingExpr",

// Whether to use optimistic defaults for bytecode and/or source code.
// The option takes same arguments as "useConservativeDefaultsForUncheckedCode".
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The ordering of the options should be switched, instead of having a forward reference here.


The new command-line option `-AuseOptimisticDefaultsForUncheckedCode` takes `source` and `bytecode` argument, similar to
`-AuseConservativeDefaultsForUnCheckedCode` but apply to optimistic default, that is, Top for method parameter type and
Bottom for method return and field type.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We also need to go through the manual and discuss this new option there.

@wmdietl wmdietl assigned aosen-xiong and unassigned wmdietl Mar 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce optimistic default

2 participants