diff options
Diffstat (limited to 'crates/hir_expand')
-rw-r--r-- | crates/hir_expand/src/builtin_macro.rs | 27 | ||||
-rw-r--r-- | crates/hir_expand/src/db.rs | 14 | ||||
-rw-r--r-- | crates/hir_expand/src/eager.rs | 4 | ||||
-rw-r--r-- | crates/hir_expand/src/name.rs | 2 | ||||
-rw-r--r-- | crates/hir_expand/src/quote.rs | 1 |
5 files changed, 33 insertions, 15 deletions
diff --git a/crates/hir_expand/src/builtin_macro.rs b/crates/hir_expand/src/builtin_macro.rs index 4d52904b9..75ec4196b 100644 --- a/crates/hir_expand/src/builtin_macro.rs +++ b/crates/hir_expand/src/builtin_macro.rs | |||
@@ -1,10 +1,10 @@ | |||
1 | //! Builtin macro | 1 | //! Builtin macro |
2 | use crate::{ | 2 | use crate::{ |
3 | db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId, | 3 | db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId, |
4 | MacroDefId, MacroDefKind, TextSize, | 4 | MacroCallLoc, MacroDefId, MacroDefKind, TextSize, |
5 | }; | 5 | }; |
6 | 6 | ||
7 | use base_db::{AnchoredPath, FileId}; | 7 | use base_db::{AnchoredPath, Edition, FileId}; |
8 | use cfg::CfgExpr; | 8 | use cfg::CfgExpr; |
9 | use either::Either; | 9 | use either::Either; |
10 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; | 10 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult}; |
@@ -111,6 +111,8 @@ register_builtin! { | |||
111 | (llvm_asm, LlvmAsm) => asm_expand, | 111 | (llvm_asm, LlvmAsm) => asm_expand, |
112 | (asm, Asm) => asm_expand, | 112 | (asm, Asm) => asm_expand, |
113 | (cfg, Cfg) => cfg_expand, | 113 | (cfg, Cfg) => cfg_expand, |
114 | (core_panic, CorePanic) => panic_expand, | ||
115 | (std_panic, StdPanic) => panic_expand, | ||
114 | 116 | ||
115 | EAGER: | 117 | EAGER: |
116 | (compile_error, CompileError) => compile_error_expand, | 118 | (compile_error, CompileError) => compile_error_expand, |
@@ -284,6 +286,25 @@ fn cfg_expand( | |||
284 | ExpandResult::ok(expanded) | 286 | ExpandResult::ok(expanded) |
285 | } | 287 | } |
286 | 288 | ||
289 | fn panic_expand( | ||
290 | db: &dyn AstDatabase, | ||
291 | id: LazyMacroId, | ||
292 | tt: &tt::Subtree, | ||
293 | ) -> ExpandResult<tt::Subtree> { | ||
294 | let loc: MacroCallLoc = db.lookup_intern_macro(id); | ||
295 | // Expand to a macro call `$crate::panic::panic_{edition}` | ||
296 | let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() }; | ||
297 | let mut call = if db.crate_graph()[loc.krate].edition == Edition::Edition2021 { | ||
298 | quote!(#krate::panic::panic_2021!) | ||
299 | } else { | ||
300 | quote!(#krate::panic::panic_2015!) | ||
301 | }; | ||
302 | |||
303 | // Pass the original arguments | ||
304 | call.token_trees.push(tt::TokenTree::Subtree(tt.clone())); | ||
305 | ExpandResult::ok(call) | ||
306 | } | ||
307 | |||
287 | fn unquote_str(lit: &tt::Literal) -> Option<String> { | 308 | fn unquote_str(lit: &tt::Literal) -> Option<String> { |
288 | let lit = ast::make::tokens::literal(&lit.to_string()); | 309 | let lit = ast::make::tokens::literal(&lit.to_string()); |
289 | let token = ast::String::cast(lit)?; | 310 | let token = ast::String::cast(lit)?; |
@@ -563,7 +584,7 @@ mod tests { | |||
563 | }; | 584 | }; |
564 | 585 | ||
565 | let args = macro_call.token_tree().unwrap(); | 586 | let args = macro_call.token_tree().unwrap(); |
566 | let parsed_args = mbe::ast_to_token_tree(&args).unwrap().0; | 587 | let parsed_args = mbe::ast_to_token_tree(&args).0; |
567 | let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)); | 588 | let call_id = AstId::new(file_id.into(), ast_id_map.ast_id(¯o_call)); |
568 | 589 | ||
569 | let arg_id = db.intern_eager_expansion({ | 590 | let arg_id = db.intern_eager_expansion({ |
diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index c0ab70b60..10fe60821 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs | |||
@@ -119,7 +119,7 @@ pub fn expand_hypothetical( | |||
119 | token_to_map: syntax::SyntaxToken, | 119 | token_to_map: syntax::SyntaxToken, |
120 | ) -> Option<(SyntaxNode, syntax::SyntaxToken)> { | 120 | ) -> Option<(SyntaxNode, syntax::SyntaxToken)> { |
121 | let macro_file = MacroFile { macro_call_id: actual_macro_call }; | 121 | let macro_file = MacroFile { macro_call_id: actual_macro_call }; |
122 | let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax()).unwrap(); | 122 | let (tt, tmap_1) = mbe::syntax_node_to_token_tree(hypothetical_args.syntax()); |
123 | let range = | 123 | let range = |
124 | token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?; | 124 | token_to_map.text_range().checked_sub(hypothetical_args.syntax().text_range().start())?; |
125 | let token_id = tmap_1.token_by_range(range)?; | 125 | let token_id = tmap_1.token_by_range(range)?; |
@@ -143,10 +143,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, | |||
143 | MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) { | 143 | MacroDefKind::Declarative(ast_id) => match ast_id.to_node(db) { |
144 | syntax::ast::Macro::MacroRules(macro_rules) => { | 144 | syntax::ast::Macro::MacroRules(macro_rules) => { |
145 | let arg = macro_rules.token_tree()?; | 145 | let arg = macro_rules.token_tree()?; |
146 | let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { | 146 | let (tt, tmap) = mbe::ast_to_token_tree(&arg); |
147 | log::warn!("fail on macro_rules to token tree: {:#?}", arg); | ||
148 | None | ||
149 | })?; | ||
150 | let rules = match MacroRules::parse(&tt) { | 147 | let rules = match MacroRules::parse(&tt) { |
151 | Ok(it) => it, | 148 | Ok(it) => it, |
152 | Err(err) => { | 149 | Err(err) => { |
@@ -159,10 +156,7 @@ fn macro_def(db: &dyn AstDatabase, id: MacroDefId) -> Option<Arc<(TokenExpander, | |||
159 | } | 156 | } |
160 | syntax::ast::Macro::MacroDef(macro_def) => { | 157 | syntax::ast::Macro::MacroDef(macro_def) => { |
161 | let arg = macro_def.body()?; | 158 | let arg = macro_def.body()?; |
162 | let (tt, tmap) = mbe::ast_to_token_tree(&arg).or_else(|| { | 159 | let (tt, tmap) = mbe::ast_to_token_tree(&arg); |
163 | log::warn!("fail on macro_def to token tree: {:#?}", arg); | ||
164 | None | ||
165 | })?; | ||
166 | let rules = match MacroDef::parse(&tt) { | 160 | let rules = match MacroDef::parse(&tt) { |
167 | Ok(it) => it, | 161 | Ok(it) => it, |
168 | Err(err) => { | 162 | Err(err) => { |
@@ -202,7 +196,7 @@ fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> { | |||
202 | 196 | ||
203 | fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> { | 197 | fn macro_arg(db: &dyn AstDatabase, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> { |
204 | let arg = db.macro_arg_text(id)?; | 198 | let arg = db.macro_arg_text(id)?; |
205 | let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?; | 199 | let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg)); |
206 | Some(Arc::new((tt, tmap))) | 200 | Some(Arc::new((tt, tmap))) |
207 | } | 201 | } |
208 | 202 | ||
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs index 9eedc8461..9705526fa 100644 --- a/crates/hir_expand/src/eager.rs +++ b/crates/hir_expand/src/eager.rs | |||
@@ -106,7 +106,7 @@ pub fn expand_eager_macro( | |||
106 | mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), | 106 | mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError), |
107 | ) -> Result<EagerMacroId, ErrorEmitted> { | 107 | ) -> Result<EagerMacroId, ErrorEmitted> { |
108 | let parsed_args = diagnostic_sink.option_with( | 108 | let parsed_args = diagnostic_sink.option_with( |
109 | || Some(mbe::ast_to_token_tree(¯o_call.value.token_tree()?)?.0), | 109 | || Some(mbe::ast_to_token_tree(¯o_call.value.token_tree()?).0), |
110 | || err("malformed macro invocation"), | 110 | || err("malformed macro invocation"), |
111 | )?; | 111 | )?; |
112 | 112 | ||
@@ -161,7 +161,7 @@ pub fn expand_eager_macro( | |||
161 | } | 161 | } |
162 | 162 | ||
163 | fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> { | 163 | fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> { |
164 | let mut subtree = mbe::syntax_node_to_token_tree(node)?.0; | 164 | let mut subtree = mbe::syntax_node_to_token_tree(node).0; |
165 | subtree.delimiter = None; | 165 | subtree.delimiter = None; |
166 | Some(subtree) | 166 | Some(subtree) |
167 | } | 167 | } |
diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs index 203ebbe85..a0f8766b0 100644 --- a/crates/hir_expand/src/name.rs +++ b/crates/hir_expand/src/name.rs | |||
@@ -208,6 +208,8 @@ pub mod known { | |||
208 | line, | 208 | line, |
209 | module_path, | 209 | module_path, |
210 | assert, | 210 | assert, |
211 | core_panic, | ||
212 | std_panic, | ||
211 | stringify, | 213 | stringify, |
212 | concat, | 214 | concat, |
213 | include, | 215 | include, |
diff --git a/crates/hir_expand/src/quote.rs b/crates/hir_expand/src/quote.rs index 08bc5aa49..c82487ef0 100644 --- a/crates/hir_expand/src/quote.rs +++ b/crates/hir_expand/src/quote.rs | |||
@@ -104,6 +104,7 @@ macro_rules! __quote { | |||
104 | ( . ) => {$crate::__quote!(@PUNCT '.')}; | 104 | ( . ) => {$crate::__quote!(@PUNCT '.')}; |
105 | ( < ) => {$crate::__quote!(@PUNCT '<')}; | 105 | ( < ) => {$crate::__quote!(@PUNCT '<')}; |
106 | ( > ) => {$crate::__quote!(@PUNCT '>')}; | 106 | ( > ) => {$crate::__quote!(@PUNCT '>')}; |
107 | ( ! ) => {$crate::__quote!(@PUNCT '!')}; | ||
107 | 108 | ||
108 | ( $first:tt $($tail:tt)+ ) => { | 109 | ( $first:tt $($tail:tt)+ ) => { |
109 | { | 110 | { |