Skip to content

Commit 6b145c4

Browse files
David Tolnayfacebook-github-bot
David Tolnay
authored andcommitted
Allow alias's actual to be None
Summary: I found this useful for expressing `hashbrown`'s conditional dependency on `allocator-api2`. `actual = None` is a way to disappear a dependency at the incoming edge (like how `incoming_transition` applies), as opposed to the outgoing edge (like `transition_dep`). ```lang=toml # fixups/hashbrown/fixups.toml # Hashbrown does not really depend on allocator-api2 but `cargo metadata` is # claiming that it is, likely because of rust-lang/cargo#10801 omit_deps = ["allocator_api2"] extra_deps = ["//allocator:allocator-api2"] [rustc_flags_select] "//constraints:compiler" = [] "//constraints:library" = ["-Zforce-unstable-if-unmarked"] ``` ```lang=starlark # allocator/BUCK alias( name = "allocator-api2", actual = select({ "//constraints:library": None, # <--- "//constraints:compiler": "//:allocator-api2", }), visibility = ["//:"], ) ``` ```lang=starlark # BUCK, generated by Reindeer rust_bootstrap_library( name = "hashbrown-0.15.2", srcs = [":hashbrown-0.15.2.crate"], crate = "hashbrown", crate_root = "hashbrown-0.15.2.crate/src/lib.rs", edition = "2021", features = select({ "//constraints:compiler": ["default-hasher"], "//constraints:library": [ "alloc", "compiler_builtins", "core", "nightly", "raw-entry", "rustc-dep-of-std", "rustc-internal-api", ], }), named_deps = select({ "//constraints:compiler": {}, "//constraints:library": { "alloc": ":rustc-std-workspace-alloc-1.99.0", "core": ":rustc-std-workspace-core-1.99.0", }, }), rustc_flags = select({ "//constraints:compiler": [], "//constraints:library": ["-Zforce-unstable-if-unmarked"], }), visibility = [], deps = select({ "//constraints:compiler": [ ":foldhash-0.1.5", "//allocator:allocator-api2", ], "//constraints:library": [ ":compiler_builtins-0.1.146", "//allocator:allocator-api2", ], }), ) ``` The correct dependency would be something like `deps = ... + select({"//constraints:library": [], "//constraints:compiler": ["//:allocator-api2"]})` but this is not something that Reindeer's `extra_deps` can express. Maybe when we drop TOML and use Starlark fixups. Note that this diff does not use `attrs.option(..., default = None)` so the new meaning of `None` only kicks in if `actual = None` is written explicitly, as opposed to by default when `actual` is omitted from the `alias` call. Reviewed By: JakobDegen Differential Revision: D73325154 fbshipit-source-id: 9618446cc1762933fbb476ad20e3c9f5d1877d4c
1 parent cb8f9b9 commit 6b145c4

File tree

3 files changed

+10
-8
lines changed

3 files changed

+10
-8
lines changed

prelude/alias.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
# Implementation of aliases build rules.
99

1010
def alias_impl(ctx: AnalysisContext) -> list[Provider]:
11-
return ctx.attrs.actual.providers
11+
if ctx.attrs.actual:
12+
return ctx.attrs.actual.providers
13+
else:
14+
return [DefaultInfo()]
1215

1316
def configured_alias_impl(ctx: AnalysisContext) -> list[Provider]:
1417
if ctx.attrs.configured_actual != None and ctx.attrs.fallback_actual != None:
@@ -19,9 +22,6 @@ def configured_alias_impl(ctx: AnalysisContext) -> list[Provider]:
1922
return ctx.attrs.fallback_actual.providers
2023
fail("must set one of `configured_actual` or `fallback_actual`")
2124

22-
def toolchain_alias_impl(ctx: AnalysisContext) -> list[Provider]:
23-
return ctx.attrs.actual.providers
24-
2525
def versioned_alias_impl(_ctx: AnalysisContext) -> list[Provider]:
2626
# Should be intercepted in macro stub and converted to `alias`.
2727
fail("unsupported")

prelude/decls/core_rules.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ alias = prelude_rule(
3030
attrs = (
3131
# @unsorted-dict-items
3232
{
33-
"actual": attrs.dep(pulls_and_pushes_plugins = plugins.All),
33+
"actual": attrs.option(attrs.dep(pulls_and_pushes_plugins = plugins.All)),
3434
"contacts": attrs.list(attrs.string(), default = []),
3535
"default_host_platform": attrs.option(attrs.configuration_label(), default = None),
3636
"labels": attrs.list(attrs.string(), default = []),
@@ -1092,7 +1092,9 @@ expected to be a toolchain_rule as well.
10921092
""",
10931093
examples = None,
10941094
further = None,
1095-
attrs = {"actual": attrs.toolchain_dep(doc = "The actual toolchain that is being aliased. This should be a toolchain rule.")},
1095+
attrs = {
1096+
"actual": attrs.option(attrs.toolchain_dep(doc = "The actual toolchain that is being aliased. This should be a toolchain rule.")),
1097+
},
10961098
)
10971099

10981100
versioned_alias = prelude_rule(

prelude/rules_impl.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# License, Version 2.0 found in the LICENSE-APACHE file in the root directory
66
# of this source tree.
77

8-
load("@prelude//:alias.bzl", "alias_impl", "configured_alias_impl", "toolchain_alias_impl", "versioned_alias_impl")
8+
load("@prelude//:alias.bzl", "alias_impl", "configured_alias_impl", "versioned_alias_impl")
99
load("@prelude//:command_alias.bzl", "command_alias_impl")
1010
load("@prelude//:export_file.bzl", "export_file_impl")
1111
load("@prelude//:filegroup.bzl", "filegroup_impl")
@@ -155,7 +155,7 @@ extra_implemented_rules = struct(
155155
sh_binary = sh_binary_impl,
156156
sh_test = sh_test_impl,
157157
test_suite = test_suite_impl,
158-
toolchain_alias = toolchain_alias_impl,
158+
toolchain_alias = alias_impl,
159159
versioned_alias = versioned_alias_impl,
160160
worker_tool = worker_tool,
161161

0 commit comments

Comments
 (0)