Skip to content

Commit 4634bfb

Browse files
Give better diagnostic if OUT_DIR is unset
1 parent 17542d0 commit 4634bfb

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

crates/hir_def/src/body/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ fn f() {
100100
env!(invalid);
101101
//^^^^^^^^^^^^^ could not convert tokens
102102
103+
env!("OUT_DIR");
104+
//^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix
105+
103106
// Lazy:
104107
105108
format_args!();

crates/hir_expand/src/builtin_macro.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,25 @@ fn env_expand(
417417
Err(e) => return ExpandResult::only_err(e),
418418
};
419419

420-
// FIXME:
421-
// If the environment variable is not defined int rustc, then a compilation error will be emitted.
422-
// We might do the same if we fully support all other stuffs.
423-
// But for now on, we should return some dummy string for better type infer purpose.
424-
// However, we cannot use an empty string here, because for
425-
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
426-
// `include!("foo.rs"), which might go to infinite loop
427-
let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| "__RA_UNIMPLEMENTED__".to_string());
420+
let mut err = None;
421+
let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| {
422+
// The only variable rust-analyzer ever sets is `OUT_DIR`, so only diagnose that to avoid
423+
// unnecessary diagnostics for eg. `CARGO_PKG_NAME`.
424+
if key == "OUT_DIR" {
425+
err = Some(mbe::ExpandError::Other(
426+
r#"`OUT_DIR` not set, enable "load out dirs from check" to fix"#.into(),
427+
));
428+
}
429+
430+
// If the variable is unset, still return a dummy string to help type inference along.
431+
// We cannot use an empty string here, because for
432+
// `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become
433+
// `include!("foo.rs"), which might go to infinite loop
434+
"__RA_UNIMPLEMENTED__".to_string()
435+
});
428436
let expanded = quote! { #s };
429437

430-
ExpandResult::ok(Some((expanded, FragmentKind::Expr)))
438+
ExpandResult { value: Some((expanded, FragmentKind::Expr)), err }
431439
}
432440

433441
fn option_env_expand(

0 commit comments

Comments
 (0)