Skip to content

[Tests] Add a test to round-trip types through mangled names. #38649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/Testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,16 @@ code for the target that is not the build machine:

Add ``REQUIRES: static_stdlib`` to the test.

* ``%target-rtti-opt``: the ``-frtti`` or ``-fno-rtti`` option required to
link with the Swift libraries on the target platform.

* ``%target-cxx-lib``: the argument to add to the command line when using
``swiftc`` and linking in a C++ object file. Typically ``-lc++`` or
``-lstdc++`` depending on platform.

* ``%target-msvc-runtime-opt``: for Windows, the MSVC runtime option, e.g.
``-MD``, to use when building C/C++ code to link with Swift.

Always use ``%target-*`` substitutions unless you have a good reason. For
example, an exception would be a test that checks how the compiler handles
mixing module files for incompatible platforms (that test would need to compile
Expand Down
10 changes: 7 additions & 3 deletions stdlib/public/runtime/Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,9 @@ class TypeInfo {
const ContextDescriptor *
_searchConformancesByMangledTypeName(Demangle::NodePointer node);

SWIFT_RUNTIME_EXPORT
Demangle::NodePointer _swift_buildDemanglingForMetadata(const Metadata *type,
Demangle::Demangler &Dem);
Demangle::Demangler &Dem);

/// Callback used to provide the substitution of a generic parameter
/// (described by depth/index) to its metadata.
Expand Down Expand Up @@ -367,13 +368,15 @@ class TypeInfo {
unsigned index) const;
};

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreturn-type-c-linkage"
/// Retrieve the type metadata described by the given demangled type name.
///
/// \p substGenericParam Function that provides generic argument metadata
/// given a particular generic parameter specified by depth/index.
/// \p substWitnessTable Function that provides witness tables given a
/// particular dependent conformance index.
SWIFT_CC(swift)
SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
TypeLookupErrorOr<TypeInfo> swift_getTypeByMangledNode(
MetadataRequest request,
Demangler &demangler,
Expand All @@ -388,13 +391,14 @@ class TypeInfo {
/// given a particular generic parameter specified by depth/index.
/// \p substWitnessTable Function that provides witness tables given a
/// particular dependent conformance index.
SWIFT_CC(swift)
SWIFT_RUNTIME_EXPORT SWIFT_CC(swift)
TypeLookupErrorOr<TypeInfo> swift_getTypeByMangledName(
MetadataRequest request,
StringRef typeName,
const void * const *arguments,
SubstGenericParameterFn substGenericParam,
SubstDependentWitnessTableFn substWitnessTable);
#pragma clang diagnostic pop

/// Function object that produces substitutions for the generic parameters
/// that occur within a mangled name, using the complete set of generic
Expand Down
74 changes: 74 additions & 0 deletions test/TypeRoundTrip/Inputs/RoundTrip/RoundTrip.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "swift/ABI/Metadata.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Reflection/TypeRefBuilder.h"
#include "swift/Remote/MetadataReader.h"
#include "swift/Remote/InProcessMemoryReader.h"

#include "Private.h"

#include <cstdio>

using namespace swift;

static std::string nameForMetadata(const Metadata *md)
{
Demangle::StackAllocatedDemangler<1024> dem;
auto nodeTree = _swift_buildDemanglingForMetadata(md, dem);
if (!nodeTree)
return "<unknown>";

std::string result = Demangle::nodeToString(nodeTree);
return result;
}

extern "C" SWIFT_CC(swift) void roundTripType(const Metadata *md) {
// Get a name for it
const std::string mdName = ::nameForMetadata(md);

// Convert it to a Node tree
Demangle::StackAllocatedDemangler<1024> dem;
auto nodeTree = _swift_buildDemanglingForMetadata(md, dem);

// Mangle that
std::string mangledName = Demangle::mangleNode(nodeTree);

// Look up the result
auto result = swift_getTypeByMangledName(MetadataState::Abstract,
mangledName,
nullptr,
[](unsigned, unsigned){ return nullptr; },
[](const Metadata *, unsigned) { return nullptr; });
if (result.isError()) {
auto err = result.getError();
char *errStr = err->copyErrorString();
printf("FAIL: %s (%p) -> %s -> ERROR %s\n",
mdName.c_str(), md, mangledName.c_str(), errStr);
err->freeErrorString(errStr);
nodeTree->dump();

result = swift_getTypeByMangledNode(MetadataState::Abstract,
dem,
nodeTree,
nullptr,
[](unsigned, unsigned){ return nullptr; },
[](const Metadata *, unsigned) { return nullptr; });
if (result.isError()) {
err = result.getError();
char *errStr = err->copyErrorString();
printf("=> Also failed on node: %s\n", errStr);
err->freeErrorString(errStr);
}
return;
}

const Metadata *md2 = result.getType().getMetadata();

std::string md2Name = "<FAIL>";

if (md2)
md2Name = ::nameForMetadata(md2);

printf("%s: %s (%p) -> %s -> %s (%p)\n",
md == md2 ? "PASS" : "FAIL",
mdName.c_str(), md, mangledName.c_str(), md2Name.c_str(), md2);
}
2 changes: 2 additions & 0 deletions test/TypeRoundTrip/Inputs/RoundTrip/RoundTrip.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@_silgen_name("roundTripType")
public func roundTripType(_: Any.Type)
19 changes: 19 additions & 0 deletions test/TypeRoundTrip/Inputs/testcases/builtins.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import RoundTrip

public func test() {
roundTripType(Int.self)
roundTripType(UInt.self)

roundTripType(Float.self)
roundTripType(Double.self)

roundTripType(Int8.self)
roundTripType(Int16.self)
roundTripType(Int32.self)
roundTripType(Int64.self)

roundTripType(UInt8.self)
roundTripType(UInt16.self)
roundTripType(UInt32.self)
roundTripType(UInt64.self)
}
6 changes: 6 additions & 0 deletions test/TypeRoundTrip/Inputs/testcases/concurrency.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import RoundTrip

public func test() {
roundTripType(Array<(Int) async -> ()>.self)
roundTripType(Array<(Int) async throws -> ()>.self)
}
35 changes: 35 additions & 0 deletions test/TypeRoundTrip/Inputs/testcases/existentials.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import RoundTrip

protocol P {}
protocol Q {}
class C {}
class D : C, P, Q {}

public func test() {
roundTripType(Any.self)
roundTripType(AnyObject.self)
roundTripType(P.self)
roundTripType((C & P).self)
roundTripType((P & AnyObject).self)
roundTripType((P & Q).self)
roundTripType((C & P & Q).self)
roundTripType((P & Q & AnyObject).self)

roundTripType(Any.Type.self)
roundTripType(AnyObject.Type.self)
roundTripType(P.Type.self)
roundTripType((C & P).Type.self)
roundTripType((P & AnyObject).Type.self)
roundTripType((P & Q).Type.self)
roundTripType((C & P & Q).Type.self)
roundTripType((P & Q & AnyObject).Type.self)

roundTripType(Any.Protocol.self)
roundTripType(AnyObject.Protocol.self)
roundTripType(P.Protocol.self)
roundTripType((C & P).Protocol.self)
roundTripType((P & AnyObject).Protocol.self)
roundTripType((P & Q).Protocol.self)
roundTripType((C & P & Q).Protocol.self)
roundTripType((P & Q & AnyObject).Protocol.self)
}
34 changes: 34 additions & 0 deletions test/TypeRoundTrip/Inputs/testcases/extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import RoundTrip

struct Concrete {}

extension Concrete {
struct Nested {}
}

struct Generic<T> {}

protocol Proto {}

struct Foo : Proto {}

class Bar {}

extension Generic where T : Proto {
struct Nested1 {}
}

extension Generic where T == Int {
struct Nested2 {}
}

extension Generic where T: AnyObject {
struct NestedViaAnyObject {}
}

public func test() {
roundTripType(Concrete.Nested.self)
roundTripType(Generic<Foo>.Nested1.self)
roundTripType(Generic<Int>.Nested2.self)
roundTripType(Generic<Bar>.NestedViaAnyObject.self)
}
14 changes: 14 additions & 0 deletions test/TypeRoundTrip/Inputs/testcases/function_types.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import RoundTrip

class Class {}

let fn: (Int, Class, __owned Class, Any, inout Int) -> (Int, Class, Any) = {
_, _, _, _, _ in fatalError()
}

let fn2: () throws -> () = {}

public func test() {
roundTripType(type(of: fn))
roundTripType(type(of: fn2))
}
84 changes: 84 additions & 0 deletions test/TypeRoundTrip/Inputs/testcases/generics.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import RoundTrip

protocol First {
associatedtype Assoc : First

// Just to confuse things -- a method with the same name as an
// associated type
func Assoc(_: Int) -> Int
}

protocol Second {
associatedtype Assoc : Second
}

struct OuterFirst<A : First, B : First> {
struct Inner<C : First, D : First> {
func method(a: A, b: B, c: C, d: D) {
do {
let _: (A, A.Assoc, A.Assoc.Assoc) -> () = { _, _, _ in }
}
do {
let _: (B, B.Assoc, B.Assoc.Assoc) -> () = { _, _, _ in }
}
do {
let _: (C, C.Assoc, C.Assoc.Assoc) -> () = { _, _, _ in }
}
do {
let _: (D, D.Assoc, D.Assoc.Assoc) -> () = { _, _, _ in }
}
}
}
}

struct OuterBoth<A : First & Second, B : First & Second> {
struct Inner<C : First & Second, D : First & Second> {
func method(a: A, b: B, c: C, d: D) {
do {
let _: (A, A.Assoc, A.Assoc.Assoc) -> () = { _, _, _ in }
}
do {
let _: (B, B.Assoc, B.Assoc.Assoc) -> () = { _, _, _ in }
}
do {
let _: (C, C.Assoc, C.Assoc.Assoc) -> () = { _, _, _ in }
}
do {
let _: (D, D.Assoc, D.Assoc.Assoc) -> () = { _, _, _ in }
}
}
}
}

struct F1: First {
typealias Assoc = F2

func Assoc(_ x: Int) -> Int { return x + 1 }
}

struct F2: First {
typealias Assoc = F1

func Assoc(_ x: Int) -> Int { return x * 2 }
}

struct FS1: First, Second {
typealias Assoc = FS2

func Assoc(_ x: Int) -> Int { return x - 1 }
}

struct FS2: First, Second {
typealias Assoc = FS1

func Assoc(_ x: Int) -> Int { return x / 2 }
}

public func test() {
roundTripType(OuterFirst<F1,F2>.self)
roundTripType(OuterBoth<FS1,FS2>.self)
roundTripType(OuterFirst<F1,F2>.Inner<F2,F1>.self)
roundTripType(OuterBoth<FS1,FS2>.Inner<FS2,FS1>.self)
roundTripType(type(of:OuterFirst<F1,F2>.Inner<F2,F1>.method))
roundTripType(type(of:OuterBoth<FS1,FS2>.Inner<FS2,FS1>.method))
}
Loading