Skip to content
Permalink
Browse files

8236005: local records shouldn't capture any non-static state from an…

…y enclosing type

Reviewed-by: mcimadamore
  • Loading branch information
Vicente Romero
Vicente Romero committed Jan 14, 2020
1 parent 6d03f91 commit 5ff1d721850114508e96e1462f7722f46564afdd
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2020, 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
@@ -1489,7 +1489,13 @@ Symbol findVar(Env<AttrContext> env, Name name) {
if (sym.exists()) {
if (staticOnly &&
sym.kind == VAR &&
sym.owner.kind == TYP &&
// if it is a field
(sym.owner.kind == TYP ||
// or it is a local variable but it is not declared inside of the static local type
// only records so far, then error
(sym.owner.kind == MTH) &&
(env.enclClass.sym.flags() & STATIC) != 0 &&
sym.enclClass() != env.enclClass.sym) &&
(sym.flags() & STATIC) == 0)
return new StaticError(sym);
else
@@ -377,10 +377,44 @@ public void testLocalRecords() {
" }\n" +
"}");

// Capture locals from local record
// Cant capture locals
assertFail("compiler.err.non-static.cant.be.ref",
"class R { \n" +
" void m(int y) { \n" +
" record RR(int x) { public int x() { return y; }};\n" +
" }\n" +
"}");

assertFail("compiler.err.non-static.cant.be.ref",
"class R { \n" +
" void m() {\n" +
" int y;\n" +
" record RR(int x) { public int x() { return y; }};\n" +
" }\n" +
"}");

// instance fields
assertFail("compiler.err.non-static.cant.be.ref",
"class R { \n" +
" int z = 0;\n" +
" void m() { \n" +
" record RR(int x) { public int x() { return z; }};\n" +
" }\n" +
"}");

// or type variables
assertFail("compiler.err.non-static.cant.be.ref",
"class R<T> { \n" +
" void m() { \n" +
" record RR(T t) {};\n" +
" }\n" +
"}");

// but static fields are OK
assertOK("class R { \n" +
" void m(int y) { \n" +
" record RR(int x) { public int x() { return y; }};\n" +
" static int z = 0;\n" +
" void m() { \n" +
" record RR(int x) { public int x() { return z; }};\n" +
" }\n" +
"}");

0 comments on commit 5ff1d72

Please sign in to comment.