Skip to content

Ensure AttrContext.returnResult's checkContext is set to Check.basicHandler in javac's Scopes, to avoid it throwing exceptions.#8016

Merged
lahodaj merged 1 commit intoapache:masterfrom
lahodaj:ensure-checkContext-is-basicHandler-in-Scopes
Dec 5, 2024
Merged

Ensure AttrContext.returnResult's checkContext is set to Check.basicHandler in javac's Scopes, to avoid it throwing exceptions.#8016
lahodaj merged 1 commit intoapache:masterfrom
lahodaj:ensure-checkContext-is-basicHandler-in-Scopes

Conversation

@lahodaj
Copy link
Copy Markdown
Contributor

@lahodaj lahodaj commented Dec 3, 2024

Consider this code, with a code completion invocation:

package test;

public class Test {
    private void test(Runnable r) {
        test(() -> {
            new Base<Void, Void>() {
                //code completion here
            };
        });
    }
}

class Base<T1, T2> {
    public T1 test(T1 p) {}
}

invoke the code completion at the marked place, and select the "test(Void p) - override" option. Nothing will happen, nothing will added to the source code.

The reason is this:

  • there is an (API) Scope taken at the place of the cursor. The Scope's Env<AttrContext> has a returnResult filled, and this returnResult has a checkContext that throws exceptions. This is part of method overload resolution in javac.
  • the code completion will delegate to GeneratorUtilities.createOverridingMethod, attempting to create the method.
  • createOverridingMethod will try to parse&attribute code like:
{
    return super.test(p);
}

in the context of the above Scope. But the return statement here does not match the expected return type from the Scope, and so it will go to the checkContext, and fail with the exception.

This is a bit tricky, as a) the Scope here is used in a bit unintended way; b) many of the fields and types responsible for this are package private.

The proposal in this PR is to replace the checkContext in the returnResult with the default value (Check.basicHandler). That should, hopefully, solve this and any other similar problems.

…andler in javac's Scopes, to avoid it throwing exceptions.
@lahodaj lahodaj added Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests labels Dec 3, 2024
@lahodaj lahodaj added this to the NB25 milestone Dec 3, 2024
@lahodaj lahodaj requested a review from dbalek December 3, 2024 16:14
@lahodaj lahodaj merged commit 583e9ca into apache:master Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Java [ci] enable extra Java tests (java.completion, java.source.base, java.hints, refactoring.java, form) LSP [ci] enable Language Server Protocol tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants