diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir_def/src/body.rs | 12 | ||||
-rw-r--r-- | crates/ra_hir_def/src/body/lower.rs | 48 | ||||
-rw-r--r-- | crates/ra_hir_def/src/data.rs | 28 | ||||
-rw-r--r-- | crates/ra_hir_def/src/resolver.rs | 15 | ||||
-rw-r--r-- | crates/ra_hir_def/src/visibility.rs | 33 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/macros.rs | 20 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/complete_dot.rs | 32 | ||||
-rw-r--r-- | crates/ra_ide/src/goto_definition.rs | 15 |
8 files changed, 176 insertions, 27 deletions
diff --git a/crates/ra_hir_def/src/body.rs b/crates/ra_hir_def/src/body.rs index 57ba45b45..2bc405a59 100644 --- a/crates/ra_hir_def/src/body.rs +++ b/crates/ra_hir_def/src/body.rs | |||
@@ -47,13 +47,19 @@ impl Expander { | |||
47 | pub(crate) fn enter_expand<T: ast::AstNode, DB: DefDatabase>( | 47 | pub(crate) fn enter_expand<T: ast::AstNode, DB: DefDatabase>( |
48 | &mut self, | 48 | &mut self, |
49 | db: &DB, | 49 | db: &DB, |
50 | local_scope: Option<&ItemScope>, | ||
50 | macro_call: ast::MacroCall, | 51 | macro_call: ast::MacroCall, |
51 | ) -> Option<(Mark, T)> { | 52 | ) -> Option<(Mark, T)> { |
52 | let macro_call = InFile::new(self.current_file_id, ¯o_call); | 53 | let macro_call = InFile::new(self.current_file_id, ¯o_call); |
53 | 54 | ||
54 | if let Some(call_id) = | 55 | if let Some(call_id) = macro_call.as_call_id(db, |path| { |
55 | macro_call.as_call_id(db, |path| self.resolve_path_as_macro(db, &path)) | 56 | if let Some(local_scope) = local_scope { |
56 | { | 57 | if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) { |
58 | return Some(def); | ||
59 | } | ||
60 | } | ||
61 | self.resolve_path_as_macro(db, &path) | ||
62 | }) { | ||
57 | let file_id = call_id.as_file(); | 63 | let file_id = call_id.as_file(); |
58 | if let Some(node) = db.parse_or_expand(file_id) { | 64 | if let Some(node) = db.parse_or_expand(file_id) { |
59 | if let Some(expr) = T::cast(node) { | 65 | if let Some(expr) = T::cast(node) { |
diff --git a/crates/ra_hir_def/src/body/lower.rs b/crates/ra_hir_def/src/body/lower.rs index ec1b0c2e7..54b5591d3 100644 --- a/crates/ra_hir_def/src/body/lower.rs +++ b/crates/ra_hir_def/src/body/lower.rs | |||
@@ -3,7 +3,10 @@ | |||
3 | 3 | ||
4 | use either::Either; | 4 | use either::Either; |
5 | 5 | ||
6 | use hir_expand::name::{name, AsName, Name}; | 6 | use hir_expand::{ |
7 | name::{name, AsName, Name}, | ||
8 | MacroDefId, MacroDefKind, | ||
9 | }; | ||
7 | use ra_arena::Arena; | 10 | use ra_arena::Arena; |
8 | use ra_syntax::{ | 11 | use ra_syntax::{ |
9 | ast::{ | 12 | ast::{ |
@@ -452,19 +455,30 @@ where | |||
452 | None => self.alloc_expr(Expr::Missing, syntax_ptr), | 455 | None => self.alloc_expr(Expr::Missing, syntax_ptr), |
453 | } | 456 | } |
454 | } | 457 | } |
455 | // FIXME expand to statements in statement position | ||
456 | ast::Expr::MacroCall(e) => { | 458 | ast::Expr::MacroCall(e) => { |
457 | let macro_call = self.expander.to_source(AstPtr::new(&e)); | 459 | if let Some(name) = is_macro_rules(&e) { |
458 | match self.expander.enter_expand(self.db, e) { | 460 | let mac = MacroDefId { |
459 | Some((mark, expansion)) => { | 461 | krate: Some(self.expander.module.krate), |
460 | self.source_map | 462 | ast_id: Some(self.expander.ast_id(&e)), |
461 | .expansions | 463 | kind: MacroDefKind::Declarative, |
462 | .insert(macro_call, self.expander.current_file_id); | 464 | }; |
463 | let id = self.collect_expr(expansion); | 465 | self.body.item_scope.define_legacy_macro(name, mac); |
464 | self.expander.exit(self.db, mark); | 466 | |
465 | id | 467 | // FIXME: do we still need to allocate this as missing ? |
468 | self.alloc_expr(Expr::Missing, syntax_ptr) | ||
469 | } else { | ||
470 | let macro_call = self.expander.to_source(AstPtr::new(&e)); | ||
471 | match self.expander.enter_expand(self.db, Some(&self.body.item_scope), e) { | ||
472 | Some((mark, expansion)) => { | ||
473 | self.source_map | ||
474 | .expansions | ||
475 | .insert(macro_call, self.expander.current_file_id); | ||
476 | let id = self.collect_expr(expansion); | ||
477 | self.expander.exit(self.db, mark); | ||
478 | id | ||
479 | } | ||
480 | None => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
466 | } | 481 | } |
467 | None => self.alloc_expr(Expr::Missing, syntax_ptr), | ||
468 | } | 482 | } |
469 | } | 483 | } |
470 | 484 | ||
@@ -686,6 +700,16 @@ where | |||
686 | } | 700 | } |
687 | } | 701 | } |
688 | 702 | ||
703 | fn is_macro_rules(m: &ast::MacroCall) -> Option<Name> { | ||
704 | let name = m.path()?.segment()?.name_ref()?.as_name(); | ||
705 | |||
706 | if name == name![macro_rules] { | ||
707 | Some(m.name()?.as_name()) | ||
708 | } else { | ||
709 | None | ||
710 | } | ||
711 | } | ||
712 | |||
689 | impl From<ast::BinOp> for BinaryOp { | 713 | impl From<ast::BinOp> for BinaryOp { |
690 | fn from(ast_op: ast::BinOp) -> Self { | 714 | fn from(ast_op: ast::BinOp) -> Self { |
691 | match ast_op { | 715 | match ast_op { |
diff --git a/crates/ra_hir_def/src/data.rs b/crates/ra_hir_def/src/data.rs index a72eb5369..c0b16b7fa 100644 --- a/crates/ra_hir_def/src/data.rs +++ b/crates/ra_hir_def/src/data.rs | |||
@@ -34,7 +34,8 @@ pub struct FunctionData { | |||
34 | 34 | ||
35 | impl FunctionData { | 35 | impl FunctionData { |
36 | pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> { | 36 | pub(crate) fn fn_data_query(db: &impl DefDatabase, func: FunctionId) -> Arc<FunctionData> { |
37 | let src = func.lookup(db).source(db); | 37 | let loc = func.lookup(db); |
38 | let src = loc.source(db); | ||
38 | let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); | 39 | let name = src.value.name().map(|n| n.as_name()).unwrap_or_else(Name::missing); |
39 | let mut params = Vec::new(); | 40 | let mut params = Vec::new(); |
40 | let mut has_self_param = false; | 41 | let mut has_self_param = false; |
@@ -76,7 +77,9 @@ impl FunctionData { | |||
76 | ret_type | 77 | ret_type |
77 | }; | 78 | }; |
78 | 79 | ||
79 | let visibility = RawVisibility::from_ast(db, src.map(|s| s.visibility())); | 80 | let vis_default = RawVisibility::default_for_container(loc.container); |
81 | let visibility = | ||
82 | RawVisibility::from_ast_with_default(db, vis_default, src.map(|s| s.visibility())); | ||
80 | 83 | ||
81 | let sig = FunctionData { name, params, ret_type, has_self_param, visibility }; | 84 | let sig = FunctionData { name, params, ret_type, has_self_param, visibility }; |
82 | Arc::new(sig) | 85 | Arc::new(sig) |
@@ -105,10 +108,13 @@ impl TypeAliasData { | |||
105 | db: &impl DefDatabase, | 108 | db: &impl DefDatabase, |
106 | typ: TypeAliasId, | 109 | typ: TypeAliasId, |
107 | ) -> Arc<TypeAliasData> { | 110 | ) -> Arc<TypeAliasData> { |
108 | let node = typ.lookup(db).source(db); | 111 | let loc = typ.lookup(db); |
112 | let node = loc.source(db); | ||
109 | let name = node.value.name().map_or_else(Name::missing, |n| n.as_name()); | 113 | let name = node.value.name().map_or_else(Name::missing, |n| n.as_name()); |
110 | let type_ref = node.value.type_ref().map(TypeRef::from_ast); | 114 | let type_ref = node.value.type_ref().map(TypeRef::from_ast); |
111 | let visibility = RawVisibility::from_ast(db, node.map(|n| n.visibility())); | 115 | let vis_default = RawVisibility::default_for_container(loc.container); |
116 | let visibility = | ||
117 | RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility())); | ||
112 | Arc::new(TypeAliasData { name, type_ref, visibility }) | 118 | Arc::new(TypeAliasData { name, type_ref, visibility }) |
113 | } | 119 | } |
114 | } | 120 | } |
@@ -230,22 +236,26 @@ pub struct ConstData { | |||
230 | 236 | ||
231 | impl ConstData { | 237 | impl ConstData { |
232 | pub(crate) fn const_data_query(db: &impl DefDatabase, konst: ConstId) -> Arc<ConstData> { | 238 | pub(crate) fn const_data_query(db: &impl DefDatabase, konst: ConstId) -> Arc<ConstData> { |
233 | let node = konst.lookup(db).source(db); | 239 | let loc = konst.lookup(db); |
234 | Arc::new(ConstData::new(db, node)) | 240 | let node = loc.source(db); |
241 | let vis_default = RawVisibility::default_for_container(loc.container); | ||
242 | Arc::new(ConstData::new(db, vis_default, node)) | ||
235 | } | 243 | } |
236 | 244 | ||
237 | pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> { | 245 | pub(crate) fn static_data_query(db: &impl DefDatabase, konst: StaticId) -> Arc<ConstData> { |
238 | let node = konst.lookup(db).source(db); | 246 | let node = konst.lookup(db).source(db); |
239 | Arc::new(ConstData::new(db, node)) | 247 | Arc::new(ConstData::new(db, RawVisibility::private(), node)) |
240 | } | 248 | } |
241 | 249 | ||
242 | fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>( | 250 | fn new<N: NameOwner + TypeAscriptionOwner + VisibilityOwner>( |
243 | db: &impl DefDatabase, | 251 | db: &impl DefDatabase, |
252 | vis_default: RawVisibility, | ||
244 | node: InFile<N>, | 253 | node: InFile<N>, |
245 | ) -> ConstData { | 254 | ) -> ConstData { |
246 | let name = node.value.name().map(|n| n.as_name()); | 255 | let name = node.value.name().map(|n| n.as_name()); |
247 | let type_ref = TypeRef::from_ast_opt(node.value.ascribed_type()); | 256 | let type_ref = TypeRef::from_ast_opt(node.value.ascribed_type()); |
248 | let visibility = RawVisibility::from_ast(db, node.map(|n| n.visibility())); | 257 | let visibility = |
258 | RawVisibility::from_ast_with_default(db, vis_default, node.map(|n| n.visibility())); | ||
249 | ConstData { name, type_ref, visibility } | 259 | ConstData { name, type_ref, visibility } |
250 | } | 260 | } |
251 | } | 261 | } |
@@ -280,7 +290,7 @@ fn collect_impl_items_in_macro( | |||
280 | return Vec::new(); | 290 | return Vec::new(); |
281 | } | 291 | } |
282 | 292 | ||
283 | if let Some((mark, items)) = expander.enter_expand(db, m) { | 293 | if let Some((mark, items)) = expander.enter_expand(db, None, m) { |
284 | let items: InFile<ast::MacroItems> = expander.to_source(items); | 294 | let items: InFile<ast::MacroItems> = expander.to_source(items); |
285 | let mut res = collect_impl_items( | 295 | let mut res = collect_impl_items( |
286 | db, | 296 | db, |
diff --git a/crates/ra_hir_def/src/resolver.rs b/crates/ra_hir_def/src/resolver.rs index 2734d51a0..123fae72a 100644 --- a/crates/ra_hir_def/src/resolver.rs +++ b/crates/ra_hir_def/src/resolver.rs | |||
@@ -381,6 +381,11 @@ impl Resolver { | |||
381 | db: &impl DefDatabase, | 381 | db: &impl DefDatabase, |
382 | path: &ModPath, | 382 | path: &ModPath, |
383 | ) -> Option<MacroDefId> { | 383 | ) -> Option<MacroDefId> { |
384 | // Search item scope legacy macro first | ||
385 | if let Some(def) = self.resolve_local_macro_def(path) { | ||
386 | return Some(def); | ||
387 | } | ||
388 | |||
384 | let (item_map, module) = self.module_scope()?; | 389 | let (item_map, module) = self.module_scope()?; |
385 | item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros() | 390 | item_map.resolve_path(db, module, &path, BuiltinShadowMode::Other).0.take_macros() |
386 | } | 391 | } |
@@ -413,6 +418,16 @@ impl Resolver { | |||
413 | }) | 418 | }) |
414 | } | 419 | } |
415 | 420 | ||
421 | fn resolve_local_macro_def(&self, path: &ModPath) -> Option<MacroDefId> { | ||
422 | let name = path.as_ident()?; | ||
423 | self.scopes.iter().rev().find_map(|scope| { | ||
424 | if let Scope::LocalItemsScope(body) = scope { | ||
425 | return body.item_scope.get_legacy_macro(name); | ||
426 | } | ||
427 | None | ||
428 | }) | ||
429 | } | ||
430 | |||
416 | pub fn module(&self) -> Option<ModuleId> { | 431 | pub fn module(&self) -> Option<ModuleId> { |
417 | let (def_map, local_id) = self.module_scope()?; | 432 | let (def_map, local_id) = self.module_scope()?; |
418 | Some(ModuleId { krate: def_map.krate, local_id }) | 433 | Some(ModuleId { krate: def_map.krate, local_id }) |
diff --git a/crates/ra_hir_def/src/visibility.rs b/crates/ra_hir_def/src/visibility.rs index d8296da4b..e0c59e905 100644 --- a/crates/ra_hir_def/src/visibility.rs +++ b/crates/ra_hir_def/src/visibility.rs | |||
@@ -6,7 +6,7 @@ use ra_syntax::ast; | |||
6 | use crate::{ | 6 | use crate::{ |
7 | db::DefDatabase, | 7 | db::DefDatabase, |
8 | path::{ModPath, PathKind}, | 8 | path::{ModPath, PathKind}, |
9 | ModuleId, | 9 | AssocContainerId, ModuleId, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | /// Visibility of an item, not yet resolved. | 12 | /// Visibility of an item, not yet resolved. |
@@ -20,11 +20,30 @@ pub enum RawVisibility { | |||
20 | } | 20 | } |
21 | 21 | ||
22 | impl RawVisibility { | 22 | impl RawVisibility { |
23 | const fn private() -> RawVisibility { | 23 | pub(crate) const fn private() -> RawVisibility { |
24 | let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() }; | 24 | let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() }; |
25 | RawVisibility::Module(path) | 25 | RawVisibility::Module(path) |
26 | } | 26 | } |
27 | 27 | ||
28 | pub(crate) fn default_for_container(container_id: AssocContainerId) -> Self { | ||
29 | match container_id { | ||
30 | AssocContainerId::TraitId(_) => RawVisibility::Public, | ||
31 | _ => RawVisibility::private(), | ||
32 | } | ||
33 | } | ||
34 | |||
35 | pub(crate) fn from_ast_with_default( | ||
36 | db: &impl DefDatabase, | ||
37 | default: RawVisibility, | ||
38 | node: InFile<Option<ast::Visibility>>, | ||
39 | ) -> RawVisibility { | ||
40 | Self::from_ast_with_hygiene_and_default( | ||
41 | node.value, | ||
42 | default, | ||
43 | &Hygiene::new(db, node.file_id), | ||
44 | ) | ||
45 | } | ||
46 | |||
28 | pub(crate) fn from_ast( | 47 | pub(crate) fn from_ast( |
29 | db: &impl DefDatabase, | 48 | db: &impl DefDatabase, |
30 | node: InFile<Option<ast::Visibility>>, | 49 | node: InFile<Option<ast::Visibility>>, |
@@ -36,8 +55,16 @@ impl RawVisibility { | |||
36 | node: Option<ast::Visibility>, | 55 | node: Option<ast::Visibility>, |
37 | hygiene: &Hygiene, | 56 | hygiene: &Hygiene, |
38 | ) -> RawVisibility { | 57 | ) -> RawVisibility { |
58 | Self::from_ast_with_hygiene_and_default(node, RawVisibility::private(), hygiene) | ||
59 | } | ||
60 | |||
61 | pub(crate) fn from_ast_with_hygiene_and_default( | ||
62 | node: Option<ast::Visibility>, | ||
63 | default: RawVisibility, | ||
64 | hygiene: &Hygiene, | ||
65 | ) -> RawVisibility { | ||
39 | let node = match node { | 66 | let node = match node { |
40 | None => return RawVisibility::private(), | 67 | None => return default, |
41 | Some(node) => node, | 68 | Some(node) => node, |
42 | }; | 69 | }; |
43 | match node.kind() { | 70 | match node.kind() { |
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index 32457bbf7..3b7022ad5 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs | |||
@@ -363,6 +363,26 @@ fn main() { | |||
363 | } | 363 | } |
364 | 364 | ||
365 | #[test] | 365 | #[test] |
366 | fn infer_local_macro() { | ||
367 | assert_snapshot!( | ||
368 | infer(r#" | ||
369 | fn main() { | ||
370 | macro_rules! foo { | ||
371 | () => { 1usize } | ||
372 | } | ||
373 | let _a = foo!(); | ||
374 | } | ||
375 | "#), | ||
376 | @r###" | ||
377 | ![0; 6) '1usize': usize | ||
378 | [11; 90) '{ ...!(); }': () | ||
379 | [17; 66) 'macro_... }': {unknown} | ||
380 | [75; 77) '_a': usize | ||
381 | "### | ||
382 | ); | ||
383 | } | ||
384 | |||
385 | #[test] | ||
366 | fn infer_builtin_macros_line() { | 386 | fn infer_builtin_macros_line() { |
367 | assert_snapshot!( | 387 | assert_snapshot!( |
368 | infer(r#" | 388 | infer(r#" |
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index 81e5037aa..f07611d88 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs | |||
@@ -402,6 +402,38 @@ mod tests { | |||
402 | } | 402 | } |
403 | 403 | ||
404 | #[test] | 404 | #[test] |
405 | fn completes_trait_method_from_other_module() { | ||
406 | assert_debug_snapshot!( | ||
407 | do_ref_completion( | ||
408 | r" | ||
409 | struct A {} | ||
410 | mod m { | ||
411 | pub trait Trait { fn the_method(&self); } | ||
412 | } | ||
413 | use m::Trait; | ||
414 | impl Trait for A {} | ||
415 | fn foo(a: A) { | ||
416 | a.<|> | ||
417 | } | ||
418 | ", | ||
419 | ), | ||
420 | @r###" | ||
421 | [ | ||
422 | CompletionItem { | ||
423 | label: "the_method()", | ||
424 | source_range: [219; 219), | ||
425 | delete: [219; 219), | ||
426 | insert: "the_method()$0", | ||
427 | kind: Method, | ||
428 | lookup: "the_method", | ||
429 | detail: "fn the_method(&self)", | ||
430 | }, | ||
431 | ] | ||
432 | "### | ||
433 | ); | ||
434 | } | ||
435 | |||
436 | #[test] | ||
405 | fn test_no_non_self_method() { | 437 | fn test_no_non_self_method() { |
406 | assert_debug_snapshot!( | 438 | assert_debug_snapshot!( |
407 | do_ref_completion( | 439 | do_ref_completion( |
diff --git a/crates/ra_ide/src/goto_definition.rs b/crates/ra_ide/src/goto_definition.rs index a55a13ffc..a7be92ce3 100644 --- a/crates/ra_ide/src/goto_definition.rs +++ b/crates/ra_ide/src/goto_definition.rs | |||
@@ -788,6 +788,21 @@ mod tests { | |||
788 | } | 788 | } |
789 | 789 | ||
790 | #[test] | 790 | #[test] |
791 | fn goto_def_in_local_macro() { | ||
792 | check_goto( | ||
793 | " | ||
794 | //- /lib.rs | ||
795 | fn bar() { | ||
796 | macro_rules! foo { () => { () } } | ||
797 | <|>foo!(); | ||
798 | } | ||
799 | ", | ||
800 | "foo MACRO_CALL FileId(1) [15; 48) [28; 31)", | ||
801 | "macro_rules! foo { () => { () } }|foo", | ||
802 | ); | ||
803 | } | ||
804 | |||
805 | #[test] | ||
791 | fn goto_def_for_field_init_shorthand() { | 806 | fn goto_def_for_field_init_shorthand() { |
792 | covers!(ra_ide_db::goto_def_for_field_init_shorthand); | 807 | covers!(ra_ide_db::goto_def_for_field_init_shorthand); |
793 | check_goto( | 808 | check_goto( |