diff --git a/examples/hello_runfiles/BUILD b/examples/hello_runfiles/BUILD index 55032515c5..58f1b3221b 100644 --- a/examples/hello_runfiles/BUILD +++ b/examples/hello_runfiles/BUILD @@ -9,6 +9,7 @@ load( rust_binary( name = "hello_runfiles", + edition = "2018", srcs = ["hello_runfiles.rs"], data = ["hello_runfiles.rs"], # Yes, we're being cute. deps = ["@io_bazel_rules_rust//tools/runfiles"], diff --git a/examples/hello_runfiles/hello_runfiles.rs b/examples/hello_runfiles/hello_runfiles.rs index caeb81a1ba..6487f9cb12 100644 --- a/examples/hello_runfiles/hello_runfiles.rs +++ b/examples/hello_runfiles/hello_runfiles.rs @@ -1,5 +1,3 @@ -extern crate runfiles; - use std::io::prelude::*; use std::fs::File; @@ -13,6 +11,5 @@ fn main() { let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); - assert_eq!(buffer.len(), 427); - println!("This program's source is:\n```\n{}\n```", buffer); + println!("This program's source is {} characters long.", buffer.len()); } diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 29f909825c..7f9dda7de9 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -183,6 +183,9 @@ _rust_common_attrs = { ), "deps": attr.label_list(), "crate_features": attr.string_list(), + "edition": attr.string( + doc="The rust edition to use for this crate.", + ), "rustc_flags": attr.string_list(), "version": attr.string(default = "0.0.0"), "out_dir_tar": attr.label( diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 994ceaa2b9..5002179cbf 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -217,6 +217,7 @@ def rustc_compile_action( if hasattr(ctx.attr, "crate_features"): args.add_all(getattr(ctx.attr, "crate_features"), before_each = "--cfg", format_each = 'feature="%s"') args.add_all(rust_flags) + args.add("--edition", _get_edition(ctx, toolchain)) args.add_all(getattr(ctx.attr, "rustc_flags", [])) # Link! @@ -263,6 +264,12 @@ def rustc_compile_action( ), ] +def _get_edition(ctx, toolchain): + if getattr(ctx.attr, "edition"): + return ctx.attr.edition + else: + return toolchain.default_edition + def _create_out_dir_action(ctx): tar_file = getattr(ctx.file, "out_dir_tar", None) if not tar_file: diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index a8b902f60e..970e343241 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -25,6 +25,7 @@ def _rust_toolchain_impl(ctx): target_triple = ctx.attr.target_triple, exec_triple = ctx.attr.exec_triple, os = ctx.attr.os, + default_edition = ctx.attr.default_edition, compilation_mode_opts = compilation_mode_opts, crosstool_files = ctx.files._crosstool, ) @@ -40,6 +41,10 @@ rust_toolchain = rule( "staticlib_ext": attr.string(mandatory = True), "dylib_ext": attr.string(mandatory = True), "os": attr.string(mandatory = True), + "default_edition": attr.string( + doc = "The edition to use for rust_* rules that don't specify an edition.", + default = "2015", + ), "exec_triple": attr.string(), "target_triple": attr.string(), "_crosstool": attr.label(