diff options
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 3 | ||||
-rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 26 |
2 files changed, 20 insertions, 9 deletions
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index f2b57aebe..c7003f2a6 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -100,6 +100,9 @@ fn f() { | |||
100 | env!(invalid); | 100 | env!(invalid); |
101 | //^^^^^^^^^^^^^ could not convert tokens | 101 | //^^^^^^^^^^^^^ could not convert tokens |
102 | 102 | ||
103 | env!("OUT_DIR"); | ||
104 | //^^^^^^^^^^^^^^^ `OUT_DIR` not set, enable "load out dirs from check" to fix | ||
105 | |||
103 | // Lazy: | 106 | // Lazy: |
104 | 107 | ||
105 | format_args!(); | 108 | format_args!(); |
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 7f4db106d..7bb42be6c 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -417,17 +417,25 @@ fn env_expand( | |||
417 | Err(e) => return ExpandResult::only_err(e), | 417 | Err(e) => return ExpandResult::only_err(e), |
418 | }; | 418 | }; |
419 | 419 | ||
420 | // FIXME: | 420 | let mut err = None; |
421 | // If the environment variable is not defined int rustc, then a compilation error will be emitted. | 421 | let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| { |
422 | // We might do the same if we fully support all other stuffs. | 422 | // The only variable rust-analyzer ever sets is `OUT_DIR`, so only diagnose that to avoid |
423 | // But for now on, we should return some dummy string for better type infer purpose. | 423 | // unnecessary diagnostics for eg. `CARGO_PKG_NAME`. |
424 | // However, we cannot use an empty string here, because for | 424 | if key == "OUT_DIR" { |
425 | // `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become | 425 | err = Some(mbe::ExpandError::Other( |
426 | // `include!("foo.rs"), which might go to infinite loop | 426 | r#"`OUT_DIR` not set, enable "load out dirs from check" to fix"#.into(), |
427 | let s = get_env_inner(db, arg_id, &key).unwrap_or_else(|| "__RA_UNIMPLEMENTED__".to_string()); | 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 | }); | ||
428 | let expanded = quote! { #s }; | 436 | let expanded = quote! { #s }; |
429 | 437 | ||
430 | ExpandResult::ok(Some((expanded, FragmentKind::Expr))) | 438 | ExpandResult { value: Some((expanded, FragmentKind::Expr)), err } |
431 | } | 439 | } |
432 | 440 | ||
433 | fn option_env_expand( | 441 | fn option_env_expand( |