Default subscript implementation added via a protocol is used instead type's own subscripts #62866
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
overload resolution
Area → compiler → type checker: Overload resolution (ranking)
overloading
Feature: Overloading symbol names
regression
type checker
Area → compiler: Semantic analysis
Description
I have a project that began crashing at runtime upon upgrading to Xcode 14. I narrowed the problem down to a protocol we wrote and some extensions adding conformance to this protocol to library types, like Dictionary. The protocol also has a default implementation of subscripts, and prior to the upgrade to Xcode 14, Dictionary would still use its own subscripts. After the upgrade, now Dictionary seems to use the default subscript from our protocol instead, which happened to lead to circular calls and a stack overflow.
Steps to reproduce
Small code example that should demonstrate the problem more clearly:
Expected behavior
With Xcode 13.4, the above code sample will run and print "could successfully use value()". On Xcode 14.2, it will crash before reaching the print statement. Checking the stack in the debugger shows that Dictionary.value(forKey:) invokes KeyValueStore's subscript implementation rather than Dictionary's own, which in turn calls Dictionary.value(forKey:) again and so on.
I'm not sure if Dictionary or KeyValueStore's subscript implementation should be used here, or if this is not actually defined behavior. It's easy enough to work around in my code but since the behaviour of Swift changed here between versions I figured I should report it in case there is a bug.
Environment
The sample code above runs fine on Xcode 13.4.1/Swift 5.6.1
The code crashes on Xcode 14.2/Swift 5.7.2
The text was updated successfully, but these errors were encountered: