Skip to content

Commit 08beb21

Browse files
tetrominocopybara-github
authored andcommitted
Put macro attribute inheritance behind an off-by-default --experimental_enable_macro_inherit_attrs flag
We still have open questions about how macro attribute inheritance ought to interact with the tracking of whether rule attributes were or were not explicitly provided. In effect, this re-opens #24066 Part of addressing #24319 RELNOTES: symbolic macro attribute inheritance is now marked experimental; set --experimental_enable_macro_inherit_attrs flag to enable it. PiperOrigin-RevId: 696582223 Change-Id: I3d7cb434bf8fe2da9cd10019e6075990205e7153
1 parent 12dc0b9 commit 08beb21

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/main/java/com/google/devtools/build/lib/packages/semantics/BuildLanguageOptions.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,16 @@ public final class BuildLanguageOptions extends OptionsBase {
244244
help = "If set to true, enables the `macro()` construct for defining symbolic macros.")
245245
public boolean experimentalEnableFirstClassMacros;
246246

247+
@Option(
248+
name = "experimental_enable_macro_inherit_attrs",
249+
defaultValue = "false",
250+
documentationCategory = OptionDocumentationCategory.STARLARK_SEMANTICS,
251+
effectTags = OptionEffectTag.BUILD_FILE_SEMANTICS,
252+
help =
253+
"If set to true, enables attribute inheritance in symbolic macros and the inherit_attrs"
254+
+ " parameter in the macro() built-in")
255+
public boolean experimentalEnableMacroInheritAttrs;
256+
247257
@Option(
248258
name = "experimental_enable_scl_dialect",
249259
defaultValue = "true",
@@ -857,6 +867,7 @@ public StarlarkSemantics toStarlarkSemantics() {
857867
EXPERIMENTAL_SINGLE_PACKAGE_TOOLCHAIN_BINDING,
858868
experimentalSinglePackageToolchainBinding)
859869
.setBool(EXPERIMENTAL_ENABLE_FIRST_CLASS_MACROS, experimentalEnableFirstClassMacros)
870+
.setBool(EXPERIMENTAL_ENABLE_MACRO_INHERIT_ATTRS, experimentalEnableMacroInheritAttrs)
860871
.setBool(EXPERIMENTAL_ENABLE_SCL_DIALECT, experimentalEnableSclDialect)
861872
.setBool(ENABLE_BZLMOD, enableBzlmod)
862873
.setBool(ENABLE_WORKSPACE, enableWorkspace)
@@ -972,6 +983,8 @@ public StarlarkSemantics toStarlarkSemantics() {
972983
"-experimental_single_package_toolchain_binding";
973984
public static final String EXPERIMENTAL_ENABLE_FIRST_CLASS_MACROS =
974985
"+experimental_enable_first_class_macros";
986+
public static final String EXPERIMENTAL_ENABLE_MACRO_INHERIT_ATTRS =
987+
"-experimental_enable_macro_inherit_attrs";
975988
public static final String EXPERIMENTAL_ENABLE_SCL_DIALECT = "+experimental_enable_scl_dialect";
976989
public static final String ENABLE_BZLMOD = "+enable_bzlmod";
977990
public static final String ENABLE_WORKSPACE = "-enable_workspace";

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/StarlarkRuleFunctionsApi.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,8 @@ site of the rule. Such attributes can be assigned a default value (as in
264264
positional = false,
265265
named = true,
266266
defaultValue = "None",
267+
enableOnlyWithFlag = BuildLanguageOptions.EXPERIMENTAL_ENABLE_MACRO_INHERIT_ATTRS,
268+
valueWhenDisabled = "None",
267269
doc =
268270
"""
269271
A rule symbol, macro symbol, or the name of a built-in common attribute list (see below) from which

src/test/java/com/google/devtools/build/lib/analysis/SymbolicMacroTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1582,8 +1582,33 @@ def _impl(name, visibility, **kwargs):
15821582
.doesNotContainKey("disabled_attr");
15831583
}
15841584

1585+
@Test
1586+
public void inheritAttrs_disabledByDefault() throws Exception {
1587+
scratch.file(
1588+
"pkg/foo.bzl",
1589+
"""
1590+
def _my_macro_impl(name, visibility, **kwargs):
1591+
pass
1592+
1593+
my_macro = macro(
1594+
implementation = _my_macro_impl,
1595+
inherit_attrs = native.cc_library,
1596+
)
1597+
""");
1598+
scratch.file(
1599+
"pkg/BUILD",
1600+
"""
1601+
load(":foo.bzl", "my_macro")
1602+
""");
1603+
reporter.removeHandler(failFastHandler);
1604+
assertThat(getPackage("pkg")).isNull();
1605+
assertContainsEvent(
1606+
"parameter 'inherit_attrs' is experimental and thus unavailable with the current flags");
1607+
}
1608+
15851609
@Test
15861610
public void inheritAttrs_fromInvalidSource_fails() throws Exception {
1611+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
15871612
scratch.file(
15881613
"pkg/foo.bzl",
15891614
"""
@@ -1608,6 +1633,7 @@ def _my_macro_impl(name, visibility, **kwargs):
16081633

16091634
@Test
16101635
public void inheritAttrs_withoutKwargsInImplementation_fails() throws Exception {
1636+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
16111637
scratch.file(
16121638
"pkg/foo.bzl",
16131639
"""
@@ -1632,6 +1658,7 @@ def _my_macro_impl(name, visibility, tags):
16321658

16331659
@Test
16341660
public void inheritAttrs_fromCommon_withOverrides() throws Exception {
1661+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
16351662
scratch.file(
16361663
"pkg/my_macro.bzl",
16371664
"""
@@ -1694,6 +1721,7 @@ public void inheritAttrs_fromAnyNativeRule() throws Exception {
16941721
// * a new AttributeValueSource or a new attribute type is introduced, and symbolic macros
16951722
// cannot inherit an attribute with a default with this source or of such a type (to fix, add
16961723
// a check for it in MacroClass#forceDefaultToNone).
1724+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
16971725
for (RuleClass ruleClass : getBuiltinRuleClasses(false)) {
16981726
if (ruleClass.getAttributes().isEmpty()) {
16991727
continue;
@@ -1771,6 +1799,7 @@ def _impl(name, visibility, **kwargs):
17711799

17721800
@Test
17731801
public void inheritAttrs_fromExportedStarlarkRule() throws Exception {
1802+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
17741803
scratch.file(
17751804
"pkg/my_rule.bzl",
17761805
"""
@@ -1813,6 +1842,7 @@ def _my_macro_impl(name, visibility, **kwargs):
18131842

18141843
@Test
18151844
public void inheritAttrs_fromUnexportedStarlarkRule() throws Exception {
1845+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
18161846
scratch.file(
18171847
"pkg/my_macro.bzl",
18181848
"""
@@ -1850,6 +1880,7 @@ def _my_macro_impl(name, visibility, **kwargs):
18501880

18511881
@Test
18521882
public void inheritAttrs_fromExportedMacro() throws Exception {
1883+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
18531884
scratch.file(
18541885
"pkg/other_macro.bzl",
18551886
"""
@@ -1890,6 +1921,7 @@ def _my_macro_impl(name, visibility, **kwargs):
18901921

18911922
@Test
18921923
public void inheritAttrs_fromUnexportedMacro() throws Exception {
1924+
setBuildLanguageOptions("--experimental_enable_macro_inherit_attrs");
18931925
scratch.file(
18941926
"pkg/my_macro.bzl",
18951927
"""

0 commit comments

Comments
 (0)