diff options
Diffstat (limited to 'crates/ra_hir_expand/src/builtin_macro.rs')
-rw-r--r-- | crates/ra_hir_expand/src/builtin_macro.rs | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index 9fc33e4b1..3f60b1cca 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs | |||
@@ -7,6 +7,7 @@ use crate::{ | |||
7 | 7 | ||
8 | use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId}; | 8 | use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId}; |
9 | use either::Either; | 9 | use either::Either; |
10 | use mbe::parse_to_token_tree; | ||
10 | use ra_db::{FileId, RelativePath}; | 11 | use ra_db::{FileId, RelativePath}; |
11 | use ra_parser::FragmentKind; | 12 | use ra_parser::FragmentKind; |
12 | 13 | ||
@@ -142,7 +143,10 @@ fn env_expand( | |||
142 | _tt: &tt::Subtree, | 143 | _tt: &tt::Subtree, |
143 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 144 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
144 | // dummy implementation for type-checking purposes | 145 | // dummy implementation for type-checking purposes |
145 | let expanded = quote! { "" }; | 146 | // we cannot use an empty string here, because for |
147 | // `include!(concat!(env!("OUT_DIR"), "/foo.rs"))` will become | ||
148 | // `include!("foo.rs"), which maybe infinite loop | ||
149 | let expanded = quote! { "__RA_UNIMPLEMENTATED__" }; | ||
146 | 150 | ||
147 | Ok(expanded) | 151 | Ok(expanded) |
148 | } | 152 | } |
@@ -276,7 +280,12 @@ fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Opti | |||
276 | let call_site = call_id.as_file().original_file(db); | 280 | let call_site = call_id.as_file().original_file(db); |
277 | let path = RelativePath::new(&path); | 281 | let path = RelativePath::new(&path); |
278 | 282 | ||
279 | db.resolve_relative_path(call_site, &path) | 283 | let res = db.resolve_relative_path(call_site, &path)?; |
284 | // Prevent include itself | ||
285 | if res == call_site { | ||
286 | return None; | ||
287 | } | ||
288 | Some(res) | ||
280 | } | 289 | } |
281 | 290 | ||
282 | fn include_expand( | 291 | fn include_expand( |
@@ -298,10 +307,9 @@ fn include_expand( | |||
298 | 307 | ||
299 | // FIXME: | 308 | // FIXME: |
300 | // Handle include as expression | 309 | // Handle include as expression |
301 | let node = | 310 | let res = parse_to_token_tree(&db.file_text(file_id.into())) |
302 | db.parse_or_expand(file_id.into()).ok_or_else(|| mbe::ExpandError::ConversionError)?; | 311 | .ok_or_else(|| mbe::ExpandError::ConversionError)? |
303 | let res = | 312 | .0; |
304 | mbe::syntax_node_to_token_tree(&node).ok_or_else(|| mbe::ExpandError::ConversionError)?.0; | ||
305 | 313 | ||
306 | Ok((res, FragmentKind::Items)) | 314 | Ok((res, FragmentKind::Items)) |
307 | } | 315 | } |
@@ -394,7 +402,7 @@ mod tests { | |||
394 | "#, | 402 | "#, |
395 | ); | 403 | ); |
396 | 404 | ||
397 | assert_eq!(expanded, "\"\""); | 405 | assert_eq!(expanded, "\"__RA_UNIMPLEMENTATED__\""); |
398 | } | 406 | } |
399 | 407 | ||
400 | #[test] | 408 | #[test] |