git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v3 02/14] odb: fix flags parameter to be unsigned

From
Patrick Steinhardt <ps@pks.im>
Date
Jan 23, 2026, 10:57 UTC
Message-ID
<aXNUNJudud_KuT33@pks.im>
In-Reply-To
<20260122192337.GC2098026@coredump.intra.peff.net>
On Thu, Jan 22, 2026 at 02:23:37PM -0500, Jeff King wrote:
Show 63 quoted lines
> On Thu, Jan 22, 2026 at 07:41:51AM -0800, Junio C Hamano wrote:
> 
> > Taylor Blau <me@ttaylorr.com> writes:
> > 
> > > I agree with you that we should be using an enum in these cases over
> > > unsigned for the reasons you suggest. I've stumbled over this in the
> > > past, so perhaps this is worth adding to the CodingGuidelines?
> > 
> > I am OK with declaring our preference of "enum" over "#define"d
> > constants.  The only two minor hesitation I have against the use of
> > "enum", especially for bitset but not for enumeration, are that
> 
> I don't think there's any disagreement over using enums in general. It's
> just a question of what type to declare in function interfaces.
> 
> >  (1) enum gives a false sense of type safety to casual coders. If I
> >      have two enum types and pass one to as a parameter to a
> >      function that expects the other one, would the compiler help me
> >      catch that as a potential mistake?  -Wenum-conversion is not
> >      enabled even with -Wall so I am assuming that the compiler
> >      folks fells that it is not reliable enough.
> 
> It is enabled with -Wextra, which we turn on with DEVELOPER=1. I think
> gcc will catch the most obvious mismatches like:
> 
>   enum one { FOO };
>   enum two { BAR };
>   void func(enum one value);
>   void doit(void) { func(BAR); }
> 
> which yields:
> 
>   $ gcc -c -Wall -Wextra foo.c
>   foo.c: In function ‘doit’:
>   foo.c:4:24: warning: implicit conversion from ‘enum two’ to ‘enum one’ [-Wenum-conversion]
>       4 | void doit(void) { func(BAR); }
>         |                        ^~~
> 
> What it doesn't help with is passing arbitrary integers, which includes
> #define'd constants. Swapping out "enum two" for:
> 
>   #define BAR 1
> 
> will not produce a warning. That's the issue that I ran into with the
> color code in:
> 
>   https://lore.kernel.org/git/20250916202748.GM612873@coredump.intra.peff.net/
> 
> Unfortunately bit operations on enum values seem to lose the "type" for
> the purposes of this warning, and just become regular integers. So if we
> modify our example to:
> 
>   num one { FOO_A = 1 << 0, FOO_B = 1 << 1 };
>   enum two { BAR_A = 1 << 0, BAR_B = 1 << 1 };
>   void func(enum one value);
>   void doit(void) { func(BAR_A | BAR_B); }
> 
> it no longer complains.
> 
> I still think we are better off declaring the flag parameters with the
> enum type, though. It will catch some problematic cases. And even if
> there were no compiler support at all, I think the hint to humans about
> the expected type is worth it.

I don't care strongly enough myself, but do you or Taylor maybe want to send a patch that documents our preference? If so I'll be happy to adapt my series to use whatever style we agree on.

Thanks!
Patrick
Previous: Jeff KingNext: Junio C Hamano
Message 59 of 120 in “odb: introduce `odb_for_each_object()`”
  1. 00/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 15, 2026
  2. 01/14 odb: rename `FOR_EACH_OBJECT_*` flagsPatrick Steinhardt, Jan 15, 2026
  3. Justin ToblerJan 15, 2026
  4. 02/14 odb: fix flags parameter to be unsignedPatrick Steinhardt, Jan 15, 2026
  5. 03/14 object-file: extract function to read object info from pathPatrick Steinhardt, Jan 15, 2026
  6. Justin ToblerJan 15, 2026
  7. Patrick SteinhardtJan 16, 2026
  8. Karthik NayakJan 20, 2026
  9. 04/14 object-file: introduce function to iterate through objectsPatrick Steinhardt, Jan 15, 2026
  10. Justin ToblerJan 15, 2026
  11. Patrick SteinhardtJan 16, 2026
  12. Karthik NayakJan 20, 2026
  13. 05/14 packfile: extract function to iterate through objects of a storePatrick Steinhardt, Jan 15, 2026
  14. 06/14 packfile: introduce function to iterate through objectsPatrick Steinhardt, Jan 15, 2026
  15. 07/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 15, 2026
  16. Justin ToblerJan 15, 2026
  17. Patrick SteinhardtJan 16, 2026
  18. Justin ToblerJan 16, 2026
  19. Patrick SteinhardtJan 19, 2026
  20. Karthik NayakJan 20, 2026
  21. Patrick SteinhardtJan 21, 2026
  22. 08/14 builtin/fsck: refactor to use `odb_for_each_object()`Patrick Steinhardt, Jan 15, 2026
  23. Justin ToblerJan 15, 2026
  24. 09/14 treewide: enumerate promisor objects via `odb_for_each_object()`Patrick Steinhardt, Jan 15, 2026
  25. 10/14 treewide: drop uses of `for_each_{loose,packed}_object()`Patrick Steinhardt, Jan 15, 2026
  26. Justin ToblerJan 15, 2026
  27. Patrick SteinhardtJan 16, 2026
  28. Justin ToblerJan 16, 2026
  29. Patrick SteinhardtJan 19, 2026
  30. 11/14 odb: introduce mtime fields for object info requestsPatrick Steinhardt, Jan 15, 2026
  31. 12/14 builtin/pack-objects: use `packfile_store_for_each_object()`Patrick Steinhardt, Jan 15, 2026
  32. 13/14 reachable: convert to use `odb_for_each_object()`Patrick Steinhardt, Jan 15, 2026
  33. 14/14 odb: drop unused `for_each_{loose,packed}_object()` functionsPatrick Steinhardt, Jan 15, 2026
  34. Junio C HamanoJan 15, 2026
  35. Patrick SteinhardtJan 16, 2026
  36. Junio C HamanoJan 16, 2026
  37. 00/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 20, 2026
  38. 01/14 odb: rename `FOR_EACH_OBJECT_*` flagsPatrick Steinhardt, Jan 20, 2026
  39. 02/14 odb: fix flags parameter to be unsignedPatrick Steinhardt, Jan 20, 2026
  40. 03/14 object-file: extract function to read object info from pathPatrick Steinhardt, Jan 20, 2026
  41. 04/14 object-file: introduce function to iterate through objectsPatrick Steinhardt, Jan 20, 2026
  42. 05/14 packfile: extract function to iterate through objects of a storePatrick Steinhardt, Jan 20, 2026
  43. 06/14 packfile: introduce function to iterate through objectsPatrick Steinhardt, Jan 20, 2026
  44. 07/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 20, 2026
  45. 08/14 builtin/fsck: refactor to use `odb_for_each_object()`Patrick Steinhardt, Jan 20, 2026
  46. 09/14 treewide: enumerate promisor objects via `odb_for_each_object()`Patrick Steinhardt, Jan 20, 2026
  47. 10/14 treewide: drop uses of `for_each_{loose,packed}_object()`Patrick Steinhardt, Jan 20, 2026
  48. 11/14 odb: introduce mtime fields for object info requestsPatrick Steinhardt, Jan 20, 2026
  49. 12/14 builtin/pack-objects: use `packfile_store_for_each_object()`Patrick Steinhardt, Jan 20, 2026
  50. 13/14 reachable: convert to use `odb_for_each_object()`Patrick Steinhardt, Jan 20, 2026
  51. 14/14 odb: drop unused `for_each_{loose,packed}_object()` functionsPatrick Steinhardt, Jan 20, 2026
  52. 00/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 21, 2026
  53. 01/14 odb: rename `FOR_EACH_OBJECT_*` flagsPatrick Steinhardt, Jan 21, 2026
  54. 02/14 odb: fix flags parameter to be unsignedPatrick Steinhardt, Jan 21, 2026
  55. Jeff KingJan 21, 2026
  56. Taylor BlauJan 22, 2026
  57. Junio C HamanoJan 22, 2026
  58. Jeff KingJan 22, 2026
  59. Patrick SteinhardtJan 23, 2026
  60. Junio C HamanoJan 26, 2026
  61. Patrick SteinhardtJan 22, 2026
  62. Taylor BlauJan 22, 2026
  63. 03/14 object-file: extract function to read object info from pathPatrick Steinhardt, Jan 21, 2026
  64. Taylor BlauJan 22, 2026
  65. Patrick SteinhardtJan 22, 2026
  66. Taylor BlauJan 22, 2026
  67. 04/14 object-file: introduce function to iterate through objectsPatrick Steinhardt, Jan 21, 2026
  68. Taylor BlauJan 22, 2026
  69. Patrick SteinhardtJan 22, 2026
  70. Taylor BlauJan 23, 2026
  71. 05/14 packfile: extract function to iterate through objects of a storePatrick Steinhardt, Jan 21, 2026
  72. Taylor BlauJan 22, 2026
  73. 06/14 packfile: introduce function to iterate through objectsPatrick Steinhardt, Jan 21, 2026
  74. Taylor BlauJan 23, 2026
  75. Patrick SteinhardtJan 23, 2026
  76. Chris TorekJan 23, 2026
  77. Junio C HamanoJan 23, 2026
  78. Taylor BlauJan 23, 2026
  79. 07/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 21, 2026
  80. Taylor BlauJan 23, 2026
  81. 08/14 builtin/fsck: refactor to use `odb_for_each_object()`Patrick Steinhardt, Jan 21, 2026
  82. Taylor BlauJan 23, 2026
  83. Patrick SteinhardtJan 23, 2026
  84. 09/14 treewide: enumerate promisor objects via `odb_for_each_object()`Patrick Steinhardt, Jan 21, 2026
  85. Taylor BlauJan 23, 2026
  86. 10/14 treewide: drop uses of `for_each_{loose,packed}_object()`Patrick Steinhardt, Jan 21, 2026
  87. Taylor BlauJan 23, 2026
  88. Patrick SteinhardtJan 23, 2026
  89. 11/14 odb: introduce mtime fields for object info requestsPatrick Steinhardt, Jan 21, 2026
  90. Taylor BlauJan 23, 2026
  91. Patrick SteinhardtJan 23, 2026
  92. Taylor BlauJan 23, 2026
  93. Patrick SteinhardtJan 26, 2026
  94. 12/14 builtin/pack-objects: use `packfile_store_for_each_object()`Patrick Steinhardt, Jan 21, 2026
  95. Taylor BlauJan 23, 2026
  96. Patrick SteinhardtJan 23, 2026
  97. Taylor BlauJan 23, 2026
  98. Patrick SteinhardtJan 26, 2026
  99. Jeff KingJan 29, 2026
  100. Patrick SteinhardtJan 30, 2026
  101. 13/14 reachable: convert to use `odb_for_each_object()`Patrick Steinhardt, Jan 21, 2026
  102. 14/14 odb: drop unused `for_each_{loose,packed}_object()` functionsPatrick Steinhardt, Jan 21, 2026
  103. Taylor BlauJan 22, 2026
  104. Junio C HamanoJan 22, 2026
  105. 00/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 26, 2026
  106. 01/14 odb: rename `FOR_EACH_OBJECT_*` flagsPatrick Steinhardt, Jan 26, 2026
  107. 02/14 odb: fix flags parameter to be unsignedPatrick Steinhardt, Jan 26, 2026
  108. 03/14 object-file: extract function to read object info from pathPatrick Steinhardt, Jan 26, 2026
  109. 04/14 object-file: introduce function to iterate through objectsPatrick Steinhardt, Jan 26, 2026
  110. 05/14 packfile: extract function to iterate through objects of a storePatrick Steinhardt, Jan 26, 2026
  111. 06/14 packfile: introduce function to iterate through objectsPatrick Steinhardt, Jan 26, 2026
  112. 07/14 odb: introduce `odb_for_each_object()`Patrick Steinhardt, Jan 26, 2026
  113. 08/14 builtin/fsck: refactor to use `odb_for_each_object()`Patrick Steinhardt, Jan 26, 2026
  114. 09/14 treewide: enumerate promisor objects via `odb_for_each_object()`Patrick Steinhardt, Jan 26, 2026
  115. 10/14 treewide: drop uses of `for_each_{loose,packed}_object()`Patrick Steinhardt, Jan 26, 2026
  116. 11/14 odb: introduce mtime fields for object info requestsPatrick Steinhardt, Jan 26, 2026
  117. 12/14 builtin/pack-objects: use `packfile_store_for_each_object()`Patrick Steinhardt, Jan 26, 2026
  118. 13/14 reachable: convert to use `odb_for_each_object()`Patrick Steinhardt, Jan 26, 2026
  119. 14/14 odb: drop unused `for_each_{loose,packed}_object()` functionsPatrick Steinhardt, Jan 26, 2026
  120. Junio C HamanoFeb 20, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.