Skip to content

Commit b0e767f

Browse files
committed
Add warnings for crate-type = ["proc-macro"].
1 parent e295a90 commit b0e767f

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

src/cargo/util/toml/targets.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,23 @@ fn clean_lib(
206206
// A plugin requires exporting plugin_registrar so a crate cannot be
207207
// both at once.
208208
let crate_types = match (lib.crate_types(), lib.plugin, lib.proc_macro()) {
209+
(Some(kinds), _, _) if kinds.contains(&"proc-macro".to_string()) => {
210+
if let Some(true) = lib.plugin {
211+
// This is a warning to retain backwards compatibility.
212+
warnings.push(format!(
213+
"proc-macro library `{}` should not specify `plugin = true`",
214+
lib.name()
215+
));
216+
}
217+
warnings.push(format!(
218+
"library `{}` should only specify `proc-macro = true` instead of setting `crate-type`",
219+
lib.name()
220+
));
221+
if kinds.len() > 1 {
222+
bail!("cannot mix `proc-macro` crate type with others");
223+
}
224+
vec![LibKind::ProcMacro]
225+
}
209226
(_, Some(true), Some(true)) => bail!("lib.plugin and lib.proc-macro cannot both be true"),
210227
(Some(kinds), _, _) => kinds.iter().map(|s| s.into()).collect(),
211228
(None, Some(true), _) => vec![LibKind::Dylib],

tests/testsuite/proc_macro.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,79 @@ fn proc_macro_crate_type() {
345345
.with_stdout_contains_n("test [..] ... ok", 2)
346346
.run();
347347
}
348+
349+
#[test]
350+
fn proc_macro_crate_type_warning() {
351+
let foo = project()
352+
.file(
353+
"Cargo.toml",
354+
r#"
355+
[package]
356+
name = "foo"
357+
version = "0.1.0"
358+
[lib]
359+
crate-type = ["proc-macro"]
360+
"#,
361+
)
362+
.file("src/lib.rs", "")
363+
.build();
364+
365+
foo.cargo("build")
366+
.with_stderr_contains(
367+
"[WARNING] library `foo` should only specify `proc-macro = true` instead of setting `crate-type`")
368+
.run();
369+
}
370+
371+
#[test]
372+
fn proc_macro_crate_type_warning_plugin() {
373+
let foo = project()
374+
.file(
375+
"Cargo.toml",
376+
r#"
377+
[package]
378+
name = "foo"
379+
version = "0.1.0"
380+
[lib]
381+
crate-type = ["proc-macro"]
382+
plugin = true
383+
"#,
384+
)
385+
.file("src/lib.rs", "")
386+
.build();
387+
388+
foo.cargo("build")
389+
.with_stderr_contains(
390+
"[WARNING] proc-macro library `foo` should not specify `plugin = true`")
391+
.with_stderr_contains(
392+
"[WARNING] library `foo` should only specify `proc-macro = true` instead of setting `crate-type`")
393+
.run();
394+
}
395+
396+
#[test]
397+
fn proc_macro_crate_type_multiple() {
398+
let foo = project()
399+
.file(
400+
"Cargo.toml",
401+
r#"
402+
[package]
403+
name = "foo"
404+
version = "0.1.0"
405+
[lib]
406+
crate-type = ["proc-macro", "rlib"]
407+
"#,
408+
)
409+
.file("src/lib.rs", "")
410+
.build();
411+
412+
foo.cargo("build")
413+
.with_stderr(
414+
"\
415+
[ERROR] failed to parse manifest at `[..]/foo/Cargo.toml`
416+
417+
Caused by:
418+
cannot mix `proc-macro` crate type with others
419+
",
420+
)
421+
.with_status(101)
422+
.run();
423+
}

0 commit comments

Comments
 (0)