Skip to content

Commit b4b9828

Browse files
author
Vicente Romero
committed
8254784: javac should reject records with @SafeVarargs applied to varargs record component
Reviewed-by: mcimadamore
1 parent dcf63f8 commit b4b9828

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3003,11 +3003,11 @@ private void validateAnnotation(JCAnnotation a, JCTree declarationTree, Symbol s
30033003
Set<Name> applicableTargets = applicableTargetsOp.get();
30043004
boolean notApplicableOrIsTypeUseOnly = applicableTargets.isEmpty() ||
30053005
applicableTargets.size() == 1 && applicableTargets.contains(names.TYPE_USE);
3006-
boolean isRecordMemberWithNonApplicableDeclAnno =
3007-
isRecordMember && (s.flags_field & Flags.GENERATED_MEMBER) != 0 && notApplicableOrIsTypeUseOnly;
3006+
boolean isCompGeneratedRecordElement = isRecordMember && (s.flags_field & Flags.GENERATED_MEMBER) != 0;
3007+
boolean isCompRecordElementWithNonApplicableDeclAnno = isCompGeneratedRecordElement && notApplicableOrIsTypeUseOnly;
30083008

3009-
if (applicableTargets.isEmpty() || isRecordMemberWithNonApplicableDeclAnno) {
3010-
if (isRecordMemberWithNonApplicableDeclAnno) {
3009+
if (applicableTargets.isEmpty() || isCompRecordElementWithNonApplicableDeclAnno) {
3010+
if (isCompRecordElementWithNonApplicableDeclAnno) {
30113011
/* so we have found an annotation that is not applicable to a record member that was generated by the
30123012
* compiler. This was intentionally done at TypeEnter, now is the moment strip away the annotations
30133013
* that are not applicable to the given record member
@@ -3031,6 +3031,13 @@ private void validateAnnotation(JCAnnotation a, JCTree declarationTree, Symbol s
30313031
log.error(a.pos(), Errors.AnnotationTypeNotApplicable);
30323032
}
30333033
}
3034+
/* if we are seeing the @SafeVarargs annotation applied to a compiler generated accessor,
3035+
* then this is an error as we know that no compiler generated accessor will be a varargs
3036+
* method, better to fail asap
3037+
*/
3038+
if (isCompGeneratedRecordElement && !isRecordField && a.type.tsym == syms.trustMeType.tsym && declarationTree.hasTag(METHODDEF)) {
3039+
log.error(a.pos(), Errors.VarargsInvalidTrustmeAnno(syms.trustMeType.tsym, Fragments.VarargsTrustmeOnNonVarargsAccessor(s)));
3040+
}
30343041
}
30353042
}
30363043

src/jdk.compiler/share/classes/com/sun/tools/javac/resources/compiler.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,6 +1419,10 @@ compiler.err.instanceof.pattern.no.subtype=\
14191419
compiler.misc.varargs.trustme.on.non.varargs.meth=\
14201420
Method {0} is not a varargs method.
14211421

1422+
# 0: symbol
1423+
compiler.misc.varargs.trustme.on.non.varargs.accessor=\
1424+
Accessor {0} is not a varargs method.
1425+
14221426
# 0: symbol
14231427
compiler.misc.varargs.trustme.on.virtual.varargs=\
14241428
Instance method {0} is neither final nor private.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
// key: compiler.err.varargs.invalid.trustme.anno
25+
// key: compiler.misc.varargs.trustme.on.non.varargs.accessor
26+
// key: compiler.note.preview.filename
27+
// key: compiler.note.preview.recompile
28+
// options: --enable-preview -source ${jdk.version}
29+
30+
record R(@SafeVarargs String... s) {}

test/langtools/tools/javac/records/RecordCompilationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,4 +1825,21 @@ static void test() {
18251825
"""
18261826
);
18271827
}
1828+
1829+
public void testSaveVarargsAnno() {
1830+
// the compiler would generate an erronous accessor
1831+
assertFail("compiler.err.varargs.invalid.trustme.anno",
1832+
"""
1833+
record R(@SafeVarargs String... s) {}
1834+
"""
1835+
);
1836+
// but this is OK
1837+
assertOK(
1838+
"""
1839+
record R(@SafeVarargs String... s) {
1840+
public String[] s() { return s; }
1841+
}
1842+
"""
1843+
);
1844+
}
18281845
}

0 commit comments

Comments
 (0)