Skip to content

Commit ef9bc3e

Browse files
committed
v3
1 parent c3f90bb commit ef9bc3e

File tree

2 files changed

+31
-57
lines changed

2 files changed

+31
-57
lines changed

crates/base-db/src/input.rs

+25-54
Original file line numberDiff line numberDiff line change
@@ -353,20 +353,18 @@ impl CrateData {
353353
return false;
354354
}
355355

356-
let mut opts = self.cfg_options.diff(&other.cfg_options).into_iter();
357-
match opts.len() {
358-
0 => (),
359-
1 => {
360-
// Don't care if rust_analyzer CfgAtom is the only cfg in the difference set of self's and other's cfgs.
361-
// https://github.com/rust-lang/rust-analyzer/blob/0840038f02daec6ba3238f05d8caa037d28701a0/crates/project-model/src/workspace.rs#L894
362-
if let Some(cfg) = opts.next() {
363-
if cfg.to_string() != "rust_analyzer" {
364-
return false;
365-
}
366-
}
356+
let mut opts = self.cfg_options.difference(&other.cfg_options);
357+
if let Some(it) = opts.next() {
358+
// Don't care if rust_analyzer CfgAtom is the only cfg in the difference set of self's and other's cfgs.
359+
// https://github.com/rust-lang/rust-analyzer/blob/0840038f02daec6ba3238f05d8caa037d28701a0/crates/project-model/src/workspace.rs#L894
360+
if it.to_string() != "rust_analyzer" {
361+
return false;
367362
}
368-
_ => return false,
369-
};
363+
364+
if let Some(_) = opts.next() {
365+
return false;
366+
}
367+
}
370368

371369
if self.env != other.env {
372370
return false;
@@ -378,8 +376,8 @@ impl CrateData {
378376
if ignore_dev_deps {
379377
return slf_deps
380378
.clone()
381-
.filter(|it| it.kind == DependencyKind::Normal)
382-
.eq(other_deps.clone().filter(|it| it.kind == DependencyKind::Normal));
379+
.filter(|it| it.kind != DependencyKind::Dev)
380+
.eq(other_deps.clone().filter(|it| it.kind != DependencyKind::Dev));
383381
}
384382

385383
slf_deps.eq(other_deps)
@@ -524,7 +522,7 @@ impl CrateGraph {
524522

525523
self.check_cycle_after_dependency(from, dep.crate_id)?;
526524

527-
self.arena[from].add_dep_unchecked(dep);
525+
self.arena[from].add_dep(dep);
528526
Ok(())
529527
}
530528

@@ -667,7 +665,12 @@ impl CrateGraph {
667665
}
668666
(a @ CrateOrigin::Local { .. }, CrateOrigin::Library { .. })
669667
| (a @ CrateOrigin::Library { .. }, CrateOrigin::Local { .. }) => {
670-
// See #15656 for a relevant example.
668+
// If the origins differ, check if the two crates are equal without
669+
// considering the dev dependencies, if they are, they most likely are in
670+
// different loaded workspaces which may cause issues. We keep the local
671+
// version and discard the library one as the local version may have
672+
// dev-dependencies that we want to keep resolving. See #15656 for more
673+
// information.
671674
if data.eq_ignoring_origin_and_deps(&crate_data, true) {
672675
return Some((id, if a.is_local() { false } else { true }));
673676
}
@@ -681,22 +684,12 @@ impl CrateGraph {
681684
if let Some((res, should_update_lib_to_local)) = res {
682685
id_map.insert(topo, res);
683686
if should_update_lib_to_local {
684-
let origin_old = self.arena[res].origin.clone();
685-
assert!(origin_old.is_lib());
686-
687-
if let CrateOrigin::Library { repo, name } = origin_old {
688-
self.arena[res].origin = CrateOrigin::Local { repo, name: Some(name) };
689-
}
687+
assert!(self.arena[res].origin.is_lib());
688+
assert!(crate_data.origin.is_local());
689+
self.arena[res].origin = crate_data.origin.clone();
690690

691691
// Move local's dev dependencies into the newly-local-formerly-lib crate.
692-
let dev_deps = crate_data
693-
.dependencies
694-
.clone()
695-
.into_iter()
696-
.filter(|dep| dep.kind() == DependencyKind::Dev)
697-
.collect::<Vec<Dependency>>();
698-
699-
self.arena[res].add_dep(dev_deps).unwrap_or_default();
692+
self.arena[res].dependencies = crate_data.dependencies.clone();
700693
}
701694
} else {
702695
let id = self.arena.alloc(crate_data.clone());
@@ -766,34 +759,12 @@ impl ops::Index<CrateId> for CrateGraph {
766759
}
767760
}
768761

769-
struct ExistingDepsError(Vec<Dependency>);
770-
771762
impl CrateData {
772763
/// Add a dependency to `self` without checking if the dependency
773764
// is existent among `self.dependencies`.
774-
fn add_dep_unchecked(&mut self, dep: Dependency) {
765+
fn add_dep(&mut self, dep: Dependency) {
775766
self.dependencies.push(dep)
776767
}
777-
778-
/// Add `deps` to `self` if the dependency is not already listed.
779-
/// Finally returning an `Err` propagating the dependencies it couldn't add.
780-
fn add_dep(&mut self, deps: Vec<Dependency>) -> Result<(), ExistingDepsError> {
781-
let mut existing_deps: Vec<Dependency> = vec![];
782-
783-
deps.into_iter().for_each(|dep| {
784-
if !self.dependencies.contains(&dep) {
785-
self.dependencies.push(dep);
786-
} else {
787-
existing_deps.push(dep);
788-
}
789-
});
790-
791-
if !existing_deps.is_empty() {
792-
return Err(ExistingDepsError(existing_deps));
793-
}
794-
795-
Ok(())
796-
}
797768
}
798769

799770
impl FromStr for Edition {

crates/cfg/src/lib.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ mod dnf;
77
#[cfg(test)]
88
mod tests;
99

10-
use std::{collections::HashSet, fmt};
10+
use std::fmt;
1111

1212
use rustc_hash::FxHashSet;
1313
use tt::SmolStr;
@@ -58,8 +58,11 @@ impl CfgOptions {
5858
self.enabled.insert(CfgAtom::KeyValue { key, value });
5959
}
6060

61-
pub fn diff<'a>(&'a self, other: &'a CfgOptions) -> HashSet<&CfgAtom> {
62-
self.enabled.difference(&other.enabled).collect()
61+
pub fn difference<'a>(
62+
&'a self,
63+
other: &'a CfgOptions,
64+
) -> impl Iterator<Item = &'a CfgAtom> + 'a {
65+
self.enabled.difference(&other.enabled)
6366
}
6467

6568
pub fn apply_diff(&mut self, diff: CfgDiff) {

0 commit comments

Comments
 (0)