Skip to content

Commit 841e994

Browse files
committed
Auto merge of #8319 - alexcrichton:fix-access, r=Eh2406
Fix an accidental raw access of field The manifest has a few different ways of specifying whether a crate is a procedural macro, and there's a `TomlTarget::proc_macro()` method to unify these various lines. Unfortunately though we had a bug where one location forgot to call the method and read the raw field! This led to surprising behavior where the different ways to specify a proc macro would have subtly different changes in behavior. The fix here in this PR is to ensure that we access the property always via the method. Closes #8315
2 parents 5f63be6 + 442f629 commit 841e994

File tree

3 files changed

+73
-4
lines changed

3 files changed

+73
-4
lines changed

Diff for: src/cargo/util/toml/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1767,9 +1767,9 @@ struct TomlTarget {
17671767
doc: Option<bool>,
17681768
plugin: Option<bool>,
17691769
#[serde(rename = "proc-macro")]
1770-
proc_macro: Option<bool>,
1770+
proc_macro_raw: Option<bool>,
17711771
#[serde(rename = "proc_macro")]
1772-
proc_macro2: Option<bool>,
1772+
proc_macro_raw2: Option<bool>,
17731773
harness: Option<bool>,
17741774
#[serde(rename = "required-features")]
17751775
required_features: Option<Vec<String>>,
@@ -1824,7 +1824,7 @@ impl TomlTarget {
18241824
}
18251825

18261826
fn proc_macro(&self) -> Option<bool> {
1827-
self.proc_macro.or(self.proc_macro2).or_else(|| {
1827+
self.proc_macro_raw.or(self.proc_macro_raw2).or_else(|| {
18281828
if let Some(types) = self.crate_types() {
18291829
if types.contains(&"proc-macro".to_string()) {
18301830
return Some(true);

Diff for: src/cargo/util/toml/targets.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -774,7 +774,7 @@ fn configure(features: &Features, toml: &TomlTarget, target: &mut Target) -> Car
774774
.set_doctest(toml.doctest.unwrap_or_else(|| t2.doctested()))
775775
.set_benched(toml.bench.unwrap_or_else(|| t2.benched()))
776776
.set_harness(toml.harness.unwrap_or_else(|| t2.harness()))
777-
.set_proc_macro(toml.proc_macro.unwrap_or_else(|| t2.proc_macro()))
777+
.set_proc_macro(toml.proc_macro().unwrap_or_else(|| t2.proc_macro()))
778778
.set_for_host(match (toml.plugin, toml.proc_macro()) {
779779
(None, None) => t2.for_host(),
780780
(Some(true), _) | (_, Some(true)) => true,

Diff for: tests/testsuite/proc_macro.rs

+69
Original file line numberDiff line numberDiff line change
@@ -470,3 +470,72 @@ fn proc_macro_extern_prelude() {
470470
p.cargo("test").run();
471471
p.cargo("doc").run();
472472
}
473+
474+
#[cargo_test]
475+
fn proc_macro_built_once() {
476+
let p = project()
477+
.file(
478+
"Cargo.toml",
479+
r#"
480+
[workspace]
481+
members = ['a', 'b']
482+
"#,
483+
)
484+
.file(
485+
"a/Cargo.toml",
486+
r#"
487+
[package]
488+
name = "a"
489+
version = "0.1.0"
490+
491+
[build-dependencies]
492+
the-macro = { path = '../the-macro' }
493+
"#,
494+
)
495+
.file("a/build.rs", "fn main() {}")
496+
.file("a/src/main.rs", "fn main() {}")
497+
.file(
498+
"b/Cargo.toml",
499+
r#"
500+
[package]
501+
name = "b"
502+
version = "0.1.0"
503+
504+
[dependencies]
505+
the-macro = { path = '../the-macro', features = ['a'] }
506+
"#,
507+
)
508+
.file("b/src/main.rs", "fn main() {}")
509+
.file(
510+
"the-macro/Cargo.toml",
511+
r#"
512+
[package]
513+
name = "the-macro"
514+
version = "0.1.0"
515+
516+
[lib]
517+
proc_macro = true
518+
519+
[features]
520+
a = []
521+
"#,
522+
)
523+
.file("the-macro/src/lib.rs", "")
524+
.build();
525+
p.cargo("build -Zfeatures=all --verbose")
526+
.masquerade_as_nightly_cargo()
527+
.with_stderr_unordered(
528+
"\
529+
[COMPILING] the-macro [..]
530+
[RUNNING] `rustc --crate-name the_macro [..]`
531+
[COMPILING] b [..]
532+
[RUNNING] `rustc --crate-name b [..]`
533+
[COMPILING] a [..]
534+
[RUNNING] `rustc --crate-name build_script_build [..]`
535+
[RUNNING] `[..]build[..]script[..]build[..]`
536+
[RUNNING] `rustc --crate-name a [..]`
537+
[FINISHED] [..]
538+
",
539+
)
540+
.run();
541+
}

0 commit comments

Comments
 (0)