Skip to content

Commit 14654f3

Browse files
committed
Auto merge of #7626 - ehuss:remove-dep-targets, r=alexcrichton
Remove dep_targets. This is just some code cleanup. The `dep_targets` method has been replaced by `unit_deps`, which doesn't force the creation of a new Vec.
2 parents a41c8ea + cc9f5a8 commit 14654f3

File tree

7 files changed

+61
-73
lines changed

7 files changed

+61
-73
lines changed

src/cargo/core/compiler/build_plan.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@ impl BuildPlan {
113113
let id = self.plan.invocations.len();
114114
self.invocation_map.insert(unit.buildkey(), id);
115115
let deps = cx
116-
.dep_targets(unit)
116+
.unit_deps(unit)
117117
.iter()
118-
.map(|dep| self.invocation_map[&dep.buildkey()])
118+
.map(|dep| self.invocation_map[&dep.unit.buildkey()])
119119
.collect();
120120
let invocation = Invocation::new(unit, deps);
121121
self.plan.invocations.push(invocation);

src/cargo/core/compiler/context/compilation_files.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,8 @@ fn metadata_of<'a, 'cfg>(
458458
if !metas.contains_key(unit) {
459459
let meta = compute_metadata(unit, cx, metas);
460460
metas.insert(*unit, meta);
461-
for unit in cx.dep_targets(unit) {
462-
metadata_of(&unit, cx, metas);
461+
for dep in cx.unit_deps(unit) {
462+
metadata_of(&dep.unit, cx, metas);
463463
}
464464
}
465465
metas[unit].clone()
@@ -532,15 +532,13 @@ fn compute_metadata<'a, 'cfg>(
532532
unit.features.hash(&mut hasher);
533533

534534
// Mix in the target-metadata of all the dependencies of this target.
535-
{
536-
let mut deps_metadata = cx
537-
.dep_targets(unit)
538-
.iter()
539-
.map(|dep| metadata_of(dep, cx, metas))
540-
.collect::<Vec<_>>();
541-
deps_metadata.sort();
542-
deps_metadata.hash(&mut hasher);
543-
}
535+
let mut deps_metadata = cx
536+
.unit_deps(unit)
537+
.iter()
538+
.map(|dep| metadata_of(&dep.unit, cx, metas))
539+
.collect::<Vec<_>>();
540+
deps_metadata.sort();
541+
deps_metadata.hash(&mut hasher);
544542

545543
// Throw in the profile we're compiling with. This helps caching
546544
// `panic=abort` and `panic=unwind` artifacts, additionally with various

src/cargo/core/compiler/context/mod.rs

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,20 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
187187

188188
// If the unit has a build script, add `OUT_DIR` to the
189189
// environment variables.
190-
for dep in self.dep_targets(unit).iter() {
191-
if !unit.target.is_lib() {
192-
continue;
193-
}
194-
195-
if dep.mode.is_run_custom_build() {
196-
let out_dir = self.files().build_script_out_dir(dep).display().to_string();
197-
self.compilation
198-
.extra_env
199-
.entry(dep.pkg.package_id())
200-
.or_insert_with(Vec::new)
201-
.push(("OUT_DIR".to_string(), out_dir));
190+
if unit.target.is_lib() {
191+
for dep in &self.unit_dependencies[unit] {
192+
if dep.unit.mode.is_run_custom_build() {
193+
let out_dir = self
194+
.files()
195+
.build_script_out_dir(&dep.unit)
196+
.display()
197+
.to_string();
198+
self.compilation
199+
.extra_env
200+
.entry(dep.unit.pkg.package_id())
201+
.or_insert_with(Vec::new)
202+
.push(("OUT_DIR".to_string(), out_dir));
203+
}
202204
}
203205
}
204206

@@ -361,18 +363,6 @@ impl<'a, 'cfg> Context<'a, 'cfg> {
361363
self.files.as_ref().unwrap().outputs(unit, self.bcx)
362364
}
363365

364-
/// For a package, return all targets which are registered as dependencies
365-
/// for that package.
366-
/// NOTE: This is deprecated, use `unit_deps` instead.
367-
//
368-
// TODO: this ideally should be `-> &[Unit<'a>]`.
369-
pub fn dep_targets(&self, unit: &Unit<'a>) -> Vec<Unit<'a>> {
370-
self.unit_dependencies[unit]
371-
.iter()
372-
.map(|dep| dep.unit)
373-
.collect()
374-
}
375-
376366
/// Direct dependencies for the given unit.
377367
pub fn unit_deps(&self, unit: &Unit<'a>) -> &[UnitDep<'a>] {
378368
&self.unit_dependencies[unit]

src/cargo/core/compiler/custom_build.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ fn emit_build_output(
133133
fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoResult<Job> {
134134
assert!(unit.mode.is_run_custom_build());
135135
let bcx = &cx.bcx;
136-
let dependencies = cx.dep_targets(unit);
136+
let dependencies = cx.unit_deps(unit);
137137
let build_script_unit = dependencies
138138
.iter()
139-
.find(|d| !d.mode.is_run_custom_build() && d.target.is_custom_build())
139+
.find(|d| !d.unit.mode.is_run_custom_build() && d.unit.target.is_custom_build())
140+
.map(|d| &d.unit)
140141
.expect("running a script not depending on an actual script");
141142
let script_dir = cx.files().build_script_dir(build_script_unit);
142143
let script_out_dir = cx.files().build_script_out_dir(unit);
@@ -225,21 +226,19 @@ fn build_work<'a, 'cfg>(cx: &mut Context<'a, 'cfg>, unit: &Unit<'a>) -> CargoRes
225226
//
226227
// This information will be used at build-time later on to figure out which
227228
// sorts of variables need to be discovered at that time.
228-
let lib_deps = {
229-
dependencies
230-
.iter()
231-
.filter_map(|unit| {
232-
if unit.mode.is_run_custom_build() {
233-
Some((
234-
unit.pkg.manifest().links().unwrap().to_string(),
235-
unit.pkg.package_id(),
236-
))
237-
} else {
238-
None
239-
}
240-
})
241-
.collect::<Vec<_>>()
242-
};
229+
let lib_deps = dependencies
230+
.iter()
231+
.filter_map(|dep| {
232+
if dep.unit.mode.is_run_custom_build() {
233+
Some((
234+
dep.unit.pkg.manifest().links().unwrap().to_string(),
235+
dep.unit.pkg.package_id(),
236+
))
237+
} else {
238+
None
239+
}
240+
})
241+
.collect::<Vec<_>>();
243242
let pkg_name = unit.pkg.to_string();
244243
let build_script_outputs = Arc::clone(&cx.build_script_outputs);
245244
let id = unit.pkg.package_id();
@@ -563,15 +562,15 @@ fn prepare_metabuild<'a, 'cfg>(
563562
deps: &[String],
564563
) -> CargoResult<()> {
565564
let mut output = Vec::new();
566-
let available_deps = cx.dep_targets(unit);
565+
let available_deps = cx.unit_deps(unit);
567566
// Filter out optional dependencies, and look up the actual lib name.
568567
let meta_deps: Vec<_> = deps
569568
.iter()
570569
.filter_map(|name| {
571570
available_deps
572571
.iter()
573-
.find(|u| u.pkg.name().as_str() == name.as_str())
574-
.map(|dep| dep.target.crate_name())
572+
.find(|d| d.unit.pkg.name().as_str() == name.as_str())
573+
.map(|d| d.unit.target.crate_name())
575574
})
576575
.collect();
577576
for dep in &meta_deps {
@@ -669,7 +668,7 @@ pub fn build_map<'b, 'cfg>(cx: &mut Context<'b, 'cfg>, units: &[Unit<'b>]) -> Ca
669668
// to rustc invocation caching schemes, so be sure to generate the same
670669
// set of build script dependency orderings via sorting the targets that
671670
// come out of the `Context`.
672-
let mut dependencies = cx.dep_targets(unit);
671+
let mut dependencies: Vec<Unit<'_>> = cx.unit_deps(unit).iter().map(|d| d.unit).collect();
673672
dependencies.sort_by_key(|u| u.pkg.package_id());
674673

675674
for dep_unit in dependencies.iter() {

src/cargo/core/compiler/job_queue.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -159,26 +159,25 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
159159
unit: &Unit<'a>,
160160
job: Job,
161161
) -> CargoResult<()> {
162-
let dependencies = cx.dep_targets(unit);
162+
let dependencies = cx.unit_deps(unit);
163163
let mut queue_deps = dependencies
164164
.iter()
165-
.cloned()
166-
.filter(|unit| {
165+
.filter(|dep| {
167166
// Binaries aren't actually needed to *compile* tests, just to run
168167
// them, so we don't include this dependency edge in the job graph.
169-
!unit.target.is_test() && !unit.target.is_bin()
168+
!dep.unit.target.is_test() && !dep.unit.target.is_bin()
170169
})
171170
.map(|dep| {
172171
// Handle the case here where our `unit -> dep` dependency may
173172
// only require the metadata, not the full compilation to
174173
// finish. Use the tables in `cx` to figure out what kind
175174
// of artifact is associated with this dependency.
176-
let artifact = if cx.only_requires_rmeta(unit, &dep) {
175+
let artifact = if cx.only_requires_rmeta(unit, &dep.unit) {
177176
Artifact::Metadata
178177
} else {
179178
Artifact::All
180179
};
181-
(dep, artifact)
180+
(dep.unit, artifact)
182181
})
183182
.collect::<HashMap<_, _>>();
184183

@@ -205,17 +204,17 @@ impl<'a, 'cfg> JobQueue<'a, 'cfg> {
205204
// transitively contains the `Metadata` edge.
206205
if unit.requires_upstream_objects() {
207206
for dep in dependencies {
208-
depend_on_deps_of_deps(cx, &mut queue_deps, dep);
207+
depend_on_deps_of_deps(cx, &mut queue_deps, dep.unit);
209208
}
210209

211210
fn depend_on_deps_of_deps<'a>(
212211
cx: &Context<'a, '_>,
213212
deps: &mut HashMap<Unit<'a>, Artifact>,
214213
unit: Unit<'a>,
215214
) {
216-
for dep in cx.dep_targets(&unit) {
217-
if deps.insert(dep, Artifact::All).is_none() {
218-
depend_on_deps_of_deps(cx, deps, dep);
215+
for dep in cx.unit_deps(&unit) {
216+
if deps.insert(dep.unit, Artifact::All).is_none() {
217+
depend_on_deps_of_deps(cx, deps, dep.unit);
219218
}
220219
}
221220
}

src/cargo/core/compiler/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,9 @@ fn compile<'a, 'cfg: 'a>(
157157
drop(p);
158158

159159
// Be sure to compile all dependencies of this target as well.
160-
for unit in cx.dep_targets(unit).iter() {
161-
compile(cx, jobs, plan, unit, exec, false)?;
160+
let deps = Vec::from(cx.unit_deps(unit)); // Create vec due to mutable borrow.
161+
for dep in deps {
162+
compile(cx, jobs, plan, &dep.unit, exec, false)?;
162163
}
163164
if build_plan {
164165
plan.add(cx, unit)?;

src/cargo/core/compiler/output_depinfo.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,11 @@ fn add_deps_for_unit<'a, 'b>(
9090
}
9191

9292
// Recursively traverse all transitive dependencies
93-
for dep_unit in context.dep_targets(unit).iter() {
94-
let source_id = dep_unit.pkg.package_id().source_id();
93+
let unit_deps = Vec::from(context.unit_deps(unit)); // Create vec due to mutable borrow.
94+
for dep in unit_deps {
95+
let source_id = dep.unit.pkg.package_id().source_id();
9596
if source_id.is_path() {
96-
add_deps_for_unit(deps, context, dep_unit, visited)?;
97+
add_deps_for_unit(deps, context, &dep.unit, visited)?;
9798
}
9899
}
99100
Ok(())

0 commit comments

Comments
 (0)