Fix crash with pattern anomalies when writing a pattern with different type than the scrutinee
Take this contract with a minimal reproducing issue:
type t = Unit
let Unit = ()
This will cause the compiler to crash, and it's annoying for LSP users, who will also see a crash:
[Error - 1:13:26 PM] Unexpected exception: (Option.value_exn
src/passes/10-checking/pattern_anomalies.ml:95:32)
Raised at Base__Error.raise in file "src/error.ml" (inlined), line 9, characters 14-30
Called from Base__Option.value_exn in file "src/option.ml", line 114, characters 4-21
Called from Checking__Pattern_anomalies.to_simple_pattern in file "src/passes/10-checking/pattern_anomalies.ml", line 95, characters 8-78
Called from Base__List.count_map in file "src/list.ml", line 479, characters 13-17
Called from Base__List.map in file "src/list.ml" (inlined), line 510, characters 15-31
Called from Checking__Pattern_anomalies.check_anomalies in file "src/passes/10-checking/pattern_anomalies.ml", line 540, characters 15-48
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Checking__Elaboration.bind in file "src/passes/10-checking/elaboration.ml", line 25, characters 7-38
Called from Simple_utils__Trace.trace.try_body in file "vendored-dune/ligo-utils/simple-utils/trace.ml", line 160, characters 16-24
Called from Simple_utils__Trace.try_with in file "vendored-dune/ligo-utils/simple-utils/trace.ml", line 53, characters 6-21
Called from Simple_utils__Trace.to_stdlib_result.(fun) in file "vendored-dune/ligo-utils/simple-utils/trace.ml", line 96, characters 18-26
Called from Simple_utils__Trace.try_with in file "vendored-dune/ligo-utils/simple-utils/trace.ml", line 53, characters 6-21
Called from Scopes__Types_pass.Typing_env.resolve in file "src/main/scopes/types_pass.ml", line 781, characters 6-269
Called from Scopes__Types_pass.resolve in file "src/main/scopes/types_pass.ml", line 903, characters 23-83
Called from Scopes.run in file "src/main/scopes/scopes.ml", line 100, characters 9-77
Called from Lsp_helpers__Get_scope.get_defs_and_diagnostics.(fun) in file "src/main/ligo_lsp/lsp_helpers/get_scope.ml", line 346, characters 17-81
Called from Lsp_helpers__Get_scope.get_defs_and_diagnostics.(fun) in file "src/main/ligo_lsp/lsp_helpers/get_scope.ml", line 340, characters 11-1023
Called from Lwt.Sequential_composition.backtrace_catch in file "src/core/lwt.ml", line 2077, characters 10-14
Re-raised at Simple_utils__Trace.try_with_lwt.(fun) in file "vendored-dune/ligo-utils/simple-utils/trace.ml", line 73, characters 2-60
Re-raised at Ligo_lsp__Crash.try_with_handler.(fun) in file "src/main/ligo_lsp/crash.ml", line 95, characters 2-158
The same can be achieved by using something like let x = match () with Unit -> 1
instead.
Acceptance criteria
- A pattern with a type mismatch won't cause a crash.
- Tests added.