diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/hir_def/src/body/tests.rs | 2 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/collector.rs | 34 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests.rs | 19 | ||||
-rw-r--r-- | crates/hir_def/src/nameres/tests/diagnostics.rs | 2 | ||||
-rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 21 |
5 files changed, 50 insertions, 28 deletions
diff --git a/crates/hir_def/src/body/tests.rs b/crates/hir_def/src/body/tests.rs index f8e6f70e8..faa133297 100644 --- a/crates/hir_def/src/body/tests.rs +++ b/crates/hir_def/src/body/tests.rs | |||
@@ -137,7 +137,7 @@ fn f() { | |||
137 | include!(invalid); | 137 | include!(invalid); |
138 | //^^^^^^^^^^^^^^^^^ could not convert tokens | 138 | //^^^^^^^^^^^^^^^^^ could not convert tokens |
139 | include!("does not exist"); | 139 | include!("does not exist"); |
140 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ could not convert tokens | 140 | //^^^^^^^^^^^^^^^^^^^^^^^^^^ failed to load file `does not exist` |
141 | 141 | ||
142 | env!(invalid); | 142 | env!(invalid); |
143 | //^^^^^^^^^^^^^ could not convert tokens | 143 | //^^^^^^^^^^^^^ could not convert tokens |
diff --git a/crates/hir_def/src/nameres/collector.rs b/crates/hir_def/src/nameres/collector.rs index d73f895b7..b5ae5a9e4 100644 --- a/crates/hir_def/src/nameres/collector.rs +++ b/crates/hir_def/src/nameres/collector.rs | |||
@@ -656,26 +656,28 @@ impl DefCollector<'_> { | |||
656 | } | 656 | } |
657 | } | 657 | } |
658 | } else { | 658 | } else { |
659 | match import.path.segments().last() { | 659 | let name = match &import.alias { |
660 | Some(last_segment) => { | 660 | Some(ImportAlias::Alias(name)) => Some(name.clone()), |
661 | let name = match &import.alias { | 661 | Some(ImportAlias::Underscore) => None, |
662 | Some(ImportAlias::Alias(name)) => Some(name.clone()), | 662 | None => match import.path.segments().last() { |
663 | Some(ImportAlias::Underscore) => None, | 663 | Some(last_segment) => Some(last_segment.clone()), |
664 | None => Some(last_segment.clone()), | 664 | None => { |
665 | }; | 665 | cov_mark::hit!(bogus_paths); |
666 | log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); | 666 | return; |
667 | |||
668 | // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658 | ||
669 | if import.is_extern_crate && module_id == self.def_map.root { | ||
670 | if let (Some(def), Some(name)) = (def.take_types(), name.as_ref()) { | ||
671 | self.def_map.extern_prelude.insert(name.clone(), def); | ||
672 | } | ||
673 | } | 667 | } |
668 | }, | ||
669 | }; | ||
670 | |||
671 | log::debug!("resolved import {:?} ({:?}) to {:?}", name, import, def); | ||
674 | 672 | ||
675 | self.update(module_id, &[(name, def)], vis, ImportType::Named); | 673 | // extern crates in the crate root are special-cased to insert entries into the extern prelude: rust-lang/rust#54658 |
674 | if import.is_extern_crate && module_id == self.def_map.root { | ||
675 | if let (Some(def), Some(name)) = (def.take_types(), name.as_ref()) { | ||
676 | self.def_map.extern_prelude.insert(name.clone(), def); | ||
676 | } | 677 | } |
677 | None => cov_mark::hit!(bogus_paths), | ||
678 | } | 678 | } |
679 | |||
680 | self.update(module_id, &[(name, def)], vis, ImportType::Named); | ||
679 | } | 681 | } |
680 | } | 682 | } |
681 | 683 | ||
diff --git a/crates/hir_def/src/nameres/tests.rs b/crates/hir_def/src/nameres/tests.rs index de3aa4f9a..4f2e7a2f9 100644 --- a/crates/hir_def/src/nameres/tests.rs +++ b/crates/hir_def/src/nameres/tests.rs | |||
@@ -713,3 +713,22 @@ pub fn f() {} | |||
713 | "#]], | 713 | "#]], |
714 | ); | 714 | ); |
715 | } | 715 | } |
716 | |||
717 | #[test] | ||
718 | fn use_crate_as() { | ||
719 | check( | ||
720 | r#" | ||
721 | use crate as foo; | ||
722 | |||
723 | use foo::bar as baz; | ||
724 | |||
725 | fn bar() {} | ||
726 | "#, | ||
727 | expect![[r#" | ||
728 | crate | ||
729 | bar: v | ||
730 | baz: v | ||
731 | foo: t | ||
732 | "#]], | ||
733 | ); | ||
734 | } | ||
diff --git a/crates/hir_def/src/nameres/tests/diagnostics.rs b/crates/hir_def/src/nameres/tests/diagnostics.rs index 6becd9ff1..a89061c2e 100644 --- a/crates/hir_def/src/nameres/tests/diagnostics.rs +++ b/crates/hir_def/src/nameres/tests/diagnostics.rs | |||
@@ -196,7 +196,7 @@ fn builtin_macro_fails_expansion() { | |||
196 | macro_rules! include { () => {} } | 196 | macro_rules! include { () => {} } |
197 | 197 | ||
198 | include!("doesntexist"); | 198 | include!("doesntexist"); |
199 | //^^^^^^^^^^^^^^^^^^^^^^^^ could not convert tokens | 199 | //^^^^^^^^^^^^^^^^^^^^^^^^ failed to load file `doesntexist` |
200 | "#, | 200 | "#, |
201 | ); | 201 | ); |
202 | } | 202 | } |
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 2a79c892b..fce09a9e7 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -333,17 +333,19 @@ fn concat_expand( | |||
333 | fn relative_file( | 333 | fn relative_file( |
334 | db: &dyn AstDatabase, | 334 | db: &dyn AstDatabase, |
335 | call_id: MacroCallId, | 335 | call_id: MacroCallId, |
336 | path: &str, | 336 | path_str: &str, |
337 | allow_recursion: bool, | 337 | allow_recursion: bool, |
338 | ) -> Option<FileId> { | 338 | ) -> Result<FileId, mbe::ExpandError> { |
339 | let call_site = call_id.as_file().original_file(db); | 339 | let call_site = call_id.as_file().original_file(db); |
340 | let path = AnchoredPath { anchor: call_site, path }; | 340 | let path = AnchoredPath { anchor: call_site, path: path_str }; |
341 | let res = db.resolve_path(path)?; | 341 | let res = db |
342 | .resolve_path(path) | ||
343 | .ok_or_else(|| mbe::ExpandError::Other(format!("failed to load file `{}`", path_str)))?; | ||
342 | // Prevent include itself | 344 | // Prevent include itself |
343 | if res == call_site && !allow_recursion { | 345 | if res == call_site && !allow_recursion { |
344 | None | 346 | Err(mbe::ExpandError::Other(format!("recursive inclusion of `{}`", path_str))) |
345 | } else { | 347 | } else { |
346 | Some(res) | 348 | Ok(res) |
347 | } | 349 | } |
348 | } | 350 | } |
349 | 351 | ||
@@ -364,8 +366,7 @@ fn include_expand( | |||
364 | ) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> { | 366 | ) -> ExpandResult<Option<(tt::Subtree, FragmentKind)>> { |
365 | let res = (|| { | 367 | let res = (|| { |
366 | let path = parse_string(tt)?; | 368 | let path = parse_string(tt)?; |
367 | let file_id = relative_file(db, arg_id.into(), &path, false) | 369 | let file_id = relative_file(db, arg_id.into(), &path, false)?; |
368 | .ok_or_else(|| mbe::ExpandError::ConversionError)?; | ||
369 | 370 | ||
370 | Ok(parse_to_token_tree(&db.file_text(file_id)) | 371 | Ok(parse_to_token_tree(&db.file_text(file_id)) |
371 | .ok_or_else(|| mbe::ExpandError::ConversionError)? | 372 | .ok_or_else(|| mbe::ExpandError::ConversionError)? |
@@ -417,8 +418,8 @@ fn include_str_expand( | |||
417 | // Ideally, we'd be able to offer a precise expansion if the user asks for macro | 418 | // Ideally, we'd be able to offer a precise expansion if the user asks for macro |
418 | // expansion. | 419 | // expansion. |
419 | let file_id = match relative_file(db, arg_id.into(), &path, true) { | 420 | let file_id = match relative_file(db, arg_id.into(), &path, true) { |
420 | Some(file_id) => file_id, | 421 | Ok(file_id) => file_id, |
421 | None => { | 422 | Err(_) => { |
422 | return ExpandResult::ok(Some((quote!(""), FragmentKind::Expr))); | 423 | return ExpandResult::ok(Some((quote!(""), FragmentKind::Expr))); |
423 | } | 424 | } |
424 | }; | 425 | }; |