Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Caml-list] Warning 69 (unused record fields) and polymorphic reads
@ 2025-06-03  9:11 Andreas Rossberg
  2025-06-03  9:23 ` Florian Angeletti
  0 siblings, 1 reply; 3+ messages in thread
From: Andreas Rossberg @ 2025-06-03  9:11 UTC (permalink / raw)
  To: caml-list

Today, I ran into a slight annoyance with warning 69 (unused record fields). Obviously, the warning does not consider uses of polymorphic operators like `=` or `compare`, which technically are reads of the fields. Unfortunately, it turns out that there are reasonable use cases where these are the _only_ reads, resulting in bogus warnings.

There probably isn't much that can be done about it(?), since such access could hide in any polymorphic function invocation. Hence I didn’t file a bug. But for the record, I thought I'll show the counter example anyway.

Consider code that implements some processing akin to SQL `group by`, as in:
```
SELECT artist, album, COUNT(*), SUM(time), ... FROM Tracks GROUP BY artist, album;
```
Intuitively, this extracts all known albums from a list of track (song) meta data, and computes their total running time, among other values.

Here is a sketch of how to achieve something similar in OCaml:
```
module GroupKey =
   struct
      type t = {artist : string; title : string}
      let compare = compare
   end
module GroupMap = Map.Make(GroupKey)

type track = ...
type acc = ...  (* result type *)
val empty_acc : acc
val accumulate : entry -> acc -> acc  (* combine result *)

let albums =
   tracks
   |> List.fold_left (fun map (entry : track) ->
         let group = {artist = entry.artist; title = entry.title} in
         let acc = Option.value (GroupMap.find_opt group map) ~default: empty_acc in
         GroupMap.add group (accumulate acc entry) map
      ) GroupMap.empty
   |> GroupMap.bindings |> List.map snd
```
The only purpose of the `GroupKey.t` type in this code is to identify entries belonging to the same group. Its fields are read implicitly by `GroupMap.find/add`, which invokes `compare` on them. Yet, this code produces warnings that `artist` and `title` are never read, which technically isn’t quite correct.

In my actual code, the key record has more fields, which is why I didn’t want to replace it with a tuple.

Perhaps there is some annotation magic I’m missing that could be applied to the type definition to suppress the warning?

/Andreas


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Warning 69 (unused record fields) and polymorphic reads
  2025-06-03  9:11 [Caml-list] Warning 69 (unused record fields) and polymorphic reads Andreas Rossberg
@ 2025-06-03  9:23 ` Florian Angeletti
  2025-06-04  9:15   ` Andreas Rossberg
  0 siblings, 1 reply; 3+ messages in thread
From: Florian Angeletti @ 2025-06-03  9:23 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 3030 bytes --]

Those warnings can be suppressed on the type definition by adding a 
`@@warning` attribute

    module M: sig type t end = struct
       type t = { x:int; y:int } [@@warning "-unused-field"]
    end

With this attribute, the compiler will not register those fields for the 
usage checker and thus no unused
warning fields will be emitted for the fields `x` and `y`

For this specific warning, the compiler even supports disabling the 
usage checker field-by-field.
For instance, this code will only disable the warning for the `M.x` 
field, and thus warns

    module M: sig type t end = struct
       type t = { x:int[@warning "-unused-field"] ; y:int }
    end

  that the field `y` is being unused while omitting the check for the  
field `x`.

— Florian.

Le 03/06/2025 à 11:11, Andreas Rossberg a écrit :
> Today, I ran into a slight annoyance with warning 69 (unused record fields). Obviously, the warning does not consider uses of polymorphic operators like `=` or `compare`, which technically are reads of the fields. Unfortunately, it turns out that there are reasonable use cases where these are the _only_ reads, resulting in bogus warnings.
>
> There probably isn't much that can be done about it(?), since such access could hide in any polymorphic function invocation. Hence I didn’t file a bug. But for the record, I thought I'll show the counter example anyway.
>
> Consider code that implements some processing akin to SQL `group by`, as in:
> ```
> SELECT artist, album, COUNT(*), SUM(time), ... FROM Tracks GROUP BY artist, album;
> ```
> Intuitively, this extracts all known albums from a list of track (song) meta data, and computes their total running time, among other values.
>
> Here is a sketch of how to achieve something similar in OCaml:
> ```
> module GroupKey =
>     struct
>        type t = {artist : string; title : string}
>        let compare = compare
>     end
> module GroupMap = Map.Make(GroupKey)
>
> type track = ...
> type acc = ...  (* result type *)
> val empty_acc : acc
> val accumulate : entry -> acc -> acc  (* combine result *)
>
> let albums =
>     tracks
>     |> List.fold_left (fun map (entry : track) ->
>           let group = {artist = entry.artist; title = entry.title} in
>           let acc = Option.value (GroupMap.find_opt group map) ~default: empty_acc in
>           GroupMap.add group (accumulate acc entry) map
>        ) GroupMap.empty
>     |> GroupMap.bindings |> List.map snd
> ```
> The only purpose of the `GroupKey.t` type in this code is to identify entries belonging to the same group. Its fields are read implicitly by `GroupMap.find/add`, which invokes `compare` on them. Yet, this code produces warnings that `artist` and `title` are never read, which technically isn’t quite correct.
>
> In my actual code, the key record has more fields, which is why I didn’t want to replace it with a tuple.
>
> Perhaps there is some annotation magic I’m missing that could be applied to the type definition to suppress the warning?
>
> /Andreas
>

[-- Attachment #2: Type: text/html, Size: 3709 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] Warning 69 (unused record fields) and polymorphic reads
  2025-06-03  9:23 ` Florian Angeletti
@ 2025-06-04  9:15   ` Andreas Rossberg
  0 siblings, 0 replies; 3+ messages in thread
From: Andreas Rossberg @ 2025-06-04  9:15 UTC (permalink / raw)
  To: caml-list

Excellent, thanks Léo and Florian! This is the information I was looking for (and can’t find in the manual ;) ).

/Andreas


> On 3. Jun 2025, at 11:23, Florian Angeletti <octa@polychoron.fr> wrote:
> 
> Those warnings can be suppressed on the type definition by adding a `@@warning` attribute
> module M: sig type t end = struct
>   type t = { x:int; y:int } [@@warning "-unused-field"]
> end
> With this attribute, the compiler will not register those fields for the usage checker and thus no unused
> warning fields will be emitted for the fields `x` and `y`
> 
> For this specific warning, the compiler even supports disabling the usage checker field-by-field.
> For instance, this code will only disable the warning for the `M.x` field, and thus warns
> module M: sig type t end = struct
>   type t = { x:int[@warning "-unused-field"] ; y:int }
> end
>  that the field `y` is being unused while omitting the check for the  field `x`.
> 
> — Florian.
> 
> Le 03/06/2025 à 11:11, Andreas Rossberg a écrit :
>> Today, I ran into a slight annoyance with warning 69 (unused record fields). Obviously, the warning does not consider uses of polymorphic operators like `=` or `compare`, which technically are reads of the fields. Unfortunately, it turns out that there are reasonable use cases where these are the _only_ reads, resulting in bogus warnings.
>> 
>> There probably isn't much that can be done about it(?), since such access could hide in any polymorphic function invocation. Hence I didn’t file a bug. But for the record, I thought I'll show the counter example anyway.
>> 
>> Consider code that implements some processing akin to SQL `group by`, as in:
>> ```
>> SELECT artist, album, COUNT(*), SUM(time), ... FROM Tracks GROUP BY artist, album;
>> ```
>> Intuitively, this extracts all known albums from a list of track (song) meta data, and computes their total running time, among other values.
>> 
>> Here is a sketch of how to achieve something similar in OCaml:
>> ```
>> module GroupKey =
>> struct
>> type t = {artist : string; title : string}
>> let compare = compare
>> end
>> module GroupMap = Map.Make(GroupKey)
>> 
>> type track = ...
>> type acc = ... (* result type *)
>> val empty_acc : acc
>> val accumulate : entry -> acc -> acc (* combine result *)
>> 
>> let albums =
>> tracks
>> |> List.fold_left (fun map (entry : track) ->
>> let group = {artist = entry.artist; title = entry.title} in
>> let acc = Option.value (GroupMap.find_opt group map) ~default: empty_acc in
>> GroupMap.add group (accumulate acc entry) map
>> ) GroupMap.empty
>> |> GroupMap.bindings |> List.map snd
>> ```
>> The only purpose of the `GroupKey.t` type in this code is to identify entries belonging to the same group. Its fields are read implicitly by `GroupMap.find/add`, which invokes `compare` on them. Yet, this code produces warnings that `artist` and `title` are never read, which technically isn’t quite correct.
>> 
>> In my actual code, the key record has more fields, which is why I didn’t want to replace it with a tuple.
>> 
>> Perhaps there is some annotation magic I’m missing that could be applied to the type definition to suppress the warning?
>> 
>> /Andreas
>> 
>> 
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-06-04  9:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-06-03  9:11 [Caml-list] Warning 69 (unused record fields) and polymorphic reads Andreas Rossberg
2025-06-03  9:23 ` Florian Angeletti
2025-06-04  9:15   ` Andreas Rossberg

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox