Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007788OCamlmiscpublic2018-05-01 11:562018-05-10 12:35
Assigned To 
PlatformOSOS Version
Product Version4.06.1 
Target VersionFixed in Version 
Summary0007788: missing (?) kind of warning
DescriptionOcaml compiler(s) currently support the following warning:

  4 ... Fragile pattern matching: matching that will remain complete even if additional constructors are added to one of the variant types matched.

Wouldn't it make sense to also provide an analogous warning for records?

For example, if I write the following pattern:

  {f1; f2; _}

the compiler would warn me:

  "Fragile pattern matching: matching will remain complete even if additional fields are added to a record type".

What do you think?
TagsNo tags attached.
Attached Files

- Relationships

-  Notes
gasche (developer)
2018-05-01 22:25

In fact, the pattern "; _" was added precisely to disable warning 9 (Missing fields in a record pattern), when you use a precise record pattern on a value with more fields than the pattern.

I don't think that the situation for the sums/variants and the products/records are symmetric.

For sums/variants, it is the default when you add a new case to want to review existing pattern matches (because cases become differences in control-flow, and no one but the user knows what to do with a new flow), and on the contrary it is convenient (often the shortes thing) to use a "| _ ->" catch-all case in a series of clauses -- so you need to fight that convenience by adding a warning to prevent people from the easy thing.

For products/records, adding a new field is adding more information to each record argument; there is an effective default on what to do, which is to just ignore this new information (you didn't need it before). Finally, using the "; _" construction to silence the "some fields are not matched" warning is the *hard* path, not the shortest/easy one, so we can safely assume that it explicitly expresses the intent to ignore all other fields -- as opposed to not writing anything at all, which already warn.

For these reasons, I am not sure that a new warning when "; _" is used would be very useful. Do you have an example of a use-case?
kosik (reporter)
2018-05-01 23:55
edited on: 2018-05-01 23:55

Thank you Gabriel for the reply.

One of the things I do not get in your response is a claim that:

  using the "; _" construction to silence the "some fields are not matched"
  warning is the *hard* path.

Do you mean that if I have a record with 5 fields: f1, f2, f3, f4, f5, then this:

  (* here I only bind two fields: "f1" and "f2" *)
  {f1; f2; _}

is a harder path than:

  (* here, like above, I only bind two fields: "f1" and "f2" *)
  {f1; f2; f3 = _; f4 = _; f5 = _}


Isn't it exactly the other way around?

stedolan (developer)
2018-05-10 12:04

I think the analogy to pattern matching on variants is better made with record *construction*, rather than record matching.

One could imagine an alternate version of OCaml which allowed records to be constructed by only specifying values for a subset of the record's fields, causing a runtime error to be raised if anyone tries to access an unfilled field. This would be symmetric with OCaml variants, which allow variants to be matched by only specifying actions for a subset of the variant's cases, causing a runtime error to be raised if anyone tries to supply an unhandled case.

To deal effectively with such "partial records", we'd need a copy of all of the partial-matching machinery: warnings when records are constructed with some fields missing (like nonexhaustive matches), warnings on fragile record constructions, etc.

However, actual OCaml demands that every field is present when records are constructed, but does allow some cases to be missing when variants are matched. So, the machinery for dealing with partiality and fragility is only needed for variants, not records.
kosik (reporter)
2018-05-10 12:35


Alternate versions of Ocaml might make sense,
but my original post is related to the current/existing/actual Ocaml version.

- Issue History
Date Modified Username Field Change
2018-05-01 11:56 kosik New Issue
2018-05-01 22:25 gasche Note Added: 0019085
2018-05-01 22:25 gasche Status new => feedback
2018-05-01 23:55 kosik Note Added: 0019086
2018-05-01 23:55 kosik Status feedback => new
2018-05-01 23:55 kosik Note Edited: 0019086 View Revisions
2018-05-10 12:04 stedolan Note Added: 0019111
2018-05-10 12:35 kosik Note Added: 0019112

Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker