Skip to content

Commit e4c404e

Browse files
committed
Auto merge of rust-lang#17973 - Veykril:proc-macro-curr-dir, r=Veykril
Expand proc-macros in workspace root, not package root Should fix rust-lang/rust-analyzer#17748. The approach is generally not perfect though as rust-project.json projects don't benefit from this (still, nothing changes in that regard)
2 parents 6593688 + df4580b commit e4c404e

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::{
@@ -1479,12 +1479,14 @@ fn generic_args_sans_defaults<'ga>(
14791479
}
14801480
// otherwise, if the arg is equal to the param default, hide it (unless the
14811481
// default is an error which can happen for the trait Self type)
1482-
#[allow(unstable_name_collisions)]
1483-
default_parameters.get(i).is_none_or(|default_parameter| {
1484-
// !is_err(default_parameter.skip_binders())
1485-
// &&
1486-
arg != &default_parameter.clone().substitute(Interner, &parameters)
1487-
})
1482+
match default_parameters.get(i) {
1483+
None => true,
1484+
Some(default_parameter) => {
1485+
// !is_err(default_parameter.skip_binders())
1486+
// &&
1487+
arg != &default_parameter.clone().substitute(Interner, &parameters)
1488+
}
1489+
}
14881490
};
14891491
let mut default_from = 0;
14901492
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
@@ -241,6 +241,10 @@ impl TargetKind {
241241
pub fn is_executable(self) -> bool {
242242
matches!(self, TargetKind::Bin | TargetKind::Example)
243243
}
244+
245+
pub fn is_proc_macro(self) -> bool {
246+
matches!(self, TargetKind::Lib { is_proc_macro: true })
247+
}
244248
}
245249

246250
// 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
@@ -278,7 +278,7 @@ fn crate_graph_dedup() {
278278
assert_eq!(regex_crate_graph.iter().count(), 60);
279279

280280
crate_graph.extend(regex_crate_graph, &mut regex_proc_macros, |(_, a), (_, b)| a == b);
281-
assert_eq!(crate_graph.iter().count(), 118);
281+
assert_eq!(crate_graph.iter().count(), 119);
282282
}
283283

284284
#[test]

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1027,6 +1027,7 @@ fn cargo_to_crate_graph(
10271027
let crate_id = add_target_crate_root(
10281028
crate_graph,
10291029
proc_macros,
1030+
cargo,
10301031
pkg_data,
10311032
build_data,
10321033
cfg_options.clone(),
@@ -1246,6 +1247,7 @@ fn handle_rustc_crates(
12461247
let crate_id = add_target_crate_root(
12471248
crate_graph,
12481249
proc_macros,
1250+
rustc_workspace,
12491251
&rustc_workspace[pkg],
12501252
build_scripts.get_output(pkg),
12511253
cfg_options.clone(),
@@ -1305,6 +1307,7 @@ fn handle_rustc_crates(
13051307
fn add_target_crate_root(
13061308
crate_graph: &mut CrateGraph,
13071309
proc_macros: &mut ProcMacroPaths,
1310+
cargo: &CargoWorkspace,
13081311
pkg: &PackageData,
13091312
build_data: Option<&BuildScriptOutput>,
13101313
cfg_options: CfgOptions,
@@ -1338,7 +1341,7 @@ fn add_target_crate_root(
13381341
let mut env = Env::default();
13391342
inject_cargo_package_env(&mut env, pkg);
13401343
inject_cargo_env(&mut env);
1341-
inject_rustc_tool_env(&mut env, cargo_name, kind);
1344+
inject_rustc_tool_env(&mut env, cargo, cargo_name, kind);
13421345

13431346
if let Some(envs) = build_data.map(|it| &it.envs) {
13441347
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
@@ -94,7 +94,7 @@ fn test_deduplicate_origin_dev() {
9494
}
9595
}
9696

97-
assert!(crates_named_p2.len() == 1);
97+
assert_eq!(crates_named_p2.len(), 1);
9898
let p2 = crates_named_p2[0];
9999
assert!(p2.origin.is_local());
100100
}
@@ -120,7 +120,7 @@ fn test_deduplicate_origin_dev_rev() {
120120
}
121121
}
122122

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

0 commit comments

Comments
 (0)