Skip to content

Commit 5d52ff9

Browse files
dtolnayshepmaster
authored andcommitted
Support #![crate_type = "proc-macro"]
1 parent e33f46b commit 5d52ff9

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

compiler/base/modify-cargo-toml/src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,20 @@ fn set_crate_type(cargo_toml: Value, crate_type: &str) -> Value {
114114
#[derive(Debug, Default, Serialize, Deserialize)]
115115
#[serde(rename_all = "kebab-case")]
116116
struct Lib {
117-
#[serde(default)]
117+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
118118
crate_type: Vec<String>,
119+
#[serde(default)]
120+
proc_macro: bool,
119121
#[serde(flatten)]
120122
other: Other,
121123
}
122124

123125
modify(cargo_toml, |mut cargo_toml: CargoToml| {
124-
ensure_string_in_vec(&mut cargo_toml.lib.crate_type, crate_type);
126+
if crate_type == "proc-macro" {
127+
cargo_toml.lib.proc_macro = true;
128+
} else {
129+
ensure_string_in_vec(&mut cargo_toml.lib.crate_type, crate_type);
130+
}
125131
cargo_toml
126132
})
127133
}

tests/spec/features/automatic_primary_action_spec.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,33 @@
3232
end
3333
end
3434

35+
scenario "when the crate is a procedural macro" do
36+
editor.set <<~EOF
37+
#![crate_type = "proc-macro"]
38+
39+
use proc_macro::TokenStream;
40+
41+
/// Example:
42+
/// ```
43+
/// playground::demo!();
44+
/// type T = aaa;
45+
/// ```
46+
#[proc_macro]
47+
pub fn demo(_input: TokenStream) -> TokenStream {
48+
eprintln!("wow wow");
49+
"struct Aaa;".parse().unwrap()
50+
}
51+
52+
/*
53+
#[test]*/
54+
EOF
55+
click_on("Test")
56+
57+
within(:output, :stdout) do
58+
expect(page).to have_content 'a struct with a similar name exists: `Aaa`'
59+
end
60+
end
61+
3562
scenario "when the crate is a library with tests" do
3663
editor.set <<~EOF
3764
#![crate_type="lib"]

ui/frontend/selectors/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const autoPrimaryActionSelector = createSelector(
2121
hasTestsSelector,
2222
hasMainFunctionSelector,
2323
(crateType, hasTests, hasMainFunction) => {
24-
if (crateType) {
24+
if (crateType && crateType !== 'proc-macro') {
2525
if (crateType === 'bin') {
2626
return PrimaryActionCore.Execute;
2727
} else {
@@ -43,9 +43,19 @@ export const runAsTest = createSelector(
4343
autoPrimaryActionSelector,
4444
primaryAction => primaryAction === PrimaryActionCore.Test,
4545
);
46+
4647
export const getCrateType = createSelector(
48+
crateTypeSelector,
4749
autoPrimaryActionSelector,
48-
primaryAction => primaryAction === PrimaryActionCore.Execute ? 'bin' : 'lib',
50+
(crateType, primaryAction) => {
51+
if (crateType) {
52+
return crateType;
53+
} else if (primaryAction === PrimaryActionCore.Execute) {
54+
return 'bin';
55+
} else {
56+
return 'lib';
57+
}
58+
},
4959
);
5060

5161
const rawPrimaryActionSelector = (state: State) => state.configuration.primaryAction;

0 commit comments

Comments
 (0)