|
1 | 1 | //! Builtin macro
|
2 | 2 | use crate::{
|
3 | 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 | 8 | use cfg::CfgExpr;
|
9 | 9 | use either::Either;
|
10 | 10 | use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult};
|
@@ -111,6 +111,8 @@ register_builtin! {
|
111 | 111 | (llvm_asm, LlvmAsm) => asm_expand,
|
112 | 112 | (asm, Asm) => asm_expand,
|
113 | 113 | (cfg, Cfg) => cfg_expand,
|
| 114 | + (core_panic, CorePanic) => panic_expand, |
| 115 | + (std_panic, StdPanic) => panic_expand, |
114 | 116 |
|
115 | 117 | EAGER:
|
116 | 118 | (compile_error, CompileError) => compile_error_expand,
|
@@ -284,6 +286,25 @@ fn cfg_expand(
|
284 | 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 | 308 | fn unquote_str(lit: &tt::Literal) -> Option<String> {
|
288 | 309 | let lit = ast::make::tokens::literal(&lit.to_string());
|
289 | 310 | let token = ast::String::cast(lit)?;
|
|
0 commit comments