JS: don't report about inline modifier on inline extension fun to external class

This commit is contained in:
Alexey Andreev
2017-01-20 16:08:08 +03:00
parent 0ca2ae7279
commit bc0550d7b7
9 changed files with 110 additions and 6 deletions
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.psi.KtNamedFunction;
import org.jetbrains.kotlin.psi.KtProperty;
import org.jetbrains.kotlin.resolve.inline.InlineAnalyzerExtension;
import org.jetbrains.kotlin.resolve.inline.InlineUtil;
import org.jetbrains.kotlin.resolve.inline.ReasonableInlineRule;
import java.util.Collections;
import java.util.List;
@@ -35,9 +36,11 @@ public class AnalyzerExtensions {
}
@NotNull private final BindingTrace trace;
@NotNull private final Iterable<ReasonableInlineRule> reasonableInlineRules;
public AnalyzerExtensions(@NotNull BindingTrace trace) {
public AnalyzerExtensions(@NotNull BindingTrace trace, @NotNull Iterable<ReasonableInlineRule> reasonableInlineRules) {
this.trace = trace;
this.reasonableInlineRules = reasonableInlineRules;
}
public void process(@NotNull BodiesResolveContext bodiesResolveContext) {
@@ -61,17 +64,17 @@ public class AnalyzerExtensions {
}
@NotNull
private static List<InlineAnalyzerExtension> getFunctionExtensions(@NotNull FunctionDescriptor functionDescriptor) {
private List<InlineAnalyzerExtension> getFunctionExtensions(@NotNull FunctionDescriptor functionDescriptor) {
if (InlineUtil.isInline(functionDescriptor)) {
return Collections.singletonList(InlineAnalyzerExtension.INSTANCE);
return Collections.singletonList(new InlineAnalyzerExtension(reasonableInlineRules));
}
return Collections.emptyList();
}
@NotNull
private static List<InlineAnalyzerExtension> getPropertyExtensions(@NotNull PropertyDescriptor propertyDescriptor) {
private List<InlineAnalyzerExtension> getPropertyExtensions(@NotNull PropertyDescriptor propertyDescriptor) {
if (InlineUtil.hasInlineAccessors(propertyDescriptor)) {
return Collections.singletonList(InlineAnalyzerExtension.INSTANCE);
return Collections.singletonList(new InlineAnalyzerExtension(reasonableInlineRules));
}
return Collections.emptyList();
}
@@ -26,7 +26,7 @@ import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
object InlineAnalyzerExtension : AnalyzerExtensions.AnalyzerExtension {
class InlineAnalyzerExtension(private val reasonableInlineRules: Iterable<ReasonableInlineRule>) : AnalyzerExtensions.AnalyzerExtension {
override fun process(descriptor: CallableMemberDescriptor, functionOrProperty: KtCallableDeclaration, trace: BindingTrace) {
checkModalityAndOverrides(descriptor, functionOrProperty, trace)
@@ -143,6 +143,8 @@ object InlineAnalyzerExtension : AnalyzerExtensions.AnalyzerExtension {
functionDescriptor.isInlineOnlyOrReifiable() ||
functionDescriptor.isHeader) return
if (reasonableInlineRules.any { it.isInlineReasonable(functionDescriptor, function, trace.bindingContext) }) return
val reportOn = function.modifierList?.getModifier(KtTokens.INLINE_KEYWORD) ?: function
trace.report(Errors.NOTHING_TO_INLINE.on(reportOn, functionDescriptor))
}
@@ -0,0 +1,25 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.resolve.inline
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.resolve.BindingContext
interface ReasonableInlineRule {
fun isInlineReasonable(descriptor: CallableMemberDescriptor, declaration: KtCallableDeclaration, context: BindingContext): Boolean
}
@@ -0,0 +1,3 @@
public final fun foo(/*0*/ p0: dynamic): dynamic
public final fun get(/*0*/ p0: dynamic): dynamic
public final fun get(/*0*/ p0: dynamic): dynamic
@@ -0,0 +1,9 @@
external class A {
class B
}
inline fun A.foo(x: Int): String = asDynamic().foo(x)
inline operator fun A.get(x: Int): String = asDynamic()[x]
inline operator fun A.B.get(x: Int): String = asDynamic()[x]
@@ -0,0 +1,19 @@
package
public inline fun A.foo(/*0*/ x: kotlin.Int): kotlin.String
public operator inline fun A.get(/*0*/ x: kotlin.Int): kotlin.String
public operator inline fun A.B.get(/*0*/ x: kotlin.Int): kotlin.String
public external final class A {
public constructor A()
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 B {
public constructor 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
}
}
@@ -653,6 +653,12 @@ public class DiagnosticsTestWithJsStdLibGenerated extends AbstractDiagnosticsTes
doTest(fileName);
}
@TestMetadata("inlineExtensionToNative.kt")
public void testInlineExtensionToNative() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/inlineExtensionToNative.kt");
doTest(fileName);
}
@TestMetadata("innerClass.kt")
public void testInnerClass() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithJsStdLib/native/innerClass.kt");
@@ -0,0 +1,36 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.resolve
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.inline.ReasonableInlineRule
object ExtensionFunctionToExternalIsInlinable : ReasonableInlineRule {
override fun isInlineReasonable(
descriptor: CallableMemberDescriptor,
declaration: KtCallableDeclaration,
context: BindingContext
): Boolean {
val receiverParameter = descriptor.extensionReceiverParameter ?: return false
val receiverClass = receiverParameter.value.type.constructor.declarationDescriptor as? ClassDescriptor ?: return false
return DescriptorUtils.isEffectivelyExternal(receiverClass)
}
}
@@ -67,5 +67,6 @@ object JsPlatformConfigurator : PlatformConfigurator(
container.useImpl<JsReflectionAPICallChecker>()
container.useImpl<JsNativeRttiChecker>()
container.useImpl<JsReifiedNativeChecker>()
container.useInstance(ExtensionFunctionToExternalIsInlinable)
}
}