Add --incompatible_remove_enabled_toolchain_types flag.

This removes the "enabled_toolchain_types" field on the platform
configuration fragment in Starlark.

Part of #10262.

Closes #10263.

RELNOTES[INC]: Adds --incompatible_remove_enabled_toolchain_types.

PiperOrigin-RevId: 281324862
diff --git a/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java b/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java
index 01911bc..b0e2dc2 100644
--- a/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java
+++ b/src/main/java/com/google/devtools/build/lib/packages/StarlarkSemanticsOptions.java
@@ -489,6 +489,20 @@
   public boolean incompatibleRemapMainRepo;
 
   @Option(
+      name = "incompatible_remove_enabled_toolchain_types",
+      defaultValue = "false",
+      documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
+      effectTags = {OptionEffectTag.BUILD_FILE_SEMANTICS},
+      metadataTags = {
+        OptionMetadataTag.INCOMPATIBLE_CHANGE,
+        OptionMetadataTag.TRIGGERED_BY_ALL_INCOMPATIBLE_CHANGES
+      },
+      help =
+          "If set to true, the platform configuration fragment cannot access the (deprecated) list"
+              + " of enabled toolchain types.")
+  public boolean incompatibleRemoveEnabledToolchainTypes;
+
+  @Option(
       name = "incompatible_remove_native_maven_jar",
       defaultValue = "true",
       documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
@@ -640,6 +654,7 @@
             .incompatibleNoSupportToolsInActionInputs(incompatibleNoSupportToolsInActionInputs)
             .incompatibleNoTargetOutputGroup(incompatibleNoTargetOutputGroup)
             .incompatibleRemapMainRepo(incompatibleRemapMainRepo)
+            .incompatibleRemoveEnabledToolchainTypes(incompatibleRemoveEnabledToolchainTypes)
             .incompatibleRemoveNativeMavenJar(incompatibleRemoveNativeMavenJar)
             .incompatibleRestrictNamedParams(incompatibleRestrictNamedParams)
             .incompatibleRunShellCommandString(incompatibleRunShellCommandString)
diff --git a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/platform/PlatformConfigurationApi.java b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/platform/PlatformConfigurationApi.java
index c2e551f..c9ef392 100644
--- a/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/platform/PlatformConfigurationApi.java
+++ b/src/main/java/com/google/devtools/build/lib/skylarkbuildapi/platform/PlatformConfigurationApi.java
@@ -20,6 +20,7 @@
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModule;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory;
 import com.google.devtools.build.lib.skylarkinterface.SkylarkValue;
+import com.google.devtools.build.lib.syntax.StarlarkSemantics.FlagIdentifier;
 import java.util.List;
 
 /** The platform configuration. */
@@ -46,6 +47,7 @@
   @SkylarkCallable(
       name = "enabled_toolchain_types",
       structField = true,
+      disableWithFlag = FlagIdentifier.INCOMPATIBLE_REMOVE_ENABLE_TOOLCHAIN_TYPES,
       doc = "The set of toolchain types enabled for platform-based toolchain selection.")
   List<Label> getEnabledToolchainTypes();
 }
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java b/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java
index 133c397..81ab452 100644
--- a/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java
+++ b/src/main/java/com/google/devtools/build/lib/syntax/StarlarkSemantics.java
@@ -67,6 +67,8 @@
     INCOMPATIBLE_NO_TARGET_OUTPUT_GROUP(StarlarkSemantics::incompatibleNoTargetOutputGroup),
     INCOMPATIBLE_NO_ATTR_LICENSE(StarlarkSemantics::incompatibleNoAttrLicense),
     INCOMPATIBLE_ALLOW_TAGS_PROPAGATION(StarlarkSemantics::experimentalAllowTagsPropagation),
+    INCOMPATIBLE_REMOVE_ENABLE_TOOLCHAIN_TYPES(
+        StarlarkSemantics::incompatibleRemoveEnabledToolchainTypes),
     NONE(null);
 
     // Using a Function here makes the enum definitions far cleaner, and, since this is
@@ -187,6 +189,8 @@
 
   public abstract boolean incompatibleRemapMainRepo();
 
+  public abstract boolean incompatibleRemoveEnabledToolchainTypes();
+
   public abstract boolean incompatibleRemoveNativeMavenJar();
 
   public abstract boolean incompatibleRestrictNamedParams();
@@ -272,6 +276,7 @@
           .incompatibleNoSupportToolsInActionInputs(true)
           .incompatibleNoTargetOutputGroup(true)
           .incompatibleRemapMainRepo(true)
+          .incompatibleRemoveEnabledToolchainTypes(false)
           .incompatibleRemoveNativeMavenJar(true)
           .incompatibleRunShellCommandString(false)
           .incompatibleRestrictNamedParams(true)
@@ -350,6 +355,8 @@
 
     public abstract Builder incompatibleRemapMainRepo(boolean value);
 
+    public abstract Builder incompatibleRemoveEnabledToolchainTypes(boolean value);
+
     public abstract Builder incompatibleRemoveNativeMavenJar(boolean value);
 
     public abstract Builder incompatibleRestrictNamedParams(boolean value);
diff --git a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
index e2e1f51..63ad8c8 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
@@ -153,6 +153,7 @@
         "--incompatible_no_support_tools_in_action_inputs=" + rand.nextBoolean(),
         "--incompatible_no_target_output_group=" + rand.nextBoolean(),
         "--incompatible_remap_main_repo=" + rand.nextBoolean(),
+        "--incompatible_remove_enabled_toolchain_types=" + rand.nextBoolean(),
         "--incompatible_remove_native_maven_jar=" + rand.nextBoolean(),
         "--incompatible_restrict_named_params=" + rand.nextBoolean(),
         "--incompatible_run_shell_command_string=" + rand.nextBoolean(),
@@ -203,6 +204,7 @@
         .incompatibleNoSupportToolsInActionInputs(rand.nextBoolean())
         .incompatibleNoTargetOutputGroup(rand.nextBoolean())
         .incompatibleRemapMainRepo(rand.nextBoolean())
+        .incompatibleRemoveEnabledToolchainTypes(rand.nextBoolean())
         .incompatibleRemoveNativeMavenJar(rand.nextBoolean())
         .incompatibleRestrictNamedParams(rand.nextBoolean())
         .incompatibleRunShellCommandString(rand.nextBoolean())
diff --git a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformConfigurationApiTest.java b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformConfigurationApiTest.java
index 34437c6..0787fe6 100644
--- a/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformConfigurationApiTest.java
+++ b/src/test/java/com/google/devtools/build/lib/rules/platform/PlatformConfigurationApiTest.java
@@ -14,15 +14,15 @@
 package com.google.devtools.build.lib.rules.platform;
 
 import static com.google.common.truth.Truth.assertThat;
+import static com.google.devtools.build.lib.testutil.MoreAsserts.assertThrows;
 
-import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.devtools.build.lib.analysis.ConfiguredTarget;
 import com.google.devtools.build.lib.analysis.util.BuildViewTestCase;
 import com.google.devtools.build.lib.cmdline.Label;
 import com.google.devtools.build.lib.packages.SkylarkProvider.SkylarkKey;
 import com.google.devtools.build.lib.packages.StructImpl;
-import com.google.devtools.build.lib.skylarkbuildapi.platform.PlatformConfigurationApi;
+import java.util.List;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.junit.runners.JUnit4;
@@ -35,42 +35,64 @@
   public void testHostPlatform() throws Exception {
     scratch.file("platforms/BUILD", "platform(name = 'test_platform')");
 
-    useConfiguration("--host_platform=//platforms:test_platform");
-    ruleBuilder().build();
     scratch.file(
-        "foo/BUILD",
-        "load(':extension.bzl', 'my_rule')",
-        "my_rule(",
-        "  name = 'my_skylark_rule',",
+        "verify/verify.bzl",
+        "result = provider()",
+        "def _impl(ctx):",
+        "  platformConfig = ctx.fragments.platform",
+        "  host_platform = platformConfig.host_platform",
+        "  return [result(",
+        "    host_platform = host_platform,",
+        "  )]",
+        "verify = rule(",
+        "  implementation = _impl,",
+        "  fragments = ['platform'],",
         ")");
-    assertNoEvents();
+    scratch.file("verify/BUILD", "load(':verify.bzl', 'verify')", "verify(name = 'verify')");
 
-    PlatformConfigurationApi platformConfiguration = fetchPlatformConfiguration();
-    assertThat(platformConfiguration).isNotNull();
-    assertThat(platformConfiguration.getHostPlatform())
-        .isEqualTo(Label.parseAbsoluteUnchecked("//platforms:test_platform"));
+    useConfiguration("--host_platform=//platforms:test_platform");
+
+    ConfiguredTarget myRuleTarget = getConfiguredTarget("//verify:verify");
+    StructImpl info =
+        (StructImpl)
+            myRuleTarget.get(
+                new SkylarkKey(
+                    Label.parseAbsolute("//verify:verify.bzl", ImmutableMap.of()), "result"));
+
+    Label hostPlatform = (Label) info.getValue("host_platform");
+    assertThat(hostPlatform).isEqualTo(Label.parseAbsoluteUnchecked("//platforms:test_platform"));
   }
 
   @Test
   public void testTargetPlatform_single() throws Exception {
     scratch.file("platforms/BUILD", "platform(name = 'test_platform')");
 
-    useConfiguration("--platforms=//platforms:test_platform");
-    ruleBuilder().build();
     scratch.file(
-        "foo/BUILD",
-        "load(':extension.bzl', 'my_rule')",
-        "my_rule(",
-        "  name = 'my_skylark_rule',",
+        "verify/verify.bzl",
+        "result = provider()",
+        "def _impl(ctx):",
+        "  platformConfig = ctx.fragments.platform",
+        "  target_platform = platformConfig.platform",
+        "  return [result(",
+        "    target_platform = target_platform,",
+        "  )]",
+        "verify = rule(",
+        "  implementation = _impl,",
+        "  fragments = ['platform'],",
         ")");
-    assertNoEvents();
+    scratch.file("verify/BUILD", "load(':verify.bzl', 'verify')", "verify(name = 'verify')");
 
-    PlatformConfigurationApi platformConfiguration = fetchPlatformConfiguration();
-    assertThat(platformConfiguration).isNotNull();
-    assertThat(platformConfiguration.getTargetPlatform())
-        .isEqualTo(Label.parseAbsoluteUnchecked("//platforms:test_platform"));
-    assertThat(platformConfiguration.getTargetPlatforms())
-        .containsExactly(Label.parseAbsoluteUnchecked("//platforms:test_platform"));
+    useConfiguration("--platforms=//platforms:test_platform");
+
+    ConfiguredTarget myRuleTarget = getConfiguredTarget("//verify:verify");
+    StructImpl info =
+        (StructImpl)
+            myRuleTarget.get(
+                new SkylarkKey(
+                    Label.parseAbsolute("//verify:verify.bzl", ImmutableMap.of()), "result"));
+
+    Label targetPlatform = (Label) info.getValue("target_platform");
+    assertThat(targetPlatform).isEqualTo(Label.parseAbsoluteUnchecked("//platforms:test_platform"));
   }
 
   @Test
@@ -81,54 +103,73 @@
         "toolchain_type(name = 'test_toolchain_type2')",
         "toolchain_type(name = 'test_toolchain_type3')");
 
+    scratch.file(
+        "verify/verify.bzl",
+        "result = provider()",
+        "def _impl(ctx):",
+        "  platformConfig = ctx.fragments.platform",
+        "  enabled_toolchain_types = platformConfig.enabled_toolchain_types",
+        "  return [result(",
+        "    enabled_toolchain_types = enabled_toolchain_types,",
+        "  )]",
+        "verify = rule(",
+        "  implementation = _impl,",
+        "  fragments = ['platform'],",
+        ")");
+    scratch.file("verify/BUILD", "load(':verify.bzl', 'verify')", "verify(name = 'verify',", ")");
+
+    setSkylarkSemanticsOptions("--incompatible_remove_enabled_toolchain_types=false");
     useConfiguration(
         "--enabled_toolchain_types="
             + "//toolchains:test_toolchain_type1,//toolchains:test_toolchain_type3");
-    ruleBuilder().build();
-    scratch.file(
-        "foo/BUILD",
-        "load(':extension.bzl', 'my_rule')",
-        "my_rule(",
-        "  name = 'my_skylark_rule',",
-        ")");
-    assertNoEvents();
 
-    PlatformConfigurationApi platformConfiguration = fetchPlatformConfiguration();
-    assertThat(platformConfiguration).isNotNull();
-    assertThat(platformConfiguration.getEnabledToolchainTypes())
+    ConfiguredTarget myRuleTarget = getConfiguredTarget("//verify:verify");
+    StructImpl info =
+        (StructImpl)
+            myRuleTarget.get(
+                new SkylarkKey(
+                    Label.parseAbsolute("//verify:verify.bzl", ImmutableMap.of()), "result"));
+
+    @SuppressWarnings("unchecked")
+    List<Label> enabledToolchainTypes = (List<Label>) info.getValue("enabled_toolchain_types");
+    assertThat(enabledToolchainTypes)
         .containsExactly(
             Label.parseAbsoluteUnchecked("//toolchains:test_toolchain_type1"),
             Label.parseAbsoluteUnchecked("//toolchains:test_toolchain_type3"));
   }
 
-  private RuleBuilder ruleBuilder() {
-    return new RuleBuilder();
-  }
+  @Test
+  public void testEnabledToolchainTypes_disabled() throws Exception {
+    scratch.file(
+        "toolchains/BUILD",
+        "toolchain_type(name = 'test_toolchain_type1')",
+        "toolchain_type(name = 'test_toolchain_type2')",
+        "toolchain_type(name = 'test_toolchain_type3')");
 
-  private class RuleBuilder {
-    private void build() throws Exception {
-      ImmutableList.Builder<String> lines = ImmutableList.builder();
-      lines.add(
-          "result = provider()",
-          "def _impl(ctx):",
-          "  platformConfig = ctx.fragments.platform",
-          "  return [result(property = platformConfig)]");
-      lines.add("my_rule = rule(", "  implementation = _impl,", "  fragments = ['platform'],", ")");
+    scratch.file(
+        "verify/verify.bzl",
+        "result = provider()",
+        "def _impl(ctx):",
+        "  platformConfig = ctx.fragments.platform",
+        "  enabled_toolchain_types = platformConfig.enabled_toolchain_types",
+        "  return [result(",
+        "    enabled_toolchain_types = enabled_toolchain_types,",
+        "  )]",
+        "verify = rule(",
+        "  implementation = _impl,",
+        "  fragments = ['platform'],",
+        ")");
+    scratch.file("verify/BUILD", "load(':verify.bzl', 'verify')", "verify(name = 'verify',", ")");
 
-      scratch.file("foo/extension.bzl", lines.build().toArray(new String[] {}));
-    }
-  }
+    setSkylarkSemanticsOptions("--incompatible_remove_enabled_toolchain_types");
+    useConfiguration(
+        "--enabled_toolchain_types="
+            + "//toolchains:test_toolchain_type1,//toolchains:test_toolchain_type3");
 
-  private PlatformConfigurationApi fetchPlatformConfiguration() throws Exception {
-    ConfiguredTarget myRuleTarget = getConfiguredTarget("//foo:my_skylark_rule");
-    StructImpl info =
-        (StructImpl)
-            myRuleTarget.get(
-                new SkylarkKey(
-                    Label.parseAbsolute("//foo:extension.bzl", ImmutableMap.of()), "result"));
-
-    @SuppressWarnings("unchecked")
-    PlatformConfigurationApi javaInfo = (PlatformConfigurationApi) info.getValue("property");
-    return javaInfo;
+    AssertionError error =
+        assertThrows(AssertionError.class, () -> getConfiguredTarget("//verify:verify"));
+    assertThat(error)
+        .hasMessageThat()
+        .contains("object of type 'platform' has no field 'enabled_toolchain_types'");
   }
 }