Skip to content

Commit 3c80038

Browse files
committed
Outline formatting code from the panic! macro
1 parent 15e52b0 commit 3c80038

File tree

29 files changed

+770
-118
lines changed

29 files changed

+770
-118
lines changed

compiler/rustc_ast/src/ast.rs

+6
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,12 @@ pub enum Const {
24392439
No,
24402440
}
24412441

2442+
impl Const {
2443+
pub fn is_const(self) -> bool {
2444+
matches!(self, Const::Yes { .. })
2445+
}
2446+
}
2447+
24422448
/// Item defaultness.
24432449
/// For details see the [RFC #2532](https://github.com/rust-lang/rfcs/pull/2532).
24442450
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]

compiler/rustc_ast/src/format.rs

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::ptr::P;
2+
use crate::Const;
23
use crate::Expr;
4+
use crate::NodeId;
35
use rustc_data_structures::fx::FxHashMap;
46
use rustc_span::symbol::{Ident, Symbol};
57
use rustc_span::Span;
@@ -44,6 +46,20 @@ pub struct FormatArgs {
4446
pub span: Span,
4547
pub template: Vec<FormatArgsPiece>,
4648
pub arguments: FormatArguments,
49+
pub panic: FormatPanicKind,
50+
}
51+
52+
#[derive(Clone, Encodable, Decodable, Debug)]
53+
pub enum FormatPanicKind {
54+
/// Regular `format_args!`.
55+
Format,
56+
57+
/// Format and panic. Used by `panic_args!`.
58+
Panic {
59+
/// The id of the generated cold path function.
60+
id: NodeId,
61+
constness: Const,
62+
},
4763
}
4864

4965
/// A piece of a format template string.

compiler/rustc_ast/src/mut_visit.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ pub trait MutVisitor: Sized {
109109
noop_visit_item_kind(i, self);
110110
}
111111

112+
fn visit_assoc_item_kind(&mut self, i: &mut AssocItemKind) {
113+
noop_visit_assoc_item_kind(i, self);
114+
}
115+
112116
fn flat_map_trait_item(&mut self, i: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> {
113117
noop_flat_map_assoc_item(i, self)
114118
}
@@ -1120,6 +1124,13 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11201124
visitor.visit_ident(ident);
11211125
visitor.visit_vis(vis);
11221126
visit_attrs(attrs, visitor);
1127+
visitor.visit_assoc_item_kind(kind);
1128+
visitor.visit_span(span);
1129+
visit_lazy_tts(tokens, visitor);
1130+
smallvec![item]
1131+
}
1132+
1133+
pub fn noop_visit_assoc_item_kind<T: MutVisitor>(kind: &mut AssocItemKind, visitor: &mut T) {
11231134
match kind {
11241135
AssocItemKind::Const(item) => {
11251136
visit_const_item(item, visitor);
@@ -1147,9 +1158,6 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
11471158
}
11481159
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
11491160
}
1150-
visitor.visit_span(span);
1151-
visit_lazy_tts(tokens, visitor);
1152-
smallvec![item]
11531161
}
11541162

11551163
fn visit_const_item<T: MutVisitor>(
@@ -1313,6 +1321,13 @@ pub fn noop_visit_inline_asm_sym<T: MutVisitor>(
13131321
}
13141322

13151323
pub fn noop_visit_format_args<T: MutVisitor>(fmt: &mut FormatArgs, vis: &mut T) {
1324+
match &mut fmt.panic {
1325+
FormatPanicKind::Format => (),
1326+
FormatPanicKind::Panic { id, constness } => {
1327+
vis.visit_id(id);
1328+
visit_constness(constness, vis);
1329+
}
1330+
}
13161331
for arg in fmt.arguments.all_args_mut() {
13171332
if let FormatArgumentKind::Named(name) = &mut arg.kind {
13181333
vis.visit_ident(name);

compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1790,7 +1790,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
17901790
self.arena.alloc(self.expr_call_mut(span, e, args))
17911791
}
17921792

1793-
fn expr_call_lang_item_fn_mut(
1793+
pub(super) fn expr_call_lang_item_fn_mut(
17941794
&mut self,
17951795
span: Span,
17961796
lang_item: hir::LangItem,
@@ -1811,7 +1811,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18111811
self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args, hir_id))
18121812
}
18131813

1814-
fn expr_lang_item_path(
1814+
pub(super) fn expr_lang_item_path(
18151815
&mut self,
18161816
span: Span,
18171817
lang_item: hir::LangItem,

0 commit comments

Comments
 (0)