-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[K/N][Tests] Fix crashes when casting to an Obj-C class companion
- Loading branch information
Showing
5 changed files
with
108 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 85 additions & 8 deletions
93
native/native.tests/testData/codegen/cinterop/objc/kt65260.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,120 @@ | ||
import java.lang.Exception | ||
|
||
/* | ||
* Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
*/ | ||
// TARGET_BACKEND: NATIVE | ||
// DISABLE_NATIVE: isAppleTarget=false | ||
|
||
// MODULE: cinterop | ||
// FILE: conversion.def | ||
language = Objective-C | ||
headers = conversion.h | ||
headerFilter = conversion.h | ||
|
||
// FILE: conversion.h | ||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
__attribute__((objc_runtime_name("Foo"))) | ||
@interface A : NSObject | ||
@end | ||
|
||
__attribute__((objc_runtime_name("Bar"))) | ||
@interface B : A | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END | ||
|
||
// FILE: conversion.m | ||
#import "conversion.h" | ||
|
||
@implementation A | ||
@end | ||
|
||
@implementation B | ||
@end | ||
|
||
// MODULE: lib(cinterop) | ||
// FILE: lib.kt | ||
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) | ||
import conversion.* | ||
import platform.darwin.* | ||
import kotlinx.cinterop.* | ||
import kotlin.test.* | ||
|
||
class ANativeHeir : A() { | ||
companion object | ||
} | ||
|
||
class BNativeHeir : B() { | ||
companion object | ||
} | ||
|
||
fun testExternalObjCMetaClassCast() { | ||
try { | ||
BNativeHeir as A.Companion | ||
} catch (e: Exception) { | ||
assertTrue(e is ClassCastException) | ||
} | ||
|
||
try { | ||
ANativeHeir as A.Companion | ||
} catch (e: Exception) { | ||
assertTrue(e is ClassCastException) | ||
} | ||
|
||
assertTrue(A is A.Companion) | ||
|
||
val fooObjectClass: Any = A | ||
val barObjectClass: Any = B | ||
assertFalse(fooObjectClass is BMeta) | ||
assertTrue(barObjectClass is AMeta) | ||
} | ||
|
||
|
||
// MODULE: main(cinterop, lib) | ||
// FILE: main.kt | ||
@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) | ||
import platform.Foundation.* | ||
import platform.darwin.* | ||
import kotlinx.cinterop.internal.* | ||
import kotlin.test.* | ||
|
||
fun test1() { | ||
fun testAnyCast() { | ||
try { | ||
Any() as NSObject.Companion | ||
} catch (e: Exception) { | ||
assertTrue(e is ClassCastException) | ||
} | ||
} | ||
|
||
fun test2() { | ||
try { | ||
Any() as NSNumber.Companion | ||
} catch (e: Exception) { | ||
assertTrue(e is ClassCastException) | ||
} | ||
} | ||
|
||
fun test3() { | ||
fun testMetaClassCast() { | ||
assertFalse(Any() is NSObject.Companion) | ||
assertFalse(Any() is NSNumber.Companion) | ||
|
||
val nsObjectClass: Any = NSObject | ||
assertFalse(nsObjectClass is NSNumberMeta) | ||
|
||
try { | ||
NSNumber as NSObject.Companion | ||
} catch (e: Exception) { | ||
assertTrue(e is ClassCastException) | ||
} | ||
assertTrue(NSData is NSData.Companion) | ||
} | ||
|
||
fun box(): String { | ||
test1() | ||
test2() | ||
test3() | ||
testAnyCast() | ||
testMetaClassCast() | ||
testExternalObjCMetaClassCast() | ||
|
||
return "OK" | ||
} |
3 changes: 2 additions & 1 deletion
3
...ests-gen/org/jetbrains/kotlin/konan/test/blackbox/FirNativeCodegenLocalTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
...s/tests-gen/org/jetbrains/kotlin/konan/test/blackbox/NativeCodegenLocalTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.