Skip to content

Commit

Permalink
8234445: spurious error message for record constructors with receiver…
Browse files Browse the repository at this point in the history
… parameters

Reviewed-by: mcimadamore
  • Loading branch information
Vicente Romero committed Dec 19, 2019
1 parent a170a4a commit 9695ddf
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 13 deletions.
Expand Up @@ -884,6 +884,24 @@ protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
completing = prevCompleting; completing = prevCompleting;
} }
} }

void enterThisAndSuper(ClassSymbol sym, Env<AttrContext> env) {
ClassType ct = (ClassType)sym.type;
// enter symbols for 'this' into current scope.
VarSymbol thisSym =
new VarSymbol(FINAL | HASINIT, names._this, sym.type, sym);
thisSym.pos = Position.FIRSTPOS;
env.info.scope.enter(thisSym);
// if this is a class, enter symbol for 'super' into current scope.
if ((sym.flags_field & INTERFACE) == 0 &&
ct.supertype_field.hasTag(CLASS)) {
VarSymbol superSym =
new VarSymbol(FINAL | HASINIT, names._super,
ct.supertype_field, sym);
superSym.pos = Position.FIRSTPOS;
env.info.scope.enter(superSym);
}
}
} }


private final class RecordPhase extends AbstractMembersPhase { private final class RecordPhase extends AbstractMembersPhase {
Expand All @@ -902,6 +920,9 @@ protected void runPhase(Env<AttrContext> env) {
for (JCVariableDecl field : fields) { for (JCVariableDecl field : fields) {
sym.getRecordComponent(field.sym, true); sym.getRecordComponent(field.sym, true);
} }

enterThisAndSuper(sym, env);

// lets enter all constructors // lets enter all constructors
for (JCTree def : tree.defs) { for (JCTree def : tree.defs) {
if (TreeInfo.isConstructor(def)) { if (TreeInfo.isConstructor(def)) {
Expand Down Expand Up @@ -932,19 +953,8 @@ protected void runPhase(Env<AttrContext> env) {
JCTree constrDef = defaultConstructor(make.at(tree.pos), helper); JCTree constrDef = defaultConstructor(make.at(tree.pos), helper);
tree.defs = tree.defs.prepend(constrDef); tree.defs = tree.defs.prepend(constrDef);
} }
// enter symbols for 'this' into current scope. if (!sym.isRecord()) {
VarSymbol thisSym = enterThisAndSuper(sym, env);
new VarSymbol(FINAL | HASINIT, names._this, sym.type, sym);
thisSym.pos = Position.FIRSTPOS;
env.info.scope.enter(thisSym);
// if this is a class, enter symbol for 'super' into current scope.
if ((sym.flags_field & INTERFACE) == 0 &&
ct.supertype_field.hasTag(CLASS)) {
VarSymbol superSym =
new VarSymbol(FINAL | HASINIT, names._super,
ct.supertype_field, sym);
superSym.pos = Position.FIRSTPOS;
env.info.scope.enter(superSym);
} }


if (!tree.typarams.isEmpty()) { if (!tree.typarams.isEmpty()) {
Expand Down
28 changes: 28 additions & 0 deletions test/langtools/tools/javac/records/RecordCompilationTests.java
Expand Up @@ -469,4 +469,32 @@ record B() { }
} }
"""); """);
} }

public void testReceiverParameter() {
assertFail("compiler.err.receiver.parameter.not.applicable.constructor.toplevel.class",
"""
record R(int i) {
public R(R this, int i) {
this.i = i;
}
}
""");
assertFail("compiler.err.non-static.cant.be.ref",
"""
class Outer {
record R(int i) {
public R(Outer Outer.this, int i) {
this.i = i;
}
}
}
""");
assertOK(
"""
record R(int i) {
void m(R this) {}
public int i(R this) { return i; }
}
""");
}
} }

0 comments on commit 9695ddf

Please sign in to comment.