Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Treat "proc-macro" crate type the same as proc-macro = true #6256

Merged
merged 2 commits into from
Nov 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/cargo/util/toml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,14 @@ impl TomlTarget {
}

fn proc_macro(&self) -> Option<bool> {
self.proc_macro.or(self.proc_macro2)
self.proc_macro.or(self.proc_macro2).or_else(|| {
if let Some(types) = self.crate_types() {
if types.contains(&"proc-macro".to_string()) {
return Some(true);
}
}
None
})
}

fn crate_types(&self) -> Option<&Vec<String>> {
Expand Down
17 changes: 17 additions & 0 deletions src/cargo/util/toml/targets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ fn clean_lib(
// A plugin requires exporting plugin_registrar so a crate cannot be
// both at once.
let crate_types = match (lib.crate_types(), lib.plugin, lib.proc_macro()) {
(Some(kinds), _, _) if kinds.contains(&"proc-macro".to_string()) => {
if let Some(true) = lib.plugin {
// This is a warning to retain backwards compatibility.
warnings.push(format!(
"proc-macro library `{}` should not specify `plugin = true`",
lib.name()
));
}
warnings.push(format!(
"library `{}` should only specify `proc-macro = true` instead of setting `crate-type`",
lib.name()
));
if kinds.len() > 1 {
bail!("cannot mix `proc-macro` crate type with others");
}
vec![LibKind::ProcMacro]
}
(_, Some(true), Some(true)) => bail!("lib.plugin and lib.proc-macro cannot both be true"),
(Some(kinds), _, _) => kinds.iter().map(|s| s.into()).collect(),
(None, Some(true), _) => vec![LibKind::Dylib],
Expand Down
142 changes: 142 additions & 0 deletions tests/testsuite/proc_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,145 @@ fn a() {
.with_stdout_contains_n("test [..] ... ok", 2)
.run();
}

#[test]
fn proc_macro_crate_type() {
// Verify that `crate-type = ["proc-macro"]` is the same as `proc-macro = true`
// and that everything, including rustdoc, works correctly.
let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
pm = { path = "pm" }
"#,
)
.file(
"src/lib.rs",
r#"
//! ```
//! use foo::THING;
//! assert_eq!(THING, 123);
//! ```
#[macro_use]
extern crate pm;
#[derive(MkItem)]
pub struct S;
#[cfg(test)]
mod tests {
use super::THING;
#[test]
fn it_works() {
assert_eq!(THING, 123);
}
}
"#,
)
.file(
"pm/Cargo.toml",
r#"
[package]
name = "pm"
version = "0.1.0"
[lib]
crate-type = ["proc-macro"]
"#,
)
.file(
"pm/src/lib.rs",
r#"
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro_derive(MkItem)]
pub fn mk_item(_input: TokenStream) -> TokenStream {
"pub const THING: i32 = 123;".parse().unwrap()
}
"#,
)
.build();

foo.cargo("test")
.with_stdout_contains("test tests::it_works ... ok")
.with_stdout_contains_n("test [..] ... ok", 2)
.run();
}

#[test]
fn proc_macro_crate_type_warning() {
let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[lib]
crate-type = ["proc-macro"]
"#,
)
.file("src/lib.rs", "")
.build();

foo.cargo("build")
.with_stderr_contains(
"[WARNING] library `foo` should only specify `proc-macro = true` instead of setting `crate-type`")
.run();
}

#[test]
fn proc_macro_crate_type_warning_plugin() {
let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[lib]
crate-type = ["proc-macro"]
plugin = true
"#,
)
.file("src/lib.rs", "")
.build();

foo.cargo("build")
.with_stderr_contains(
"[WARNING] proc-macro library `foo` should not specify `plugin = true`")
.with_stderr_contains(
"[WARNING] library `foo` should only specify `proc-macro = true` instead of setting `crate-type`")
.run();
}

#[test]
fn proc_macro_crate_type_multiple() {
let foo = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[lib]
crate-type = ["proc-macro", "rlib"]
"#,
)
.file("src/lib.rs", "")
.build();

foo.cargo("build")
.with_stderr(
"\
[ERROR] failed to parse manifest at `[..]/foo/Cargo.toml`

Caused by:
cannot mix `proc-macro` crate type with others
",
)
.with_status(101)
.run();
}