Skip to content
Permalink
Browse files

8234445: spurious error message for record constructors with receiver…

… parameters

Reviewed-by: mcimadamore
  • Loading branch information
Vicente Romero
Vicente Romero committed Dec 19, 2019
1 parent a170a4a commit 9695ddfffcda9eacab9cff882e16edbd6f1c0f02
@@ -884,6 +884,24 @@ protected void doCompleteEnvs(List<Env<AttrContext>> envs) {
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 {
@@ -902,6 +920,9 @@ protected void runPhase(Env<AttrContext> env) {
for (JCVariableDecl field : fields) {
sym.getRecordComponent(field.sym, true);
}

enterThisAndSuper(sym, env);

// lets enter all constructors
for (JCTree def : tree.defs) {
if (TreeInfo.isConstructor(def)) {
@@ -932,19 +953,8 @@ protected void runPhase(Env<AttrContext> env) {
JCTree constrDef = defaultConstructor(make.at(tree.pos), helper);
tree.defs = tree.defs.prepend(constrDef);
}
// 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);
if (!sym.isRecord()) {
enterThisAndSuper(sym, env);
}

if (!tree.typarams.isEmpty()) {
@@ -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.
You can’t perform that action at this time.