8254784: javac should reject records with @SafeVarargs applied to varargs record component

Reviewed-by: mcimadamore
This commit is contained in:
Vicente Romero 2020-12-07 16:45:51 +00:00
parent dcf63f8578
commit b4b9828cb0
4 changed files with 62 additions and 4 deletions
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac

@ -3003,11 +3003,11 @@ public class Check {
Set<Name> applicableTargets = applicableTargetsOp.get();
boolean notApplicableOrIsTypeUseOnly = applicableTargets.isEmpty() ||
applicableTargets.size() == 1 && applicableTargets.contains(names.TYPE_USE);
boolean isRecordMemberWithNonApplicableDeclAnno =
isRecordMember && (s.flags_field & Flags.GENERATED_MEMBER) != 0 && notApplicableOrIsTypeUseOnly;
boolean isCompGeneratedRecordElement = isRecordMember && (s.flags_field & Flags.GENERATED_MEMBER) != 0;
boolean isCompRecordElementWithNonApplicableDeclAnno = isCompGeneratedRecordElement && notApplicableOrIsTypeUseOnly;
if (applicableTargets.isEmpty() || isRecordMemberWithNonApplicableDeclAnno) {
if (isRecordMemberWithNonApplicableDeclAnno) {
if (applicableTargets.isEmpty() || isCompRecordElementWithNonApplicableDeclAnno) {
if (isCompRecordElementWithNonApplicableDeclAnno) {
/* so we have found an annotation that is not applicable to a record member that was generated by the
* compiler. This was intentionally done at TypeEnter, now is the moment strip away the annotations
* that are not applicable to the given record member
@ -3031,6 +3031,13 @@ public class Check {
log.error(a.pos(), Errors.AnnotationTypeNotApplicable);
}
}
/* if we are seeing the @SafeVarargs annotation applied to a compiler generated accessor,
* then this is an error as we know that no compiler generated accessor will be a varargs
* method, better to fail asap
*/
if (isCompGeneratedRecordElement && !isRecordField && a.type.tsym == syms.trustMeType.tsym && declarationTree.hasTag(METHODDEF)) {
log.error(a.pos(), Errors.VarargsInvalidTrustmeAnno(syms.trustMeType.tsym, Fragments.VarargsTrustmeOnNonVarargsAccessor(s)));
}
}
}

@ -1419,6 +1419,10 @@ compiler.err.instanceof.pattern.no.subtype=\
compiler.misc.varargs.trustme.on.non.varargs.meth=\
Method {0} is not a varargs method.
# 0: symbol
compiler.misc.varargs.trustme.on.non.varargs.accessor=\
Accessor {0} is not a varargs method.
# 0: symbol
compiler.misc.varargs.trustme.on.virtual.varargs=\
Instance method {0} is neither final nor private.

@ -0,0 +1,30 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
// key: compiler.err.varargs.invalid.trustme.anno
// key: compiler.misc.varargs.trustme.on.non.varargs.accessor
// key: compiler.note.preview.filename
// key: compiler.note.preview.recompile
// options: --enable-preview -source ${jdk.version}
record R(@SafeVarargs String... s) {}

@ -1825,4 +1825,21 @@ public class RecordCompilationTests extends CompilationTestCase {
"""
);
}
public void testSaveVarargsAnno() {
// the compiler would generate an erronous accessor
assertFail("compiler.err.varargs.invalid.trustme.anno",
"""
record R(@SafeVarargs String... s) {}
"""
);
// but this is OK
assertOK(
"""
record R(@SafeVarargs String... s) {
public String[] s() { return s; }
}
"""
);
}
}