Skip to content

Commit 2dd670b

Browse files
committed
Add test cases for special subscript name
1 parent f8c2692 commit 2dd670b

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

test/Interpreter/subscripting.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %target-run-simple-swift | %FileCheck %s
2+
// REQUIRES: executable_test
3+
4+
// Check that subscripts and functions named subscript can exist side-by-side
5+
struct Foo {
6+
subscript() -> String {
7+
return "subscript"
8+
}
9+
10+
func `subscript`() -> String {
11+
return "func"
12+
}
13+
}
14+
15+
let f = Foo()
16+
print(f[]) // CHECK: subscript
17+
print(f.subscript()) // CHECK: func
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-swift-frontend %s -emit-silgen | %FileCheck %s
2+
3+
// Make sure that we can parse escaped subscripts (representing functions named "subscript") in vtables
4+
5+
sil_stage raw
6+
7+
import Builtin
8+
import Swift
9+
import SwiftShims
10+
11+
class SubscriptAsFunction {
12+
func `subscript`()
13+
}
14+
15+
sil hidden @_T04test19SubscriptAsFunctionC9subscriptyyF : $@convention(method) (@guaranteed SubscriptAsFunction) -> () {
16+
bb0(%0 : $SubscriptAsFunction):
17+
return undef : $()
18+
}
19+
20+
sil_vtable SubscriptAsFunction {
21+
// CHECK: #SubscriptAsFunction.`subscript`!1: (SubscriptAsFunction) -> () -> () : _T04test19SubscriptAsFunctionC9subscriptyyF
22+
#SubscriptAsFunction.`subscript`!1: (SubscriptAsFunction) -> () -> () : _T04test19SubscriptAsFunctionC9subscriptyyF
23+
}

test/SILGen/vtables.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,15 @@ class DerivedWithoutDefaults : BaseWithDefaults {
171171

172172
// CHECK-LABEL: sil_vtable DerivedWithoutDefaults {
173173
// CHECK: #BaseWithDefaults.a!1: {{.*}} : _T07vtables22DerivedWithoutDefaultsC1a{{[_0-9a-zA-Z]*}}F
174+
175+
176+
177+
// Escape identifiers that represent special names
178+
179+
class SubscriptAsFunction {
180+
func `subscript`() {}
181+
}
182+
183+
// CHECK-LABEL: sil_vtable SubscriptAsFunction {
184+
// CHECK-NOT: #SubscriptAsFunction.subscript
185+
// CHECK: #SubscriptAsFunction.`subscript`!1

test/decl/protocol/conforms/fixit_stub.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,9 @@ class Adopter17: ProtocolParallel3, ProtocolParallel4 { // expected-error {{type
192192
func bar2() {}
193193
}
194194

195+
protocol ProtocolHasSubscriptFunction {
196+
func `subscript`() // expected-note{{protocol requires function 'subscript()' with type '() -> ()'; do you want to add a stub?}} {{74-74=\n func `subscript`() {\n <#code#>\n \}\n}}
197+
}
198+
class ProtocolHasSubscriptFunctionAdopter: ProtocolHasSubscriptFunction { // expected-error{{type 'ProtocolHasSubscriptFunctionAdopter' does not conform to protocol 'ProtocolHasSubscriptFunction'}}
199+
200+
}

test/decl/subscript/subscripting.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,12 @@ struct S4 {
310310
}
311311
}
312312
}
313+
314+
// SR-2575
315+
struct SR2575 {
316+
subscript() -> Int {
317+
return 1
318+
}
319+
}
320+
321+
SR2575().subscript() // expected-error{{type 'SR2575' has no member 'subscript'}}

0 commit comments

Comments
 (0)