Files
swift-mirror/test/Interpreter/subscripting.swift
Joe Groff abdc7036eb SILGen: Open-code materializeForSet in more situations.
Since this code was first written, we've added more language features that introduce the opportunity for a materializeForSet protocol witness to have an incompatible polymorphic convention with its concrete implementation:

- In a conditional conformance, if a witness comes from a constrained extension with additional protocol requirements, then the witness will require those conformances as additional polymorphic arguments, making its materializeForSet uncallable from code using the protocol witness.
- Given a subscript requirement, the witness may be a generic subscript with a more general signature than the witness, making the generic arguments to the concrete materializeForSet callback incompatible with those expected for the witness.

Longer term, representing materializeForSet patterns using accessor coroutines should obviate the need for this hack. For now, it's necessary for correctness, addressing rdar://problem/35760754.
2018-04-12 10:50:31 -07:00

43 lines
730 B
Swift

// RUN: %target-run-simple-swift | %FileCheck %s
// REQUIRES: executable_test
// Check that subscripts and functions named subscript can exist side-by-side
struct Foo {
subscript() -> String {
return "subscript"
}
func `subscript`() -> String {
return "func"
}
}
let f = Foo()
print(f[]) // CHECK: subscript
print(f.subscript()) // CHECK: func
// SR-7418
protocol P {
subscript<T : Y>(_: T) -> Int { get set }
}
struct Q : P {
subscript<T : X>(_ idx: T) -> Int {
get { return 0 } set { idx.foo() }
}
}
protocol Y : X {}
protocol X { func foo() }
struct Idx : Y {
func foo() { print("I survived") }
}
func foo<T : P>(_ t: inout T) {
t[Idx()] += 1
}
var q = Q()
foo(&q) // CHECK: I survived