Mantis Bug Tracker

View Issue Details Jump to Notes ] Issue History ] Print ]
IDProjectCategoryView StatusDate SubmittedLast Update
0007211OCamltypingpublic2016-04-05 11:362016-11-12 16:23
Reporterpcouderc 
Assigned Togasche 
PrioritylowSeverityminorReproducibilityalways
StatusresolvedResolutionfixed 
PlatformOSOS Version
Product Version4.02.3 
Target Version4.03.1+devFixed in Version4.05.0 +dev/beta1/beta2/beta3/rc1 
Summary0007211: Strange behavior of exhaustive record copy with disambiguation
DescriptionWhen copying a record and redefining all the fields, the compiler gives the following warning:
Warning 23: all the fields are explicitly listed in this record:
the 'with' clause is useless.

For example:
# type t = { v : int };;
type t = { v : int; }

# let x = { v = 0 };;
val x : t = {v = 0}

# let y = {x with v = 1 };;
Warning 23: all the fields are explicitly listed in this record:
the 'with' clause is useless.
val y : t = {v = 1}

Surprisingly, the following value is accepted:
# let z = { "What?" with v = 2 };;
Warning 23: all the fields are explicitly listed in this record:
the 'with' clause is useless.
val z : t = {v = 2}

It is surprising, but it is possible thanks to the fields disambiguation. It will not break programs since the string will not be accessed.
However, the following program is not accepted:
# type t' = { u : int };;
type t' = { u : int; }
# let x' = { u = 0 };;
val x' : t' = {u = 0}
# let y' = { x' with v = 2 };;
Error: This record expression is expected to have type t'
       The field v does not belong to type t'

A record of another type is not accepted as the unused copy, even if this copied record will not be accessed.

Finally, one can write the following function:
# let f x = { x with v = 42 };;
Warning 23: all the fields are explicitly listed in this record:
the 'with' clause is useless.
val f : 'a -> t = <fun>

It will be polymorphic, so it will accept any other record type as argument.
On a "theoretic" point of view, it breaks the subject reduction I guess, but it will not actually break programs since the "copied record" is never accessed for the copy.

However, one problem is that it can break APIs of modules that does not have an explicit signature: if the previous function is exported, it can be used polymorphically and becomes monomorphic if the record is extended with new fields. If the user of the module use it with any argument with a different type than the record, his code will not typecheck anymore.
Additional InformationWorks since 4.00.
TagsNo tags attached.
Attached Files

- Relationships
duplicate of 0006608resolvedgarrigue Lax typing for functional record updates 

-  Notes
(0016562)
gasche (administrator)
2016-11-12 16:23

This is a (nice and more detailed) report of the same issue as PR#6608, which has now being resolved in trunk.

- Issue History
Date Modified Username Field Change
2016-04-05 11:36 pcouderc New Issue
2016-04-05 16:22 doligez Status new => acknowledged
2016-04-05 16:22 doligez Target Version => 4.03.1+dev
2016-11-12 16:22 gasche Relationship added duplicate of 0006608
2016-11-12 16:23 gasche Note Added: 0016562
2016-11-12 16:23 gasche Status acknowledged => resolved
2016-11-12 16:23 gasche Fixed in Version => 4.05.0 +dev/beta1/beta2/beta3/rc1
2016-11-12 16:23 gasche Resolution open => fixed
2016-11-12 16:23 gasche Assigned To => gasche
2017-02-23 16:45 doligez Category OCaml typing => typing


Copyright © 2000 - 2011 MantisBT Group
Powered by Mantis Bugtracker