Skip to content

Commit 7688d89

Browse files
committed
Auto merge of #13135 - linyihai:limit-priv-to-library, r=epage
Limit exported-private-dependencies lints to libraries ### What does this PR try to resolve? Completed #13039. This PR limit `exported-private-dependencies` lint in libraray `Target`. ### How should we test and review this PR? Your can checkout out 2348ac2 and run test, it will failed and then it will be passed in the commit 2348ac2 ### Additional information
2 parents 991ad8c + 58c673d commit 7688d89

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

src/cargo/core/compiler/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,7 @@ pub fn extern_args(
14341434
.require(Feature::public_dependency())
14351435
.is_ok()
14361436
&& !dep.public
1437+
&& unit.target.is_lib()
14371438
{
14381439
opts.push("priv");
14391440
*unstable_opts = true;

tests/testsuite/pub_priv.rs

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,227 @@ Caused by:
255255
)
256256
.run()
257257
}
258+
259+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
260+
fn allow_priv_in_tests() {
261+
Package::new("priv_dep", "0.1.0")
262+
.file("src/lib.rs", "pub struct FromPriv;")
263+
.publish();
264+
265+
let p = project()
266+
.file(
267+
"Cargo.toml",
268+
r#"
269+
cargo-features = ["public-dependency"]
270+
271+
[package]
272+
name = "foo"
273+
version = "0.0.1"
274+
275+
[dependencies]
276+
priv_dep = {version = "0.1.0", public = false}
277+
"#,
278+
)
279+
.file(
280+
"tests/mod.rs",
281+
"
282+
extern crate priv_dep;
283+
pub fn use_priv(_: priv_dep::FromPriv) {}
284+
",
285+
)
286+
.build();
287+
288+
p.cargo("check --tests --message-format=short")
289+
.masquerade_as_nightly_cargo(&["public-dependency"])
290+
.with_stderr(
291+
"\
292+
[UPDATING] `[..]` index
293+
[DOWNLOADING] crates ...
294+
[DOWNLOADED] priv_dep v0.1.0 ([..])
295+
[CHECKING] priv_dep v0.1.0
296+
[CHECKING] foo v0.0.1 ([CWD])
297+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
298+
",
299+
)
300+
.run()
301+
}
302+
303+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
304+
fn allow_priv_in_benchs() {
305+
Package::new("priv_dep", "0.1.0")
306+
.file("src/lib.rs", "pub struct FromPriv;")
307+
.publish();
308+
309+
let p = project()
310+
.file(
311+
"Cargo.toml",
312+
r#"
313+
cargo-features = ["public-dependency"]
314+
315+
[package]
316+
name = "foo"
317+
version = "0.0.1"
318+
319+
[dependencies]
320+
priv_dep = {version = "0.1.0", public = false}
321+
"#,
322+
)
323+
.file(
324+
"benches/mod.rs",
325+
"
326+
extern crate priv_dep;
327+
pub fn use_priv(_: priv_dep::FromPriv) {}
328+
",
329+
)
330+
.build();
331+
332+
p.cargo("check --benches --message-format=short")
333+
.masquerade_as_nightly_cargo(&["public-dependency"])
334+
.with_stderr(
335+
"\
336+
[UPDATING] `[..]` index
337+
[DOWNLOADING] crates ...
338+
[DOWNLOADED] priv_dep v0.1.0 ([..])
339+
[CHECKING] priv_dep v0.1.0
340+
[CHECKING] foo v0.0.1 ([CWD])
341+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
342+
",
343+
)
344+
.run()
345+
}
346+
347+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
348+
fn allow_priv_in_bins() {
349+
Package::new("priv_dep", "0.1.0")
350+
.file("src/lib.rs", "pub struct FromPriv;")
351+
.publish();
352+
353+
let p = project()
354+
.file(
355+
"Cargo.toml",
356+
r#"
357+
cargo-features = ["public-dependency"]
358+
359+
[package]
360+
name = "foo"
361+
version = "0.0.1"
362+
363+
[dependencies]
364+
priv_dep = {version = "0.1.0", public = false}
365+
"#,
366+
)
367+
.file(
368+
"src/main.rs",
369+
"
370+
extern crate priv_dep;
371+
pub fn use_priv(_: priv_dep::FromPriv) {}
372+
fn main() {}
373+
",
374+
)
375+
.build();
376+
377+
p.cargo("check --bins --message-format=short")
378+
.masquerade_as_nightly_cargo(&["public-dependency"])
379+
.with_stderr(
380+
"\
381+
[UPDATING] `[..]` index
382+
[DOWNLOADING] crates ...
383+
[DOWNLOADED] priv_dep v0.1.0 ([..])
384+
[CHECKING] priv_dep v0.1.0
385+
[CHECKING] foo v0.0.1 ([CWD])
386+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
387+
",
388+
)
389+
.run()
390+
}
391+
392+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
393+
fn allow_priv_in_examples() {
394+
Package::new("priv_dep", "0.1.0")
395+
.file("src/lib.rs", "pub struct FromPriv;")
396+
.publish();
397+
398+
let p = project()
399+
.file(
400+
"Cargo.toml",
401+
r#"
402+
cargo-features = ["public-dependency"]
403+
404+
[package]
405+
name = "foo"
406+
version = "0.0.1"
407+
408+
[dependencies]
409+
priv_dep = {version = "0.1.0", public = false}
410+
"#,
411+
)
412+
.file(
413+
"examples/lib.rs",
414+
"
415+
extern crate priv_dep;
416+
pub fn use_priv(_: priv_dep::FromPriv) {}
417+
fn main() {}
418+
",
419+
)
420+
.build();
421+
422+
p.cargo("check --examples --message-format=short")
423+
.masquerade_as_nightly_cargo(&["public-dependency"])
424+
.with_stderr(
425+
"\
426+
[UPDATING] `[..]` index
427+
[DOWNLOADING] crates ...
428+
[DOWNLOADED] priv_dep v0.1.0 ([..])
429+
[CHECKING] priv_dep v0.1.0
430+
[CHECKING] foo v0.0.1 ([CWD])
431+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
432+
",
433+
)
434+
.run()
435+
}
436+
437+
#[cargo_test(nightly, reason = "exported_private_dependencies lint is unstable")]
438+
fn allow_priv_in_custom_build() {
439+
Package::new("priv_dep", "0.1.0")
440+
.file("src/lib.rs", "pub struct FromPriv;")
441+
.publish();
442+
443+
let p = project()
444+
.file(
445+
"Cargo.toml",
446+
r#"
447+
cargo-features = ["public-dependency"]
448+
449+
[package]
450+
name = "foo"
451+
version = "0.0.1"
452+
453+
[build-dependencies]
454+
priv_dep = "0.1.0"
455+
"#,
456+
)
457+
.file("src/main.rs", "fn main() {}")
458+
.file(
459+
"build.rs",
460+
"
461+
extern crate priv_dep;
462+
pub fn use_priv(_: priv_dep::FromPriv) {}
463+
fn main() {}
464+
",
465+
)
466+
.build();
467+
468+
p.cargo("check --all-targets --message-format=short")
469+
.masquerade_as_nightly_cargo(&["public-dependency"])
470+
.with_stderr(
471+
"\
472+
[UPDATING] `[..]` index
473+
[DOWNLOADING] crates ...
474+
[DOWNLOADED] priv_dep v0.1.0 ([..])
475+
[COMPILING] priv_dep v0.1.0
476+
[COMPILING] foo v0.0.1 ([CWD])
477+
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
478+
",
479+
)
480+
.run()
481+
}

0 commit comments

Comments
 (0)