Skip to content

Commit 0369b8e

Browse files
author
Sean Klein
authored
Add proc macro example, propagate crate-type through rustdoc (#303)
- Added a no-op proc macro to "examples". - While adding this macro, the following error message came from "rust_doc": warning: Trying to document proc macro crate without passing '--crate-type proc-macro to rustdoc To resolve this issue, the rust_doc rule was modified to propagate the underlying crate type.
1 parent a1d8936 commit 0369b8e

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

examples/hello_macro/BUILD

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package(default_visibility = ["//visibility:public"])
2+
3+
load(
4+
"@io_bazel_rules_rust//rust:rust.bzl",
5+
"rust_doc",
6+
"rust_doc_test",
7+
"rust_library",
8+
"rust_test",
9+
)
10+
11+
rust_library(
12+
name = "hello_macro",
13+
srcs = [
14+
"src/lib.rs",
15+
],
16+
crate_type = "proc-macro",
17+
)
18+
19+
rust_test(
20+
name = "hello_macro_test",
21+
crate = ":hello_macro",
22+
)
23+
24+
rust_test(
25+
name = "greeting_test",
26+
srcs = ["tests/greeting.rs"],
27+
deps = [":hello_macro"],
28+
)
29+
30+
rust_doc(
31+
name = "hello_macro_doc",
32+
dep = ":hello_macro",
33+
)
34+
35+
rust_doc_test(
36+
name = "hello_macro_doc_test",
37+
dep = ":hello_macro",
38+
)

examples/hello_macro/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2020 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
extern crate proc_macro;
16+
17+
use proc_macro::TokenStream;
18+
19+
/// This macro is a no-op; it is exceedingly simple as a result
20+
/// of avoiding dependencies on both the syn and quote crates.
21+
#[proc_macro_derive(HelloWorld)]
22+
pub fn hello_world(_input: TokenStream) -> TokenStream {
23+
TokenStream::new()
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2020 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
extern crate hello_macro;
16+
17+
use hello_macro::HelloWorld;
18+
19+
#[derive(HelloWorld)]
20+
struct TestStruct {
21+
}
22+
23+
#[test]
24+
fn test_hello_world_macro() {
25+
let _ = TestStruct {};
26+
}

rust/private/rustdoc.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def _rust_doc_impl(ctx):
3838
args = ctx.actions.args()
3939
args.add(crate.root.path)
4040
args.add("--crate-name", crate.name)
41+
args.add("--crate-type", crate.type)
4142
args.add("--output", output_dir.path)
4243
add_edition_flags(args, crate)
4344

0 commit comments

Comments
 (0)