You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SR-8524 Shadowing in protocols leads to difficult to find bugs
Issue Description:
When pattern matching, it's common to bind a variable or constant. It's uncommon but legal to use a bound value as an argument. Consider the following enumeration and values:
// An enum with one, two, or three associated values
enum Value<T> { case one(T), two(T, T), three(T, T, T) }
// An example with two associated values
let example2: Value<Character> = .two("a", "b")
// A bound symbol
let oldValue = "x"
This code's goal is to conditionally bind newValue and pattern match the value stored in the oldValue symbol. The first example succeeds. The second example compiles and runs but does not match the coder's intent. Using an external let creates a new oldValue shadow instead of pattern matching oldValue's stored value.
// Safe
if case .two(let newValue, oldValue) = example2 {
...
}
// Syntactically legal but incorrect
if case let .two(newValue, oldValue) = example2 {
...
}
The compiler should detect same-name shadowing of an already-bound variable during pattern matching and emit a compile-time warning.
Additional Detail from JIRA
md5: 161deecb8c2d61dc752c8d993c43c08a
relates to:
Issue Description:
When pattern matching, it's common to bind a variable or constant. It's uncommon but legal to use a bound value as an argument. Consider the following enumeration and values:
This code's goal is to conditionally bind newValue and pattern match the value stored in the oldValue symbol. The first example succeeds. The second example compiles and runs but does not match the coder's intent. Using an external let creates a new oldValue shadow instead of pattern matching oldValue's stored value.
The compiler should detect same-name shadowing of an already-bound variable during pattern matching and emit a compile-time warning.
References:
Draft Discussion https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033308.html
Redirection to bug report https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20170227/033369.html
The text was updated successfully, but these errors were encountered: