Report warning on characters which can cause problems on Windows

As soon as we fix KT-17438, this warning will be turned into an error.
This commit is contained in:
Alexander Udalov
2020-06-30 17:17:37 +02:00
parent 181965f6e8
commit ba948cda38
9 changed files with 102 additions and 8 deletions
@@ -5616,6 +5616,11 @@ public class FirOldFrontendDiagnosticsTestGenerated extends AbstractFirOldFronte
runTest("compiler/testData/diagnostics/tests/declarationChecks/MultiDeclarationErrors.kt");
}
@TestMetadata("nameWithDangerousCharacters.kt")
public void testNameWithDangerousCharacters() throws Exception {
runTest("compiler/testData/diagnostics/tests/declarationChecks/nameWithDangerousCharacters.kt");
}
@TestMetadata("namedFunAsLastExpressionInBlock.kt")
public void testNamedFunAsLastExpressionInBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/declarationChecks/namedFunAsLastExpressionInBlock.kt");
@@ -21,11 +21,15 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.IdentifierChecker
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
object JvmSimpleNameBacktickChecker : IdentifierChecker {
// See The Java Virtual Machine Specification, section 4.7.9.1 https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-4.html#jvms-4.7.9.1
val INVALID_CHARS = setOf('.', ';', '[', ']', '/', '<', '>', ':', '\\')
// These characters can cause problems on Windows. '?*"|' are not allowed in file names, and % leads to unexpected env var expansion.
private val DANGEROUS_CHARS = setOf('?', '*', '"', '|', '%')
override fun checkIdentifier(simpleNameExpression: KtSimpleNameExpression, diagnosticHolder: DiagnosticSink) {
reportIfNeeded(simpleNameExpression.getReferencedName(), { simpleNameExpression.getIdentifier() }, diagnosticHolder)
}
@@ -53,15 +57,23 @@ object JvmSimpleNameBacktickChecker : IdentifierChecker {
private fun reportIfNeeded(name: String, reportOn: () -> PsiElement?, diagnosticHolder: DiagnosticSink) {
val text = KtPsiUtil.unquoteIdentifier(name)
if (text.isEmpty()) {
diagnosticHolder.report(Errors.INVALID_CHARACTERS.on(reportOn() ?: return, "should not be empty"))
} else if (text.any { it in INVALID_CHARS }) {
diagnosticHolder.report(
Errors.INVALID_CHARACTERS.on(
reportOn() ?: return,
"contains illegal characters: ${INVALID_CHARS.intersect(text.toSet()).joinToString("")}"
when {
text.isEmpty() -> {
diagnosticHolder.report(Errors.INVALID_CHARACTERS.on(reportOn() ?: return, "should not be empty"))
}
text.any { it in INVALID_CHARS } -> {
diagnosticHolder.report(
Errors.INVALID_CHARACTERS.on(
reportOn() ?: return,
"contains illegal characters: ${INVALID_CHARS.intersect(text.toSet()).joinToString("")}"
)
)
)
}
text.any { it in DANGEROUS_CHARS } -> {
diagnosticHolder.report(
ErrorsJvm.DANGEROUS_CHARACTERS.on(reportOn() ?: return, DANGEROUS_CHARS.intersect(text.toSet()).joinToString(""))
)
}
}
}
}
@@ -165,6 +165,8 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
"or annotate the class with @JvmDefaultWithoutCompatibility. " +
"Please refer to KT-39603 for details",
COMPACT, SHORT_NAMES_IN_TYPES);
MAP.put(DANGEROUS_CHARACTERS, "Name contains characters which can cause problems on Windows: {0}", STRING);
}
@NotNull
@@ -141,6 +141,8 @@ public interface ErrorsJvm {
DiagnosticFactory2<KtDeclaration, CallableDescriptor, CallableDescriptor> EXPLICIT_OVERRIDE_REQUIRED_IN_COMPATIBILITY_MODE = DiagnosticFactory2.create(ERROR, DECLARATION_SIGNATURE_OR_DEFAULT);
DiagnosticFactory1<PsiElement, String> DANGEROUS_CHARACTERS = DiagnosticFactory1.create(WARNING);
@SuppressWarnings("UnusedDeclaration")
Object _initializer = new Object() {
{
@@ -0,0 +1,12 @@
class `A?B`
class `A*B`
class `A"B`
class `A|B`
class `A%B`
fun `?*"|%`(): Int {
val `?` = 0
return `?`
}
val `"a"+"b"` = "c"
@@ -0,0 +1,12 @@
class <!DANGEROUS_CHARACTERS!>`A?B`<!>
class <!DANGEROUS_CHARACTERS!>`A*B`<!>
class <!DANGEROUS_CHARACTERS!>`A"B`<!>
class <!DANGEROUS_CHARACTERS!>`A|B`<!>
class <!DANGEROUS_CHARACTERS!>`A%B`<!>
fun <!DANGEROUS_CHARACTERS!>`?*"|%`<!>(): Int {
val <!DANGEROUS_CHARACTERS!>`?`<!> = 0
return `?`
}
val <!DANGEROUS_CHARACTERS!>`"a"+"b"`<!> = "c"
@@ -0,0 +1,39 @@
package
public val `"a"+"b"`: kotlin.String = "c"
public fun `?*"|%`(): kotlin.Int
public final class `A"B` {
public constructor `A"B`()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class `A%B` {
public constructor `A%B`()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class `A*B` {
public constructor `A*B`()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class `A?B` {
public constructor `A?B`()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final class `A|B` {
public constructor `A|B`()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -5623,6 +5623,11 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTestWithFirVali
runTest("compiler/testData/diagnostics/tests/declarationChecks/MultiDeclarationErrors.kt");
}
@TestMetadata("nameWithDangerousCharacters.kt")
public void testNameWithDangerousCharacters() throws Exception {
runTest("compiler/testData/diagnostics/tests/declarationChecks/nameWithDangerousCharacters.kt");
}
@TestMetadata("namedFunAsLastExpressionInBlock.kt")
public void testNamedFunAsLastExpressionInBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/declarationChecks/namedFunAsLastExpressionInBlock.kt");
@@ -5618,6 +5618,11 @@ public class DiagnosticsUsingJavacTestGenerated extends AbstractDiagnosticsUsing
runTest("compiler/testData/diagnostics/tests/declarationChecks/MultiDeclarationErrors.kt");
}
@TestMetadata("nameWithDangerousCharacters.kt")
public void testNameWithDangerousCharacters() throws Exception {
runTest("compiler/testData/diagnostics/tests/declarationChecks/nameWithDangerousCharacters.kt");
}
@TestMetadata("namedFunAsLastExpressionInBlock.kt")
public void testNamedFunAsLastExpressionInBlock() throws Exception {
runTest("compiler/testData/diagnostics/tests/declarationChecks/namedFunAsLastExpressionInBlock.kt");