Skip to content

Commit df4580b

Browse files
committed
Expand proc-macros in workspace root, not package root
1 parent 39cc5b6 commit df4580b

File tree

7 files changed

+34
-14
lines changed

7 files changed

+34
-14
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use rustc_apfloat::{
3434
};
3535
use smallvec::SmallVec;
3636
use span::Edition;
37-
use stdx::{never, IsNoneOr};
37+
use stdx::never;
3838
use triomphe::Arc;
3939

4040
use crate::{
@@ -1489,12 +1489,14 @@ fn generic_args_sans_defaults<'ga>(
14891489
}
14901490
// otherwise, if the arg is equal to the param default, hide it (unless the
14911491
// default is an error which can happen for the trait Self type)
1492-
#[allow(unstable_name_collisions)]
1493-
default_parameters.get(i).is_none_or(|default_parameter| {
1494-
// !is_err(default_parameter.skip_binders())
1495-
// &&
1496-
arg != &default_parameter.clone().substitute(Interner, &parameters)
1497-
})
1492+
match default_parameters.get(i) {
1493+
None => true,
1494+
Some(default_parameter) => {
1495+
// !is_err(default_parameter.skip_binders())
1496+
// &&
1497+
arg != &default_parameter.clone().substitute(Interner, &parameters)
1498+
}
1499+
}
14981500
};
14991501
let mut default_from = 0;
15001502
for (i, parameter) in parameters.iter().enumerate() {

src/tools/rust-analyzer/crates/proc-macro-api/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ impl ProcMacro {
154154
mixed_site: Span,
155155
) -> Result<Result<tt::Subtree<Span>, PanicMessage>, ServerError> {
156156
let version = self.process.version();
157-
let current_dir = env.get("CARGO_MANIFEST_DIR");
157+
let current_dir =
158+
env.get("CARGO_RUSTC_CURRENT_DIR").or_else(|| env.get("CARGO_MANIFEST_DIR"));
158159

159160
let mut span_data_table = SpanDataIndexMap::default();
160161
let def_site = span_data_table.insert_full(def_site).0;

src/tools/rust-analyzer/crates/project-model/src/cargo_workspace.rs

+4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ impl TargetKind {
243243
pub fn is_executable(self) -> bool {
244244
matches!(self, TargetKind::Bin | TargetKind::Example)
245245
}
246+
247+
pub fn is_proc_macro(self) -> bool {
248+
matches!(self, TargetKind::Lib { is_proc_macro: true })
249+
}
246250
}
247251

248252
// Deserialize helper for the cargo metadata

src/tools/rust-analyzer/crates/project-model/src/env.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use base_db::Env;
33
use rustc_hash::FxHashMap;
44
use toolchain::Tool;
55

6-
use crate::{utf8_stdout, ManifestPath, PackageData, Sysroot, TargetKind};
6+
use crate::{utf8_stdout, CargoWorkspace, ManifestPath, PackageData, Sysroot, TargetKind};
77

88
/// Recreates the compile-time environment variables that Cargo sets.
99
///
@@ -50,13 +50,23 @@ pub(crate) fn inject_cargo_env(env: &mut Env) {
5050
env.set("CARGO", Tool::Cargo.path().to_string());
5151
}
5252

53-
pub(crate) fn inject_rustc_tool_env(env: &mut Env, cargo_name: &str, kind: TargetKind) {
53+
pub(crate) fn inject_rustc_tool_env(
54+
env: &mut Env,
55+
cargo: &CargoWorkspace,
56+
cargo_name: &str,
57+
kind: TargetKind,
58+
) {
5459
_ = kind;
5560
// FIXME
5661
// if kind.is_executable() {
5762
// env.set("CARGO_BIN_NAME", cargo_name);
5863
// }
5964
env.set("CARGO_CRATE_NAME", cargo_name.replace('-', "_"));
65+
// NOTE: Technically we should set this for all crates, but that will worsen the deduplication
66+
// logic so for now just keeping it proc-macros ought to be fine.
67+
if kind.is_proc_macro() {
68+
env.set("CARGO_RUSTC_CURRENT_DIR", cargo.manifest_path().to_string());
69+
}
6070
}
6171

6272
pub(crate) fn cargo_config_env(

src/tools/rust-analyzer/crates/project-model/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ fn crate_graph_dedup() {
270270
assert_eq!(regex_crate_graph.iter().count(), 60);
271271

272272
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros, |(_, a), (_, b)| a == b);
273-
assert_eq!(crate_graph.iter().count(), 118);
273+
assert_eq!(crate_graph.iter().count(), 119);
274274
}
275275

276276
#[test]

src/tools/rust-analyzer/crates/project-model/src/workspace.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1020,6 +1020,7 @@ fn cargo_to_crate_graph(
10201020
let crate_id = add_target_crate_root(
10211021
crate_graph,
10221022
proc_macros,
1023+
cargo,
10231024
pkg_data,
10241025
build_data,
10251026
cfg_options.clone(),
@@ -1239,6 +1240,7 @@ fn handle_rustc_crates(
12391240
let crate_id = add_target_crate_root(
12401241
crate_graph,
12411242
proc_macros,
1243+
rustc_workspace,
12421244
&rustc_workspace[pkg],
12431245
build_scripts.get_output(pkg),
12441246
cfg_options.clone(),
@@ -1298,6 +1300,7 @@ fn handle_rustc_crates(
12981300
fn add_target_crate_root(
12991301
crate_graph: &mut CrateGraph,
13001302
proc_macros: &mut ProcMacroPaths,
1303+
cargo: &CargoWorkspace,
13011304
pkg: &PackageData,
13021305
build_data: Option<&BuildScriptOutput>,
13031306
cfg_options: CfgOptions,
@@ -1331,7 +1334,7 @@ fn add_target_crate_root(
13311334
let mut env = Env::default();
13321335
inject_cargo_package_env(&mut env, pkg);
13331336
inject_cargo_env(&mut env);
1334-
inject_rustc_tool_env(&mut env, cargo_name, kind);
1337+
inject_rustc_tool_env(&mut env, cargo, cargo_name, kind);
13351338

13361339
if let Some(envs) = build_data.map(|it| &it.envs) {
13371340
for (k, v) in envs {

src/tools/rust-analyzer/crates/rust-analyzer/tests/crate_graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ fn test_deduplicate_origin_dev() {
9393
}
9494
}
9595

96-
assert!(crates_named_p2.len() == 1);
96+
assert_eq!(crates_named_p2.len(), 1);
9797
let p2 = crates_named_p2[0];
9898
assert!(p2.origin.is_local());
9999
}
@@ -119,7 +119,7 @@ fn test_deduplicate_origin_dev_rev() {
119119
}
120120
}
121121

122-
assert!(crates_named_p2.len() == 1);
122+
assert_eq!(crates_named_p2.len(), 1);
123123
let p2 = crates_named_p2[0];
124124
assert!(p2.origin.is_local());
125125
}

0 commit comments

Comments
 (0)