Remove DEFAULT_RUST_EDITION (#1256)
* Recognize `edition = "required"` idiom
* Remove DEFAULT_RUST_EDITION
diff --git a/docs/flatten.md b/docs/flatten.md
index e92d250..33cbbc9 100644
--- a/docs/flatten.md
+++ b/docs/flatten.md
@@ -787,7 +787,7 @@
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <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 | |
-| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "2018" |
+| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "" |
| <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")] |
| <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 |
| <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 @@
| <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 |
| <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 |
| <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"} |
-| <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" |
+| <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 | "" |
| <a id="rust_toolchain-dylib_ext"></a>dylib_ext | The extension for dynamic libraries created from rustc. | String | required | |
| <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 | |
| <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 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 | |
| <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 | {} |
| <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 |
-| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2018" |
+| <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 | "" |
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
| <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 | [] |
| <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 @@
| <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> |
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. Defaults to None. | <code>None</code> |
| <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> |
-| <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> |
+| <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> |
| <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> |
| <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> |
| <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> |
diff --git a/docs/rust_proto.md b/docs/rust_proto.md
index dfba0b2..f37657f 100644
--- a/docs/rust_proto.md
+++ b/docs/rust_proto.md
@@ -252,7 +252,7 @@
| Name | Description | Type | Mandatory | Default |
| :------------- | :------------- | :------------- | :------------- | :------------- |
| <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 | |
-| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "2018" |
+| <a id="rust_proto_toolchain-edition"></a>edition | The edition used by the generated rust source. | String | optional | "" |
| <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")] |
| <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 |
| <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")] |
diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md
index 8f68ff7..04f052c 100644
--- a/docs/rust_repositories.md
+++ b/docs/rust_repositories.md
@@ -92,7 +92,7 @@
| <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 |
| <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 |
| <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"} |
-| <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" |
+| <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 | "" |
| <a id="rust_toolchain-dylib_ext"></a>dylib_ext | The extension for dynamic libraries created from rustc. | String | required | |
| <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 | |
| <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 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 | |
| <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 | {} |
| <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 |
-| <a id="rust_toolchain_repository-edition"></a>edition | The rust edition to be used by default. | String | optional | "2018" |
+| <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 | "" |
| <a id="rust_toolchain_repository-exec_triple"></a>exec_triple | The Rust-style target that this compiler runs on | String | required | |
| <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 | [] |
| <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 @@
| <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> |
| <a id="rust_repository_set-iso_date"></a>iso_date | The date of the tool. Defaults to None. | <code>None</code> |
| <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> |
-| <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> |
+| <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> |
| <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> |
| <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> |
| <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> |
diff --git a/proto/toolchain.bzl b/proto/toolchain.bzl
index 5192436..80c5ccc 100644
--- a/proto/toolchain.bzl
+++ b/proto/toolchain.bzl
@@ -14,8 +14,6 @@
"""Toolchain for compiling rust stubs from protobuf and gRPC."""
-load("//rust:defs.bzl", "rust_common")
-
# buildifier: disable=bzl-visibility
load("//rust/private:utils.bzl", "name_to_crate_name")
@@ -146,7 +144,6 @@
attrs = {
"edition": attr.string(
doc = "The edition used by the generated rust source.",
- default = rust_common.default_edition,
),
"grpc_compile_deps": attr.label_list(
doc = "The crates the generated grpc libraries depends on.",
diff --git a/rust/private/common.bzl b/rust/private/common.bzl
index 2171b3c..021a197 100644
--- a/rust/private/common.bzl
+++ b/rust/private/common.bzl
@@ -25,14 +25,13 @@
load(":providers.bzl", "CrateInfo", "DepInfo", "StdLibInfo")
-# These constants only represent the default value for attributes and macros
-# defiend in `rules_rust`. Like any attribute public attribute, they can be
-# overwritten by the user on the rules they're defiend on.
+# This constant only represents the default value for attributes and macros
+# defined in `rules_rust`. Like any attribute public attribute, it can be
+# overwritten by the user on the rules they're defined on.
#
# Note: Code in `.github/workflows/crate_universe.yaml` looks for this line, if
# you remove it or change its format, you will also need to update that code.
DEFAULT_RUST_VERSION = "1.60.0"
-DEFAULT_RUST_EDITION = "2018"
def _create_crate_info(**kwargs):
"""A constructor for a `CrateInfo` provider
@@ -55,6 +54,5 @@
crate_info = CrateInfo,
dep_info = DepInfo,
stdlib_info = StdLibInfo,
- default_edition = DEFAULT_RUST_EDITION,
default_version = DEFAULT_RUST_VERSION,
)
diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl
index 13d18df..6a7f230 100644
--- a/rust/private/rust.bzl
+++ b/rust/private/rust.bzl
@@ -106,18 +106,21 @@
extension = extension,
)
-def get_edition(attr, toolchain):
+def get_edition(attr, toolchain, label):
"""Returns the Rust edition from either the current rule's attirbutes or the current `rust_toolchain`
Args:
attr (struct): The current rule's attributes
toolchain (rust_toolchain): The `rust_toolchain` for the current target
+ label (Label): The label of the target being built
Returns:
str: The target Rust edition
"""
if getattr(attr, "edition"):
return attr.edition
+ elif not toolchain.default_edition:
+ fail("Attribute `edition` is required for {}.".format(label))
else:
return toolchain.default_edition
@@ -280,7 +283,7 @@
proc_macro_deps = depset(proc_macro_deps),
aliases = ctx.attr.aliases,
output = rust_lib,
- edition = get_edition(ctx.attr, toolchain),
+ edition = get_edition(ctx.attr, toolchain, ctx.label),
rustc_env = ctx.attr.rustc_env,
is_test = False,
compile_data = depset(ctx.files.compile_data),
@@ -320,7 +323,7 @@
proc_macro_deps = depset(proc_macro_deps),
aliases = ctx.attr.aliases,
output = output,
- edition = get_edition(ctx.attr, toolchain),
+ edition = get_edition(ctx.attr, toolchain, ctx.label),
rustc_env = ctx.attr.rustc_env,
is_test = False,
compile_data = depset(ctx.files.compile_data),
@@ -386,7 +389,7 @@
proc_macro_deps = depset(proc_macro_deps),
aliases = ctx.attr.aliases,
output = output,
- edition = get_edition(ctx.attr, toolchain),
+ edition = get_edition(ctx.attr, toolchain, ctx.label),
rustc_env = ctx.attr.rustc_env,
is_test = True,
compile_data = depset(ctx.files.compile_data),
diff --git a/rust/repositories.bzl b/rust/repositories.bzl
index bff0860..69ab452 100644
--- a/rust/repositories.bzl
+++ b/rust/repositories.bzl
@@ -101,7 +101,7 @@
Args:
dev_components (bool, optional): Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly".
- edition (str, optional): The rust edition to be used by default (2015, 2018 (default), or 2021)
+ 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.
include_rustc_srcs (bool, optional): Whether to download rustc's src code. This is required in order to use rust-analyzer support.
See [rust_toolchain_repository.include_rustc_srcs](#rust_toolchain_repository-include_rustc_srcs). for more details
iso_date (str, optional): The date of the nightly or beta release (ignored if the version is a specific version).
@@ -212,8 +212,10 @@
default = False,
),
"edition": attr.string(
- doc = "The rust edition to be used by default.",
- default = rust_common.default_edition,
+ doc = (
+ "The rust edition to be used by default (2015, 2018, or 2021). " +
+ "If absent, every rule is required to specify its `edition` attribute."
+ ),
),
"exec_triple": attr.string(
doc = "The Rust-style target that this compiler runs on",
@@ -311,7 +313,7 @@
iso_date (str, optional): The date of the tool. Defaults to None.
rustfmt_version (str, optional): The version of rustfmt to be associated with the
toolchain. Defaults to None.
- edition (str, optional): The rust edition to be used by default (2015, 2018 (if None), or 2021).
+ 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.
dev_components (bool, optional): Whether to download the rustc-dev components.
Requires version to be "nightly". Defaults to False.
sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See
diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl
index 687e0df..85dc424 100644
--- a/rust/toolchain.bzl
+++ b/rust/toolchain.bzl
@@ -519,8 +519,10 @@
},
),
"default_edition": attr.string(
- doc = "The edition to use for rust_* rules that don't specify an edition.",
- default = rust_common.default_edition,
+ doc = (
+ "The edition to use for rust_* rules that don't specify an edition. " +
+ "If absent, every rule is required to specify its `edition` attribute."
+ ),
),
"dylib_ext": attr.string(
doc = "The extension for dynamic libraries created from rustc.",