fix: Don't duplicate sysroot crates in rustc workspace

This commit is contained in:
Lukas Wirth 2023-05-31 15:37:35 +02:00
parent 3c862507b9
commit ecb8616870
4 changed files with 30 additions and 12 deletions

View file

@ -2703,8 +2703,8 @@ impl LocalSource {
self.source.file_id self.source.file_id
} }
pub fn name(&self) -> Option<ast::Name> { pub fn name(&self) -> Option<InFile<ast::Name>> {
self.source.value.name() self.source.as_ref().map(|it| it.name()).transpose()
} }
pub fn syntax(&self) -> &SyntaxNode { pub fn syntax(&self) -> &SyntaxNode {

View file

@ -18,7 +18,8 @@ pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Diagno
let use_range = d.span.value.text_range(); let use_range = d.span.value.text_range();
for source in d.local.sources(ctx.sema.db) { for source in d.local.sources(ctx.sema.db) {
let Some(ast) = source.name() else { continue }; let Some(ast) = source.name() else { continue };
edit_builder.insert(ast.syntax().text_range().start(), "mut ".to_string()); // FIXME: macros
edit_builder.insert(ast.value.syntax().text_range().start(), "mut ".to_string());
} }
let edit = edit_builder.finish(); let edit = edit_builder.finish();
Some(vec![fix( Some(vec![fix(

View file

@ -74,7 +74,7 @@ pub(super) fn hints(
capture.display_place(sema.db) capture.display_place(sema.db)
), ),
None, None,
source.name().and_then(|name| sema.original_range_opt(name.syntax())), source.name().and_then(|name| name.syntax().original_file_range_opt(sema.db)),
), ),
text_edit: None, text_edit: None,
position: InlayHintPosition::After, position: InlayHintPosition::After,

View file

@ -24,7 +24,7 @@ use crate::{
rustc_cfg, rustc_cfg,
sysroot::SysrootCrate, sysroot::SysrootCrate,
target_data_layout, utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath, target_data_layout, utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath,
Package, ProjectJson, ProjectManifest, Sysroot, TargetKind, WorkspaceBuildScripts, Package, ProjectJson, ProjectManifest, Sysroot, TargetData, TargetKind, WorkspaceBuildScripts,
}; };
/// A set of cfg-overrides per crate. /// A set of cfg-overrides per crate.
@ -900,7 +900,24 @@ fn cargo_to_crate_graph(
// https://github.com/rust-lang/rust-analyzer/issues/11300 // https://github.com/rust-lang/rust-analyzer/issues/11300
continue; continue;
} }
let Some(file_id) = load(&cargo[tgt].root) else { continue }; let &TargetData { ref name, kind, is_proc_macro, ref root, .. } = &cargo[tgt];
if kind == TargetKind::Lib
&& sysroot.map_or(false, |sysroot| root.starts_with(sysroot.src_root()))
{
if let Some(&(_, crate_id, _)) =
public_deps.deps.iter().find(|(dep_name, ..)| dep_name.as_smol_str() == name)
{
pkg_crates.entry(pkg).or_insert_with(Vec::new).push((crate_id, kind));
lib_tgt = Some((crate_id, name.clone()));
pkg_to_lib_crate.insert(pkg, crate_id);
// sysroot is inside the workspace, prevent the sysroot crates from being duplicated here
continue;
}
}
let Some(file_id) = load(root) else { continue };
let crate_id = add_target_crate_root( let crate_id = add_target_crate_root(
crate_graph, crate_graph,
@ -909,23 +926,23 @@ fn cargo_to_crate_graph(
build_scripts.get_output(pkg), build_scripts.get_output(pkg),
cfg_options.clone(), cfg_options.clone(),
file_id, file_id,
&cargo[tgt].name, name,
cargo[tgt].is_proc_macro, is_proc_macro,
target_layout.clone(), target_layout.clone(),
false, false,
channel, channel,
); );
if cargo[tgt].kind == TargetKind::Lib { if kind == TargetKind::Lib {
lib_tgt = Some((crate_id, cargo[tgt].name.clone())); lib_tgt = Some((crate_id, name.clone()));
pkg_to_lib_crate.insert(pkg, crate_id); pkg_to_lib_crate.insert(pkg, crate_id);
} }
// Even crates that don't set proc-macro = true are allowed to depend on proc_macro // Even crates that don't set proc-macro = true are allowed to depend on proc_macro
// (just none of the APIs work when called outside of a proc macro). // (just none of the APIs work when called outside of a proc macro).
if let Some(proc_macro) = libproc_macro { if let Some(proc_macro) = libproc_macro {
add_proc_macro_dep(crate_graph, crate_id, proc_macro, cargo[tgt].is_proc_macro); add_proc_macro_dep(crate_graph, crate_id, proc_macro, is_proc_macro);
} }
pkg_crates.entry(pkg).or_insert_with(Vec::new).push((crate_id, cargo[tgt].kind)); pkg_crates.entry(pkg).or_insert_with(Vec::new).push((crate_id, kind));
} }
// Set deps to the core, std and to the lib target of the current package // Set deps to the core, std and to the lib target of the current package