Skip to content

Add support for exhaustive checking of switch on enum#4483

Draft
turbolent wants to merge 1 commit into
masterfrom
bastian/exhaustive-switch
Draft

Add support for exhaustive checking of switch on enum#4483
turbolent wants to merge 1 commit into
masterfrom
bastian/exhaustive-switch

Conversation

@turbolent
Copy link
Copy Markdown
Member

@turbolent turbolent commented Apr 24, 2026

Description

In many cases it is desirable to ensure that a switch covers all potential values.

Start introducing exhaustiveness checking for switches on enum types: Check that all cases of the enum are covered by cases of the switch.

Introduce this check in a backward-compatible way, to avoid breaking existing contracts that already exhaustively check all cases and e.g. have a default case (would become unreachable); or exit in all cases and have code after the switch (would become unreachable); etc. This is achieved by requiring the developer to annotate the switch with a new pragma #exhaustive.

For example:

enum Color: UInt8 {
    case red
    case green
    case blue
}

fun test(c: Color): String {
    #exhaustive
    switch c {
    case Color.red:
        return "red"
    case Color.green:
        return "green"
    case Color.blue:
        return "blue"
    }
}

This is just one potential idea I had, and is very much open for discussion. Alternatively we could e.g. introduce the pragma on the enum declaration, use a keyword for the switch instead of a pragma, etc.


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@turbolent turbolent self-assigned this Apr 24, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 24, 2026

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

Scanned Files

None

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 24, 2026

Benchstat comparison

  • Base branch: onflow:master
  • Base commit: 95d26d9
Results

old.txtnew.txt
time/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-41.30µs ± 0%1.36µs ± 0%~(p=1.000 n=1+1)
ByteArrayValueToByteSlice-472.9ns ± 0%72.8ns ± 0%~(p=1.000 n=1+1)
ByteSliceToByteArrayValue-41.32µs ± 0%1.21µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-4121µs ± 0%121µs ± 0%~(p=1.000 n=1+1)
CompileTime-432.1µs ± 0%32.4µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4358µs ± 0%360µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-4215µs ± 0%214µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-43.88µs ± 0%3.43µs ± 0%~(p=1.000 n=1+1)
Emit-44.64ms ± 0%4.80ms ± 0%~(p=1.000 n=1+1)
EnumTransfer-41.46µs ± 0%1.57µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4309ns ± 0%307ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-466.0ns ± 0%66.5ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-4145µs ± 0%148µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
FlowTokenContract-4582µs ± 0%584µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-423.1µs ± 0%23.2µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-425.0µs ± 0%25.3µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewCompilerNewVM-448.6µs ± 0%48.8µs ± 0%~(p=1.000 n=1+1)
ImperativeFibNewVM-427.0µs ± 0%27.2µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-42.23ms ± 0%2.26ms ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-4118µs ± 0%118µs ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-423.1µs ± 0%23.4µs ± 0%~(p=1.000 n=1+1)
InterpreterNewStruct-468.1µs ± 0%67.5µs ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-443.5µs ± 0%43.3µs ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-461.8µs ± 0%63.1µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-4876ns ± 0%854ns ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-4336ns ± 0%360ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-495.5µs ± 0%94.9µs ± 0%~(p=1.000 n=1+1)
NewStruct-444.2µs ± 0%45.9µs ± 0%~(p=1.000 n=1+1)
NewStructRaw-43.15µs ± 0%3.32µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
QualifiedIdentifierCreation/One_level-41.75ns ± 0%1.73ns ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/Three_levels-477.5ns ± 0%76.4ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
RecursionFib-41.49ms ± 0%1.65ms ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransfer-4774µs ± 0%774µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4614µs ± 0%641µs ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4692µs ± 0%701µs ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-42.70ms ± 0%2.74ms ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-49.61ms ± 0%9.69ms ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-414.5µs ± 0%15.0µs ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-438.7µs ± 0%38.7µs ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
SuperTypeInference/arrays-4220ns ± 0%235ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/composites-4109ns ± 0%111ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/integers-4302ns ± 0%301ns ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-454.0ns ± 0%56.2ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-41.02kB ± 0%1.02kB ± 0%~(p=1.000 n=1+1)
ByteArrayValueToByteSlice-432.0B ± 0%32.0B ± 0%~(all equal)
ByteSliceToByteArrayValue-4877B ± 0%884B ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-482.9kB ± 0%82.9kB ± 0%~(p=1.000 n=1+1)
CompileTime-416.9kB ± 0%16.9kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-4144kB ± 0%144kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-475.5kB ± 0%75.3kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-42.45kB ± 0%2.45kB ± 0%~(p=1.000 n=1+1)
Emit-41.50MB ± 0%1.51MB ± 0%~(p=1.000 n=1+1)
EnumTransfer-4838B ± 0%850B ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-4120B ± 0%120B ± 0%~(all equal)
ExportType/simple_type-40.00B 0.00B ~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-443.7kB ± 0%43.7kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
FlowTokenContract-4226kB ± 0%226kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-48.30kB ± 0%8.30kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-45.41kB ± 0%5.41kB ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-425.8kB ± 0%25.8kB ± 0%~(all equal)
ImperativeFibNewVM-48.10kB ± 0%8.10kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-41.19MB ± 0%1.19MB ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-442.0kB ± 0%42.0kB ± 0%~(p=1.000 n=1+1)
InterpreterImperativeFib-48.29kB ± 0%8.29kB ± 0%~(all equal)
InterpreterNewStruct-424.9kB ± 0%24.9kB ± 0%~(p=1.000 n=1+1)
MethodCall/concrete_type_method_call-410.2kB ± 0%10.2kB ± 0%~(p=1.000 n=1+1)
MethodCall/interface_method_call-417.1kB ± 0%17.1kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-4976B ± 0%976B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-4232B ± 0%232B ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-444.0kB ± 0%44.0kB ± 0%~(p=1.000 n=1+1)
NewStruct-413.4kB ± 0%13.5kB ± 0%~(p=1.000 n=1+1)
NewStructRaw-41.70kB ± 0%1.70kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
QualifiedIdentifierCreation/One_level-40.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-464.0B ± 0%64.0B ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
RecursionFib-4283kB ± 0%283kB ± 0%~(all equal)
RuntimeFungibleTokenTransfer-4241kB ± 0%240kB ± 0%~(p=1.000 n=1+1)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-4165kB ± 0%165kB ± 0%~(p=1.000 n=1+1)
RuntimeFungibleTokenTransferVM-4188kB ± 0%188kB ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-41.76MB ± 0%1.76MB ± 0%~(p=1.000 n=1+1)
RuntimeResourceTracking-46.97MB ± 0%6.98MB ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-48.08kB ± 0%8.10kB ± 0%~(p=1.000 n=1+1)
RuntimeVMInvokeContractImperativeFib-413.4kB ± 0%13.4kB ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
SuperTypeInference/arrays-472.0B ± 0%72.0B ± 0%~(all equal)
SuperTypeInference/composites-40.00B 0.00B ~(all equal)
SuperTypeInference/integers-40.00B 0.00B ~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-432.0B ± 0%32.0B ± 0%~(all equal)
 
allocs/opdelta
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ByteArrayTransfer-47.00 ± 0%7.00 ± 0%~(all equal)
ByteArrayValueToByteSlice-41.00 ± 0%1.00 ± 0%~(all equal)
ByteSliceToByteArrayValue-45.00 ± 0%5.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/compiler goos:linux goarch:amd64
CompileFungibleTokenTransferTransaction-4576 ± 0%576 ± 0%~(all equal)
CompileTime-4209 ± 0%209 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ContractFunctionInvocation-42.25k ± 0%2.25k ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ContractImport-41.29k ± 0%1.29k ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
EMVAddressTransfer-429.0 ± 0%29.0 ± 0%~(all equal)
Emit-440.0k ± 0%40.0k ± 0%~(p=1.000 n=1+1)
EnumTransfer-413.0 ± 0%13.0 ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
ExportType/composite_type-43.00 ± 0%3.00 ± 0%~(all equal)
ExportType/simple_type-40.00 0.00 ~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
FTTransfer-41.40k ± 0%1.40k ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
FlowTokenContract-43.58k ± 0%3.58k ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ImperativeFib-4176 ± 0%176 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
ImperativeFib-4266 ± 0%266 ± 0%~(all equal)
ImperativeFibNewCompilerNewVM-4473 ± 0%473 ± 0%~(all equal)
ImperativeFibNewVM-4308 ± 0%308 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
InterpretRecursionFib-417.7k ± 0%17.7k ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
InterpreterFTTransfer-4916 ± 0%916 ± 0%~(all equal)
InterpreterImperativeFib-4175 ± 0%175 ± 0%~(all equal)
InterpreterNewStruct-4418 ± 0%418 ± 0%~(all equal)
MethodCall/concrete_type_method_call-4354 ± 0%354 ± 0%~(all equal)
MethodCall/interface_method_call-4464 ± 0%464 ± 0%~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
NewInterpreter/new_interpreter-415.0 ± 0%15.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-44.00 ± 0%4.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
NewResource-4951 ± 0%951 ± 0%~(all equal)
NewStruct-4388 ± 0%388 ± 0%~(all equal)
NewStructRaw-430.0 ± 0%30.0 ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
QualifiedIdentifierCreation/One_level-40.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-42.00 ± 0%2.00 ± 0%~(all equal)
pkg:github.com/onflow/cadence/bbq/vm/test goos:linux goarch:amd64
RecursionFib-415.8k ± 0%15.8k ± 0%~(all equal)
RuntimeFungibleTokenTransfer-44.09k ± 0%4.09k ± 0%~(all equal)
pkg:github.com/onflow/cadence/runtime goos:linux goarch:amd64
RuntimeFungibleTokenTransferInterpreter-43.08k ± 0%3.08k ± 0%~(all equal)
RuntimeFungibleTokenTransferVM-43.61k ± 0%3.61k ± 0%~(all equal)
RuntimeResourceDictionaryValues-436.7k ± 0%36.7k ± 0%~(all equal)
RuntimeResourceTracking-4129k ± 0%129k ± 0%~(all equal)
RuntimeScriptNoop-4114 ± 0%114 ± 0%~(all equal)
RuntimeVMInvokeContractImperativeFib-4426 ± 0%426 ± 0%~(all equal)
pkg:github.com/onflow/cadence/sema goos:linux goarch:amd64
SuperTypeInference/arrays-43.00 ± 0%3.00 ± 0%~(all equal)
SuperTypeInference/composites-40.00 0.00 ~(all equal)
SuperTypeInference/integers-40.00 0.00 ~(all equal)
pkg:github.com/onflow/cadence/interpreter goos:linux goarch:amd64
ValueIsSubtypeOfSemaType-41.00 ± 0%1.00 ± 0%~(all equal)
 

@turbolent turbolent force-pushed the bastian/exhaustive-switch branch from 2e081b4 to 500e8d9 Compare April 24, 2026 20:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant