Skip to content
Permalink
Browse files
8240454: incorrect error message: as of release 13, 'record' is a res…
…tricted type name

Reviewed-by: jlahoda
  • Loading branch information
Vicente Romero committed Mar 5, 2020
1 parent 3607ddd commit 4a32eda417fa27b7498948cc06a77d1069d3050f
@@ -3262,7 +3262,7 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
int startPos = Position.NOPOS;
if (elemType.hasTag(IDENT)) {
Name typeName = ((JCIdent)elemType).name;
if (isRestrictedTypeName(typeName, pos, !compound && localDecl)) {
if (restrictedTypeNameStartingAtSource(typeName, pos, !compound && localDecl) != null) {
if (type.hasTag(TYPEARRAY) && !compound) {
//error - 'var' and arrays
reportSyntaxError(pos, Errors.RestrictedTypeNotAllowedArray(typeName));
@@ -3288,37 +3288,37 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
Name restrictedTypeName(JCExpression e, boolean shouldWarn) {
switch (e.getTag()) {
case IDENT:
return isRestrictedTypeName(((JCIdent)e).name, e.pos, shouldWarn) ? ((JCIdent)e).name : null;
return restrictedTypeNameStartingAtSource(((JCIdent)e).name, e.pos, shouldWarn) != null ? ((JCIdent)e).name : null;
case TYPEARRAY:
return restrictedTypeName(((JCArrayTypeTree)e).elemtype, shouldWarn);
default:
return null;
}
}

boolean isRestrictedTypeName(Name name, int pos, boolean shouldWarn) {
Source restrictedTypeNameStartingAtSource(Name name, int pos, boolean shouldWarn) {
if (name == names.var) {
if (Feature.LOCAL_VARIABLE_TYPE_INFERENCE.allowedInSource(source)) {
return true;
return Source.JDK10;
} else if (shouldWarn) {
log.warning(pos, Warnings.RestrictedTypeNotAllowed(name, Source.JDK10));
}
}
if (name == names.yield) {
if (allowYieldStatement) {
return true;
return Source.JDK14;
} else if (shouldWarn) {
log.warning(pos, Warnings.RestrictedTypeNotAllowed(name, Source.JDK14));
}
}
if (name == names.record) {
if (allowRecords) {
return true;
return Source.JDK14;
} else if (shouldWarn) {
log.warning(pos, Warnings.RestrictedTypeNotAllowedPreview(name, Source.JDK14));
}
}
return false;
return null;
}

/** VariableDeclaratorId = Ident BracketsOpt
@@ -3766,8 +3766,9 @@ protected JCClassDecl recordDeclaration(JCModifiers mods, Comment dc) {
Name typeName() {
int pos = token.pos;
Name name = ident();
if (isRestrictedTypeName(name, pos, true)) {
reportSyntaxError(pos, Errors.RestrictedTypeNotAllowed(name, name == names.var ? Source.JDK10 : Source.JDK13));
Source source = restrictedTypeNameStartingAtSource(name, pos, true);
if (source != null) {
reportSyntaxError(pos, Errors.RestrictedTypeNotAllowed(name, source));
}
return name;
}
@@ -27,6 +27,10 @@
import java.io.File;
import java.io.IOException;

import java.util.function.Consumer;

import javax.tools.Diagnostic;

import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
@@ -100,4 +104,8 @@ protected void assertOKWithWarning(String warning, String... constructs) {
protected void assertFail(String expectedDiag, String... constructs) {
assertCompile(expandMarkers(constructs), () -> assertCompileFailed(expectedDiag), false);
}

protected void assertFail(String expectedDiag, Consumer<Diagnostic<?>> diagConsumer, String... constructs) {
assertCompile(expandMarkers(constructs), () -> assertCompileFailed(expectedDiag, diagConsumer), false);
}
}
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
@@ -57,6 +57,15 @@ public boolean errorsFound() {
.collect(toList());
}

public Diagnostic<?> getDiagWithKey(String key) {
for (Diagnostic<?> d : diags) {
if (d.getCode().equals(key)) {
return d;
}
}
return null;
}

/** Do the diagnostics contain the specified error key? */
public boolean containsErrorKey(String key) {
return diags.stream()
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 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
@@ -38,6 +38,8 @@
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import javax.tools.Diagnostic;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
@@ -204,8 +206,20 @@ protected void assertCompileFailed() {
protected void assertCompileFailed(String key) {
if (!diags.errorsFound())
fail("Expected failed compilation: " + key);
if (!diags.containsErrorKey(key))
if (!diags.containsErrorKey(key)) {
fail(String.format("Expected compilation error with %s, found %s", key, diags.keys()));
}
}

protected void assertCompileFailed(String key, Consumer<Diagnostic<?>> diagConsumer) {
if (!diags.errorsFound())
fail("Expected failed compilation: " + key);
if (!diags.containsErrorKey(key)) {
fail(String.format("Expected compilation error with %s, found %s", key, diags.keys()));
} else {
// for additional checks
diagConsumer.accept(diags.getDiagWithKey(key));
}
}

/** Assert that a previous call to compile() failed with a specific error key */
@@ -30,6 +30,7 @@
* @summary Negative compilation tests, and positive compilation (smoke) tests for records
* @library /lib/combo /tools/lib /tools/javac/lib
* @modules
* jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.code
* jdk.compiler/com.sun.tools.javac.util
* jdk.jdeps/com.sun.tools.classfile
@@ -86,9 +87,11 @@
import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
import com.sun.tools.classfile.TypeAnnotation;

import com.sun.tools.javac.api.ClientCodeWrapper.DiagnosticSourceUnwrapper;
import com.sun.tools.javac.code.Attribute.TypeCompound;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.util.JCDiagnostic;

import org.testng.annotations.Test;
import tools.javac.combo.CompilationTestCase;
@@ -169,7 +172,15 @@ public void testRestrictedIdentifiers() {
"record record(int x) { }",
"enum record { A, B }",
"class R<record> { }")) {
assertFail("compiler.err.restricted.type.not.allowed", s);
assertFail(
"compiler.err.restricted.type.not.allowed",
diagWrapper -> {
JCDiagnostic diagnostic = ((DiagnosticSourceUnwrapper)diagWrapper).d;
Object[] args = diagnostic.getArgs();
Assert.check(args.length == 2);
Assert.check(args[1].toString().equals("JDK14"));
},
s);
}
}

@@ -1,4 +1,4 @@
WrongYieldTest.java:39:11: compiler.err.restricted.type.not.allowed: yield, 13
WrongYieldTest.java:39:11: compiler.err.restricted.type.not.allowed: yield, 14
WrongYieldTest.java:45:5: compiler.err.restricted.type.not.allowed.here: yield
WrongYieldTest.java:123:15: compiler.err.restricted.type.not.allowed.here: yield
WrongYieldTest.java:136:9: compiler.err.invalid.yield

0 comments on commit 4a32eda

Please sign in to comment.