Skip to content

Commit

Permalink
Refine warnings and better code
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
  • Loading branch information
Rustin170506 committed Aug 16, 2021
1 parent 61780a9 commit 0b543ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 63 deletions.
63 changes: 29 additions & 34 deletions src/cargo/core/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ impl<'cfg> PackageSet<'cfg> {
target_data,
force_all_targets,
);
for &pkg_id in filtered_deps.iter() {
for pkg_id in filtered_deps {
collect_used_deps(
used,
resolve,
Expand Down Expand Up @@ -568,50 +568,45 @@ impl<'cfg> PackageSet<'cfg> {
target_data: &RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
) -> BTreeMap<PackageId, Vec<&Package>> {
let mut ret = BTreeMap::new();

root_ids.iter().for_each(|&root_id| {
let pkgs: Vec<&Package> = PackageSet::filter_deps(
root_id,
resolve,
has_dev_units,
requested_kinds,
target_data,
force_all_targets,
)
root_ids
.iter()
.filter_map(|&package_id| {
if let Ok(dep_pkg) = self.get_one(package_id) {
if !dep_pkg.targets().iter().any(|t| t.is_lib()) {
Some(dep_pkg)
.map(|&root_id| {
let pkgs = PackageSet::filter_deps(
root_id,
resolve,
has_dev_units,
requested_kinds,
target_data,
force_all_targets,
)
.filter_map(|package_id| {
if let Ok(dep_pkg) = self.get_one(package_id) {
if !dep_pkg.targets().iter().any(|t| t.is_lib()) {
Some(dep_pkg)
} else {
None
}
} else {
None
}
} else {
None
}
})
.collect();
(root_id, pkgs)
})
.collect();

if !pkgs.is_empty() {
ret.insert(root_id, pkgs);
}
});

ret
.collect()
}

fn filter_deps(
fn filter_deps<'a>(
pkg_id: PackageId,
resolve: &Resolve,
resolve: &'a Resolve,
has_dev_units: HasDevUnits,
requested_kinds: &[CompileKind],
target_data: &RustcTargetData<'_>,
requested_kinds: &'a [CompileKind],
target_data: &'a RustcTargetData<'_>,
force_all_targets: ForceAllTargets,
) -> Vec<PackageId> {
) -> impl Iterator<Item = PackageId> + 'a {
resolve
.deps(pkg_id)
.filter(|&(_id, deps)| {
.filter(move |&(_id, deps)| {
deps.iter().any(|dep| {
if dep.kind() == DepKind::Development && has_dev_units == HasDevUnits::No {
return false;
Expand All @@ -629,7 +624,7 @@ impl<'cfg> PackageSet<'cfg> {
})
})
.map(|(pkg_id, _)| pkg_id)
.collect()
.into_iter()
}

pub fn sources(&self) -> Ref<'_, SourceMap<'cfg>> {
Expand Down
40 changes: 16 additions & 24 deletions src/cargo/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,22 @@ pub fn resolve_ws_with_opts<'cfg>(
feature_opts,
)?;

let no_lib_warnings: Vec<String> = pkg_set
.no_lib_pkgs(
&resolved_with_overrides,
&member_ids,
has_dev_units,
requested_targets,
target_data,
force_all_targets,
)
.iter()
.flat_map(|(pkg_id, dep_pkgs)| {
dep_pkgs.iter().map(move |dep_pkg| {
format!(
"{} has invalid dependency `{}`. `{}` has no lib package.",
pkg_id,
dep_pkg.name(),
dep_pkg.name(),
)
})
})
.collect();

for warn in no_lib_warnings {
ws.config().shell().warn(warn)?;
let no_lib_pkgs = pkg_set.no_lib_pkgs(
&resolved_with_overrides,
&member_ids,
has_dev_units,
requested_targets,
target_data,
force_all_targets,
);
for (pkg_id, dep_pkgs) in no_lib_pkgs {
for dep_pkg in dep_pkgs {
ws.config().shell().warn(&format!(
"{} ignoring invalid dependency `{}` which is missing a lib target",
pkg_id,
dep_pkg.name(),
))?;
}
}

Ok(WorkspaceResolve {
Expand Down
10 changes: 5 additions & 5 deletions tests/testsuite/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ fn run_with_bin_dep() {
p.cargo("run")
.with_stderr(
"\
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar`. `bar` has no lib package.
[WARNING] foo v0.0.1 ([CWD]) ignoring invalid dependency `bar` which is missing a lib target
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`",
Expand Down Expand Up @@ -813,8 +813,8 @@ fn run_with_bin_deps() {
p.cargo("run")
.with_stderr(
"\
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar1`. `bar1` has no lib package.
[WARNING] foo v0.0.1 ([CWD]) has invalid dependency `bar2`. `bar2` has no lib package.
[WARNING] foo v0.0.1 ([CWD]) ignoring invalid dependency `bar1` which is missing a lib target
[WARNING] foo v0.0.1 ([CWD]) ignoring invalid dependency `bar2` which is missing a lib target
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo[EXE]`",
Expand Down Expand Up @@ -903,8 +903,8 @@ available binaries: bar1, bar2, foo1, foo2",
p.cargo("run --bin foo1")
.with_stderr(
"\
[WARNING] foo1 v0.0.1 ([CWD]/foo1) has invalid dependency `bar1`. `bar1` has no lib package.
[WARNING] foo2 v0.0.1 ([CWD]/foo2) has invalid dependency `bar2`. `bar2` has no lib package.
[WARNING] foo1 v0.0.1 ([CWD]/foo1) ignoring invalid dependency `bar1` which is missing a lib target
[WARNING] foo2 v0.0.1 ([CWD]/foo2) ignoring invalid dependency `bar2` which is missing a lib target
[COMPILING] foo1 v0.0.1 ([CWD]/foo1)
[FINISHED] dev [unoptimized + debuginfo] target(s) in [..]
[RUNNING] `target/debug/foo1[EXE]`",
Expand Down

0 comments on commit 0b543ba

Please sign in to comment.