Skip to content

Commit 01ebef2

Browse files
authored
Remove DEFAULT_RUST_EDITION (#1256)
* Recognize `edition = "required"` idiom * Remove DEFAULT_RUST_EDITION
1 parent 03a7079 commit 01ebef2

File tree

8 files changed

+28
-26
lines changed

8 files changed

+28
-26
lines changed

docs/flatten.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ See @rules_rust//proto:BUILD for examples of defining the toolchain.
787787
| Name | Description | Type | Mandatory | Default |
788788
| :------------- | :------------- | :------------- | :------------- | :------------- |
789789
| <a id="rust_proto_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
790-
| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "2018" |
790+
| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "" |
791791
| <a id="rust_proto_toolchain-grpc_compile_deps"></a>grpc_compile_deps | The crates the generated grpc libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf"), Label("//proto/raze:grpc"), Label("//proto/raze:tls_api"), Label("//proto/raze:tls_api_stub")] |
792792
| <a id="rust_proto_toolchain-grpc_plugin"></a>grpc_plugin | The location of the Rust protobuf compiler plugin to generate rust gRPC stubs. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //proto:protoc_gen_rust_grpc |
793793
| <a id="rust_proto_toolchain-proto_compile_deps"></a>proto_compile_deps | The crates the generated protobuf libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf")] |
@@ -1120,7 +1120,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
11201120
| <a id="rust_toolchain-cargo"></a>cargo | The location of the <code>cargo</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
11211121
| <a id="rust_toolchain-clippy_driver"></a>clippy_driver | The location of the <code>clippy-driver</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
11221122
| <a id="rust_toolchain-debug_info"></a>debug_info | Rustc debug info levels per opt level | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {"dbg": "2", "fastbuild": "0", "opt": "0"} |
1123-
| <a id="rust_toolchain-default_edition"></a>default_edition | The edition to use for rust_* rules that don't specify an edition. | String | optional | "2018" |
1123+
| <a id="rust_toolchain-default_edition"></a>default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its <code>edition</code> attribute. | String | optional | "" |
11241124
| <a id="rust_toolchain-dylib_ext"></a>dylib_ext | The extension for dynamic libraries created from rustc. | String | required | |
11251125
| <a id="rust_toolchain-exec_triple"></a>exec_triple | The platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | required | |
11261126
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
@@ -1161,7 +1161,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito
11611161
| <a id="rust_toolchain_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
11621162
| <a id="rust_toolchain_repository-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
11631163
| <a id="rust_toolchain_repository-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False |
1164-
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2018" |
1164+
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | String | optional | "" |
11651165
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
11661166
| <a id="rust_toolchain_repository-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] |
11671167
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. An environment variable <code>RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS</code> can also be used to control this attribute. This variable will take precedence over the hard coded attribute. Setting it to <code>true</code> to activates this attribute where all other values deactivate it. | Boolean | optional | False |
@@ -1635,7 +1635,7 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with
16351635
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. Defaults to []. | <code>[]</code> |
16361636
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. Defaults to None. | <code>None</code> |
16371637
| <a id="rust_repository_set-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. Defaults to None. | <code>None</code> |
1638-
| <a id="rust_repository_set-edition"></a>edition | The rust edition to be used by default (2015, 2018 (if None), or 2021). | <code>None</code> |
1638+
| <a id="rust_repository_set-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | <code>None</code> |
16391639
| <a id="rust_repository_set-dev_components"></a>dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | <code>False</code> |
16401640
| <a id="rust_repository_set-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | <code>None</code> |
16411641
| <a id="rust_repository_set-urls"></a>urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | <code>["https://static.rust-lang.org/dist/{}.tar.gz"]</code> |

docs/rust_proto.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ See @rules_rust//proto:BUILD for examples of defining the toolchain.
252252
| Name | Description | Type | Mandatory | Default |
253253
| :------------- | :------------- | :------------- | :------------- | :------------- |
254254
| <a id="rust_proto_toolchain-name"></a>name | A unique name for this target. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
255-
| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "2018" |
255+
| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "" |
256256
| <a id="rust_proto_toolchain-grpc_compile_deps"></a>grpc_compile_deps | The crates the generated grpc libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf"), Label("//proto/raze:grpc"), Label("//proto/raze:tls_api"), Label("//proto/raze:tls_api_stub")] |
257257
| <a id="rust_proto_toolchain-grpc_plugin"></a>grpc_plugin | The location of the Rust protobuf compiler plugin to generate rust gRPC stubs. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | //proto:protoc_gen_rust_grpc |
258258
| <a id="rust_proto_toolchain-proto_compile_deps"></a>proto_compile_deps | The crates the generated protobuf libraries depends on. | <a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a> | optional | [Label("//proto/raze:protobuf")] |

docs/rust_repositories.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
9292
| <a id="rust_toolchain-cargo"></a>cargo | The location of the <code>cargo</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
9393
| <a id="rust_toolchain-clippy_driver"></a>clippy_driver | The location of the <code>clippy-driver</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
9494
| <a id="rust_toolchain-debug_info"></a>debug_info | Rustc debug info levels per opt level | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {"dbg": "2", "fastbuild": "0", "opt": "0"} |
95-
| <a id="rust_toolchain-default_edition"></a>default_edition | The edition to use for rust_* rules that don't specify an edition. | String | optional | "2018" |
95+
| <a id="rust_toolchain-default_edition"></a>default_edition | The edition to use for rust_* rules that don't specify an edition. If absent, every rule is required to specify its <code>edition</code> attribute. | String | optional | "" |
9696
| <a id="rust_toolchain-dylib_ext"></a>dylib_ext | The extension for dynamic libraries created from rustc. | String | required | |
9797
| <a id="rust_toolchain-exec_triple"></a>exec_triple | The platform triple for the toolchains execution environment. For more details see: https://docs.bazel.build/versions/master/skylark/rules.html#configurations | String | required | |
9898
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/docs/build-ref.html#labels">Label</a> | optional | None |
@@ -133,7 +133,7 @@ A given instance of this rule should be accompanied by a rust_toolchain_reposito
133133
| <a id="rust_toolchain_repository-name"></a>name | A unique name for this repository. | <a href="https://bazel.build/docs/build-ref.html#name">Name</a> | required | |
134134
| <a id="rust_toolchain_repository-auth"></a>auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | <a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a> | optional | {} |
135135
| <a id="rust_toolchain_repository-dev_components"></a>dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False |
136-
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2018" |
136+
| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | String | optional | "" |
137137
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
138138
| <a id="rust_toolchain_repository-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. | List of strings | optional | [] |
139139
| <a id="rust_toolchain_repository-include_rustc_srcs"></a>include_rustc_srcs | Whether to download and unpack the rustc source files. These are very large, and slow to unpack, but are required to support rust analyzer. An environment variable <code>RULES_RUST_TOOLCHAIN_INCLUDE_RUSTC_SRCS</code> can also be used to control this attribute. This variable will take precedence over the hard coded attribute. Setting it to <code>true</code> to activates this attribute where all other values deactivate it. | Boolean | optional | False |
@@ -214,7 +214,7 @@ N.B. A "proxy repository" is needed to allow for registering the toolchain (with
214214
| <a id="rust_repository_set-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that this set of toolchains should support. Defaults to []. | <code>[]</code> |
215215
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. Defaults to None. | <code>None</code> |
216216
| <a id="rust_repository_set-rustfmt_version"></a>rustfmt_version | The version of rustfmt to be associated with the toolchain. Defaults to None. | <code>None</code> |
217-
| <a id="rust_repository_set-edition"></a>edition | The rust edition to be used by default (2015, 2018 (if None), or 2021). | <code>None</code> |
217+
| <a id="rust_repository_set-edition"></a>edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its <code>edition</code> attribute. | <code>None</code> |
218218
| <a id="rust_repository_set-dev_components"></a>dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | <code>False</code> |
219219
| <a id="rust_repository_set-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | <code>None</code> |
220220
| <a id="rust_repository_set-urls"></a>urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | <code>["https://static.rust-lang.org/dist/{}.tar.gz"]</code> |

proto/toolchain.bzl

-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
"""Toolchain for compiling rust stubs from protobuf and gRPC."""
1616

17-
load("//rust:defs.bzl", "rust_common")
18-
1917
# buildifier: disable=bzl-visibility
2018
load("//rust/private:utils.bzl", "name_to_crate_name")
2119

@@ -146,7 +144,6 @@ rust_proto_toolchain = rule(
146144
attrs = {
147145
"edition": attr.string(
148146
doc = "The edition used by the generated rust source.",
149-
default = rust_common.default_edition,
150147
),
151148
"grpc_compile_deps": attr.label_list(
152149
doc = "The crates the generated grpc libraries depends on.",

rust/private/common.bzl

+3-5
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@ In the Bazel lingo, `rust_common` gives the access to the Rust Sandwich API.
2525

2626
load(":providers.bzl", "CrateInfo", "DepInfo", "StdLibInfo")
2727

28-
# These constants only represent the default value for attributes and macros
29-
# defiend in `rules_rust`. Like any attribute public attribute, they can be
30-
# overwritten by the user on the rules they're defiend on.
28+
# This constant only represents the default value for attributes and macros
29+
# defined in `rules_rust`. Like any attribute public attribute, it can be
30+
# overwritten by the user on the rules they're defined on.
3131
#
3232
# Note: Code in `.github/workflows/crate_universe.yaml` looks for this line, if
3333
# you remove it or change its format, you will also need to update that code.
3434
DEFAULT_RUST_VERSION = "1.60.0"
35-
DEFAULT_RUST_EDITION = "2018"
3635

3736
def _create_crate_info(**kwargs):
3837
"""A constructor for a `CrateInfo` provider
@@ -55,6 +54,5 @@ rust_common = struct(
5554
crate_info = CrateInfo,
5655
dep_info = DepInfo,
5756
stdlib_info = StdLibInfo,
58-
default_edition = DEFAULT_RUST_EDITION,
5957
default_version = DEFAULT_RUST_VERSION,
6058
)

rust/private/rust.bzl

+7-4
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,21 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = None):
106106
extension = extension,
107107
)
108108

109-
def get_edition(attr, toolchain):
109+
def get_edition(attr, toolchain, label):
110110
"""Returns the Rust edition from either the current rule's attirbutes or the current `rust_toolchain`
111111
112112
Args:
113113
attr (struct): The current rule's attributes
114114
toolchain (rust_toolchain): The `rust_toolchain` for the current target
115+
label (Label): The label of the target being built
115116
116117
Returns:
117118
str: The target Rust edition
118119
"""
119120
if getattr(attr, "edition"):
120121
return attr.edition
122+
elif not toolchain.default_edition:
123+
fail("Attribute `edition` is required for {}.".format(label))
121124
else:
122125
return toolchain.default_edition
123126

@@ -280,7 +283,7 @@ def _rust_library_common(ctx, crate_type):
280283
proc_macro_deps = depset(proc_macro_deps),
281284
aliases = ctx.attr.aliases,
282285
output = rust_lib,
283-
edition = get_edition(ctx.attr, toolchain),
286+
edition = get_edition(ctx.attr, toolchain, ctx.label),
284287
rustc_env = ctx.attr.rustc_env,
285288
is_test = False,
286289
compile_data = depset(ctx.files.compile_data),
@@ -320,7 +323,7 @@ def _rust_binary_impl(ctx):
320323
proc_macro_deps = depset(proc_macro_deps),
321324
aliases = ctx.attr.aliases,
322325
output = output,
323-
edition = get_edition(ctx.attr, toolchain),
326+
edition = get_edition(ctx.attr, toolchain, ctx.label),
324327
rustc_env = ctx.attr.rustc_env,
325328
is_test = False,
326329
compile_data = depset(ctx.files.compile_data),
@@ -386,7 +389,7 @@ def _rust_test_common(ctx, toolchain, output):
386389
proc_macro_deps = depset(proc_macro_deps),
387390
aliases = ctx.attr.aliases,
388391
output = output,
389-
edition = get_edition(ctx.attr, toolchain),
392+
edition = get_edition(ctx.attr, toolchain, ctx.label),
390393
rustc_env = ctx.attr.rustc_env,
391394
is_test = True,
392395
compile_data = depset(ctx.files.compile_data),

rust/repositories.bzl

+6-4
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def rust_register_toolchains(
101101
102102
Args:
103103
dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
104-
edition (str, optional): The rust edition to be used by default (2015, 2018 (default), or 2021)
104+
edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute.
105105
include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support.
106106
See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details
107107
iso_date (str, optional): The date of the nightly or beta release (ignored if the version is a specific version).
@@ -212,8 +212,10 @@ rust_toolchain_repository = repository_rule(
212212
default = False,
213213
),
214214
"edition": attr.string(
215-
doc = "The rust edition to be used by default.",
216-
default = rust_common.default_edition,
215+
doc = (
216+
"The rust edition to be used by default (2015, 2018, or 2021). " +
217+
"If absent, every rule is required to specify its `edition` attribute."
218+
),
217219
),
218220
"exec_triple": attr.string(
219221
doc = "The Rust-style target that this compiler runs on",
@@ -311,7 +313,7 @@ def rust_repository_set(
311313
iso_date (str, optional): The date of the tool. Defaults to None.
312314
rustfmt_version (str, optional): The version of rustfmt to be associated with the
313315
toolchain. Defaults to None.
314-
edition (str, optional): The rust edition to be used by default (2015, 2018 (if None), or 2021).
316+
edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute.
315317
dev_components (bool, optional): Whether to download the rustc-dev components.
316318
Requires version to be "nightly". Defaults to False.
317319
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See

0 commit comments

Comments
 (0)