]> git.lizzy.rs Git - rust.git/commitdiff
Implement edition-dependent builtin `panic!` macro
authorJonas Schievink <jonasschievink@gmail.com>
Sat, 3 Apr 2021 01:12:55 +0000 (03:12 +0200)
committerJonas Schievink <jonasschievink@gmail.com>
Sat, 3 Apr 2021 01:12:55 +0000 (03:12 +0200)
crates/hir_expand/src/builtin_macro.rs
crates/hir_expand/src/name.rs
crates/hir_expand/src/quote.rs

index 4d52904b94bf2aa3f77ed56b23c741ccc08cac65..3aa3d8997da3218657041386875d6d663f68a974 100644 (file)
@@ -1,10 +1,10 @@
 //! Builtin macro
 use crate::{
     db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
-    MacroDefId, MacroDefKind, TextSize,
+    MacroCallLoc, MacroDefId, MacroDefKind, TextSize,
 };
 
-use base_db::{AnchoredPath, FileId};
+use base_db::{AnchoredPath, Edition, FileId};
 use cfg::CfgExpr;
 use either::Either;
 use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult};
@@ -111,6 +111,8 @@ pub fn find_builtin_macro(
     (llvm_asm, LlvmAsm) => asm_expand,
     (asm, Asm) => asm_expand,
     (cfg, Cfg) => cfg_expand,
+    (core_panic, CorePanic) => panic_expand,
+    (std_panic, StdPanic) => panic_expand,
 
     EAGER:
     (compile_error, CompileError) => compile_error_expand,
@@ -284,6 +286,25 @@ fn cfg_expand(
     ExpandResult::ok(expanded)
 }
 
+fn panic_expand(
+    db: &dyn AstDatabase,
+    id: LazyMacroId,
+    tt: &tt::Subtree,
+) -> ExpandResult<tt::Subtree> {
+    let loc: MacroCallLoc = db.lookup_intern_macro(id);
+    // Expand to a macro call `$crate::panic::panic_{edition}`
+    let krate = tt::Ident { text: "$crate".into(), id: tt::TokenId::unspecified() };
+    let mut call = if db.crate_graph()[loc.krate].edition == Edition::Edition2021 {
+        quote!(#krate::panic::panic_2021!)
+    } else {
+        quote!(#krate::panic::panic_2015!)
+    };
+
+    // Pass the original arguments
+    call.token_trees.push(tt::TokenTree::Subtree(tt.clone()));
+    ExpandResult::ok(call)
+}
+
 fn unquote_str(lit: &tt::Literal) -> Option<String> {
     let lit = ast::make::tokens::literal(&lit.to_string());
     let token = ast::String::cast(lit)?;
index 203ebbe85caa01d2a6daca7c293aca9318656aed..a0f8766b08a21c354b35eacfce770d32512c35a7 100644 (file)
@@ -208,6 +208,8 @@ macro_rules! known_names {
         line,
         module_path,
         assert,
+        core_panic,
+        std_panic,
         stringify,
         concat,
         include,
index 08bc5aa49f2a902fc9fd966834663d9af7e0c519..c82487ef042e681379439b42d289189a530289da 100644 (file)
@@ -104,6 +104,7 @@ macro_rules! __quote {
     ( . ) => {$crate::__quote!(@PUNCT '.')};
     ( < ) => {$crate::__quote!(@PUNCT '<')};
     ( > ) => {$crate::__quote!(@PUNCT '>')};
+    ( ! ) => {$crate::__quote!(@PUNCT '!')};
 
     ( $first:tt $($tail:tt)+ ) => {
         {