Skip to content

Commit 01cc76b

Browse files
authored
Fixed inability to use crate attribute in wrapper rules with proc-macro targets. (#807)
* Fixed inability to use `rust_test::crate` with proc-macro targets. * Updated example * Renamed to `transitive_type` to `wrapped_crate_type` * Buildifier fixes * Added unittests * Updated examples and addressed feedback * Updated examples again * Addressed feedback * Fixed optional argument not being optional * Addressed feedback * Moved integration tests * Addressed nit
1 parent 4bf51b3 commit 01cc76b

File tree

15 files changed

+180
-3
lines changed

15 files changed

+180
-3
lines changed

examples/proc_macro/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ load(
99
package(default_visibility = ["//visibility:public"])
1010

1111
rust_proc_macro(
12-
name = "proc_macro_lib",
12+
name = "proc_macro_lib_2015",
1313
srcs = [
1414
"src/lib_2015.rs",
1515
],
1616
)
1717

1818
rust_proc_macro(
19-
name = "proc_macro_lib_2018",
19+
name = "proc_macro_lib",
2020
srcs = [
2121
"src/lib.rs",
2222
],

proto/proto.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr
234234
edition = proto_toolchain.edition,
235235
rustc_env = {},
236236
is_test = False,
237+
wrapped_crate_type = None,
237238
),
238239
output_hash = output_hash,
239240
)

rust/private/common.bzl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def _create_crate_info(**kwargs):
3737
Returns:
3838
CrateInfo: A provider
3939
"""
40+
if not "wrapped_crate_type" in kwargs:
41+
kwargs.update({"wrapped_crate_type": None})
4042
return CrateInfo(**kwargs)
4143

4244
rust_common = struct(

rust/private/providers.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ CrateInfo = provider(
2828
"rustc_env": "Dict[String, String]: Additional `\"key\": \"value\"` environment variables to set for rustc.",
2929
"srcs": "depset[File]: All source Files that are part of the crate.",
3030
"type": "str: The type of this crate. eg. lib or bin",
31+
"wrapped_crate_type": (
32+
"str, optional: The original crate type for targets generated using a previously defined " +
33+
"crate (typically tests using the `rust_test::crate` attribute)"
34+
),
3135
},
3236
)
3337

rust/private/rust.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def _rust_test_common(ctx, toolchain, output):
418418
edition = crate.edition,
419419
rustc_env = ctx.attr.rustc_env,
420420
is_test = True,
421+
wrapped_crate_type = crate.type,
421422
)
422423
else:
423424
# Target is a standalone crate. Build the test binary as its own crate.

rust/private/rustc.bzl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,9 @@ def construct_arguments(
467467
# These always need to be added, even if not linking this crate.
468468
add_crate_link_flags(args, dep_info)
469469

470-
if crate_info.type == "proc-macro" and crate_info.edition != "2015":
470+
needs_extern_proc_macro_flag = "proc-macro" in [crate_info.type, crate_info.wrapped_crate_type] and \
471+
crate_info.edition != "2015"
472+
if needs_extern_proc_macro_flag:
471473
args.add("--extern")
472474
args.add("proc_macro")
473475

rust/private/rustdoc.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ def _rust_doc_impl(ctx):
8585
args.add(crate.root.path)
8686
args.add("--crate-name", crate.name)
8787
args.add("--crate-type", crate.type)
88+
if crate.type == "proc-macro":
89+
args.add("--extern")
90+
args.add("proc_macro")
8891
args.add("--output", output_dir.path)
8992
add_edition_flags(args, crate)
9093

rust/private/rustdoc_test.bzl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ def _build_rustdoc_flags(dep_info, crate):
109109
link_flags.append("-Lnative={}".format(_dirname(f.short_path)))
110110
link_search_flags.append("-Lnative={}".format(_dirname(f.short_path)))
111111

112+
if crate.type == "proc-macro":
113+
link_flags.extend(["--extern", "proc_macro"])
114+
112115
edition_flags = ["--edition={}".format(crate.edition)] if crate.edition != "2015" else []
113116

114117
return link_search_flags + link_flags + edition_flags

test/proc_macro/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
load("//rust:defs.bzl", "rust_test")
2+
3+
[
4+
rust_test(
5+
name = "proc_macro_{}_integration_test".format(edition),
6+
srcs = ["proc_macro_{}_test.rs".format(edition)],
7+
edition = edition,
8+
proc_macro_deps = ["//test/unit/proc_macro:proc_macro_{}".format(edition)],
9+
)
10+
for edition in [
11+
"2015",
12+
"2018",
13+
]
14+
]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extern crate proc_macro_2015;
2+
use proc_macro_2015::make_answer;
3+
4+
make_answer!();
5+
6+
fn main() {
7+
println!("{}", answer());
8+
}

0 commit comments

Comments
 (0)