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

Unreported "illegal reference to static field from initializer" in relation to javac #3082

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,13 @@ public boolean isLambdaScope() {
}

public boolean isInsideInitializerOrConstructor() {
return (this.referenceContext instanceof TypeDeclaration)
|| (this.referenceContext instanceof ConstructorDeclaration);
// Peel away any intervening lambda(s) so they don't obscure initialization context
Scope scope = this;
while (scope.isLambdaScope())
scope = scope.parent;

return scope instanceof MethodScope mscope ?
mscope.referenceContext instanceof TypeDeclaration || mscope.referenceContext instanceof ConstructorDeclaration : false;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7433,4 +7433,40 @@ void k() {
"Cannot make a static reference to the non-static field h\n" +
"----------\n");
}
// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1590
// Unreported "illegal reference to static field from initializer" in relation to javac
public void testGHIssue1590() {
this.runNegativeTest(new String[] {
"X.java",
"""
import java.util.function.Supplier;

enum MyEnum {
;

private static MyEnum selected;

private MyEnum() {
Supplier<MyEnum> s = () -> selected = this;
}
}

enum S {
G(), B(), U();
S s=G;
}
"""
},
"----------\n" +
"1. ERROR in X.java (at line 9)\n" +
" Supplier<MyEnum> s = () -> selected = this;\n" +
" ^^^^^^^^\n" +
"Cannot refer to the static enum field MyEnum.selected within an initializer\n" +
"----------\n" +
"2. ERROR in X.java (at line 15)\n" +
" S s=G;\n" +
" ^\n" +
"Cannot refer to the static enum field S.G within an initializer\n" +
"----------\n");
}
}
Loading