Skip to content

Commit 7e8eac3

Browse files
committed
Simplify
1 parent a6c067c commit 7e8eac3

File tree

2 files changed

+31
-36
lines changed

2 files changed

+31
-36
lines changed

crates/hir/src/semantics/source_to_def.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ pub(super) struct SourceToDefCtx<'a, 'b> {
115115
}
116116

117117
impl SourceToDefCtx<'_, '_> {
118-
pub(super) fn file_to_def(&mut self, file: FileId) -> SmallVec<[ModuleId; 1]> {
118+
pub(super) fn file_to_def(&self, file: FileId) -> SmallVec<[ModuleId; 1]> {
119119
let _p = profile::span("SourceBinder::to_module_def");
120120
let mut mods = SmallVec::new();
121121
for &crate_id in self.db.relevant_crates(file).iter() {
@@ -130,7 +130,7 @@ impl SourceToDefCtx<'_, '_> {
130130
mods
131131
}
132132

133-
pub(super) fn module_to_def(&mut self, src: InFile<ast::Module>) -> Option<ModuleId> {
133+
pub(super) fn module_to_def(&self, src: InFile<ast::Module>) -> Option<ModuleId> {
134134
let _p = profile::span("module_to_def");
135135
let parent_declaration = src
136136
.syntax()
@@ -151,7 +151,7 @@ impl SourceToDefCtx<'_, '_> {
151151
Some(def_map.module_id(child_id))
152152
}
153153

154-
pub(super) fn source_file_to_def(&mut self, src: InFile<ast::SourceFile>) -> Option<ModuleId> {
154+
pub(super) fn source_file_to_def(&self, src: InFile<ast::SourceFile>) -> Option<ModuleId> {
155155
let _p = profile::span("source_file_to_def");
156156
let file_id = src.file_id.original_file(self.db.upcast());
157157
self.file_to_def(file_id).get(0).copied()

crates/project-model/src/workspace.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::{
2121
cfg_flag::CfgFlag,
2222
rustc_cfg,
2323
sysroot::SysrootCrate,
24-
utf8_stdout, CargoConfig, CargoWorkspace, ManifestPath, ProjectJson, ProjectManifest, Sysroot,
25-
TargetKind, WorkspaceBuildScripts,
24+
utf8_stdout, CargoConfig, CargoWorkspace, ManifestPath, Package, ProjectJson, ProjectManifest,
25+
Sysroot, TargetKind, WorkspaceBuildScripts,
2626
};
2727

2828
/// A set of cfg-overrides per crate.
@@ -315,6 +315,13 @@ impl ProjectWorkspace {
315315
/// The return type contains the path and whether or not
316316
/// the root is a member of the current workspace
317317
pub fn to_roots(&self) -> Vec<PackageRoot> {
318+
let mk_sysroot = |sysroot: Option<&Sysroot>| {
319+
sysroot.map(|sysroot| PackageRoot {
320+
is_local: false,
321+
include: vec![sysroot.src_root().to_path_buf()],
322+
exclude: Vec::new(),
323+
})
324+
};
318325
match self {
319326
ProjectWorkspace::Json { project, sysroot, rustc_cfg: _ } => project
320327
.crates()
@@ -325,13 +332,7 @@ impl ProjectWorkspace {
325332
})
326333
.collect::<FxHashSet<_>>()
327334
.into_iter()
328-
.chain(sysroot.as_ref().into_iter().flat_map(|sysroot| {
329-
sysroot.crates().map(move |krate| PackageRoot {
330-
is_local: false,
331-
include: vec![sysroot[krate].root.parent().to_path_buf()],
332-
exclude: Vec::new(),
333-
})
334-
}))
335+
.chain(mk_sysroot(sysroot.as_ref()))
335336
.collect::<Vec<_>>(),
336337
ProjectWorkspace::Cargo {
337338
cargo,
@@ -380,11 +381,7 @@ impl ProjectWorkspace {
380381
}
381382
PackageRoot { is_local, include, exclude }
382383
})
383-
.chain(sysroot.iter().map(|sysroot| PackageRoot {
384-
is_local: false,
385-
include: vec![sysroot.src_root().to_path_buf()],
386-
exclude: Vec::new(),
387-
}))
384+
.chain(mk_sysroot(sysroot.as_ref()))
388385
.chain(rustc.iter().flat_map(|rustc| {
389386
rustc.packages().map(move |krate| PackageRoot {
390387
is_local: false,
@@ -401,11 +398,7 @@ impl ProjectWorkspace {
401398
include: vec![detached_file.clone()],
402399
exclude: Vec::new(),
403400
})
404-
.chain(sysroot.crates().map(|krate| PackageRoot {
405-
is_local: false,
406-
include: vec![sysroot[krate].root.parent().to_path_buf()],
407-
exclude: Vec::new(),
408-
}))
401+
.chain(mk_sysroot(Some(sysroot)))
409402
.collect(),
410403
}
411404
}
@@ -639,6 +632,8 @@ fn cargo_to_crate_graph(
639632
lib_tgt = Some((crate_id, cargo[tgt].name.clone()));
640633
pkg_to_lib_crate.insert(pkg, crate_id);
641634
}
635+
// Even crates that don't set proc-macro = true are allowed to depend on proc_macro
636+
// (just none of the APIs work when called outside of a proc macro).
642637
if let Some(proc_macro) = libproc_macro {
643638
add_dep_with_prelude(
644639
&mut crate_graph,
@@ -654,19 +649,19 @@ fn cargo_to_crate_graph(
654649
}
655650

656651
// Set deps to the core, std and to the lib target of the current package
657-
for (from, kind) in pkg_crates.get(&pkg).into_iter().flatten() {
652+
for &(from, kind) in pkg_crates.get(&pkg).into_iter().flatten() {
658653
// Add sysroot deps first so that a lib target named `core` etc. can overwrite them.
659-
public_deps.add(*from, &mut crate_graph);
654+
public_deps.add(from, &mut crate_graph);
660655

661656
if let Some((to, name)) = lib_tgt.clone() {
662-
if to != *from && *kind != TargetKind::BuildScript {
657+
if to != from && kind != TargetKind::BuildScript {
663658
// (build script can not depend on its library target)
664659

665660
// For root projects with dashes in their name,
666661
// cargo metadata does not do any normalization,
667662
// so we do it ourselves currently
668663
let name = CrateName::normalize_dashes(&name);
669-
add_dep(&mut crate_graph, *from, name, to);
664+
add_dep(&mut crate_graph, from, name, to);
670665
}
671666
}
672667
}
@@ -678,17 +673,17 @@ fn cargo_to_crate_graph(
678673
for dep in cargo[pkg].dependencies.iter() {
679674
let name = CrateName::new(&dep.name).unwrap();
680675
if let Some(&to) = pkg_to_lib_crate.get(&dep.pkg) {
681-
for (from, kind) in pkg_crates.get(&pkg).into_iter().flatten() {
682-
if dep.kind == DepKind::Build && *kind != TargetKind::BuildScript {
676+
for &(from, kind) in pkg_crates.get(&pkg).into_iter().flatten() {
677+
if dep.kind == DepKind::Build && kind != TargetKind::BuildScript {
683678
// Only build scripts may depend on build dependencies.
684679
continue;
685680
}
686-
if dep.kind != DepKind::Build && *kind == TargetKind::BuildScript {
681+
if dep.kind != DepKind::Build && kind == TargetKind::BuildScript {
687682
// Build scripts may only depend on build dependencies.
688683
continue;
689684
}
690685

691-
add_dep(&mut crate_graph, *from, name.clone(), to)
686+
add_dep(&mut crate_graph, from, name.clone(), to)
692687
}
693688
}
694689
}
@@ -699,9 +694,9 @@ fn cargo_to_crate_graph(
699694
// and create dependencies on them for the crates which opt-in to that
700695
if let Some(rustc_workspace) = rustc {
701696
handle_rustc_crates(
697+
&mut crate_graph,
702698
rustc_workspace,
703699
load,
704-
&mut crate_graph,
705700
&cfg_options,
706701
override_cfg,
707702
load_proc_macro,
@@ -761,16 +756,16 @@ fn detached_files_to_crate_graph(
761756
}
762757

763758
fn handle_rustc_crates(
759+
crate_graph: &mut CrateGraph,
764760
rustc_workspace: &CargoWorkspace,
765761
load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
766-
crate_graph: &mut CrateGraph,
767762
cfg_options: &CfgOptions,
768763
override_cfg: &CfgOverrides,
769764
load_proc_macro: &mut dyn FnMut(&str, &AbsPath) -> ProcMacroLoadResult,
770-
pkg_to_lib_crate: &mut FxHashMap<la_arena::Idx<crate::PackageData>, CrateId>,
765+
pkg_to_lib_crate: &mut FxHashMap<Package, CrateId>,
771766
public_deps: &SysrootPublicDeps,
772767
cargo: &CargoWorkspace,
773-
pkg_crates: &FxHashMap<la_arena::Idx<crate::PackageData>, Vec<(CrateId, TargetKind)>>,
768+
pkg_crates: &FxHashMap<Package, Vec<(CrateId, TargetKind)>>,
774769
build_scripts: &WorkspaceBuildScripts,
775770
) {
776771
let mut rustc_pkg_crates = FxHashMap::default();
@@ -784,8 +779,8 @@ fn handle_rustc_crates(
784779
let mut queue = VecDeque::new();
785780
queue.push_back(root_pkg);
786781
while let Some(pkg) = queue.pop_front() {
787-
// Don't duplicate packages if they are dependended on a diamond pattern
788-
// N.B. if this line is omitted, we try to analyse over 4_800_000 crates
782+
// Don't duplicate packages if they are dependent on a diamond pattern
783+
// N.B. if this line is omitted, we try to analyze over 4_800_000 crates
789784
// which is not ideal
790785
if rustc_pkg_crates.contains_key(&pkg) {
791786
continue;

0 commit comments

Comments
 (0)