2021-01-09 15:52:06 +00:00
|
|
|
use rustc_ast::ptr::P;
|
2023-08-01 23:56:26 +00:00
|
|
|
use rustc_ast::token::Delimiter;
|
2021-01-09 15:52:06 +00:00
|
|
|
use rustc_ast::tokenstream::{DelimSpan, TokenStream};
|
|
|
|
use rustc_ast::*;
|
|
|
|
use rustc_expand::base::*;
|
|
|
|
use rustc_span::Span;
|
2021-10-07 10:44:46 +00:00
|
|
|
use rustc_span::edition::Edition;
|
2021-01-09 15:52:06 +00:00
|
|
|
use rustc_span::symbol::sym;
|
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// This expands to either
|
|
|
|
/// - `$crate::panic::panic_2015!(...)` or
|
|
|
|
/// - `$crate::panic::panic_2021!(...)`
|
|
|
|
/// depending on the edition.
|
|
|
|
///
|
|
|
|
/// This is used for both std::panic!() and core::panic!().
|
|
|
|
///
|
|
|
|
/// `$crate` will refer to either the `std` or `core` crate depending on which
|
|
|
|
/// one we're expanding from.
|
2024-04-25 21:56:48 +00:00
|
|
|
pub(crate) fn expand_panic<'cx>(
|
2021-01-09 15:52:06 +00:00
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 02:55:17 +00:00
|
|
|
) -> MacroExpanderResult<'cx> {
|
2022-01-21 22:04:06 +00:00
|
|
|
let mac = if use_panic_2021(sp) { sym::panic_2021 } else { sym::panic_2015 };
|
|
|
|
expand(mac, cx, sp, tts)
|
|
|
|
}
|
2021-01-09 15:52:06 +00:00
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// This expands to either
|
|
|
|
/// - `$crate::panic::unreachable_2015!(...)` or
|
|
|
|
/// - `$crate::panic::unreachable_2021!(...)`
|
|
|
|
/// depending on the edition.
|
2024-04-25 21:56:48 +00:00
|
|
|
pub(crate) fn expand_unreachable<'cx>(
|
2022-01-21 22:04:06 +00:00
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 02:55:17 +00:00
|
|
|
) -> MacroExpanderResult<'cx> {
|
2022-01-21 22:04:06 +00:00
|
|
|
let mac = if use_panic_2021(sp) { sym::unreachable_2021 } else { sym::unreachable_2015 };
|
|
|
|
expand(mac, cx, sp, tts)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand<'cx>(
|
|
|
|
mac: rustc_span::Symbol,
|
compiler: fix few needless_pass_by_ref_mut clippy lints
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:306:28
|
306 | fn err_duplicate_option(p: &mut Parser<'_>, symbol: Symbol, span: Span) {
| ^^^^^^^^^^^^^^^ help: consider changing to: `&Parser<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:318:8
|
318 | p: &mut Parser<'a>,
| ^^^^^^^^^^^^^^^ help: consider changing to: `&Parser<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\assert.rs:114:25
|
114 | fn parse_assert<'a>(cx: &mut ExtCtxt<'a>, sp: Span, stream: TokenStream) -> PResult<'a, Assert> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\asm.rs:32:10
|
32 | ecx: &mut ExtCtxt<'a>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\test.rs:99:9
|
99 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\source_util.rs:237:9
|
237 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:809:10
|
809 | ecx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:737:10
|
737 | ecx: &mut ExtCtxt<'a>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:68:24
|
68 | fn parse_args<'a>(ecx: &mut ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, MacroInput> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\format.rs:607:10
|
607 | ecx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\edition_panic.rs:43:9
|
43 | cx: &'cx mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\concat_bytes.rs:11:9
|
11 | cx: &mut ExtCtxt<'_>,
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\cfg.rs:38:22
|
38 | fn parse_cfg<'a>(cx: &mut ExtCtxt<'a>, span: Span, tts: TokenStream) -> PResult<'a, ast::MetaItem> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'a>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
warning: this argument is a mutable reference, but not used mutably
--> compiler\rustc_builtin_macros\src\cfg_accessible.rs:13:28
|
13 | fn validate_input<'a>(ecx: &mut ExtCtxt<'_>, mi: &'a ast::MetaItem) -> Option<&'a ast::Path> {
| ^^^^^^^^^^^^^^^^ help: consider changing to: `&ExtCtxt<'_>`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_ref_mut
2024-03-28 09:04:00 +00:00
|
|
|
cx: &'cx ExtCtxt<'_>,
|
2022-01-21 22:04:06 +00:00
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
2024-03-12 02:55:17 +00:00
|
|
|
) -> MacroExpanderResult<'cx> {
|
2021-01-09 15:52:06 +00:00
|
|
|
let sp = cx.with_call_site_ctxt(sp);
|
|
|
|
|
2024-03-12 02:55:17 +00:00
|
|
|
ExpandResult::Ready(MacEager::expr(
|
2021-01-09 15:52:06 +00:00
|
|
|
cx.expr(
|
|
|
|
sp,
|
2022-08-12 02:20:10 +00:00
|
|
|
ExprKind::MacCall(P(MacCall {
|
2021-01-09 15:52:06 +00:00
|
|
|
path: Path {
|
|
|
|
span: sp,
|
|
|
|
segments: cx
|
2022-01-21 22:04:06 +00:00
|
|
|
.std_path(&[sym::panic, mac])
|
2021-01-09 15:52:06 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|ident| PathSegment::from_ident(ident))
|
|
|
|
.collect(),
|
|
|
|
tokens: None,
|
|
|
|
},
|
2022-11-18 00:24:21 +00:00
|
|
|
args: P(DelimArgs {
|
|
|
|
dspan: DelimSpan::from_single(sp),
|
2023-08-01 23:56:26 +00:00
|
|
|
delim: Delimiter::Parenthesis,
|
2022-11-18 00:24:21 +00:00
|
|
|
tokens: tts,
|
|
|
|
}),
|
2022-08-12 02:20:10 +00:00
|
|
|
})),
|
2021-01-09 15:52:06 +00:00
|
|
|
),
|
2024-03-12 02:55:17 +00:00
|
|
|
))
|
2021-01-09 15:52:06 +00:00
|
|
|
}
|
2021-10-07 10:44:46 +00:00
|
|
|
|
2024-04-25 21:56:48 +00:00
|
|
|
pub(crate) fn use_panic_2021(mut span: Span) -> bool {
|
2022-03-30 05:39:38 +00:00
|
|
|
// To determine the edition, we check the first span up the expansion
|
2021-10-07 10:44:46 +00:00
|
|
|
// stack that does not have #[allow_internal_unstable(edition_panic)].
|
|
|
|
// (To avoid using the edition of e.g. the assert!() or debug_assert!() definition.)
|
|
|
|
loop {
|
|
|
|
let expn = span.ctxt().outer_expn_data();
|
|
|
|
if let Some(features) = expn.allow_internal_unstable {
|
|
|
|
if features.iter().any(|&f| f == sym::edition_panic) {
|
|
|
|
span = expn.call_site;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break expn.edition >= Edition::Edition2021;
|
|
|
|
}
|
|
|
|
}
|