Skip to content

Commit ac60077

Browse files
committed
Auto merge of #13639 - Veykril:macro-diags, r=Veykril
fix: Fix proc-macro-srv search paths for Arch Linux Fixes #13616
2 parents 791cb87 + dc8254c commit ac60077

File tree

7 files changed

+50
-67
lines changed

7 files changed

+50
-67
lines changed

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ use crate::{Diagnostic, DiagnosticsContext};
55
// This diagnostic is shown for macro expansion errors.
66
pub(crate) fn macro_error(ctx: &DiagnosticsContext<'_>, d: &hir::MacroError) -> Diagnostic {
77
// Use more accurate position if available.
8-
let display_range = d
9-
.precise_location
10-
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()).range);
11-
8+
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
129
Diagnostic::new("macro-error", d.message.clone(), display_range).experimental()
1310
}
1411

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ pub(crate) fn unresolved_macro_call(
99
d: &hir::UnresolvedMacroCall,
1010
) -> Diagnostic {
1111
// Use more accurate position if available.
12-
let display_range = d
13-
.precise_location
14-
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.macro_call.clone()).range);
15-
12+
let display_range = ctx.resolve_precise_location(&d.macro_call, d.precise_location);
1613
let bang = if d.is_bang { "!" } else { "" };
1714
Diagnostic::new(
1815
"unresolved-macro-call",

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

+1-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use hir::db::DefDatabase;
2-
use syntax::NodeOrToken;
32

43
use crate::{Diagnostic, DiagnosticsContext, Severity};
54

@@ -19,16 +18,7 @@ pub(crate) fn unresolved_proc_macro(
1918
proc_attr_macros_enabled: bool,
2019
) -> Diagnostic {
2120
// Use more accurate position if available.
22-
let display_range = (|| {
23-
let precise_location = d.precise_location?;
24-
let root = ctx.sema.parse_or_expand(d.node.file_id)?;
25-
match root.covering_element(precise_location) {
26-
NodeOrToken::Node(it) => Some(ctx.sema.original_range(&it)),
27-
NodeOrToken::Token(it) => d.node.with_value(it).original_file_range_opt(ctx.sema.db),
28-
}
29-
})()
30-
.unwrap_or_else(|| ctx.sema.diagnostics_display_range(d.node.clone()))
31-
.range;
21+
let display_range = ctx.resolve_precise_location(&d.node, d.precise_location);
3222

3323
let config_enabled = match d.kind {
3424
hir::MacroKind::Attr => proc_macros_enabled && proc_attr_macros_enabled,

crates/ide-diagnostics/src/lib.rs

+22
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,28 @@ struct DiagnosticsContext<'a> {
182182
resolve: &'a AssistResolveStrategy,
183183
}
184184

185+
impl<'a> DiagnosticsContext<'a> {
186+
fn resolve_precise_location(
187+
&self,
188+
node: &InFile<SyntaxNodePtr>,
189+
precise_location: Option<TextRange>,
190+
) -> TextRange {
191+
let sema = &self.sema;
192+
(|| {
193+
let precise_location = precise_location?;
194+
let root = sema.parse_or_expand(node.file_id)?;
195+
match root.covering_element(precise_location) {
196+
syntax::NodeOrToken::Node(it) => Some(sema.original_range(&it)),
197+
syntax::NodeOrToken::Token(it) => {
198+
node.with_value(it).original_file_range_opt(sema.db)
199+
}
200+
}
201+
})()
202+
.unwrap_or_else(|| sema.diagnostics_display_range(node.clone()))
203+
.range
204+
}
205+
}
206+
185207
pub fn diagnostics(
186208
db: &RootDatabase,
187209
config: &DiagnosticsConfig,

crates/project-model/src/workspace.rs

+15
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,21 @@ impl ProjectWorkspace {
377377
}
378378
}
379379

380+
pub fn find_sysroot_proc_macro_srv(&self) -> Option<AbsPathBuf> {
381+
match self {
382+
ProjectWorkspace::Cargo { sysroot: Some(sysroot), .. }
383+
| ProjectWorkspace::Json { sysroot: Some(sysroot), .. } => {
384+
let standalone_server_name =
385+
format!("rust-analyzer-proc-macro-srv{}", std::env::consts::EXE_SUFFIX);
386+
["libexec", "lib"]
387+
.into_iter()
388+
.map(|segment| sysroot.root().join(segment).join(&standalone_server_name))
389+
.find(|server_path| std::fs::metadata(&server_path).is_ok())
390+
}
391+
_ => None,
392+
}
393+
}
394+
380395
/// Returns the roots for the current `ProjectWorkspace`
381396
/// The return type contains the path and whether or not
382397
/// the root is a member of the current workspace

crates/rust-analyzer/src/cli/load_cargo.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -60,24 +60,12 @@ pub fn load_workspace(
6060
};
6161

6262
let proc_macro_client = if load_config.with_proc_macro {
63-
let mut path = AbsPathBuf::assert(std::env::current_exe()?);
64-
let mut args = vec!["proc-macro"];
65-
66-
if let ProjectWorkspace::Cargo { sysroot, .. } | ProjectWorkspace::Json { sysroot, .. } =
67-
&ws
68-
{
69-
if let Some(sysroot) = sysroot.as_ref() {
70-
let standalone_server_name =
71-
format!("rust-analyzer-proc-macro-srv{}", std::env::consts::EXE_SUFFIX);
72-
let server_path = sysroot.root().join("libexec").join(&standalone_server_name);
73-
if std::fs::metadata(&server_path).is_ok() {
74-
path = server_path;
75-
args = vec![];
76-
}
77-
}
78-
}
63+
let (server_path, args): (_, &[_]) = match ws.find_sysroot_proc_macro_srv() {
64+
Some(server_path) => (server_path, &[]),
65+
None => (AbsPathBuf::assert(std::env::current_exe()?), &["proc-macro"]),
66+
};
7967

80-
ProcMacroServer::spawn(path.clone(), args.clone()).map_err(|e| e.to_string())
68+
ProcMacroServer::spawn(server_path, args).map_err(|e| e.to_string())
8169
} else {
8270
Err("proc macro server disabled".to_owned())
8371
};

crates/rust-analyzer/src/reload.rs

+5-31
Original file line numberDiff line numberDiff line change
@@ -305,50 +305,24 @@ impl GlobalState {
305305
let files_config = self.config.files();
306306
let project_folders = ProjectFolders::new(&self.workspaces, &files_config.exclude);
307307

308-
let standalone_server_name =
309-
format!("rust-analyzer-proc-macro-srv{}", std::env::consts::EXE_SUFFIX);
310-
311308
if self.proc_macro_clients.is_empty() {
312309
if let Some((path, path_manually_set)) = self.config.proc_macro_srv() {
313310
tracing::info!("Spawning proc-macro servers");
314311
self.proc_macro_clients = self
315312
.workspaces
316313
.iter()
317314
.map(|ws| {
318-
let (path, args) = if path_manually_set {
315+
let (path, args): (_, &[_]) = if path_manually_set {
319316
tracing::debug!(
320317
"Pro-macro server path explicitly set: {}",
321318
path.display()
322319
);
323-
(path.clone(), vec![])
320+
(path.clone(), &[])
324321
} else {
325-
let mut sysroot_server = None;
326-
if let ProjectWorkspace::Cargo { sysroot, .. }
327-
| ProjectWorkspace::Json { sysroot, .. } = ws
328-
{
329-
if let Some(sysroot) = sysroot.as_ref() {
330-
let server_path = sysroot
331-
.root()
332-
.join("libexec")
333-
.join(&standalone_server_name);
334-
if std::fs::metadata(&server_path).is_ok() {
335-
tracing::debug!(
336-
"Sysroot proc-macro server exists at {}",
337-
server_path.display()
338-
);
339-
sysroot_server = Some(server_path);
340-
} else {
341-
tracing::debug!(
342-
"Sysroot proc-macro server does not exist at {}",
343-
server_path.display()
344-
);
345-
}
346-
}
322+
match ws.find_sysroot_proc_macro_srv() {
323+
Some(server_path) => (server_path, &[]),
324+
None => (path.clone(), &["proc-macro"]),
347325
}
348-
sysroot_server.map_or_else(
349-
|| (path.clone(), vec!["proc-macro".to_owned()]),
350-
|path| (path, vec![]),
351-
)
352326
};
353327

354328
tracing::info!(?args, "Using proc-macro server at {}", path.display(),);

0 commit comments

Comments
 (0)