Skip to content

Commit 071babd

Browse files
clef-mengasche
authored andcommitted
Forbid atomic fields in patterns.
1 parent bb919ab commit 071babd

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

typing/typecore.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ type error =
190190
| Not_an_extension_constructor
191191
| Invalid_atomic_loc_payload
192192
| Label_not_atomic of Longident.t
193+
| Atomic_in_pattern of Longident.t
193194
| Literal_overflow of string
194195
| Unknown_literal of string * char
195196
| Illegal_letrec_pat
@@ -986,6 +987,10 @@ let solve_Ppat_construct ~refine tps penv loc constr no_existentials
986987

987988
let solve_Ppat_record_field ~refine loc penv label label_lid record_ty =
988989
with_local_level_generalize_structure begin fun () ->
990+
(* [not refine] holds when the pattern comes from the user,
991+
rather than a counter-example to exhaustivity. *)
992+
if not refine && label.lbl_atomic = Atomic then
993+
raise (Error (loc, !!penv, Atomic_in_pattern label_lid.txt)) ;
989994
let (_, ty_arg, ty_res) = instance_label ~fixed:false label in
990995
begin try
991996
unify_pat_types_refine ~refine loc penv ty_res (instance record_ty)
@@ -7037,6 +7042,13 @@ let report_error ~loc env = function
70377042
| Label_not_atomic lid ->
70387043
Location.errorf ~loc "The record field %a is not atomic"
70397044
(Style.as_inline_code longident) lid
7045+
| Atomic_in_pattern lid ->
7046+
Location.errorf ~loc
7047+
"Atomic fields (here %a) are forbidden in patterns, as it is difficult \
7048+
to reason about when the atomic read will happen during pattern \
7049+
matching: the field may be read zero, one or several times depending \
7050+
on the patterns around it."
7051+
(Style.as_inline_code longident) lid
70407052
| Literal_overflow ty ->
70417053
Location.errorf ~loc
70427054
"Integer literal exceeds the range of representable integers of type %a"

typing/typecore.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ type error =
228228
| Not_an_extension_constructor
229229
| Invalid_atomic_loc_payload
230230
| Label_not_atomic of Longident.t
231+
| Atomic_in_pattern of Longident.t
231232
| Literal_overflow of string
232233
| Unknown_literal of string * char
233234
| Illegal_letrec_pat

0 commit comments

Comments
 (0)