Disparity in `if case` vs `switch` when matching non-copyable enums

I've been working with non-copyable types lately, and noticed an odd difference in the way switch enumeration cases treat them vs. other places.

Pattern matching in control flow statements that aren't switch statements seem to be implicit consumes of non-copyable enums:

enum NoncopyableEnum: ~Copyable {
    
    case someValue
    
}

func doSomething(with enumValue: borrowing NoncopyableEnum) { // 'enumValue' is borrowed and cannot be consumed
    if case .someValue = enumValue { // this triggers the above compiler error
        //  hello, hr?
    }
}

whereas switch statements aren't:

enum NoncopyableEnum: ~Copyable {
    
    case someValue
    
}

func doSomething(with enumValue: borrowing NoncopyableEnum) {
    switch enumValue {
        case .someValue: break // aww you're so sweet
    }
}

Am I missing something obvious as to why it's reasonable these two instances be treated differently, or is this a bug?

2 Likes

That's a bug. They should be treated the same.

4 Likes

This is the fastest I've ever gotten a reply to anything I've ever said

Just filed a bug.

2 Likes