Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
Improve diagnostic for invalid local class creation
  • Loading branch information
mcimadamore committed Oct 3, 2024
1 parent 4c271b7 commit 118e125
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
import com.sun.tools.javac.comp.Check.CheckContext;
import com.sun.tools.javac.comp.DeferredAttr.AttrMode;
import com.sun.tools.javac.comp.MatchBindingsComputer.MatchBindings;
import com.sun.tools.javac.comp.Resolve.InvalidSymbolError;
import com.sun.tools.javac.comp.Resolve.RefBeforeCtorCalledError;
import com.sun.tools.javac.jvm.*;

import static com.sun.tools.javac.resources.CompilerProperties.Fragments.Diamond;
Expand Down Expand Up @@ -3074,7 +3072,7 @@ void checkNewInnerClass(DiagnosticPosition pos, Env<AttrContext> env, Type type,
}
Symbol res = isLocal ?
rs.findLocalClassOwner(env, type.tsym) :
rs.findSelfContaining(pos, env, type.getEnclosingType().tsym, names._this, isSuper);
rs.findSelfContaining(pos, env, type.getEnclosingType().tsym, isSuper);
if (res.exists()) {
rs.accessBase(res, pos, env.enclClass.sym.type, names._this, true);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3787,15 +3787,13 @@ Symbol lookupMethod(Env<AttrContext> env, DiagnosticPosition pos, Symbol locatio
Symbol findSelfContaining(DiagnosticPosition pos,
Env<AttrContext> env,
TypeSymbol c,
Name name,
boolean isSuper) {
Assert.check(name == names._this);
Env<AttrContext> env1 = isSuper ? env.outer : env;
boolean staticOnly = false;
while (env1.outer != null) {
if (isStatic(env1)) staticOnly = true;
if (env1.enclClass.sym.isSubClass(c, types)) {
Symbol sym = env1.info.scope.findFirst(name);
Symbol sym = env1.info.scope.findFirst(names._this);
if (sym != null) {
if (staticOnly) {
// current class is not an inner class, stop search
Expand Down Expand Up @@ -3829,7 +3827,7 @@ Symbol findLocalClassOwner(Env<AttrContext> env, TypeSymbol c) {
while (env1.outer != null) {
if (env1.info.scope.owner == owner) {
return (staticOnly) ?
new StaticError(owner) :
new BadLocalClassCreation(c) :
owner;
}
if (isStatic(env1)) staticOnly = true;
Expand Down Expand Up @@ -4716,6 +4714,28 @@ JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
}
}

/**
* Specialization of {@link StaticError} for illegal
* creation of local class instances from a static context.
*/
class BadLocalClassCreation extends StaticError {
BadLocalClassCreation(Symbol sym) {
super(sym, "bad local class creation");
}

@Override
JCDiagnostic getDiagnostic(JCDiagnostic.DiagnosticType dkind,
DiagnosticPosition pos,
Symbol location,
Type site,
Name name,
List<Type> argtypes,
List<Type> typeargtypes) {
return diags.create(dkind, log.currentSource(), pos,
"local.cant.be.inst.static", kindName(sym), sym);
}
}

/**
* Specialization of {@link InvalidSymbolError} for illegal
* early accesses within a constructor prologue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2877,6 +2877,11 @@ compiler.err.abstract.cant.be.accessed.directly=\
compiler.err.non-static.cant.be.ref=\
non-static {0} {1} cannot be referenced from a static context

## The first argument ({0}) is a "kindname".
# 0: symbol kind, 1: symbol
compiler.err.local.cant.be.inst.static=\
local {0} {1} cannot be instantiated from a static context

# 0: symbol kind, 1: symbol
compiler.misc.bad.static.method.in.unbound.lookup=\
unexpected static {0} {1} found in unbound lookup
Expand Down
24 changes: 24 additions & 0 deletions test/langtools/tools/javac/LocalFreeVarStaticInstantiate.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,38 @@

class LocalFreeVarStaticInstantiate {

// local class in method
static void foo(Object there) {
class Local {
{
there.hashCode();
}

static {
new Local(); // can't get there from here
}

static Runnable r = () -> {
new Local(); // can't get there from here
};
}
}

// local class in lambda
static Runnable foo = () -> {
Object there = "";
class Local {
{
there.hashCode();
}

static {
new Local(); // can't get there from here
}

static Runnable r = () -> {
new Local(); // can't get there from here
};
}
};
}
7 changes: 5 additions & 2 deletions test/langtools/tools/javac/LocalFreeVarStaticInstantiate.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
LocalFreeVarStaticInstantiate.java:16:17: compiler.err.non-static.cant.be.ref: kindname.method, foo(java.lang.Object)
1 error
LocalFreeVarStaticInstantiate.java:18:17: compiler.err.local.cant.be.inst.static: kindname.class, Local
LocalFreeVarStaticInstantiate.java:22:17: compiler.err.local.cant.be.inst.static: kindname.class, Local
LocalFreeVarStaticInstantiate.java:36:17: compiler.err.local.cant.be.inst.static: kindname.class, Local
LocalFreeVarStaticInstantiate.java:40:17: compiler.err.local.cant.be.inst.static: kindname.class, Local
4 errors
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

// key: compiler.err.local.cant.be.inst.static

class LocalClassCantBeInstStatic {
static void foo(Object there) {
class Local {
{
there.hashCode();
}

static {
new Local(); // can't get there from here
}
}
}
}

0 comments on commit 118e125

Please sign in to comment.