Skip to content

Commit 178d6c3

Browse files
authored
Merge pull request #86146 from slavapestov/bogus-curry-thunk
Sema: Fix a case we form a bogus curry thunk
2 parents 5d40870 + 6c1978e commit 178d6c3

File tree

5 files changed

+95
-3
lines changed

5 files changed

+95
-3
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,8 +1047,7 @@ namespace {
10471047
// "T.init(...)" -- pretend it has two argument lists like
10481048
// a real '.' call.
10491049
if (isa<ConstructorDecl>(member) &&
1050-
isa<CallExpr>(prev) &&
1051-
isa<TypeExpr>(cast<CallExpr>(prev)->getFn())) {
1050+
isa<CallExpr>(prev)) {
10521051
assert(maxArgCount == 2);
10531052
return 2;
10541053
}

lib/Sema/PreCheckTarget.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,7 @@ TypeExpr *PreCheckTarget::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
19251925
// CSGen will diagnose cases that appear outside of pack expansion
19261926
// expressions.
19271927
options |= TypeResolutionFlags::AllowPackReferences;
1928-
const auto BaseTy = TypeResolution::resolveContextualType(
1928+
auto BaseTy = TypeResolution::resolveContextualType(
19291929
InnerTypeRepr, DC, options,
19301930
[](auto unboundTy) {
19311931
// FIXME: Don't let unbound generic types escape type resolution.
@@ -1938,6 +1938,10 @@ TypeExpr *PreCheckTarget::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
19381938
// TypeExpr pack elements are opened in CSGen.
19391939
/*packElementOpener*/ nullptr);
19401940

1941+
// Unwrap DynamicSelfType, because Self.Foo is the same as MyClass.Foo.
1942+
if (auto *SelfTy = BaseTy->getAs<DynamicSelfType>())
1943+
BaseTy = SelfTy->getSelfType();
1944+
19411945
if (BaseTy->mayHaveMembers()) {
19421946
// See if there is a member type with this name.
19431947
auto Result = TypeChecker::lookupMemberType(DC, BaseTy, Name,

test/Index/expressions.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,21 @@ func castExpr(x: Any) {
5757
// CHECK: [[@LINE+1]]:15 | struct/Swift | S1 | [[S1_USR]] | Ref
5858
_ = x as? S1
5959
}
60+
61+
// Test that initializers are indexed when called through Self.NestedType
62+
63+
// CHECK: [[@LINE+1]]:7 | class(internal)/Swift | Container | [[Container_USR:.*]] | Def
64+
class Container {
65+
// CHECK: [[@LINE+1]]:11 | class(internal)/Swift | NestedType | [[NestedType_USR:.*]] | Def
66+
class NestedType {
67+
// CHECK: [[@LINE+1]]:9 | constructor(internal)/Swift | init(value:) | [[NestedType_init_USR:.*]] | Def
68+
init(value: Int) {}
69+
}
70+
71+
func someFunc() {
72+
// CHECK: [[@LINE+3]]:13 | class/Swift | Container | [[Container_USR]] | Ref
73+
// CHECK: [[@LINE+2]]:18 | class/Swift | NestedType | [[NestedType_USR]] | Ref
74+
// CHECK: [[@LINE+1]]:18 | constructor/Swift | init(value:) | [[NestedType_init_USR]] | Ref,Call
75+
_ = Self.NestedType(value: 1)
76+
}
77+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
2+
3+
class Container {
4+
class NestedType {}
5+
6+
func someFunc1() {
7+
// This constructor call should not require a curry thunk.
8+
_ = Container.NestedType()
9+
}
10+
11+
func someFunc2() {
12+
// This constructor call should not require a curry thunk.
13+
_ = Self.NestedType()
14+
}
15+
16+
func someFunc3() {
17+
let m = Container.self
18+
19+
// This constructor call should not require a curry thunk.
20+
_ = m.NestedType()
21+
}
22+
}
23+
24+
// CHECK-LABEL: sil hidden [ossa] @$s17bogus_curry_thunk9ContainerC9someFunc1yyF : $@convention(method) (@guaranteed Container) -> () {
25+
// CHECK: bb0(%0 : @guaranteed $Container):
26+
// CHECK: [[META2:%.*]] = metatype $@thick Container.NestedType.Type
27+
// CHECK: [[FN:%.*]] = function_ref @$s17bogus_curry_thunk9ContainerC10NestedTypeCAEycfC : $@convention(method) (@thick Container.NestedType.Type) -> @owned Container.NestedType
28+
// CHECK: [[RESULT:%.*]] = apply [[FN]]([[META2]]) : $@convention(method) (@thick Container.NestedType.Type) -> @owned Container.NestedType
29+
// CHECK: ignored_use [[RESULT]]
30+
// CHECK: destroy_value [[RESULT]]
31+
// CHECK: [[TUPLE:%.*]] = tuple ()
32+
// CHECK: return [[TUPLE]]
33+
// CHECK: }
34+
35+
36+
// CHECK-LABEL: sil hidden [ossa] @$s17bogus_curry_thunk9ContainerC9someFunc2yyF : $@convention(method) (@guaranteed Container) -> () {
37+
// CHECK: bb0(%0 : @guaranteed $Container):
38+
// CHECK: [[META2:%.*]] = metatype $@thick Container.NestedType.Type
39+
// CHECK: [[FN:%.*]] = function_ref @$s17bogus_curry_thunk9ContainerC10NestedTypeCAEycfC : $@convention(method) (@thick Container.NestedType.Type) -> @owned Container.NestedType
40+
// CHECK: [[RESULT:%.*]] = apply [[FN]]([[META2]]) : $@convention(method) (@thick Container.NestedType.Type) -> @owned Container.NestedType
41+
// CHECK: ignored_use [[RESULT]]
42+
// CHECK: destroy_value [[RESULT]]
43+
// CHECK: [[TUPLE:%.*]] = tuple ()
44+
// CHECK: return [[TUPLE]]
45+
// CHECK: }
46+
47+
48+
// CHECK-LABEL: sil hidden [ossa] @$s17bogus_curry_thunk9ContainerC9someFunc3yyF : $@convention(method) (@guaranteed Container) -> () {
49+
// CHECK: bb0(%0 : @guaranteed $Container):
50+
// CHECK: [[META:%.*]] = metatype $@thick Container.Type
51+
// CHECK: [[META2:%.*]] = metatype $@thick Container.NestedType.Type
52+
// CHECK: [[FN:%.*]] = function_ref @$s17bogus_curry_thunk9ContainerC10NestedTypeCAEycfC : $@convention(method) (@thick Container.NestedType.Type) -> @owned Container.NestedType
53+
// CHECK: [[RESULT:%.*]] = apply [[FN]]([[META2]]) : $@convention(method) (@thick Container.NestedType.Type) -> @owned Container.NestedType
54+
// CHECK: ignored_use [[RESULT]]
55+
// CHECK: destroy_value [[RESULT]]
56+
// CHECK: [[TUPLE:%.*]] = tuple ()
57+
// CHECK: return [[TUPLE]]
58+
// CHECK: }

test/type/self.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,4 +380,17 @@ struct NonGeneric {
380380
protocol P {
381381
func foo() -> Self<Int>
382382
// expected-error@-1 {{cannot specialize non-generic type 'Self'}}
383+
}
384+
385+
// https://github.com/peripheryapp/periphery/issues/676
386+
387+
class Container {
388+
class NestedType {
389+
init(value: Int) {}
390+
}
391+
392+
func someFunc() {
393+
let _ = [Container.NestedType]() // ok
394+
let _ = [Self.NestedType]() // ok
395+
}
383396
}

0 commit comments

Comments
 (0)