Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bafa6c4

Browse files
committed
Auto merge of rust-lang#14935 - Veykril:sysroot-dedup, r=Veykril
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.
2 parents 526507f + ecb8616 commit bafa6c4

File tree

4 files changed

+30
-12
lines changed

4 files changed

+30
-12
lines changed

crates/hir/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2703,8 +2703,8 @@ impl LocalSource {
27032703
self.source.file_id
27042704
}
27052705

2706-
pub fn name(&self) -> Option<ast::Name> {
2707-
self.source.value.name()
2706+
pub fn name(&self) -> Option<InFile<ast::Name>> {
2707+
self.source.as_ref().map(|it| it.name()).transpose()
27082708
}
27092709

27102710
pub fn syntax(&self) -> &SyntaxNode {

crates/ide-diagnostics/src/handlers/mutability_errors.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ pub(crate) fn need_mut(ctx: &DiagnosticsContext<'_>, d: &hir::NeedMut) -> Diagno
1818
let use_range = d.span.value.text_range();
1919
for source in d.local.sources(ctx.sema.db) {
2020
let Some(ast) = source.name() else { continue };
21-
edit_builder.insert(ast.syntax().text_range().start(), "mut ".to_string());
21+
// FIXME: macros
22+
edit_builder.insert(ast.value.syntax().text_range().start(), "mut ".to_string());
2223
}
2324
let edit = edit_builder.finish();
2425
Some(vec![fix(

crates/ide/src/inlay_hints/closure_captures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(super) fn hints(
7474
capture.display_place(sema.db)
7575
),
7676
None,
77-
source.name().and_then(|name| sema.original_range_opt(name.syntax())),
77+
source.name().and_then(|name| name.syntax().original_file_range_opt(sema.db)),
7878
),
7979
text_edit: None,
8080
position: InlayHintPosition::After,

crates/project-model/src/workspace.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
rustc_cfg,
2525
sysroot::SysrootCrate,
2626
target_data_layout, utf8_stdout, CargoConfig, CargoWorkspace, InvocationStrategy, ManifestPath,
27-
Package, ProjectJson, ProjectManifest, Sysroot, TargetKind, WorkspaceBuildScripts,
27+
Package, ProjectJson, ProjectManifest, Sysroot, TargetData, TargetKind, WorkspaceBuildScripts,
2828
};
2929

3030
/// A set of cfg-overrides per crate.
@@ -900,7 +900,24 @@ fn cargo_to_crate_graph(
900900
// https://github.com/rust-lang/rust-analyzer/issues/11300
901901
continue;
902902
}
903-
let Some(file_id) = load(&cargo[tgt].root) else { continue };
903+
let &TargetData { ref name, kind, is_proc_macro, ref root, .. } = &cargo[tgt];
904+
905+
if kind == TargetKind::Lib
906+
&& sysroot.map_or(false, |sysroot| root.starts_with(sysroot.src_root()))
907+
{
908+
if let Some(&(_, crate_id, _)) =
909+
public_deps.deps.iter().find(|(dep_name, ..)| dep_name.as_smol_str() == name)
910+
{
911+
pkg_crates.entry(pkg).or_insert_with(Vec::new).push((crate_id, kind));
912+
913+
lib_tgt = Some((crate_id, name.clone()));
914+
pkg_to_lib_crate.insert(pkg, crate_id);
915+
// sysroot is inside the workspace, prevent the sysroot crates from being duplicated here
916+
continue;
917+
}
918+
}
919+
920+
let Some(file_id) = load(root) else { continue };
904921

905922
let crate_id = add_target_crate_root(
906923
crate_graph,
@@ -909,23 +926,23 @@ fn cargo_to_crate_graph(
909926
build_scripts.get_output(pkg),
910927
cfg_options.clone(),
911928
file_id,
912-
&cargo[tgt].name,
913-
cargo[tgt].is_proc_macro,
929+
name,
930+
is_proc_macro,
914931
target_layout.clone(),
915932
false,
916933
channel,
917934
);
918-
if cargo[tgt].kind == TargetKind::Lib {
919-
lib_tgt = Some((crate_id, cargo[tgt].name.clone()));
935+
if kind == TargetKind::Lib {
936+
lib_tgt = Some((crate_id, name.clone()));
920937
pkg_to_lib_crate.insert(pkg, crate_id);
921938
}
922939
// Even crates that don't set proc-macro = true are allowed to depend on proc_macro
923940
// (just none of the APIs work when called outside of a proc macro).
924941
if let Some(proc_macro) = libproc_macro {
925-
add_proc_macro_dep(crate_graph, crate_id, proc_macro, cargo[tgt].is_proc_macro);
942+
add_proc_macro_dep(crate_graph, crate_id, proc_macro, is_proc_macro);
926943
}
927944

928-
pkg_crates.entry(pkg).or_insert_with(Vec::new).push((crate_id, cargo[tgt].kind));
945+
pkg_crates.entry(pkg).or_insert_with(Vec::new).push((crate_id, kind));
929946
}
930947

931948
// Set deps to the core, std and to the lib target of the current package

0 commit comments

Comments
 (0)