]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/panic.rs
Auto merge of #86808 - fee1-dead:constify-1, r=oli-obk
[rust.git] / compiler / rustc_builtin_macros / src / panic.rs
1 use rustc_ast::ptr::P;
2 use rustc_ast::tokenstream::{DelimSpan, TokenStream};
3 use rustc_ast::*;
4 use rustc_expand::base::*;
5 use rustc_span::symbol::sym;
6 use rustc_span::Span;
7
8 // This expands to either
9 // - `$crate::panic::panic_2015!(...)` or
10 // - `$crate::panic::panic_2021!(...)`
11 // depending on the edition.
12 //
13 // This is used for both std::panic!() and core::panic!().
14 //
15 // `$crate` will refer to either the `std` or `core` crate depending on which
16 // one we're expanding from.
17 pub fn expand_panic<'cx>(
18     cx: &'cx mut ExtCtxt<'_>,
19     sp: Span,
20     tts: TokenStream,
21 ) -> Box<dyn MacResult + 'cx> {
22     let panic = if sp.rust_2021() { sym::panic_2021 } else { sym::panic_2015 };
23
24     let sp = cx.with_call_site_ctxt(sp);
25
26     MacEager::expr(
27         cx.expr(
28             sp,
29             ExprKind::MacCall(MacCall {
30                 path: Path {
31                     span: sp,
32                     segments: cx
33                         .std_path(&[sym::panic, panic])
34                         .into_iter()
35                         .map(|ident| PathSegment::from_ident(ident))
36                         .collect(),
37                     tokens: None,
38                 },
39                 args: P(MacArgs::Delimited(
40                     DelimSpan::from_single(sp),
41                     MacDelimiter::Parenthesis,
42                     tts,
43                 )),
44                 prior_type_ascription: None,
45             }),
46         ),
47     )
48 }