From f59ff5d8fe2e17acff82ec6f18d8608ca21334d0 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Tue, 10 Jun 2014 18:51:23 +0400 Subject: [PATCH] Diagnostics for illegal platform names --- .../jet/lang/diagnostics/Errors.java | 2 ++ .../rendering/DefaultErrorMessages.java | 1 + .../jet/lang/resolve/ModifiersChecker.java | 33 ++++++++++++++----- .../illegalPlatformName.kt | 14 ++++++++ ...JetDiagnosticsTestWithStdLibGenerated.java | 5 +++ 5 files changed, 46 insertions(+), 9 deletions(-) create mode 100644 compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability/illegalPlatformName.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index 731e625e138..3d0df53cdf9 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -205,6 +205,8 @@ public interface Errors { DiagnosticFactory2 CONFLICTING_OVERLOADS = DiagnosticFactory2.create(ERROR, DECLARATION); + DiagnosticFactory1 ILLEGAL_PLATFORM_NAME = DiagnosticFactory1.create(ERROR); + DiagnosticFactory0 NON_FINAL_MEMBER_IN_FINAL_CLASS = DiagnosticFactory0.create(WARNING, modifierSetPosition( JetTokens.OPEN_KEYWORD)); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java index 89a27e8c37a..deace52941b 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java @@ -419,6 +419,7 @@ public class DefaultErrorMessages { RENDER_CLASS_OR_OBJECT, FQ_NAMES_IN_TYPES); MAP.put(CONFLICTING_OVERLOADS, "''{0}'' is already defined in {1}", COMPACT_WITH_MODIFIERS, STRING); + MAP.put(ILLEGAL_PLATFORM_NAME, "Illegal platform name: ''{0}''", STRING); MAP.put(FUNCTION_EXPECTED, "Expression ''{0}''{1} cannot be invoked as a function. The function 'invoke()' is not found", ELEMENT_TEXT, new Renderer() { @NotNull diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ModifiersChecker.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ModifiersChecker.java index 155d6ded010..ee69490f8d3 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ModifiersChecker.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/ModifiersChecker.java @@ -28,7 +28,9 @@ import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.diagnostics.Errors; import org.jetbrains.jet.lang.psi.*; +import org.jetbrains.jet.lang.resolve.constants.*; import org.jetbrains.jet.lang.resolve.name.FqName; +import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lexer.JetModifierKeywordToken; import java.util.Collection; @@ -36,6 +38,7 @@ import java.util.Collections; import java.util.Map; import static org.jetbrains.jet.lang.diagnostics.Errors.ILLEGAL_MODIFIER; +import static org.jetbrains.jet.lang.diagnostics.Errors.ILLEGAL_PLATFORM_NAME; import static org.jetbrains.jet.lang.diagnostics.Errors.INAPPLICABLE_ANNOTATION; import static org.jetbrains.jet.lexer.JetTokens.*; @@ -152,15 +155,6 @@ public class ModifiersChecker { } private void checkPlatformNameApplicability(@NotNull DeclarationDescriptor descriptor) { - if (!DescriptorUtils.isTopLevelDeclaration(descriptor) || !(descriptor instanceof FunctionDescriptor)) { - AnnotationDescriptor annotation = descriptor.getAnnotations().findAnnotation(new FqName("kotlin.platform.platformName")); - if (annotation != null) { - JetAnnotationEntry annotationEntry = trace.get(BindingContext.ANNOTATION_DESCRIPTOR_TO_PSI_ELEMENT, annotation); - if (annotationEntry != null) { - trace.report(INAPPLICABLE_ANNOTATION.on(annotationEntry)); - } - } - } if (descriptor instanceof PropertyDescriptor) { PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor; if (propertyDescriptor.getGetter() != null) { @@ -170,6 +164,27 @@ public class ModifiersChecker { checkPlatformNameApplicability(propertyDescriptor.getSetter()); } } + + AnnotationDescriptor annotation = descriptor.getAnnotations().findAnnotation(new FqName("kotlin.platform.platformName")); + if (annotation == null) return; + + JetAnnotationEntry annotationEntry = trace.get(BindingContext.ANNOTATION_DESCRIPTOR_TO_PSI_ELEMENT, annotation); + if (annotationEntry == null) return; + + if (!DescriptorUtils.isTopLevelDeclaration(descriptor) || !(descriptor instanceof FunctionDescriptor)) { + trace.report(INAPPLICABLE_ANNOTATION.on(annotationEntry)); + } + + Collection> values = annotation.getAllValueArguments().values(); + if (!values.isEmpty()) { + CompileTimeConstant name = values.iterator().next(); + if (name instanceof StringValue) { + String value = ((StringValue) name).getValue(); + if (value == null || !Name.isValidIdentifier(value)) { + trace.report(ILLEGAL_PLATFORM_NAME.on(annotationEntry, String.valueOf(value))); + } + } + } } private void checkCompatibility(@Nullable JetModifierList modifierList, Collection availableModifiers, Collection... availableCombinations) { diff --git a/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability/illegalPlatformName.kt b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability/illegalPlatformName.kt new file mode 100644 index 00000000000..a799dd1a61e --- /dev/null +++ b/compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability/illegalPlatformName.kt @@ -0,0 +1,14 @@ +// !DIAGNOSTICS: -UNUSED_PARAMETER +import kotlin.platform.* + +[platformName("")] +fun foo(a: Any) {} + +[platformName(".")] +fun foo() {} + +[platformName("/")] +fun fooSlash() {} + +[platformName("<")] +fun fooLT() {} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java index 8ca556cf27f..186d535e122 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestWithStdLibGenerated.java @@ -55,6 +55,11 @@ public class JetDiagnosticsTestWithStdLibGenerated extends AbstractJetDiagnostic JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.TestsPackage", new File("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("illegalPlatformName.kt") + public void testIllegalPlatformName() throws Exception { + doTest("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability/illegalPlatformName.kt"); + } + @TestMetadata("platformName.kt") public void testPlatformName() throws Exception { doTest("compiler/testData/diagnostics/testsWithStdLib/annotations/annotationApplicability/platformName.kt");