RFR: 8270835: regression after JDK-8261006
Vicente Romero
vromero at openjdk.java.net
Tue Aug 17 17:05:43 UTC 2021
This patch is fixing a regression introduced by the fix for [JDK-8261006](https://bugs.openjdk.java.net/browse/JDK-8261006), which was trying to fix a long standing bug in javac. These are the related sections in the spec:
According to: 8.1.3 Inner Classes and Enclosing Instances:
A construct (statement, local variable declaration statement, local class declaration,
local interface declaration, or expression) occurs in a static context if the innermost:
...
• explicit constructor invocation statement
which encloses the construct is one of the following:
• an explicit constructor invocation statement (§8.8.7.1)
...
...
The purpose of a static context is to demarcate code that must not refer explicitly or
implicitly to the current instance of the class whose declaration lexically encloses the static
context. Consequently, code that occurs in a static context is restricted in the following
ways:
• Field accesses, method invocations, and method references may not be qualified by super (§15.11.2, §15.12.3, §15.13.1).
also from: 15.13 Method Reference Expressions:
If a method reference expression has the form super :: [TypeArguments] Identifier
or TypeName . super :: [TypeArguments] Identifier, it is a compile-time error if
the expression occurs in a static context (§8.1.3).
Meaning that code like:
this(Feature.super::getString); or
this(Feature.super.getString());
should be rejected by the compiler and it wasn't before the fix for [JDK-8261006](https://bugs.openjdk.java.net/browse/JDK-8261006). The regression was introduced because after [JDK-8261006](https://bugs.openjdk.java.net/browse/JDK-8261006) this code was rejected when it shouldn't:
class A extends B {
A(int i) {}
class C extends B {
class D extends S {
D(float f) {
C.super.ref.super(); // ok
}
}
}
}
class B {
B ref;
class S {}
}
so it is OK to use `super` in the constructor's invocation qualifier but not in its arguments. So while processing the arguments or the qualifier of a constructor invocation, the compiler needs to know precisely what portion of the expression it is dealing with as different rules apply for the use of `this` or `super` in them. This is why this fix is adding a new flag to AttrContext to indicate if we are dealing with a constructor invocation argument or not.
TIA
-------------
Commit messages:
- 8270835: regression after JDK-8261006
Changes: https://git.openjdk.java.net/jdk/pull/5149/files
Webrev: https://webrevs.openjdk.java.net/?repo=jdk&pr=5149&range=00
Issue: https://bugs.openjdk.java.net/browse/JDK-8270835
Stats: 52 lines in 4 files changed: 51 ins; 0 del; 1 mod
Patch: https://git.openjdk.java.net/jdk/pull/5149.diff
Fetch: git fetch https://git.openjdk.java.net/jdk pull/5149/head:pull/5149
PR: https://git.openjdk.java.net/jdk/pull/5149
More information about the compiler-dev
mailing list