about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJonas Schievink <jonas.schievink@ferrous-systems.com>2022-02-25 19:13:04 +0100
committerJonas Schievink <jonas.schievink@ferrous-systems.com>2022-02-25 19:13:04 +0100
commitaec4bcf3f25d829e55f1978780ec7529fff84401 (patch)
tree79657de8196f9720dd75a9a97451cbcbb7e5e22d
parente1541bda940ca8c06e9bc134aab64ccbf9e6c225 (diff)
downloadrust-aec4bcf3f25d829e55f1978780ec7529fff84401.tar.gz
rust-aec4bcf3f25d829e55f1978780ec7529fff84401.zip
Implement the new built-in `unreachable!` macro
-rw-r--r--crates/hir_expand/src/builtin_fn_macro.rs20
-rw-r--r--crates/hir_expand/src/name.rs1
2 files changed, 21 insertions, 0 deletions
diff --git a/crates/hir_expand/src/builtin_fn_macro.rs b/crates/hir_expand/src/builtin_fn_macro.rs
index bf6cc9989a6..17f0caca7ac 100644
--- a/crates/hir_expand/src/builtin_fn_macro.rs
+++ b/crates/hir_expand/src/builtin_fn_macro.rs
@@ -114,6 +114,7 @@ register_builtin! {
     (cfg, Cfg) => cfg_expand,
     (core_panic, CorePanic) => panic_expand,
     (std_panic, StdPanic) => panic_expand,
+    (unreachable, Unreachable) => unreachable_expand,
     (log_syntax, LogSyntax) => log_syntax_expand,
     (trace_macros, TraceMacros) => trace_macros_expand,
 
@@ -354,6 +355,25 @@ fn panic_expand(
     ExpandResult::ok(call)
 }
 
+fn unreachable_expand(
+    db: &dyn AstDatabase,
+    id: MacroCallId,
+    tt: &tt::Subtree,
+) -> ExpandResult<tt::Subtree> {
+    let loc: MacroCallLoc = db.lookup_intern_macro_call(id);
+    // Expand to a macro call `$crate::panic::unreachable_{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::unreachable_2021!)
+    } else {
+        quote!(#krate::panic::unreachable_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)?;
diff --git a/crates/hir_expand/src/name.rs b/crates/hir_expand/src/name.rs
index 7deec253da8..f62c3ca0a00 100644
--- a/crates/hir_expand/src/name.rs
+++ b/crates/hir_expand/src/name.rs
@@ -253,6 +253,7 @@ pub mod known {
         std_panic,
         stringify,
         trace_macros,
+        unreachable,
         // Builtin derives
         Copy,
         Clone,