Skip to content

Commit 58dde89

Browse files
committed
Avoid lossy OsString conversions
1 parent 5d97667 commit 58dde89

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

crates/ra_hir_expand/src/builtin_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ fn env_expand(
358358
// However, we cannot use an empty string here, because for
359359
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
360360
// `include!("foo.rs"), which might go to infinite loop
361-
let s = get_env_inner(db, arg_id, &key).unwrap_or("__RA_UNIMPLEMENTATED__".to_string());
361+
let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| "__RA_UNIMPLEMENTATED__".to_string());
362362
let expanded = quote! { #s };
363363

364364
Ok((expanded, FragmentKind::Expr))

crates/ra_project_model/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,10 @@ impl ProjectWorkspace {
285285
let mut env = Env::default();
286286
let mut extern_source = ExternSource::default();
287287
if let Some(out_dir) = &krate.out_dir {
288-
// FIXME: We probably mangle non UTF-8 paths here, figure out a better solution
289-
env.set("OUT_DIR", out_dir.to_string_lossy().to_string());
288+
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
289+
if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
290+
env.set("OUT_DIR", out_dir);
291+
}
290292
if let Some(&extern_source_id) = extern_source_roots.get(out_dir) {
291293
extern_source.set_extern_path(&out_dir, extern_source_id);
292294
}
@@ -402,8 +404,10 @@ impl ProjectWorkspace {
402404
let mut env = Env::default();
403405
let mut extern_source = ExternSource::default();
404406
if let Some(out_dir) = &cargo[pkg].out_dir {
405-
// FIXME: We probably mangle non UTF-8 paths here, figure out a better solution
406-
env.set("OUT_DIR", out_dir.to_string_lossy().to_string());
407+
// NOTE: cargo and rustc seem to hide non-UTF-8 strings from env! and option_env!()
408+
if let Some(out_dir) = out_dir.to_str().map(|s| s.to_owned()) {
409+
env.set("OUT_DIR", out_dir);
410+
}
407411
if let Some(&extern_source_id) = extern_source_roots.get(out_dir) {
408412
extern_source.set_extern_path(&out_dir, extern_source_id);
409413
}

crates/rust-analyzer/src/main_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ fn on_diagnostic_task(task: DiagnosticTask, msg_sender: &Sender<Message>, state:
696696
let uri = match url_from_path_with_drive_lowercasing(&path) {
697697
Ok(uri) => uri,
698698
Err(err) => {
699-
log::error!("Couldn't convert path to url ({}): {:?}", err, path.to_string_lossy());
699+
log::error!("Couldn't convert path to url ({}): {}", err, path.display());
700700
continue;
701701
}
702702
};

crates/rust-analyzer/src/main_loop/handlers.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ pub fn handle_runnables(
384384
args: check_args,
385385
extra_args: Vec::new(),
386386
env: FxHashMap::default(),
387-
cwd: workspace_root.map(|root| root.to_string_lossy().to_string()),
387+
cwd: workspace_root.map(|root| root.to_owned()),
388388
});
389389
Ok(res)
390390
}
@@ -984,7 +984,7 @@ fn to_lsp_runnable(
984984
m.insert("RUST_BACKTRACE".to_string(), "short".to_string());
985985
m
986986
},
987-
cwd: world.workspace_root_for(file_id).map(|root| root.to_string_lossy().to_string()),
987+
cwd: world.workspace_root_for(file_id).map(|root| root.to_owned()),
988988
})
989989
}
990990

crates/rust-analyzer/src/req.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub use lsp_types::{
1717
SignatureHelp, SymbolKind, TextDocumentEdit, TextDocumentPositionParams, TextEdit,
1818
WorkDoneProgressParams, WorkspaceEdit, WorkspaceSymbolParams,
1919
};
20+
use std::path::PathBuf;
2021

2122
pub enum AnalyzerStatus {}
2223

@@ -141,7 +142,7 @@ pub struct Runnable {
141142
pub args: Vec<String>,
142143
pub extra_args: Vec<String>,
143144
pub env: FxHashMap<String, String>,
144-
pub cwd: Option<String>,
145+
pub cwd: Option<PathBuf>,
145146
}
146147

147148
#[derive(Deserialize, Serialize, Debug)]

0 commit comments

Comments
 (0)