Skip to content

Commit b13f167

Browse files
committed
Add "-Zpublic-dependency" for public-dependency feature
1 parent c061bfb commit b13f167

File tree

23 files changed

+184
-140
lines changed

23 files changed

+184
-140
lines changed

src/cargo/core/compiler/mod.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ use crate::core::compiler::future_incompat::FutureIncompatReport;
8989
pub use crate::core::compiler::unit::{Unit, UnitInterner};
9090
use crate::core::manifest::TargetSourcePath;
9191
use crate::core::profiles::{PanicStrategy, Profile, StripInner};
92-
use crate::core::{Feature, PackageId, Target, Verbosity};
92+
use crate::core::{PackageId, Target, Verbosity};
9393
use crate::util::errors::{CargoResult, VerboseError};
9494
use crate::util::interning::InternedString;
9595
use crate::util::machine_message::{self, Message};
@@ -1437,14 +1437,7 @@ pub fn extern_args(
14371437
|dep: &UnitDep, extern_crate_name: InternedString, noprelude: bool| -> CargoResult<()> {
14381438
let mut value = OsString::new();
14391439
let mut opts = Vec::new();
1440-
if unit
1441-
.pkg
1442-
.manifest()
1443-
.unstable_features()
1444-
.require(Feature::public_dependency())
1445-
.is_ok()
1446-
&& !dep.public
1447-
&& unit.target.is_lib()
1440+
if !dep.public && unit.target.is_lib() && cx.bcx.config.cli_unstable().public_dependency
14481441
{
14491442
opts.push("priv");
14501443
*unstable_opts = true;

src/cargo/core/features.rs

+2
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ unstable_cli_options!(
764764
panic_abort_tests: bool = ("Enable support to run tests with -Cpanic=abort"),
765765
precise_pre_release: bool = ("Enable pre-release versions to be selected with `update --precise`"),
766766
profile_rustflags: bool = ("Enable the `rustflags` option in profiles in .cargo/config.toml file"),
767+
public_dependency: bool = ("Respect a dependency's `public` field in Cargo.toml to control public/private dependencies"),
767768
publish_timeout: bool = ("Enable the `publish.timeout` key in .cargo/config.toml file"),
768769
rustdoc_map: bool = ("Allow passing external documentation mappings to rustdoc"),
769770
rustdoc_scrape_examples: bool = ("Allows Rustdoc to scrape code examples from reverse-dependencies"),
@@ -1140,6 +1141,7 @@ impl CliUnstable {
11401141
"mtime-on-use" => self.mtime_on_use = parse_empty(k, v)?,
11411142
"no-index-update" => self.no_index_update = parse_empty(k, v)?,
11421143
"panic-abort-tests" => self.panic_abort_tests = parse_empty(k, v)?,
1144+
"public-dependency" => self.public_dependency = parse_empty(k, v)?,
11431145
"profile-rustflags" => self.profile_rustflags = parse_empty(k, v)?,
11441146
"precise-pre-release" => self.precise_pre_release = parse_empty(k, v)?,
11451147
"trim-paths" => self.trim_paths = parse_empty(k, v)?,

src/cargo/core/workspace.rs

-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ impl<'cfg> Workspace<'cfg> {
449449
// NOTE: Since we use ConfigRelativePath, this root isn't used as
450450
// any relative paths are resolved before they'd be joined with root.
451451
Path::new("unused-relative-path"),
452-
self.unstable_features(),
453452
/* kind */ None,
454453
)
455454
})

src/cargo/ops/cargo_package.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use crate::core::compiler::{BuildConfig, CompileMode, DefaultExecutor, Executor}
1010
use crate::core::manifest::Target;
1111
use crate::core::resolver::CliFeatures;
1212
use crate::core::{registry::PackageRegistry, resolver::HasDevUnits};
13-
use crate::core::{Feature, Shell, Verbosity, Workspace};
1413
use crate::core::{Package, PackageId, PackageSet, Resolve, SourceId};
14+
use crate::core::{Shell, Verbosity, Workspace};
1515
use crate::sources::PathSource;
1616
use crate::util::cache_lock::CacheLockMode;
1717
use crate::util::config::JobsConfig;
@@ -911,12 +911,7 @@ fn run_verify(
911911
let pkg_fingerprint = hash_all(&dst)?;
912912
let ws = Workspace::ephemeral(new_pkg, config, None, true)?;
913913

914-
let rustc_args = if pkg
915-
.manifest()
916-
.unstable_features()
917-
.require(Feature::public_dependency())
918-
.is_ok()
919-
{
914+
let rustc_args = if ws.config().cli_unstable().public_dependency {
920915
// FIXME: Turn this on at some point in the future
921916
//Some(vec!["-D exported_private_dependencies".to_string()])
922917
Some(vec![])

src/cargo/util/toml/mod.rs

+39-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::AlreadyPrintedError;
99
use anyhow::{anyhow, bail, Context as _};
1010
use cargo_platform::Platform;
1111
use cargo_util::paths;
12-
use cargo_util_schemas::manifest;
1312
use cargo_util_schemas::manifest::RustVersion;
13+
use cargo_util_schemas::manifest::{self, InheritableDependency, TomlDependency};
1414
use itertools::Itertools;
1515
use lazycell::LazyCell;
1616
use pathdiff::diff_paths;
@@ -104,13 +104,30 @@ fn read_manifest_from_str(
104104
let package_root = manifest_file.parent().unwrap();
105105

106106
let mut unused = BTreeSet::new();
107+
let mut public_deps_without_z = BTreeSet::new();
108+
107109
let deserializer = toml::de::Deserializer::new(contents);
108110
let manifest: manifest::TomlManifest = match serde_ignored::deserialize(deserializer, |path| {
109111
let mut key = String::new();
110112
stringify(&mut key, &path);
111113
unused.insert(key);
112114
}) {
113-
Ok(manifest) => manifest,
115+
Ok(manifest) => {
116+
let mut manifest: manifest::TomlManifest = manifest;
117+
if let Some(deps) = &mut manifest.dependencies {
118+
for (name, dep) in deps {
119+
if let InheritableDependency::Value(toml_dep) = dep {
120+
if let TomlDependency::Detailed(d) = toml_dep {
121+
if !config.cli_unstable().public_dependency && d.public.is_some() {
122+
d.public = None;
123+
public_deps_without_z.insert(name.clone());
124+
}
125+
}
126+
}
127+
}
128+
}
129+
manifest
130+
}
114131
Err(e) => {
115132
let Some(span) = e.span() else {
116133
return Err(e.into());
@@ -168,6 +185,13 @@ fn read_manifest_from_str(
168185
}
169186
}
170187
};
188+
let public_warn = |warnings: &mut Warnings| {
189+
for name in public_deps_without_z {
190+
warnings.add_warning(format!(
191+
"{name} uses 'public' specifier, pass `-Zpublic-dependency` to enable support for it",
192+
))
193+
}
194+
};
171195

172196
if let Some(deps) = manifest
173197
.workspace
@@ -187,6 +211,7 @@ fn read_manifest_from_str(
187211
let (mut manifest, paths) =
188212
to_real_manifest(manifest, embedded, source_id, package_root, config)?;
189213
add_unused(manifest.warnings_mut());
214+
public_warn(manifest.warnings_mut());
190215
if manifest.targets().iter().all(|t| t.is_custom_build()) {
191216
bail!(
192217
"no targets specified in the manifest\n\
@@ -198,6 +223,7 @@ fn read_manifest_from_str(
198223
} else {
199224
let (mut m, paths) = to_virtual_manifest(manifest, source_id, package_root, config)?;
200225
add_unused(m.warnings_mut());
226+
public_warn(m.warnings_mut());
201227
Ok((EitherManifest::Virtual(m), paths))
202228
};
203229

@@ -678,7 +704,6 @@ pub fn to_real_manifest(
678704
nested_paths: &mut nested_paths,
679705
config,
680706
warnings: &mut warnings,
681-
features: &features,
682707
platform: None,
683708
root: package_root,
684709
};
@@ -1153,7 +1178,6 @@ fn to_virtual_manifest(
11531178
config,
11541179
warnings: &mut warnings,
11551180
platform: None,
1156-
features: &features,
11571181
root,
11581182
};
11591183
(replace(&me, &mut cx)?, patch(&me, &mut cx)?)
@@ -1302,7 +1326,6 @@ struct Context<'a, 'b> {
13021326
warnings: &'a mut Vec<String>,
13031327
platform: Option<Platform>,
13041328
root: &'a Path,
1305-
features: &'a Features,
13061329
}
13071330

13081331
fn verify_lints(lints: Option<manifest::TomlLints>) -> CargoResult<Option<manifest::TomlLints>> {
@@ -1709,7 +1732,6 @@ pub(crate) fn to_dependency<P: ResolveToPath + Clone>(
17091732
warnings: &mut Vec<String>,
17101733
platform: Option<Platform>,
17111734
root: &Path,
1712-
features: &Features,
17131735
kind: Option<DepKind>,
17141736
) -> CargoResult<Dependency> {
17151737
dep_to_dependency(
@@ -1723,7 +1745,6 @@ pub(crate) fn to_dependency<P: ResolveToPath + Clone>(
17231745
warnings,
17241746
platform,
17251747
root,
1726-
features,
17271748
},
17281749
kind,
17291750
)
@@ -1944,7 +1965,17 @@ fn detailed_dep_to_dependency<P: ResolveToPath + Clone>(
19441965
}
19451966

19461967
if let Some(p) = orig.public {
1947-
cx.features.require(Feature::public_dependency())?;
1968+
if !cx.config.nightly_features_allowed {
1969+
bail!(
1970+
"Consider trying a newer version of Cargo (this may require the nightly release)."
1971+
);
1972+
}
1973+
if !cx.config.cli_unstable().public_dependency {
1974+
bail!(format!(
1975+
"{name} uses 'public' specifier, pass `-Zpublic-dependency` to enable support for it",
1976+
name = dep.name_in_toml(),
1977+
));
1978+
}
19481979

19491980
if dep.kind() != DepKind::Normal {
19501981
bail!("'public' specifier can only be used on regular dependencies, not {:?} dependencies", dep.kind());

src/doc/src/reference/unstable.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,9 @@ The 'public-dependency' feature allows marking dependencies as 'public'
296296
or 'private'. When this feature is enabled, additional information is passed to rustc to allow
297297
the 'exported_private_dependencies' lint to function properly.
298298

299-
This requires the appropriate key to be set in `cargo-features`:
299+
Pass `-Zpublic-dependency` to cargo to enable support for it.
300300

301301
```toml
302-
cargo-features = ["public-dependency"]
303-
304302
[dependencies]
305303
my_dep = { version = "1.2.3", public = true }
306304
private_dep = "2.0.0" # Will be 'private' by default

tests/build-std/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn enable_build_std(e: &mut Execs, arg: Option<&str>) {
3333
Some(s) => format!("-Zbuild-std={}", s),
3434
None => "-Zbuild-std".to_string(),
3535
};
36-
e.arg(arg);
36+
e.arg(arg).arg("-Zpublic-dependency");
3737
e.masquerade_as_nightly_cargo(&["build-std"]);
3838
}
3939

tests/testsuite/cargo/z_help/stdout.log

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Available unstable (nightly-only) flags:
2525
-Z panic-abort-tests Enable support to run tests with -Cpanic=abort
2626
-Z precise-pre-release Enable pre-release versions to be selected with `update --precise`
2727
-Z profile-rustflags Enable the `rustflags` option in profiles in .cargo/config.toml file
28+
-Z public-dependency Respect a dependency's `public` field in Cargo.toml to control public/private dependencies
2829
-Z publish-timeout Enable the `publish.timeout` key in .cargo/config.toml file
2930
-Z rustdoc-map Allow passing external documentation mappings to rustdoc
3031
-Z rustdoc-scrape-examples Allows Rustdoc to scrape code examples from reverse-dependencies
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[package]
32
name = "bar"
43
version = "0.0.0"

tests/testsuite/cargo_add/detect_workspace_inherit_public/out/primary/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[package]
32
name = "bar"
43
version = "0.0.0"

tests/testsuite/cargo_add/no_public/in/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/no_public/out/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_no_public/in/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_no_public/out/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_no_public_with_public/in/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_no_public_with_public/out/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_public/in/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_public/out/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_public_with_no_public/in/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/overwrite_public_with_no_public/out/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/public/in/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

tests/testsuite/cargo_add/public/out/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
cargo-features = ["public-dependency"]
21
[workspace]
32

43
[package]

0 commit comments

Comments
 (0)