public enum DemoEnum {
case firstCase
case secondCase
public static func performSwitch(with value: Int) -> DemoEnum {
switch value {
case 0:
return DemoEnum.firstCase
case 0:
return DemoEnum.firstCase
default:
return DemoEnum.secondCase
}
}
}
I can’t remember why the frontend even accepts this code. Is this intended, or just a defect in the compiler’s validity checking ofswitch statements?
public enum DemoEnum {
case firstCase
case secondCase
public static func performSwitch(with value: Int) -> DemoEnum {
switch value {
case 0:
return DemoEnum.firstCase
case 0:
return DemoEnum.firstCase
default:
return DemoEnum.secondCase
}
}
}
I can’t remember why the frontend even accepts this code. Is this intended, or just a defect in the compiler’s validity checking ofswitch statements?
It would be legitimate with, say, a condition on the first case. But yeah, this is undoubtedly just a defect in the compiler's checking.
Recall that a switch in Swift doesn't necessarily compile to a primitive switch; it's more like a series of ifs that's a bit more straightforward to optimize matching for. So the validity checking you'd expect as a matter of course in C doesn't just fall out automatically.
Also, of course, the compiler does not have primitive knowledge of Int or how it forms literals.
John.
···
On Sep 14, 2016, at 10:25 AM, Ted Kremenek via swift-dev <swift-dev@swift.org> wrote:
Thanks,
Ted
public enum DemoEnum {
case firstCase
case secondCase
public static func performSwitch(with value: Int) -> DemoEnum {
switch value {
case 0:
return DemoEnum.firstCase
case 0:
return DemoEnum.firstCase
default:
return DemoEnum.secondCase
}
}
}
I can’t remember why the frontend even accepts this code. Is this intended, or just a defect in the compiler’s validity checking ofswitch statements?
It would be legitimate with, say, a condition on the first case. But yeah, this is undoubtedly just a defect in the compiler's checking.
Recall that a switch in Swift doesn't necessarily compile to a primitive switch; it's more like a series of ifs that's a bit more straightforward to optimize matching for. So the validity checking you'd expect as a matter of course in C doesn't just fall out automatically.
Also, of course, the compiler does not have primitive knowledge of Int or how it forms literals.
John.
Joe filed a radar to improve the checking:
<rdar://problem/28301984> reject duplicate conditions when optimizing switch statement
···
On Sep 14, 2016, at 10:32 AM, John McCall via swift-dev <swift-dev@swift.org> wrote:
On Sep 14, 2016, at 10:25 AM, Ted Kremenek via swift-dev <swift-dev@swift.org <mailto:swift-dev@swift.org>> wrote:
Thanks,
Ted