why are we accepting switch statements with duplicate conditions?

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://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?

Thanks,
Ted

Hi everyone,

Joe Shajrawi nominated an optimizer fix for Swift 3.0.1 where the optimizer was crashing due to duplicate conditions in aswitchstatement:

[SR-2512] Switch case statement over integer value used to return enum cases leads to compiler crash, if optimization is enabled. · Issue #45117 · apple/swift · GitHub
[SILOptimizer] Ignore duplicate conditions when optimizing switch statement by shajrawi · Pull Request #4747 · apple/swift · GitHub
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.

···

On Sep 14, 2016, at 10:25 AM, Ted Kremenek via swift-dev <swift-dev@swift.org> wrote:
Thanks,
Ted

_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

Hi everyone,

Joe Shajrawi nominated an optimizer fix for Swift 3.0.1 where the optimizer was crashing due to duplicate conditions in aswitchstatement:

[SR-2512] Switch case statement over integer value used to return enum cases leads to compiler crash, if optimization is enabled. · Issue #45117 · apple/swift · GitHub
[SILOptimizer] Ignore duplicate conditions when optimizing switch statement by shajrawi · Pull Request #4747 · apple/swift · GitHub
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.

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

_______________________________________________
swift-dev mailing list
swift-dev@swift.org <mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

_______________________________________________
swift-dev mailing list
swift-dev@swift.org <mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev