mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
Implement edition-dependent builtin panic!
macro
This commit is contained in:
parent
eaffdae300
commit
6198eb74b2
3 changed files with 26 additions and 2 deletions
|
@ -1,10 +1,10 @@
|
||||||
//! Builtin macro
|
//! Builtin macro
|
||||||
use crate::{
|
use crate::{
|
||||||
db::AstDatabase, name, quote, AstId, CrateId, EagerMacroId, LazyMacroId, MacroCallId,
|
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 cfg::CfgExpr;
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult};
|
use mbe::{parse_exprs_with_sep, parse_to_token_tree, ExpandResult};
|
||||||
|
@ -111,6 +111,8 @@ register_builtin! {
|
||||||
(llvm_asm, LlvmAsm) => asm_expand,
|
(llvm_asm, LlvmAsm) => asm_expand,
|
||||||
(asm, Asm) => asm_expand,
|
(asm, Asm) => asm_expand,
|
||||||
(cfg, Cfg) => cfg_expand,
|
(cfg, Cfg) => cfg_expand,
|
||||||
|
(core_panic, CorePanic) => panic_expand,
|
||||||
|
(std_panic, StdPanic) => panic_expand,
|
||||||
|
|
||||||
EAGER:
|
EAGER:
|
||||||
(compile_error, CompileError) => compile_error_expand,
|
(compile_error, CompileError) => compile_error_expand,
|
||||||
|
@ -284,6 +286,25 @@ fn cfg_expand(
|
||||||
ExpandResult::ok(expanded)
|
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> {
|
fn unquote_str(lit: &tt::Literal) -> Option<String> {
|
||||||
let lit = ast::make::tokens::literal(&lit.to_string());
|
let lit = ast::make::tokens::literal(&lit.to_string());
|
||||||
let token = ast::String::cast(lit)?;
|
let token = ast::String::cast(lit)?;
|
||||||
|
|
|
@ -208,6 +208,8 @@ pub mod known {
|
||||||
line,
|
line,
|
||||||
module_path,
|
module_path,
|
||||||
assert,
|
assert,
|
||||||
|
core_panic,
|
||||||
|
std_panic,
|
||||||
stringify,
|
stringify,
|
||||||
concat,
|
concat,
|
||||||
include,
|
include,
|
||||||
|
|
|
@ -104,6 +104,7 @@ macro_rules! __quote {
|
||||||
( . ) => {$crate::__quote!(@PUNCT '.')};
|
( . ) => {$crate::__quote!(@PUNCT '.')};
|
||||||
( < ) => {$crate::__quote!(@PUNCT '<')};
|
( < ) => {$crate::__quote!(@PUNCT '<')};
|
||||||
( > ) => {$crate::__quote!(@PUNCT '>')};
|
( > ) => {$crate::__quote!(@PUNCT '>')};
|
||||||
|
( ! ) => {$crate::__quote!(@PUNCT '!')};
|
||||||
|
|
||||||
( $first:tt $($tail:tt)+ ) => {
|
( $first:tt $($tail:tt)+ ) => {
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue