Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
8236005: local records shouldn't capture any non-static state from an…
…y enclosing type
Reviewed-by: mcimadamore
- Loading branch information
|
|
@@ -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" + |
|
|
"}"); |
|
|
|
|
|