Apply post-merge feedback on CompatibilityLevel.

Change-Id: I7ed466a42fd421696f38d53b45a8e01d6a6a6964
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/29980
Reviewed-by: David Turner <digit@google.com>
Commit-Queue: David Turner <digit@google.com>
diff --git a/docs/reference.md b/docs/reference.md
index 1e117bb..f504ce4 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -8000,11 +8000,12 @@
       new compatibility level so projects can opt out until their codebase is
       updated.
 
-      If not explicitly set, we assume the latest compatibility level.
+      If not explicitly set, GN implements the features of the latest
+      compatibility level (currently 0; minimum supported is 0).
 
-      Unless we have requests to extend this deadline for a particular
-      compatibility level, GN will generally support old levels for at least 1
-      month (longer for large changes).
+      To keep GN code simple, older compatibility levels are retired after
+      some time, usually after one month of their deprecation being announced
+      on the @gn-dev mailing list.
 
   experimental_collect_validations_metadata [optional]
       NOTE: This flag is experimental and will be removed in the future.
diff --git a/src/gn/build_settings.h b/src/gn/build_settings.h
index 7239594..a67a859 100644
--- a/src/gn/build_settings.h
+++ b/src/gn/build_settings.h
@@ -190,9 +190,9 @@
     experimental_collect_validations_metadata_ = value;
   }
 
-  // Compatibility level specified in the .gn file.
+  // Returns whether a given compatibility level is supported.
   bool SupportsCompatibilityLevel(CompatibilityLevel level) const {
-    return compatibility_level_.supports(level);
+    return compatibility_level_ >= level;
   }
   void set_compatibility_level(CompatibilityLevel level) {
     compatibility_level_ = level;
diff --git a/src/gn/command_help.cc b/src/gn/command_help.cc
index f03a4e7..d4494a6 100644
--- a/src/gn/command_help.cc
+++ b/src/gn/command_help.cc
@@ -193,7 +193,7 @@
                  DECORATION_NONE, NO_ESCAPING);
   }
   PrintLongHelp(kBuildArgs_Help, "buildargs");
-  PrintLongHelp(kDotfile_Help, "dotfile");
+  PrintLongHelp(GetDotfileHelp(), "dotfile");
   PrintLongHelp(kExecution_Help, "execution");
   PrintLongHelp(kGrammar_Help, "grammar");
   PrintLongHelp(kInputOutputConversion_Help, "io_conversion");
@@ -334,7 +334,7 @@
   random_topics["all"] = PrintAllHelp;
   random_topics["execution"] = []() { PrintLongHelp(kExecution_Help); };
   random_topics["buildargs"] = []() { PrintLongHelp(kBuildArgs_Help); };
-  random_topics["dotfile"] = []() { PrintLongHelp(kDotfile_Help); };
+  random_topics["dotfile"] = []() { PrintLongHelp(GetDotfileHelp()); };
   random_topics["grammar"] = []() { PrintLongHelp(kGrammar_Help); };
   random_topics["io_conversion"] = []() {
     PrintLongHelp(kInputOutputConversion_Help);
diff --git a/src/gn/compatibility_level.cc b/src/gn/compatibility_level.cc
index cd8905f..b9b1324 100644
--- a/src/gn/compatibility_level.cc
+++ b/src/gn/compatibility_level.cc
@@ -4,11 +4,37 @@
 
 #include "gn/compatibility_level.h"
 
+#include <array>
+#include <cstdlib>
 #include <string>
+#include <string_view>
 
 #include "base/strings/string_number_conversions.h"
 #include "gn/value.h"
 
+namespace {
+
+// Descriptions of breaking changes between adjacent compatibility levels.
+// kLevelDescriptions[kVersion] describes the changes required to achieve said
+// compatibility level.
+constexpr auto kLevelDescriptions = []() consteval {
+  // Default-initialized string_views are empty so missing entries fail the
+  // compile-time check below.
+  std::array<std::string_view, CompatibilityLevel::kCurrent + 1> levels;
+  levels[CompatibilityLevel::kInitial] = "Initial";
+
+  for (const auto& level : levels) {
+    if (level.empty()) {
+      // Calling a non-constexpr function in a consteval context triggers a
+      // compile-time error.
+      std::abort();
+    }
+  }
+  return levels;
+}();
+
+}  // namespace
+
 // static
 Result<CompatibilityLevel> CompatibilityLevel::Parse(const Value* value) {
   if (!value) {
@@ -24,7 +50,7 @@
                            " (must be >= 0).");
   }
 
-  if (level.supports(CompatibilityLevel::kNext)) {
+  if (level.level_ > CompatibilityLevel::kCurrent) {
     return Err(*value,
                "Unsupported compatibility_level " +
                    base::Int64ToString(level.level_) + ".",
@@ -33,16 +59,16 @@
                    ". Please update GN.");
   }
 
-  if (!level.supports(CompatibilityLevel::kMin)) {
+  if (level < CompatibilityLevel::kMin) {
     std::string help = "The minimum supported compatibility_level is " +
                        base::Int64ToString(CompatibilityLevel::kMin) +
                        " (currently requested " +
                        base::Int64ToString(level.level_) +
                        ").\nChanges required to upgrade:\n";
-    for (int64_t i = level.level_; i < CompatibilityLevel::kMin; ++i) {
-      help += "  Level " + base::Int64ToString(i) + " -> " +
-              base::Int64ToString(i + 1) + ": " +
-              std::string(kCompatibilityLevels[i]) + "\n";
+    for (int64_t i = level.level_ + 1; i <= CompatibilityLevel::kMin; ++i) {
+      help += "  Level " + base::Int64ToString(i - 1) + " -> " +
+              base::Int64ToString(i) + ": " +
+              std::string(kLevelDescriptions[i]) + "\n";
     }
     return Err(*value,
                "compatibility_level " + base::Int64ToString(level.level_) +
diff --git a/src/gn/compatibility_level.h b/src/gn/compatibility_level.h
index 18d5b9d..85cec1f 100644
--- a/src/gn/compatibility_level.h
+++ b/src/gn/compatibility_level.h
@@ -7,9 +7,7 @@
 
 #include <stdint.h>
 
-#include <array>
-#include <cstddef>
-#include <string_view>
+#include <compare>
 
 #include "gn/err.h"
 
@@ -20,78 +18,46 @@
 // Process for making backwards-incompatible changes:
 // - File a tracking bug at https://gn.g-issues.chromium.org/issues/new for
 //   deprecating the old compatibility level, and use bugjuggler to assign it
-//   to yourself after the deprecation window has passed (at least 1 month,
-//   longer for large changes).
-// - Add a new enum value (e.g. `kNewLevel`) to `CompatibilityLevel::Level`
-//   immediately before `kNext`.
+//   to yourself after the deprecation window has passed (usually at least 1
+//   month after deprecation is announced on @gn-dev, longer for large changes).
+// - Add a new constant (e.g. `kNewLevel = kCurrent + 1`) in
+//   `CompatibilityLevel` and update `kCurrent = kNewLevel`.
 // - Gate the backwards-incompatible behavior on
 //   `build_settings->SupportsCompatibilityLevel(CompatibilityLevel::kNewLevel)`,
 //   keeping the previous behavior for older levels.
-// - Add an entry to `kCompatibilityLevels` describing the changes from the
-//   previous level (including the tracking bug URL).
+// - Populate `levels[kNewLevel]` in `kLevelDescriptions` in
+//   `compatibility_level.cc` describing the changes from the previous level
+//   (including the tracking bug URL).
 //
-// Process for removing old compatibility levels / flags (after the deprecation
-// window):
+// Process for removing old compatibility levels / flags (after announcing
+// deprecation on @gn-dev and waiting through the deprecation window):
 // - Check the tracking bug for any requests to extend the deprecation deadline.
-// - Delete the oldest enumerator(s) from `CompatibilityLevel::Level` (must be
-//   removed in order).
-// - Add the number of deleted items to `kLastUnsupported`.
-// - Keep old entries in `kCompatibilityLevels` so indices remain aligned with
-//   `Level` values.
+// - Update `kMin` to the new minimum supported level (keeping indices in
+//   `kLevelDescriptions` aligned with compatibility level values).
 // - Replace `build_settings->SupportsCompatibilityLevel(kRemovedFlag)` with
 //   `true` and simplify code paths.
 class CompatibilityLevel {
  public:
-  enum Level : int64_t {
-    // The minimum supported level - 1.
-    // This must be incremented when deleting enum entries, and enum entries
-    // must be deleted in order.
-    kLastUnsupported = -1,
+  static constexpr int64_t kInitial = 0;
 
-    kInitial,
-
-    kNext,
-  };
-
-  static constexpr Level kMin =
-      static_cast<Level>(static_cast<int64_t>(Level::kLastUnsupported) + 1);
-  static constexpr Level kCurrent =
-      static_cast<Level>(static_cast<int64_t>(Level::kNext) - 1);
+  static constexpr int64_t kMin = kInitial;
+  static constexpr int64_t kCurrent = kInitial;
 
   constexpr CompatibilityLevel() = default;
-  constexpr CompatibilityLevel(Level level) : level_(level) {}
+  constexpr CompatibilityLevel(int64_t level) : level_(level) {}
 
   // Extracts and validates `compatibility_level` against `[kMin, kCurrent]`.
+  // nullptr parses as kCurrent.
   static Result<CompatibilityLevel> Parse(const Value* value);
 
-  constexpr bool supports(CompatibilityLevel other) const {
-    return level_ >= other.level_;
+  constexpr auto operator<=>(CompatibilityLevel other) const {
+    return level_ <=> other.level_;
   }
 
  private:
-  constexpr explicit CompatibilityLevel(int64_t level)
-      : level_(static_cast<Level>(level)) {}
-
   // By defaulting to kCurrent, we ensure that any project that does not
   // explicitly specify `compatibility_level` is pinned to the latest.
-  Level level_ = kCurrent;
+  int64_t level_ = kCurrent;
 };
 
-// Descriptions of breaking changes between adjacent compatibility levels.
-// Entry `i` describes the changes between compatibility level `i` and `i + 1`.
-inline constexpr std::array<std::string_view,
-                            static_cast<size_t>(CompatibilityLevel::kCurrent)>
-    kCompatibilityLevels = {};
-
-// If we're missing an entry, it will try to default-initialize it. We don't
-// want that.
-static_assert([]() {
-  for (std::string_view desc : kCompatibilityLevels) {
-    if (desc.empty()) {
-      return false;
-    }
-  }
-  return true;
-}());
-
 #endif  // TOOLS_GN_COMPATIBILITY_LEVEL_H_
diff --git a/src/gn/compatibility_level_unittest.cc b/src/gn/compatibility_level_unittest.cc
index ebfb18d..b143e74 100644
--- a/src/gn/compatibility_level_unittest.cc
+++ b/src/gn/compatibility_level_unittest.cc
@@ -14,8 +14,8 @@
 TEST(CompatibilityLevelTest, ParseDefault) {
   Result<CompatibilityLevel> result = CompatibilityLevel::Parse(nullptr);
   ASSERT_TRUE(result.has_value());
-  EXPECT_TRUE(result->supports(CompatibilityLevel::kCurrent));
-  EXPECT_FALSE(result->supports(CompatibilityLevel::kNext));
+  EXPECT_TRUE(*result >= CompatibilityLevel::kCurrent);
+  EXPECT_FALSE(*result >= CompatibilityLevel::kCurrent + 1);
 }
 
 TEST(CompatibilityLevelTest, ParseInvalidType) {
@@ -32,45 +32,44 @@
   EXPECT_EQ("Invalid compatibility_level -5 (must be >= 0).", result.message());
 }
 
-TEST(CompatibilityLevelTest, ParseLastUnsupported) {
-  Value value(nullptr, CompatibilityLevel::kLastUnsupported);
+TEST(CompatibilityLevelTest, ParseBelowMin) {
+  int64_t below_min = CompatibilityLevel::kMin - 1;
+  Value value(nullptr, below_min);
   Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value);
   ASSERT_TRUE(result.has_error());
-  if (CompatibilityLevel::kLastUnsupported < 0) {
-    EXPECT_EQ("Invalid compatibility_level " +
-                  std::to_string(CompatibilityLevel::kLastUnsupported) +
+  if (below_min < 0) {
+    EXPECT_EQ("Invalid compatibility_level " + std::to_string(below_min) +
                   " (must be >= 0).",
               result.message());
   } else {
-    EXPECT_EQ("compatibility_level " +
-                  std::to_string(CompatibilityLevel::kLastUnsupported) +
+    EXPECT_EQ("compatibility_level " + std::to_string(below_min) +
                   " is no longer supported.",
               result.message());
   }
 }
 
 TEST(CompatibilityLevelTest, ParseMin) {
-  Value value(nullptr, static_cast<int64_t>(CompatibilityLevel::kMin));
+  Value value(nullptr, CompatibilityLevel::kMin);
   Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value);
   ASSERT_TRUE(result.has_value());
-  EXPECT_TRUE(result->supports(CompatibilityLevel::kMin));
-  EXPECT_FALSE(result->supports(CompatibilityLevel(
-      static_cast<CompatibilityLevel::Level>(CompatibilityLevel::kMin + 1))));
+  EXPECT_TRUE(*result >= CompatibilityLevel::kMin);
+  EXPECT_FALSE(*result >= CompatibilityLevel::kMin + 1);
 }
 
 TEST(CompatibilityLevelTest, ParseCurrent) {
-  Value value(nullptr, static_cast<int64_t>(CompatibilityLevel::kCurrent));
+  Value value(nullptr, CompatibilityLevel::kCurrent);
   Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value);
   ASSERT_TRUE(result.has_value());
-  EXPECT_TRUE(result->supports(CompatibilityLevel::kCurrent));
-  EXPECT_FALSE(result->supports(CompatibilityLevel::kNext));
+  EXPECT_TRUE(*result >= CompatibilityLevel::kCurrent);
+  EXPECT_FALSE(*result >= CompatibilityLevel::kCurrent + 1);
 }
 
-TEST(CompatibilityLevelTest, ParseNext) {
-  Value value(nullptr, CompatibilityLevel::kNext);
+TEST(CompatibilityLevelTest, ParseAboveCurrent) {
+  int64_t above_current = CompatibilityLevel::kCurrent + 1;
+  Value value(nullptr, above_current);
   Result<CompatibilityLevel> result = CompatibilityLevel::Parse(&value);
   ASSERT_TRUE(result.has_error());
-  EXPECT_EQ("Unsupported compatibility_level " +
-                std::to_string(CompatibilityLevel::kNext) + ".",
-            result.message());
+  EXPECT_EQ(
+      "Unsupported compatibility_level " + std::to_string(above_current) + ".",
+      result.message());
 }
diff --git a/src/gn/setup.cc b/src/gn/setup.cc
index 9f5dfd6..cb6c0a8 100644
--- a/src/gn/setup.cc
+++ b/src/gn/setup.cc
@@ -47,8 +47,9 @@
 #include "base/win/win_util.h"
 #endif
 
-const char kDotfile_Help[] =
-    R"(.gn file
+std::string GetDotfileHelp() {
+  return std::string(
+             R"(.gn file
 
   When gn starts, it will search the current directory and parent directories
   for a file called ".gn". This indicates the source root. You can override
@@ -297,11 +298,15 @@
       new compatibility level so projects can opt out until their codebase is
       updated.
 
-      If not explicitly set, we assume the latest compatibility level.
+      If not explicitly set, GN implements the features of the latest
+      compatibility level (currently )") +
+         base::Int64ToString(CompatibilityLevel::kCurrent) +
+         R"(; minimum supported is )" +
+         base::Int64ToString(CompatibilityLevel::kMin) + R"().
 
-      Unless we have requests to extend this deadline for a particular
-      compatibility level, GN will generally support old levels for at least 1
-      month (longer for large changes).
+      To keep GN code simple, older compatibility levels are retired after
+      some time, usually after one month of their deprecation being announced
+      on the @gn-dev mailing list.
 
   experimental_collect_validations_metadata [optional]
       NOTE: This flag is experimental and will be removed in the future.
@@ -330,6 +335,7 @@
     is_component_build = false
   }
 )";
+}
 
 namespace {
 
diff --git a/src/gn/setup.h b/src/gn/setup.h
index 06f7890..664d974 100644
--- a/src/gn/setup.h
+++ b/src/gn/setup.h
@@ -7,6 +7,7 @@
 
 #include <functional>
 #include <memory>
+#include <string>
 #include <vector>
 
 #include "base/files/file_path.h"
@@ -28,7 +29,7 @@
 class CommandLine;
 }
 
-extern const char kDotfile_Help[];
+std::string GetDotfileHelp();
 
 // Helper class to set up the build settings and environment for the various
 // commands to run.