Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,16 @@ _common_attrs = {
cfg = "exec",
providers = [[CrateInfo], [CrateGroupInfo]],
),
"require_explicit_unstable_features": attr.int(
doc = (
"Whether to require all unstable features to be explicitly opted in to using " +
"`-Zallow-features=...`. Possible values: [-1, 0, 1]. -1 means delegate to the " +
"toolchain.require_explicit_unstable_features boolean build setting; 0 means False; " +
"1 means True."
),
values = [-1, 0, 1],
default = -1,
),
"rustc_env": attr.string_dict(
doc = dedent("""\
Dictionary of additional `"key": "value"` environment variables to set for rustc.
Expand Down
17 changes: 17 additions & 0 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,7 @@ def construct_arguments(
build_metadata = False,
force_depend_on_objects = False,
skip_expanding_rustc_env = False,
require_explicit_unstable_features = False,
error_format = None):
"""Builds an Args object containing common rustc flags

Expand Down Expand Up @@ -867,6 +868,7 @@ def construct_arguments(
build_metadata (bool): Generate CLI arguments for building *only* .rmeta files. This requires use_json_output.
force_depend_on_objects (bool): Force using `.rlib` object files instead of metadata (`.rmeta`) files even if they are available.
skip_expanding_rustc_env (bool): Whether to skip expanding CrateInfo.rustc_env_attr
require_explicit_unstable_features (bool): Whether to require all unstable features to be explicitly opted in to using `-Zallow-features=...`.
error_format (str, optional): Error format to pass to the `--error-format` command line argument. If set to None, uses the "_error_format" entry in `attr`.

Returns:
Expand Down Expand Up @@ -895,6 +897,9 @@ def construct_arguments(

process_wrapper_flags.add_all(build_flags_files, before_each = "--arg-file")

if require_explicit_unstable_features:
process_wrapper_flags.add("--require-explicit-unstable-features", "true")

# Certain rust build processes expect to find files from the environment
# variable `$CARGO_MANIFEST_DIR`. Examples of this include pest, tera,
# asakuma.
Expand Down Expand Up @@ -1289,6 +1294,16 @@ def rustc_compile_action(
if experimental_use_cc_common_link:
emit = ["obj"]

# Determine whether to pass `--require-explicit-unstable-features true` to the process wrapper:
require_explicit_unstable_features = False
if hasattr(ctx.attr, "require_explicit_unstable_features"):
if ctx.attr.require_explicit_unstable_features == 0:
require_explicit_unstable_features = False
elif ctx.attr.require_explicit_unstable_features == 1:
require_explicit_unstable_features = True
elif ctx.attr.require_explicit_unstable_features == -1:
require_explicit_unstable_features = toolchain.require_explicit_unstable_features

args, env_from_args = construct_arguments(
ctx = ctx,
attr = attr,
Expand All @@ -1311,6 +1326,7 @@ def rustc_compile_action(
stamp = stamp,
use_json_output = bool(build_metadata) or bool(rustc_output) or bool(rustc_rmeta_output),
skip_expanding_rustc_env = skip_expanding_rustc_env,
require_explicit_unstable_features = require_explicit_unstable_features,
)

args_metadata = None
Expand All @@ -1337,6 +1353,7 @@ def rustc_compile_action(
stamp = stamp,
use_json_output = True,
build_metadata = True,
require_explicit_unstable_features = require_explicit_unstable_features,
)

env = dict(ctx.configuration.default_shell_env)
Expand Down
3 changes: 3 additions & 0 deletions rust/settings/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ load(
"no_std",
"pipelined_compilation",
"rename_first_party_crates",
"require_explicit_unstable_features",
"rustc_output_diagnostics",
"rustfmt_toml",
"third_party_dir",
Expand Down Expand Up @@ -119,6 +120,8 @@ pipelined_compilation()

rename_first_party_crates()

require_explicit_unstable_features()

rustc_output_diagnostics()

rustfmt_toml()
Expand Down
13 changes: 13 additions & 0 deletions rust/settings/settings.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ def rename_first_party_crates():
build_setting_default = False,
)

def require_explicit_unstable_features():
"""A flag controlling whether unstable features should be disallowed by default

If true, an empty `-Zallow-features=` will be added to the rustc command line whenever no other
`-Zallow-features=` is present in the rustc flags. The effect is to disallow all unstable
features by default, with the possibility to explicitly re-enable them selectively using
`-Zallow-features=...`.
"""
bool_flag(
name = "require_explicit_unstable_features",
build_setting_default = False,
)

def third_party_dir():
"""A flag specifying the location of vendored third-party rust crates within this \
repository that must not be renamed when `rename_first_party_crates` is enabled.
Expand Down
11 changes: 11 additions & 0 deletions rust/toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,9 @@ def _generate_sysroot(
def _experimental_use_cc_common_link(ctx):
return ctx.attr.experimental_use_cc_common_link[BuildSettingInfo].value

def _require_explicit_unstable_features(ctx):
return ctx.attr.require_explicit_unstable_features[BuildSettingInfo].value

def _expand_flags(ctx, attr_name, targets, make_variables):
targets = deduplicate(targets)
expanded_flags = []
Expand Down Expand Up @@ -753,6 +756,7 @@ def _rust_toolchain_impl(ctx):
target_json = target_json,
target_os = target_os,
target_triple = target_triple,
require_explicit_unstable_features = _require_explicit_unstable_features(ctx),

# Experimental and incompatible flags
_rename_first_party_crates = rename_first_party_crates,
Expand Down Expand Up @@ -904,6 +908,13 @@ rust_toolchain = rule(
"per_crate_rustc_flags": attr.string_list(
doc = "Extra flags to pass to rustc in non-exec configuration",
),
"require_explicit_unstable_features": attr.label(
default = Label(
"//rust/settings:require_explicit_unstable_features",
),
doc = ("Label to a boolean build setting that controls whether all uses of unstable " +
"Rust features must be explicitly opted in to using `-Zallow-features=...`."),
),
"rust_doc": attr.label(
doc = "The location of the `rustdoc` binary. Can be a direct source or a filegroup containing one item.",
allow_single_file = True,
Expand Down
Loading