Skip to content

Commit

Permalink
Recognize edition = "required" idiom
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Apr 20, 2022
1 parent 965044a commit c2e510b
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rust/private/rust.bzl
Expand Up @@ -106,18 +106,21 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = None):
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 toolchain.default_edition == "required":
fail("Attribute `edition` is required for {}.".format(label))
else:
return toolchain.default_edition

Expand Down Expand Up @@ -280,7 +283,7 @@ def _rust_library_common(ctx, crate_type):
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),
Expand Down Expand Up @@ -320,7 +323,7 @@ def _rust_binary_impl(ctx):
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),
Expand Down Expand Up @@ -386,7 +389,7 @@ def _rust_test_common(ctx, toolchain, output):
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),
Expand Down

0 comments on commit c2e510b

Please sign in to comment.