Skip to content

[Type checker] Validate subscript declaration before using it. #27320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions test/multifile/Inputs/sr11458.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
protocol Observed: AnyObject {
}

struct Other<Value: Equatable> {
var value: Value

func hello() -> String {
return "Hello from \(value)"
}
}

@propertyWrapper
struct Observable<Value: Equatable> {
private var stored: Value


init(wrappedValue: Value) {
self.stored = wrappedValue
}

var wrappedValue: Value {
get { fatalError("called wrappedValue getter") }
set { fatalError("called wrappedValue setter") }
}

var projectedValue: Other<Value> {
get { fatalError("called projectedValue getter") }
set { fatalError("called projectedValue setter") }
}

static subscript<EnclosingSelf: Observed, FinalValue>(
_enclosingInstance observed: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, FinalValue>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
) -> Value {
get {
fatalError("blah")
}
set {
}
}

static subscript<EnclosingSelf: Observed>(
_enclosingInstance observed: EnclosingSelf,
projected wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Other<Value>>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
) -> Other<Value> {
get {
fatalError("blah")
}
set {
}
}
}
10 changes: 10 additions & 0 deletions test/multifile/property-wrappers-sr11458.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-swift-frontend -typecheck -primary-file %s %S/Inputs/sr11458.swift

// SR-11458: crash involving subscript in other file
class MyOtherType<T: Equatable>: Observed {
@Observable var x: T

init(x: T) {
self.x = x
}
}