Make incompleteTypes test more robust.

Currently, Kotlin strings that are passed to C functions are copied under the hood on the callsite. These copies have a short lifetime which might cause problems if it should outlive current call.
This commit is contained in:
Sergey Bogolepov
2021-02-11 15:54:39 +07:00
committed by Vasily Levchenko
parent 70832b16a3
commit 0ecd76c3cb
2 changed files with 8 additions and 4 deletions
@@ -1,21 +1,24 @@
#include <string>
#include "library.h" #include "library.h"
extern "C" { extern "C" {
struct S { struct S {
const char* name; std::string name;
}; };
struct S s = { S s = {
.name = "initial" .name = "initial"
}; };
void setContent(struct S* s, const char* name) { void setContent(struct S* s, const char* name) {
// Note that copy here is intentional: we use it as a workaround
// for short lifetime of copy of the passed Kotlin string.
s->name = name; s->name = name;
} }
const char* getContent(struct S* s) { const char* getContent(struct S* s) {
return s->name; return s->name.c_str();
} }
union U { union U {
@@ -9,7 +9,8 @@ fun main() {
assertEquals("initial", getContent(s.ptr)?.toKString()) assertEquals("initial", getContent(s.ptr)?.toKString())
setContent(s.ptr, "yo") setContent(s.ptr, "yo")
assertEquals("yo", getContent(s.ptr)?.toKString()) val ptr = getContent(s.ptr)
assertEquals("yo", ptr?.toKString())
assertEquals(0.0, getDouble(u.ptr)) assertEquals(0.0, getDouble(u.ptr))
setDouble(u.ptr, Double.MIN_VALUE) setDouble(u.ptr, Double.MIN_VALUE)