Disallow named arguments for SAM adapters

SAM adapters are Java functions, and named arguments are not allowed for Java
functions

 #KT-5022 Fixed
This commit is contained in:
Alexander Udalov
2014-06-27 17:31:44 +04:00
parent 6e1047685c
commit c0fc5cfb53
12 changed files with 79 additions and 37 deletions
@@ -0,0 +1,16 @@
// FILE: test/J.java
package test;
public class J {
public J(String s, Runnable r, Boolean z) {
}
}
// FILE: usage.kt
package test
fun test() {
J("", <!NAMED_ARGUMENTS_NOT_ALLOWED!>r<!> = { }, <!NAMED_ARGUMENTS_NOT_ALLOWED!>z<!> = false)
}
@@ -0,0 +1,16 @@
// FILE: test/J.java
package test;
public class J {
public static void foo(String s, Runnable r, Boolean z) {
}
}
// FILE: usage.kt
package test
fun test() {
J.foo("", <!NAMED_ARGUMENTS_NOT_ALLOWED!>r<!> = { }, <!NAMED_ARGUMENTS_NOT_ALLOWED!>z<!> = false)
}
@@ -5480,6 +5480,16 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest("compiler/testData/diagnostics/tests/namedArguments/disallowForJavaMethods.kt"); doTest("compiler/testData/diagnostics/tests/namedArguments/disallowForJavaMethods.kt");
} }
@TestMetadata("disallowForSamAdapterConstructor.kt")
public void testDisallowForSamAdapterConstructor() throws Exception {
doTest("compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterConstructor.kt");
}
@TestMetadata("disallowForSamAdapterFunction.kt")
public void testDisallowForSamAdapterFunction() throws Exception {
doTest("compiler/testData/diagnostics/tests/namedArguments/disallowForSamAdapterFunction.kt");
}
@TestMetadata("namedArgumentsAndDefaultValues.kt") @TestMetadata("namedArgumentsAndDefaultValues.kt")
public void testNamedArgumentsAndDefaultValues() throws Exception { public void testNamedArgumentsAndDefaultValues() throws Exception {
doTest("compiler/testData/diagnostics/tests/namedArguments/namedArgumentsAndDefaultValues.kt"); doTest("compiler/testData/diagnostics/tests/namedArguments/namedArgumentsAndDefaultValues.kt");
@@ -28,13 +28,14 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
private Boolean hasStableParameterNames = null; private Boolean hasStableParameterNames = null;
private Boolean hasSynthesizedParameterNames = null; private Boolean hasSynthesizedParameterNames = null;
private JavaConstructorDescriptor( protected JavaConstructorDescriptor(
@NotNull ClassDescriptor containingDeclaration, @NotNull ClassDescriptor containingDeclaration,
@Nullable JavaConstructorDescriptor original, @Nullable JavaConstructorDescriptor original,
@NotNull Annotations annotations, @NotNull Annotations annotations,
boolean isPrimary boolean isPrimary,
@NotNull Kind kind
) { ) {
super(containingDeclaration, original, annotations, isPrimary, Kind.DECLARATION); super(containingDeclaration, original, annotations, isPrimary, kind);
} }
@NotNull @NotNull
@@ -43,7 +44,7 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
@NotNull Annotations annotations, @NotNull Annotations annotations,
boolean isPrimary boolean isPrimary
) { ) {
return new JavaConstructorDescriptor(containingDeclaration, null, annotations, isPrimary); return new JavaConstructorDescriptor(containingDeclaration, null, annotations, isPrimary, Kind.DECLARATION);
} }
@Override @Override
@@ -80,7 +81,7 @@ public class JavaConstructorDescriptor extends ConstructorDescriptorImpl impleme
"kind: " + kind); "kind: " + kind);
} }
JavaConstructorDescriptor result = JavaConstructorDescriptor result =
new JavaConstructorDescriptor((ClassDescriptor) newOwner, this, Annotations.EMPTY /* TODO */, isPrimary); new JavaConstructorDescriptor((ClassDescriptor) newOwner, this, Annotations.EMPTY /* TODO */, isPrimary, kind);
result.setHasStableParameterNames(hasStableParameterNames()); result.setHasStableParameterNames(hasStableParameterNames());
result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames()); result.setHasSynthesizedParameterNames(hasSynthesizedParameterNames());
return result; return result;
@@ -30,7 +30,7 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
private Boolean hasStableParameterNames = null; private Boolean hasStableParameterNames = null;
private Boolean hasSynthesizedParameterNames = null; private Boolean hasSynthesizedParameterNames = null;
private JavaMethodDescriptor( protected JavaMethodDescriptor(
@NotNull DeclarationDescriptor containingDeclaration, @NotNull DeclarationDescriptor containingDeclaration,
@Nullable SimpleFunctionDescriptor original, @Nullable SimpleFunctionDescriptor original,
@NotNull Annotations annotations, @NotNull Annotations annotations,
@@ -19,5 +19,6 @@ package org.jetbrains.jet.lang.resolve.java.descriptor;
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor; import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
import org.jetbrains.jet.lang.descriptors.SynthesizedCallableMemberDescriptor; import org.jetbrains.jet.lang.descriptors.SynthesizedCallableMemberDescriptor;
public interface SamAdapterDescriptor<D extends FunctionDescriptor> extends FunctionDescriptor, SynthesizedCallableMemberDescriptor<D> { public interface SamAdapterDescriptor<D extends FunctionDescriptor>
extends FunctionDescriptor, JavaCallableMemberDescriptor, SynthesizedCallableMemberDescriptor<D> {
} }
@@ -60,7 +60,7 @@ public class LazyJavaClassMemberScope(
val constructor = resolveConstructor(jCtor, getContainingDeclaration(), jClass.isStatic()) val constructor = resolveConstructor(jCtor, getContainingDeclaration(), jClass.isStatic())
val samAdapter = resolveSamAdapter(constructor) val samAdapter = resolveSamAdapter(constructor)
if (samAdapter != null) { if (samAdapter != null) {
(samAdapter as ConstructorDescriptorImpl).setReturnType(containingDeclaration.getDefaultType()) samAdapter.setReturnType(containingDeclaration.getDefaultType())
listOf(constructor, samAdapter) listOf(constructor, samAdapter)
} }
else else
@@ -108,13 +108,13 @@ public class LazyJavaClassMemberScope(
return MethodSignatureData(effectiveSignature, superFunctions, propagated.getErrors() + effectiveSignature.getErrors()) return MethodSignatureData(effectiveSignature, superFunctions, propagated.getErrors() + effectiveSignature.getErrors())
} }
private fun resolveSamAdapter(original: ConstructorDescriptor): ConstructorDescriptor? { private fun resolveSamAdapter(original: JavaConstructorDescriptor): JavaConstructorDescriptor? {
return if (SingleAbstractMethodUtils.isSamAdapterNecessary(original)) return if (SingleAbstractMethodUtils.isSamAdapterNecessary(original))
SingleAbstractMethodUtils.createSamAdapterConstructor(original) as ConstructorDescriptor SingleAbstractMethodUtils.createSamAdapterConstructor(original) as JavaConstructorDescriptor
else null else null
} }
private fun resolveConstructor(constructor: JavaMethod, classDescriptor: ClassDescriptor, isStaticClass: Boolean): ConstructorDescriptor { private fun resolveConstructor(constructor: JavaMethod, classDescriptor: ClassDescriptor, isStaticClass: Boolean): JavaConstructorDescriptor {
val constructorDescriptor = JavaConstructorDescriptor.createJavaConstructor(classDescriptor, Annotations.EMPTY, /* isPrimary = */ false) val constructorDescriptor = JavaConstructorDescriptor.createJavaConstructor(classDescriptor, Annotations.EMPTY, /* isPrimary = */ false)
val valueParameters = resolveValueParameters(c, constructorDescriptor, constructor.getValueParameters()) val valueParameters = resolveValueParameters(c, constructorDescriptor, constructor.getValueParameters())
@@ -75,7 +75,7 @@ public abstract class LazyJavaMemberScope(
(name: Name): Collection<FunctionDescriptor> (name: Name): Collection<FunctionDescriptor>
-> ->
val methods = memberIndex().findMethodsByName(name) val methods = memberIndex().findMethodsByName(name)
val functions = LinkedHashSet( val functions = LinkedHashSet<SimpleFunctionDescriptor>(
methods.stream() methods.stream()
// values() and valueOf() are added manually, see LazyJavaClassDescriptor::getClassObjectDescriptor() // values() and valueOf() are added manually, see LazyJavaClassDescriptor::getClassObjectDescriptor()
.filter{ m -> !DescriptorResolverUtils.shouldBeInEnumClassObject(m) } .filter{ m -> !DescriptorResolverUtils.shouldBeInEnumClassObject(m) }
@@ -110,7 +110,7 @@ public abstract class LazyJavaMemberScope(
abstract fun resolveMethodSignature(method: JavaMethod, methodTypeParameters: List<TypeParameterDescriptor>, abstract fun resolveMethodSignature(method: JavaMethod, methodTypeParameters: List<TypeParameterDescriptor>,
returnType: JetType, valueParameters: ResolvedValueParameters): MethodSignatureData returnType: JetType, valueParameters: ResolvedValueParameters): MethodSignatureData
fun resolveMethodToFunctionDescriptor(method: JavaMethod, record: Boolean = true): SimpleFunctionDescriptor { fun resolveMethodToFunctionDescriptor(method: JavaMethod, record: Boolean = true): JavaMethodDescriptor {
val functionDescriptorImpl = JavaMethodDescriptor.createJavaMethod(_containingDeclaration, c.resolveAnnotations(method), method.getName()) val functionDescriptorImpl = JavaMethodDescriptor.createJavaMethod(_containingDeclaration, c.resolveAnnotations(method), method.getName())
@@ -216,9 +216,9 @@ public abstract class LazyJavaMemberScope(
return ResolvedValueParameters(descriptors, synthesizedNames) return ResolvedValueParameters(descriptors, synthesizedNames)
} }
private fun resolveSamAdapter(original: SimpleFunctionDescriptor): SimpleFunctionDescriptor? { private fun resolveSamAdapter(original: JavaMethodDescriptor): JavaMethodDescriptor? {
return if (SingleAbstractMethodUtils.isSamAdapterNecessary(original)) return if (SingleAbstractMethodUtils.isSamAdapterNecessary(original))
SingleAbstractMethodUtils.createSamAdapterFunction(original) as SimpleFunctionDescriptor SingleAbstractMethodUtils.createSamAdapterFunction(original) as JavaMethodDescriptor
else null else null
} }
@@ -17,22 +17,22 @@
package org.jetbrains.jet.lang.resolve.java.sam; package org.jetbrains.jet.lang.resolve.java.sam;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ConstructorDescriptor; import org.jetbrains.jet.lang.resolve.java.descriptor.JavaConstructorDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.ConstructorDescriptorImpl;
import org.jetbrains.jet.lang.resolve.java.descriptor.SamAdapterDescriptor; import org.jetbrains.jet.lang.resolve.java.descriptor.SamAdapterDescriptor;
/* package */ class SamAdapterConstructorDescriptor extends ConstructorDescriptorImpl /* package */ class SamAdapterConstructorDescriptor extends JavaConstructorDescriptor implements SamAdapterDescriptor<JavaConstructorDescriptor> {
implements SamAdapterDescriptor<ConstructorDescriptor> { private final JavaConstructorDescriptor declaration;
private final ConstructorDescriptor declaration;
public SamAdapterConstructorDescriptor(@NotNull ConstructorDescriptor declaration) { public SamAdapterConstructorDescriptor(@NotNull JavaConstructorDescriptor declaration) {
super(declaration.getContainingDeclaration(), null, declaration.getAnnotations(), declaration.isPrimary(), Kind.SYNTHESIZED); super(declaration.getContainingDeclaration(), null, declaration.getAnnotations(), declaration.isPrimary(), Kind.SYNTHESIZED);
this.declaration = declaration; this.declaration = declaration;
setHasStableParameterNames(declaration.hasStableParameterNames());
setHasSynthesizedParameterNames(declaration.hasSynthesizedParameterNames());
} }
@NotNull @NotNull
@Override @Override
public ConstructorDescriptor getBaseForSynthesized() { public JavaConstructorDescriptor getBaseForSynthesized() {
return declaration; return declaration;
} }
} }
@@ -17,22 +17,22 @@
package org.jetbrains.jet.lang.resolve.java.sam; package org.jetbrains.jet.lang.resolve.java.sam;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor; import org.jetbrains.jet.lang.resolve.java.descriptor.JavaMethodDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.SimpleFunctionDescriptorImpl;
import org.jetbrains.jet.lang.resolve.java.descriptor.SamAdapterDescriptor; import org.jetbrains.jet.lang.resolve.java.descriptor.SamAdapterDescriptor;
/* package */ class SamAdapterFunctionDescriptor extends SimpleFunctionDescriptorImpl /* package */ class SamAdapterFunctionDescriptor extends JavaMethodDescriptor implements SamAdapterDescriptor<JavaMethodDescriptor> {
implements SamAdapterDescriptor<SimpleFunctionDescriptor> { private final JavaMethodDescriptor declaration;
private final SimpleFunctionDescriptor declaration;
public SamAdapterFunctionDescriptor(@NotNull SimpleFunctionDescriptor declaration) { public SamAdapterFunctionDescriptor(@NotNull JavaMethodDescriptor declaration) {
super(declaration.getContainingDeclaration(), null, declaration.getAnnotations(), declaration.getName(), Kind.SYNTHESIZED); super(declaration.getContainingDeclaration(), null, declaration.getAnnotations(), declaration.getName(), Kind.SYNTHESIZED);
this.declaration = declaration; this.declaration = declaration;
setHasStableParameterNames(declaration.hasStableParameterNames());
setHasSynthesizedParameterNames(declaration.hasSynthesizedParameterNames());
} }
@NotNull @NotNull
@Override @Override
public SimpleFunctionDescriptor getBaseForSynthesized() { public JavaMethodDescriptor getBaseForSynthesized() {
return declaration; return declaration;
} }
} }
@@ -22,9 +22,7 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.Annotations; import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl; import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
import org.jetbrains.jet.lang.descriptors.impl.ValueParameterDescriptorImpl; import org.jetbrains.jet.lang.descriptors.impl.ValueParameterDescriptorImpl;
import org.jetbrains.jet.lang.resolve.java.descriptor.JavaClassDescriptor; import org.jetbrains.jet.lang.resolve.java.descriptor.*;
import org.jetbrains.jet.lang.resolve.java.descriptor.SamAdapterDescriptor;
import org.jetbrains.jet.lang.resolve.java.descriptor.SamConstructorDescriptor;
import org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils; import org.jetbrains.jet.lang.resolve.java.resolver.DescriptorResolverUtils;
import org.jetbrains.jet.lang.resolve.java.structure.*; import org.jetbrains.jet.lang.resolve.java.structure.*;
import org.jetbrains.jet.lang.resolve.name.FqName; import org.jetbrains.jet.lang.resolve.name.FqName;
@@ -180,7 +178,7 @@ public class SingleAbstractMethodUtils {
} }
@NotNull @NotNull
public static SamAdapterDescriptor<SimpleFunctionDescriptor> createSamAdapterFunction(@NotNull final SimpleFunctionDescriptor original) { public static SamAdapterDescriptor<JavaMethodDescriptor> createSamAdapterFunction(@NotNull final JavaMethodDescriptor original) {
final SamAdapterFunctionDescriptor result = new SamAdapterFunctionDescriptor(original); final SamAdapterFunctionDescriptor result = new SamAdapterFunctionDescriptor(original);
return initSamAdapter(original, result, new FunctionInitializer() { return initSamAdapter(original, result, new FunctionInitializer() {
@Override @Override
@@ -203,7 +201,7 @@ public class SingleAbstractMethodUtils {
} }
@NotNull @NotNull
public static SamAdapterDescriptor<ConstructorDescriptor> createSamAdapterConstructor(@NotNull final ConstructorDescriptor original) { public static SamAdapterDescriptor<JavaConstructorDescriptor> createSamAdapterConstructor(@NotNull final JavaConstructorDescriptor original) {
final SamAdapterConstructorDescriptor result = new SamAdapterConstructorDescriptor(original); final SamAdapterConstructorDescriptor result = new SamAdapterConstructorDescriptor(original);
return initSamAdapter(original, result, new FunctionInitializer() { return initSamAdapter(original, result, new FunctionInitializer() {
@Override @Override
@@ -255,9 +255,9 @@ private object KotlinResolveDataProvider {
TopDownAnalysisParameters.createForLazy( TopDownAnalysisParameters.createForLazy(
resolveSession.getStorageManager(), resolveSession.getStorageManager(),
resolveSession.getExceptionTracker(), resolveSession.getExceptionTracker(),
analyzeCompletely = { true }, /* analyzeCompletely = */ { true },
analyzingBootstrapLibrary = false, /* analyzingBootstrapLibrary = */ false,
declaredLocally = false /* declaredLocally = */ false
), ),
listOf(analyzableElement) listOf(analyzableElement)
) )