Skip to content

Commit 9c8e8a9

Browse files
committed
Auto merge of #11323 - weihanglo:issue/11310, r=ehuss
fix: gleaning rustdocflags from `target.cfg(…)` is not supported ### What does this PR try to resolve? Fixes #11310 The bug was `rustflags_from_target` trying to learn rustdocflags from `target.cfg(…).rustflags`, which is definitely wrong. As of this writing, either `target.cfg(…).rustdocflags` or `target.<triple>.rustdocflags` is not supported. ### How should we test and review this PR? See f8d1b30 as an example. <!-- homu-ignore:end -->
2 parents f3905fa + a4d2e29 commit 9c8e8a9

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/cargo/core/compiler/build_context/target_info.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,9 +749,15 @@ fn rustflags_from_target(
749749
.target_cfgs()?
750750
.iter()
751751
.filter_map(|(key, cfg)| {
752-
cfg.rustflags
753-
.as_ref()
754-
.map(|rustflags| (key, &rustflags.val))
752+
match flag {
753+
Flags::Rust => cfg
754+
.rustflags
755+
.as_ref()
756+
.map(|rustflags| (key, &rustflags.val)),
757+
// `target.cfg(…).rustdocflags` is currently not supported.
758+
// In fact, neither is `target.<triple>.rustdocflags`.
759+
Flags::Rustdoc => None,
760+
}
755761
})
756762
.filter(|(key, _rustflags)| CfgExpr::matches_key(key, target_cfg))
757763
.for_each(|(_key, cfg_rustflags)| {

tests/testsuite/rustdocflags.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,34 @@ fn whitespace() {
122122
let contents = p.read_file("target/doc/foo/index.html");
123123
assert!(contents.contains(SPACED_VERSION));
124124
}
125+
126+
#[cargo_test]
127+
fn not_affected_by_target_rustflags() {
128+
let cfg = if cfg!(windows) { "windows" } else { "unix" };
129+
let p = project()
130+
.file("src/lib.rs", "")
131+
.file(
132+
".cargo/config",
133+
&format!(
134+
r#"
135+
[target.'cfg({cfg})']
136+
rustflags = ["-D", "missing-docs"]
137+
138+
[build]
139+
rustdocflags = ["--cfg", "foo"]
140+
"#,
141+
),
142+
)
143+
.build();
144+
145+
// `cargo build` should fail due to missing docs.
146+
p.cargo("build -v")
147+
.with_status(101)
148+
.with_stderr_contains("[RUNNING] `rustc [..] -D missing-docs[..]`")
149+
.run();
150+
151+
// `cargo doc` shouldn't fail.
152+
p.cargo("doc -v")
153+
.with_stderr_contains("[RUNNING] `rustdoc [..] --cfg foo[..]`")
154+
.run();
155+
}

0 commit comments

Comments
 (0)