Skip to content

Commit

Permalink
Auto merge of #14935 - Veykril:sysroot-dedup, r=Veykril
Browse files Browse the repository at this point in the history
fix: Don't duplicate sysroot crates in rustc workspace

Since we handle `library` as the sysroot source directly in the rustc workspace, we now duplicate the crates there, once as sysroot and once as just plain workspace crate. This causes a variety of issues for `vec!` macros and similar that emit `$crate` tokens across crates.
  • Loading branch information
bors committed May 31, 2023
2 parents 526507f + ecb8616 commit bafa6c4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
4 changes: 2 additions & 2 deletions crates/hir/src/lib.rs
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion crates/ide-diagnostics/src/handlers/mutability_errors.rs
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion crates/ide/src/inlay_hints/closure_captures.rs
Expand Up @@ -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,
Expand Down
33 changes: 25 additions & 8 deletions crates/project-model/src/workspace.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit bafa6c4

Please sign in to comment.