Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit 3eb6678

Browse files
David Tolnayfacebook-github-bot
authored andcommitted
Pass --extern=proc_macro to Rust procedural macros
Summary: Rebase of D21840371 with `throws InterruptedException` added to unbreak RustBinaryIntegrationTest.java. --- This diff updates Buck's behavior to match the behavior of Cargo 1.42+ ([rust-lang/cargo#7700](rust-lang/cargo#7700)) to no longer require procedural macro crates to specify "extern crate proc_macro". See the release announcement of this change in https://blog.rust-lang.org/2020/03/12/Rust-1.42.html#use-proc_macrotokenstream-now-works. Buck technically has no lower compiler version bound on its rustc support, so if someone requires support for rustc <1.42 in Buck then we can consider introducing a buckconfig to opt out of --extern=proc_macro. Reviewed By: jsgf fbshipit-source-id: 74fac22e9a67e6967151ab7e38f7e97be7300a4d
1 parent f8088cf commit 3eb6678

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

src/com/facebook/buck/features/rust/RustCompileUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,13 @@ private static RustCompileRule createBuild(
294294
args.add(StringArg.of("-Zsave-analysis"));
295295
}
296296

297+
// Don't require "extern crate proc_macro"
298+
if (crateType == CrateType.PROC_MACRO) {
299+
// pathless extern was stabilized in 1.42
300+
// https://blog.rust-lang.org/2020/03/12/Rust-1.42.html#use-proc_macrotokenstream-now-works
301+
args.add(StringArg.of("--extern=proc_macro"));
302+
}
303+
297304
if (incremental.isPresent()) {
298305
Path path =
299306
projectFilesystem

test/com/facebook/buck/features/rust/RustBinaryIntegrationTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -998,11 +998,13 @@ public void includeFileMissing() throws IOException {
998998
}
999999

10001000
@Test
1001-
public void procmacroCompile() throws IOException {
1001+
public void procmacroCompile() throws IOException, InterruptedException {
10021002
ProjectWorkspace workspace =
10031003
TestDataHelper.createProjectWorkspaceForScenario(this, "procmacro", tmp);
10041004
workspace.setUp();
10051005

1006+
RustAssumptions.assumeVersion(workspace, "1.42");
1007+
10061008
assertThat(
10071009
// Check that we can build a procmacro crate
10081010
workspace.runBuckCommand("run", "//:test").assertSuccess("link with procmacro").getStdout(),
@@ -1011,22 +1013,26 @@ public void procmacroCompile() throws IOException {
10111013
}
10121014

10131015
@Test
1014-
public void procmacroCompileCheck() throws IOException {
1016+
public void procmacroCompileCheck() throws IOException, InterruptedException {
10151017
ProjectWorkspace workspace =
10161018
TestDataHelper.createProjectWorkspaceForScenario(this, "procmacro", tmp);
10171019
workspace.setUp();
10181020

1021+
RustAssumptions.assumeVersion(workspace, "1.42");
1022+
10191023
workspace.runBuckBuild("//:test#check").assertSuccess();
10201024
BuckBuildLog buildLog = workspace.getBuildLog();
10211025
buildLog.assertTargetBuiltLocally("//:test#check");
10221026
}
10231027

10241028
@Test
1025-
public void procmacroCompileShared() throws IOException {
1029+
public void procmacroCompileShared() throws IOException, InterruptedException {
10261030
ProjectWorkspace workspace =
10271031
TestDataHelper.createProjectWorkspaceForScenario(this, "procmacro", tmp);
10281032
workspace.setUp();
10291033

1034+
RustAssumptions.assumeVersion(workspace, "1.42");
1035+
10301036
assertThat(
10311037
// Check that we can build a procmacro crate
10321038
workspace
@@ -1038,11 +1044,13 @@ public void procmacroCompileShared() throws IOException {
10381044
}
10391045

10401046
@Test
1041-
public void procmacroCompileSharedForceRlib() throws IOException {
1047+
public void procmacroCompileSharedForceRlib() throws IOException, InterruptedException {
10421048
ProjectWorkspace workspace =
10431049
TestDataHelper.createProjectWorkspaceForScenario(this, "procmacro", tmp);
10441050
workspace.setUp();
10451051

1052+
RustAssumptions.assumeVersion(workspace, "1.42");
1053+
10461054
assertThat(
10471055
// Check that we can build a procmacro crate
10481056
workspace

test/com/facebook/buck/features/rust/testdata/procmacro/BUCK.fixture

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ rust_library(
55
proc_macro = True,
66
)
77

8+
rust_library(
9+
name = "helloworld_derive_no_extern",
10+
srcs = ["helloworld_derive_no_extern.rs"],
11+
deps = [":hellodep"],
12+
proc_macro = True,
13+
)
14+
815
rust_library(
916
name = "hellodep",
1017
srcs = ["hellodep.rs"],
@@ -15,6 +22,7 @@ rust_binary(
1522
srcs = ["main.rs"],
1623
deps = [
1724
":helloworld_derive",
25+
":helloworld_derive_no_extern",
1826
],
1927
)
2028

@@ -24,5 +32,6 @@ rust_binary(
2432
link_style = "shared",
2533
deps = [
2634
":helloworld_derive",
35+
":helloworld_derive_no_extern",
2736
],
2837
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern crate hellodep;
2+
3+
use proc_macro::TokenStream;
4+
use std::str::FromStr;
5+
6+
#[proc_macro_derive(HelloWorldNoExtern)]
7+
pub fn hello_world_no_extern(_input: TokenStream) -> TokenStream {
8+
println!("hellodep returned: {}", hellodep::hellodep());
9+
TokenStream::new(); // no-op
10+
}

test/com/facebook/buck/features/rust/testdata/procmacro/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#[macro_use]
44
extern crate helloworld_derive;
5+
extern crate helloworld_derive_no_extern;
56

67
trait HelloWorld {
78
fn hello_world();
@@ -13,6 +14,9 @@ struct FrenchToast;
1314
#[derive(HelloWorld)]
1415
struct Waffles;
1516

17+
#[derive(HelloWorldNoExtern)]
18+
struct WafflesNoExtern;
19+
1620
fn main() {
1721
println!("Hello");
1822
}

0 commit comments

Comments
 (0)