Skip to content

Commit 590ac72

Browse files
committed
[Parse] Parse subscript with '#' expressions
Fixes: https://bugs.swift.org/browse/SR-3439
1 parent 1860fec commit 590ac72

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

lib/Parse/ParseExpr.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,14 @@ ParserResult<Expr> Parser::parseExprSuper(bool isExprBasic) {
787787
/*Implicit=*/false));
788788
}
789789

790+
// NOTE: l_square_lit is for migrating the old object literal syntax.
791+
// Eventually this block can be removed.
792+
if (Tok.is(tok::l_square_lit) && !Tok.isAtStartOfLine() &&
793+
isCollectionLiteralStartingWithLSquareLit()) {
794+
assert(Tok.getLength() == 1);
795+
Tok.setKind(tok::l_square);
796+
}
797+
790798
if (Tok.isFollowingLSquare()) {
791799
// super[expr]
792800
SourceLoc lSquareLoc, rSquareLoc;
@@ -1538,12 +1546,20 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
15381546
continue;
15391547
}
15401548

1541-
// If there is an expr-call-suffix, parse it and form a call.
1549+
// If there is an expr-call-suffix, parse it and form a call.
15421550
if (Tok.isFollowingLParen()) {
15431551
Result = parseExprCallSuffix(Result, isExprBasic);
15441552
continue;
15451553
}
15461554

1555+
// NOTE: l_square_lit is for migrating the old object literal syntax.
1556+
// Eventually this block can be removed.
1557+
if (Tok.is(tok::l_square_lit) && !Tok.isAtStartOfLine() &&
1558+
isCollectionLiteralStartingWithLSquareLit()) {
1559+
assert(Tok.getLength() == 1);
1560+
Tok.setKind(tok::l_square);
1561+
}
1562+
15471563
// Check for a [expr] suffix.
15481564
// Note that this cannot be the start of a new line.
15491565
if (Tok.isFollowingLSquare()) {

test/Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@ public struct Selector : ExpressibleByStringLiteral {
5959
public init (stringLiteral value: String) {
6060
self = sel_registerName(value)
6161
}
62+
63+
public var hashValue: Int {
64+
return ptr.hashValue
65+
}
6266
}
6367

64-
extension Selector : Equatable {}
68+
extension Selector : Equatable, Hashable {}
6569

6670
public func ==(lhs: Selector, rhs: Selector) -> Bool {
6771
return sel_isEqual(lhs, rhs)

test/expr/expressions.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,3 +922,24 @@ func se0101<P: Pse0101>(x: Cse0101<P>) {
922922
_ = strideof(P.Type.self) // expected-error {{'strideof' is unavailable: use MemoryLayout<T>.stride instead.}} {{7-16=MemoryLayout<}} {{22-28=>.stride}} {{none}}
923923
_ = sizeof(type(of: x)) // expected-error {{'sizeof' is unavailable: use MemoryLayout<T>.size instead.}} {{7-26=MemoryLayout<Cse0101<P>>.size}} {{none}}
924924
}
925+
926+
// SR-3439 subscript with pound exprssions.
927+
Sr3439: do {
928+
class B {
929+
init() {}
930+
subscript(x: Int) -> Int { return x }
931+
subscript(x: String) -> String { return x }
932+
933+
func foo() {
934+
_ = self[#line] // Ok.
935+
}
936+
}
937+
class C : B {
938+
func bar() {
939+
_ = super[#file] // Ok.
940+
}
941+
}
942+
943+
let obj = C();
944+
_ = obj[#column] // Ok.
945+
}

test/expr/unary/keypath/keypath.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func testKeyPath(a: A, b: B) {
9898
// Nested type of a bridged type (rdar://problem/28061409).
9999
typealias IntArray = [Int]
100100
let _: String = #keyPath(IntArray.Foo.propString)
101+
102+
let dict: [String: Int] = [:]
103+
let _: Int? = dict[#keyPath(A.propB)]
101104
}
102105

103106
func testAsStaticString() {

test/expr/unary/selector/selector.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func testSelector(_ c1: C1, p1: P1, obj: AnyObject) {
8383
let sel2: Selector
8484
sel2 = sel1
8585
_ = sel2
86+
87+
let dict: [Selector: Int] = [:]
88+
let _: Int? = dict[#selector(c1.method1)]
8689
}
8790

8891
func testAmbiguity() {

0 commit comments

Comments
 (0)