Skip to content

Commit 997fabb

Browse files
committed
[Type checker] Validate subscript declaration before using it.
Fixes SR-1148 / rdar://problem/55303660.
1 parent 3e48f71 commit 997fabb

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

lib/Sema/CSGen.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,7 @@ namespace {
10761076
// a known subscript here. This might be cleaner if we split off a new
10771077
// UnresolvedSubscriptExpr from SubscriptExpr.
10781078
if (auto decl = declOrNull) {
1079+
CS.getTypeChecker().validateDecl(decl);
10791080
OverloadChoice choice =
10801081
OverloadChoice(baseTy, decl, FunctionRefKind::DoubleApply);
10811082
CS.addBindOverloadConstraint(memberTy, choice, memberLocator,

test/multifile/Inputs/sr11458.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
protocol Observed: AnyObject {
2+
}
3+
4+
struct Other<Value: Equatable> {
5+
var value: Value
6+
7+
func hello() -> String {
8+
return "Hello from \(value)"
9+
}
10+
}
11+
12+
@propertyWrapper
13+
struct Observable<Value: Equatable> {
14+
private var stored: Value
15+
16+
17+
init(wrappedValue: Value) {
18+
self.stored = wrappedValue
19+
}
20+
21+
var wrappedValue: Value {
22+
get { fatalError("called wrappedValue getter") }
23+
set { fatalError("called wrappedValue setter") }
24+
}
25+
26+
var projectedValue: Other<Value> {
27+
get { fatalError("called projectedValue getter") }
28+
set { fatalError("called projectedValue setter") }
29+
}
30+
31+
static subscript<EnclosingSelf: Observed, FinalValue>(
32+
_enclosingInstance observed: EnclosingSelf,
33+
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, FinalValue>,
34+
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
35+
) -> Value {
36+
get {
37+
fatalError("blah")
38+
}
39+
set {
40+
}
41+
}
42+
43+
static subscript<EnclosingSelf: Observed>(
44+
_enclosingInstance observed: EnclosingSelf,
45+
projected wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Other<Value>>,
46+
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
47+
) -> Other<Value> {
48+
get {
49+
fatalError("blah")
50+
}
51+
set {
52+
}
53+
}
54+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-frontend -typecheck -primary-file %s %S/Inputs/sr11458.swift
2+
3+
// SR-11458: crash involving subscript in other file
4+
class MyOtherType<T: Equatable>: Observed {
5+
@Observable var x: T
6+
7+
init(x: T) {
8+
self.x = x
9+
}
10+
}

0 commit comments

Comments
 (0)