Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8254784: javac should reject records with @SafeVarargs applied to var…
…args record component

Reviewed-by: mcimadamore
  • Loading branch information
Vicente Romero committed Dec 7, 2020
1 parent dcf63f8 commit b4b9828
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java
Expand Up @@ -3003,11 +3003,11 @@ private void validateAnnotation(JCAnnotation a, JCTree declarationTree, Symbol s
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
Expand All @@ -3031,6 +3031,13 @@ private void validateAnnotation(JCAnnotation a, JCTree declarationTree, Symbol s
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)));
}
}
}

Expand Down
Expand Up @@ -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.
Expand Down
@@ -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) {}
17 changes: 17 additions & 0 deletions test/langtools/tools/javac/records/RecordCompilationTests.java
Expand Up @@ -1825,4 +1825,21 @@ static void test() {
"""
);
}

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; }
}
"""
);
}
}

1 comment on commit b4b9828

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.