[swift-dev] why are we accepting switch statements with duplicate conditions?

John McCall rjmccall at apple.com
Wed Sep 14 12:32:08 CDT 2016


> On Sep 14, 2016, at 10:25 AM, Ted Kremenek via swift-dev <swift-dev at swift.org> wrote:
> 
> Hi everyone,
> 
> Joe Shajrawi nominated an optimizer fix for Swift 3.0.1 where the optimizer was crashing due to duplicate conditions in aswitchstatement:
> 
> https://bugs.swift.org/browse/SR-2512 <https://bugs.swift.org/browse/SR-2512>
> https://github.com/apple/swift/pull/4747 <https://github.com/apple/swift/pull/4747>
> Here’s an example:
> 
> 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.
> Thanks,
> Ted
> 
> _______________________________________________
> swift-dev mailing list
> swift-dev at swift.org
> https://lists.swift.org/mailman/listinfo/swift-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-dev/attachments/20160914/36ac76b1/attachment.html>


More information about the swift-dev mailing list