Skip to content

Commit 65c86b7

Browse files
committed
[ReconstructType] Fix decl lookup when there are multiple constructors
We were failing to find init() decls whenever there was more than one candidate. The mangled function type we get will be (args...) -> T, but the real thing is (T.Type) -> (args...) -> T. Strip the metatype so we can match successfully.
1 parent e6a519f commit 65c86b7

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/IDE/ReconstructType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,6 +1058,15 @@ static void VisitNodeConstructor(
10581058
case TypeKind::Function: {
10591059
const AnyFunctionType *identifier_func =
10601060
identifier_type->getAs<AnyFunctionType>();
1061+
1062+
// inits are typed as (Foo.Type) -> (args...) -> Foo, but don't
1063+
// assert that in case we're dealing with broken code.
1064+
if (identifier_func->getInput()->is<AnyMetatypeType>() &&
1065+
identifier_func->getResult()->is<AnyFunctionType>()) {
1066+
identifier_func =
1067+
identifier_func->getResult()->getAs<AnyFunctionType>();
1068+
}
1069+
10611070
const AnyFunctionType *type_func =
10621071
type_result._types.front()->getAs<AnyFunctionType>();
10631072
if (CanType(identifier_func->getResult()

test/IDE/reconstruct_type_from_mangled_name.swift

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ struct Mystruct1 {
77
var intField = 3
88
// CHECK: decl: var intField: Int
99
}
10+
struct MyStruct2 {
11+
// CHECK: decl: struct MyStruct2
12+
init() {}
13+
// CHECK: decl: init()
14+
init(x: Int) {}
15+
// CHECK: decl: init(x: Int)
16+
init(x: Int, y: Int) {}
17+
// CHECK: decl: init(x: Int, y: Int)
18+
}
1019

1120
class Myclass1 {
1221
// CHECK: decl: class Myclass1
@@ -16,13 +25,14 @@ class Myclass1 {
1625

1726
func f1() {
1827
// CHECK: decl: func f1()
19-
var s1ins = Mystruct1()
28+
var s1ins = Mystruct1() // Implicit ctor
2029
// CHECK: decl: var s1ins: Mystruct1
21-
// FIXME: missing init()?
22-
// CHECK: dref: FAILURE for 'Mystruct1' usr=s:FV14swift_ide_test9Mystruct1cFT_S0_
23-
// CHECK: type: Mystruct1
30+
// CHECK: dref: init() for 'Mystruct1'
31+
_ = Mystruct1(intField: 1) // Implicit ctor
32+
// CHECK: dref: init(intField: Int) for 'Mystruct1'
2433

2534
s1ins.intField = 34
35+
// CHECK: type: Mystruct1
2636
// CHECK: type: Int
2737

2838
var c1ins = Myclass1()
@@ -70,6 +80,9 @@ class Myclass2 {
7080

7181
arr3.append(Myclass1())
7282
// CHECK: type: @lvalue Array<Myclass1> -> Myclass1 -> ()
83+
84+
_ = Myclass2.init()
85+
// CHECK: dref: init()
7386
}
7487
}
7588

0 commit comments

Comments
 (0)