diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-22 16:07:37 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-22 16:07:37 +0000 |
commit | 506131e3e01a81546ab218e1764073061875ea2e (patch) | |
tree | d9f4f83de92e8b4a38ceb9ad6003f55d544373e1 /crates | |
parent | 404493e9df8cc34fab5ff0c605eef7700e687e80 (diff) | |
parent | 786544f022418ed7813e401aded31e4bd5451391 (diff) |
Merge #2357
2357: Expand file! to dummy "" r=edwin0cheng a=kjeremy
See https://github.com/rust-analyzer/rust-analyzer/pull/2355#issuecomment-557541873
Co-authored-by: kjeremy <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 19 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/builtin_macro.rs | 26 | ||||
-rw-r--r-- | crates/ra_hir_expand/src/name.rs | 1 |
3 files changed, 45 insertions, 1 deletions
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 74c12a0a2..b4bdd81f6 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -4857,3 +4857,22 @@ fn main() { | |||
4857 | "### | 4857 | "### |
4858 | ); | 4858 | ); |
4859 | } | 4859 | } |
4860 | |||
4861 | #[test] | ||
4862 | fn infer_builtin_macros_file() { | ||
4863 | assert_snapshot!( | ||
4864 | infer(r#" | ||
4865 | #[rustc_builtin_macro] | ||
4866 | macro_rules! file {() => {}} | ||
4867 | |||
4868 | fn main() { | ||
4869 | let x = file!(); | ||
4870 | } | ||
4871 | "#), | ||
4872 | @r###" | ||
4873 | ![0; 2) '""': &str | ||
4874 | [64; 88) '{ ...!(); }': () | ||
4875 | [74; 75) 'x': &str | ||
4876 | "### | ||
4877 | ); | ||
4878 | } | ||
diff --git a/crates/ra_hir_expand/src/builtin_macro.rs b/crates/ra_hir_expand/src/builtin_macro.rs index 9628666d4..d7057e005 100644 --- a/crates/ra_hir_expand/src/builtin_macro.rs +++ b/crates/ra_hir_expand/src/builtin_macro.rs | |||
@@ -10,6 +10,7 @@ use crate::quote; | |||
10 | 10 | ||
11 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
12 | pub enum BuiltinExpander { | 12 | pub enum BuiltinExpander { |
13 | File, | ||
13 | Line, | 14 | Line, |
14 | Stringify, | 15 | Stringify, |
15 | } | 16 | } |
@@ -22,6 +23,7 @@ impl BuiltinExpander { | |||
22 | tt: &tt::Subtree, | 23 | tt: &tt::Subtree, |
23 | ) -> Result<tt::Subtree, mbe::ExpandError> { | 24 | ) -> Result<tt::Subtree, mbe::ExpandError> { |
24 | match self { | 25 | match self { |
26 | BuiltinExpander::File => file_expand(db, id, tt), | ||
25 | BuiltinExpander::Line => line_expand(db, id, tt), | 27 | BuiltinExpander::Line => line_expand(db, id, tt), |
26 | BuiltinExpander::Stringify => stringify_expand(db, id, tt), | 28 | BuiltinExpander::Stringify => stringify_expand(db, id, tt), |
27 | } | 29 | } |
@@ -34,7 +36,9 @@ pub fn find_builtin_macro( | |||
34 | ast_id: AstId<ast::MacroCall>, | 36 | ast_id: AstId<ast::MacroCall>, |
35 | ) -> Option<MacroDefId> { | 37 | ) -> Option<MacroDefId> { |
36 | // FIXME: Better registering method | 38 | // FIXME: Better registering method |
37 | if ident == &name::LINE_MACRO { | 39 | if ident == &name::FILE_MACRO { |
40 | Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::File) }) | ||
41 | } else if ident == &name::LINE_MACRO { | ||
38 | Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) }) | 42 | Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Line) }) |
39 | } else if ident == &name::STRINGIFY_MACRO { | 43 | } else if ident == &name::STRINGIFY_MACRO { |
40 | Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) }) | 44 | Some(MacroDefId { krate, ast_id, kind: MacroDefKind::BuiltIn(BuiltinExpander::Stringify) }) |
@@ -105,3 +109,23 @@ fn stringify_expand( | |||
105 | 109 | ||
106 | Ok(expanded) | 110 | Ok(expanded) |
107 | } | 111 | } |
112 | |||
113 | fn file_expand( | ||
114 | db: &dyn AstDatabase, | ||
115 | id: MacroCallId, | ||
116 | _tt: &tt::Subtree, | ||
117 | ) -> Result<tt::Subtree, mbe::ExpandError> { | ||
118 | let loc = db.lookup_intern_macro(id); | ||
119 | let macro_call = loc.ast_id.to_node(db); | ||
120 | let _ = macro_call.token_tree().ok_or_else(|| mbe::ExpandError::UnexpectedToken)?; | ||
121 | |||
122 | // FIXME: RA purposefully lacks knowledge of absolute file names | ||
123 | // so just return "". | ||
124 | let file_name = ""; | ||
125 | |||
126 | let expanded = quote! { | ||
127 | #file_name | ||
128 | }; | ||
129 | |||
130 | Ok(expanded) | ||
131 | } | ||
diff --git a/crates/ra_hir_expand/src/name.rs b/crates/ra_hir_expand/src/name.rs index c3f7e77a5..05fe6afd9 100644 --- a/crates/ra_hir_expand/src/name.rs +++ b/crates/ra_hir_expand/src/name.rs | |||
@@ -142,5 +142,6 @@ pub const TARGET_TYPE: Name = Name::new_inline_ascii(6, b"Target"); | |||
142 | pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); | 142 | pub const BOX_TYPE: Name = Name::new_inline_ascii(3, b"Box"); |
143 | 143 | ||
144 | // Builtin Macros | 144 | // Builtin Macros |
145 | pub const FILE_MACRO: Name = Name::new_inline_ascii(4, b"file"); | ||
145 | pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line"); | 146 | pub const LINE_MACRO: Name = Name::new_inline_ascii(4, b"line"); |
146 | pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify"); | 147 | pub const STRINGIFY_MACRO: Name = Name::new_inline_ascii(9, b"stringify"); |