aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_expand/src/builtin_macro.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_expand/src/builtin_macro.rs')
-rw-r--r--crates/ra_hir_expand/src/builtin_macro.rs38
1 files changed, 16 insertions, 22 deletions
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs
index 3bce8f673..b50eb347c 100644
--- a/crates/ra_hir_expand/src/builtin_macro.rs
+++ b/crates/ra_hir_expand/src/builtin_macro.rs
@@ -1,15 +1,14 @@
1//! Builtin macro 1//! Builtin macro
2use crate::db::AstDatabase;
3use crate::{ 2use crate::{
4 ast::{self, AstToken, HasStringValue}, 3 db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
5 name, AstId, CrateId, MacroDefId, MacroDefKind, TextSize, 4 MacroDefId, MacroDefKind, TextSize,
6}; 5};
7 6
8use crate::{quote, EagerMacroId, LazyMacroId, MacroCallId};
9use either::Either; 7use either::Either;
10use mbe::parse_to_token_tree; 8use mbe::parse_to_token_tree;
11use ra_db::{FileId, RelativePath}; 9use ra_db::FileId;
12use ra_parser::FragmentKind; 10use ra_parser::FragmentKind;
11use ra_syntax::ast::{self, AstToken, HasStringValue};
13 12
14macro_rules! register_builtin { 13macro_rules! register_builtin {
15 ( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => { 14 ( LAZY: $(($name:ident, $kind: ident) => $expand:ident),* , EAGER: $(($e_name:ident, $e_kind: ident) => $e_expand:ident),* ) => {
@@ -295,19 +294,13 @@ fn concat_expand(
295 294
296fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> { 295fn relative_file(db: &dyn AstDatabase, call_id: MacroCallId, path: &str) -> Option<FileId> {
297 let call_site = call_id.as_file().original_file(db); 296 let call_site = call_id.as_file().original_file(db);
298 297 let res = db.resolve_path(call_site, path)?;
299 // Handle trivial case 298 // Prevent include itself
300 if let Some(res) = db.resolve_relative_path(call_site, &RelativePath::new(&path)) { 299 if res == call_site {
301 // Prevent include itself 300 None
302 return if res == call_site { None } else { Some(res) }; 301 } else {
302 Some(res)
303 } 303 }
304
305 // Extern paths ?
306 let krate = *db.relevant_crates(call_site).get(0)?;
307 let (extern_source_id, relative_file) =
308 db.crate_graph()[krate].extern_source.extern_path(path)?;
309
310 db.resolve_extern_path(extern_source_id, &relative_file)
311} 304}
312 305
313fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> { 306fn parse_string(tt: &tt::Subtree) -> Result<String, mbe::ExpandError> {
@@ -339,10 +332,7 @@ fn include_expand(
339} 332}
340 333
341fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> { 334fn get_env_inner(db: &dyn AstDatabase, arg_id: EagerMacroId, key: &str) -> Option<String> {
342 let call_id: MacroCallId = arg_id.into(); 335 let krate = db.lookup_intern_eager_expansion(arg_id).krate;
343 let original_file = call_id.as_file().original_file(db);
344
345 let krate = *db.relevant_crates(original_file).get(0)?;
346 db.crate_graph()[krate].env.get(key) 336 db.crate_graph()[krate].env.get(key)
347} 337}
348 338
@@ -401,6 +391,7 @@ mod tests {
401 391
402 let expander = find_by_name(&macro_calls[0].name().unwrap().as_name()).unwrap(); 392 let expander = find_by_name(&macro_calls[0].name().unwrap().as_name()).unwrap();
403 393
394 let krate = CrateId(0);
404 let file_id = match expander { 395 let file_id = match expander {
405 Either::Left(expander) => { 396 Either::Left(expander) => {
406 // the first one should be a macro_rules 397 // the first one should be a macro_rules
@@ -413,6 +404,7 @@ mod tests {
413 404
414 let loc = MacroCallLoc { 405 let loc = MacroCallLoc {
415 def, 406 def,
407 krate,
416 kind: MacroCallKind::FnLike(AstId::new( 408 kind: MacroCallKind::FnLike(AstId::new(
417 file_id.into(), 409 file_id.into(),
418 ast_id_map.ast_id(&macro_calls[1]), 410 ast_id_map.ast_id(&macro_calls[1]),
@@ -425,7 +417,7 @@ mod tests {
425 Either::Right(expander) => { 417 Either::Right(expander) => {
426 // the first one should be a macro_rules 418 // the first one should be a macro_rules
427 let def = MacroDefId { 419 let def = MacroDefId {
428 krate: Some(CrateId(0)), 420 krate: Some(krate),
429 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))), 421 ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
430 kind: MacroDefKind::BuiltInEager(expander), 422 kind: MacroDefKind::BuiltInEager(expander),
431 local_inner: false, 423 local_inner: false,
@@ -439,6 +431,7 @@ mod tests {
439 def, 431 def,
440 fragment: FragmentKind::Expr, 432 fragment: FragmentKind::Expr,
441 subtree: Arc::new(parsed_args.clone()), 433 subtree: Arc::new(parsed_args.clone()),
434 krate,
442 file_id: file_id.into(), 435 file_id: file_id.into(),
443 } 436 }
444 }); 437 });
@@ -448,6 +441,7 @@ mod tests {
448 def, 441 def,
449 fragment, 442 fragment,
450 subtree: Arc::new(subtree), 443 subtree: Arc::new(subtree),
444 krate,
451 file_id: file_id.into(), 445 file_id: file_id.into(),
452 }; 446 };
453 447