Skip to content

Runtime: Fix metatype to mangling of function with single tuple-typed argument [5.0] #21156

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
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
5 changes: 2 additions & 3 deletions lib/AST/ASTMangler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1767,9 +1767,8 @@ void ASTMangler::appendFunctionInputType(
const auto &param = params.front();
auto type = param.getPlainType();

// If this is just a single parenthesized type,
// to save space in the mangled name, let's encode
// it as a single type dropping sugar.
// If the sole unlabeled parameter has a non-tuple type, encode
// the parameter list as a single type.
if (!param.hasLabel() && !param.isVariadic() &&
!isa<TupleType>(type.getPointer())) {
appendTypeListElement(Identifier(), type, param.getParameterFlags());
Expand Down
17 changes: 12 additions & 5 deletions stdlib/public/runtime/Demangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,14 +495,21 @@ swift::_swift_buildDemanglingForMetadata(const Metadata *type,
NodePointer totalInput = nullptr;
switch (inputs.size()) {
case 1: {
auto &singleParam = inputs.front();
auto singleParam = inputs.front();

// If the sole unlabeled parameter has a non-tuple type, encode
// the parameter list as a single type.
if (!singleParam.second) {
totalInput = singleParam.first;
break;
auto singleType = singleParam.first;
if (singleType->getKind() == Node::Kind::Type)
singleType = singleType->getFirstChild();
if (singleType->getKind() != Node::Kind::Tuple) {
totalInput = singleParam.first;
break;
}
}

// If single parameter has a variadic marker it
// requires a tuple wrapper.
// Otherwise it requires a tuple wrapper.
LLVM_FALLTHROUGH;
}

Expand Down
8 changes: 8 additions & 0 deletions test/RemoteAST/structural_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ typealias Fn8 = (String, Int, Double, Float) -> ()
printType(Fn8.self)
// CHECK: found type: (String, Int, Double, Float) -> ()

typealias Fn9 = ((Int, Float)) -> ()
printType(Fn9.self)
// CHECK: found type: ((Int, Float)) -> ()

typealias Fn10 = (Int...) -> ()
printType(Fn10.self)
// CHECK: found type: (Int...) -> ()

typealias Tuple1 = (Int, Float, Int)
printType(Tuple1.self)
// CHECK: found type: (Int, Float, Int)
Expand Down
6 changes: 6 additions & 0 deletions test/stdlib/TypeName.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,17 @@ TypeNameTests.test("Prints") {
typealias F = () -> ()
typealias F2 = () -> () -> ()
typealias F3 = (() -> ()) -> ()
typealias F4 = (Int, Float) -> ()
typealias F5 = ((Int, Float)) -> ()
typealias F6 = (Int...) -> ()

expectEqual("() -> ()", _typeName(F.self))
expectEqual("() -> () -> ()", _typeName(F2.self))
expectEqual("(() -> ()) -> ()", _typeName(F3.self))
expectEqual("() -> ()", _typeName((() -> ()).self))
expectEqual("(Swift.Int, Swift.Float) -> ()", _typeName(F4.self))
expectEqual("((Swift.Int, Swift.Float)) -> ()", _typeName(F5.self))
expectEqual("(Swift.Int...) -> ()", _typeName(F6.self))

expectEqual("(main.P) -> main.P2 & main.P3",
_typeName(((P) -> P2 & P3).self))
Expand Down