Repository: rust-analyzer/rust-analyzer
Pull request #4102 (2020-04-23 16:51:40)

Cleanup proc_macro config
In general, there should be no reason to call `.to_string_lossy`.
If you want to display the path, use `.display()`.
If you want to pass the path to an OS API (like std::process::Command)
than use `PathBuf` or `OsString`.



bors r+
🤖

matklad: cc @edwin0cheng 

Patch:
diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs
index fa0b1e226b..642c345740 100644
--- a/crates/rust-analyzer/src/config.rs
+++ b/crates/rust-analyzer/src/config.rs
@@ -7,6 +7,8 @@
 //! configure the server itself, feature flags are passed into analysis, and
 //! tweak things like automatic insertion of `()` in completions.
 
+use std::{ffi::OsString, path::PathBuf};
+
 use lsp_types::TextDocumentClientCapabilities;
 use ra_flycheck::FlycheckConfig;
 use ra_ide::{CompletionConfig, InlayHintsConfig};
@@ -20,7 +22,7 @@ pub struct Config {
     pub with_sysroot: bool,
     pub publish_diagnostics: bool,
     pub lru_capacity: Option<usize>,
-    pub proc_macro_srv: Option<(String, Vec<String>)>,
+    pub proc_macro_srv: Option<(PathBuf, Vec<OsString>)>,
     pub files: FilesConfig,
     pub notifications: NotificationsConfig,
 
@@ -135,7 +137,7 @@ impl Config {
         match get(value, "/procMacro/enable") {
             Some(true) => {
                 if let Ok(path) = std::env::current_exe() {
-                    self.proc_macro_srv = Some((path.to_string_lossy().to_string(), vec!["proc-macro".to_string()]));
+                    self.proc_macro_srv = Some((path, vec!["proc-macro".into()]));
                 }
             }
             _ => self.proc_macro_srv = None,
diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs
index 7c0bb42aa5..34941931b7 100644
--- a/crates/rust-analyzer/src/world.rs
+++ b/crates/rust-analyzer/src/world.rs
@@ -153,7 +153,7 @@ impl WorldState {
                 Err(err) => {
                     log::error!(
                         "Failed to run ra_proc_macro_srv from path {}, error: {:?}",
-                        path,
+                        path.display(),
                         err
                     );
                     ProcMacroClient::dummy()
diff --git a/xtask/src/install.rs b/xtask/src/install.rs
index 9ba77a3aae..bc32a04b30 100644
--- a/xtask/src/install.rs
+++ b/xtask/src/install.rs
@@ -7,7 +7,7 @@ use anyhow::{bail, format_err, Context, Result};
 use crate::not_bash::{pushd, run};
 
 // Latest stable, feel free to send a PR if this lags behind.
-const REQUIRED_RUST_VERSION: u32 = 43;
+const REQUIRED_RUST_VERSION: u32 = 41;
 
 pub struct InstallCmd {
     pub client: Option<ClientOpt>,


Test patch:
diff --git a/crates/rust-analyzer/tests/heavy_tests/main.rs b/crates/rust-analyzer/tests/heavy_tests/main.rs
index 1dd2676b6e..b31533e5ef 100644
--- a/crates/rust-analyzer/tests/heavy_tests/main.rs
+++ b/crates/rust-analyzer/tests/heavy_tests/main.rs
@@ -1,6 +1,6 @@
 mod support;
 
-use std::{collections::HashMap, time::Instant};
+use std::{collections::HashMap, path::PathBuf, time::Instant};
 
 use lsp_types::{
     CodeActionContext, DidOpenTextDocumentParams, DocumentFormattingParams, FormattingOptions,
@@ -692,15 +692,10 @@ pub fn foo(_input: TokenStream) -> TokenStream {
 "###,
     )
     .with_config(|config| {
-        // FIXME: Use env!("CARGO_BIN_EXE_ra-analyzer") instead after
-        // https://github.com/rust-lang/cargo/pull/7697 landed
-        let macro_srv_path = std::path::Path::new(std::env!("CARGO_MANIFEST_DIR"))
-            .join("../../target/debug/rust-analyzer")
-            .to_string_lossy()
-            .to_string();
+        let macro_srv_path = PathBuf::from(env!("CARGO_BIN_EXE_rust-analyzer"));
 
         config.cargo.load_out_dirs_from_check = true;
-        config.proc_macro_srv = Some((macro_srv_path, vec!["proc-macro".to_string()]));
+        config.proc_macro_srv = Some((macro_srv_path, vec!["proc-macro".into()]));
     })
     .root("foo")
     .root("bar")