Give better diagnostic if OUT_DIR is unset

This commit is contained in:
Jonas Schievink 2020-12-02 17:03:18 +01:00
parent 17542d08b4
commit 4634bfb332
2 changed files with 20 additions and 9 deletions

View file

@ -100,6 +100,9 @@ fn f() {
env!(invalid); env!(invalid);
//^^^^^^^^^^^^^ could not convert tokens //^^^^^^^^^^^^^ could not convert tokens
env!("OUT_DIR");
//^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix
// Lazy: // Lazy:
format_args!(); format_args!();

View file

@ -417,17 +417,25 @@ fn env_expand(
Err(e) => return ExpandResult::only_err(e), Err(e) => return ExpandResult::only_err(e),
}; };
// FIXME: let mut err = None;
// If the environment variable is not defined int rustc, then a compilation error will be emitted. let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| {
// We might do the same if we fully support all other stuffs. // The only variable rust-analyzer ever sets is `OUT_DIR`, so only diagnose that to avoid
// But for now on, we should return some dummy string for better type infer purpose. // unnecessary diagnostics for eg. `CARGO_PKG_NAME`.
// However, we cannot use an empty string here, because for if key == "OUT_DIR" {
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become err = Some(mbe::ExpandError::Other(
// `include!("foo.rs"), which might go to infinite loop r#"`OUT_DIR` not set, enable "load out dirs from check" to fix"#.into(),
let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| "__RA_UNIMPLEMENTED__".to_string()); ));
}
// If the variable is unset, still return a dummy string to help type inference along.
// We cannot use an empty string here, because for
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
// `include!("foo.rs"), which might go to infinite loop
"__RA_UNIMPLEMENTED__".to_string()
});
let expanded = quote! { #s }; let expanded = quote! { #s };
ExpandResult::ok(Some((expanded, FragmentKind::Expr))) ExpandResult { value: Some((expanded, FragmentKind::Expr)), err }
} }
fn option_env_expand( fn option_env_expand(