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
}
pub fn name(&self) -> Option<ast::Name> {
self.source.value.name()
pub fn name(&self) -> Option<InFile<ast::Name>> {
self.source.as_ref().map(|it| it.name()).transpose()
}
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();
for source in d.local.sources(ctx.sema.db) {
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();
Some(vec![fix(

View file

@ -74,7 +74,7 @@ pub(super) fn hints(
capture.display_place(sema.db)
),
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,
position: InlayHintPosition::After,

View file

@ -24,7 +24,7 @@ use crate::{
rustc_cfg,
sysroot::SysrootCrate,
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.
@ -900,7 +900,24 @@ fn cargo_to_crate_graph(
// https://github.com/rust-lang/rust-analyzer/issues/11300
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(
crate_graph,
@ -909,23 +926,23 @@ fn cargo_to_crate_graph(
build_scripts.get_output(pkg),
cfg_options.clone(),
file_id,
&cargo[tgt].name,
cargo[tgt].is_proc_macro,
name,
is_proc_macro,
target_layout.clone(),
false,
channel,
);
if cargo[tgt].kind == TargetKind::Lib {
lib_tgt = Some((crate_id, cargo[tgt].name.clone()));
if kind == TargetKind::Lib {
lib_tgt = Some((crate_id, name.clone()));
pkg_to_lib_crate.insert(pkg, crate_id);
}
// 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).
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