Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outline formatting code from the panic! macro #115562

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2439,6 +2439,12 @@ pub enum Const {
No,
}

impl Const {
pub fn is_const(self) -> bool {
matches!(self, Const::Yes { .. })
}
}

/// Item defaultness.
/// For details see the [RFC #2532](/~https://github.com/rust-lang/rfcs/pull/2532).
#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, HashStable_Generic)]
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_ast/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::ptr::P;
use crate::Const;
use crate::Expr;
use crate::NodeId;
use rustc_data_structures::fx::FxHashMap;
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::Span;
Expand Down Expand Up @@ -44,6 +46,20 @@ pub struct FormatArgs {
pub span: Span,
pub template: Vec<FormatArgsPiece>,
pub arguments: FormatArguments,
pub panic: FormatPanicKind,
}

#[derive(Clone, Encodable, Decodable, Debug)]
pub enum FormatPanicKind {
/// Regular `format_args!`.
Format,

/// Format and panic. Used by `panic_args!`.
Panic {
/// The id of the generated cold path function.
id: NodeId,
constness: Const,
},
}

/// A piece of a format template string.
Expand Down
21 changes: 18 additions & 3 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ pub trait MutVisitor: Sized {
noop_visit_item_kind(i, self);
}

fn visit_assoc_item_kind(&mut self, i: &mut AssocItemKind) {
noop_visit_assoc_item_kind(i, self);
}

fn flat_map_trait_item(&mut self, i: P<AssocItem>) -> SmallVec<[P<AssocItem>; 1]> {
noop_flat_map_assoc_item(i, self)
}
Expand Down Expand Up @@ -1120,6 +1124,13 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
visitor.visit_ident(ident);
visitor.visit_vis(vis);
visit_attrs(attrs, visitor);
visitor.visit_assoc_item_kind(kind);
visitor.visit_span(span);
visit_lazy_tts(tokens, visitor);
smallvec![item]
}

pub fn noop_visit_assoc_item_kind<T: MutVisitor>(kind: &mut AssocItemKind, visitor: &mut T) {
match kind {
AssocItemKind::Const(item) => {
visit_const_item(item, visitor);
Expand Down Expand Up @@ -1147,9 +1158,6 @@ pub fn noop_flat_map_assoc_item<T: MutVisitor>(
}
AssocItemKind::MacCall(mac) => visitor.visit_mac_call(mac),
}
visitor.visit_span(span);
visit_lazy_tts(tokens, visitor);
smallvec![item]
}

fn visit_const_item<T: MutVisitor>(
Expand Down Expand Up @@ -1313,6 +1321,13 @@ pub fn noop_visit_inline_asm_sym<T: MutVisitor>(
}

pub fn noop_visit_format_args<T: MutVisitor>(fmt: &mut FormatArgs, vis: &mut T) {
match &mut fmt.panic {
FormatPanicKind::Format => (),
FormatPanicKind::Panic { id, constness } => {
vis.visit_id(id);
visit_constness(constness, vis);
}
}
for arg in fmt.arguments.all_args_mut() {
if let FormatArgumentKind::Named(name) = &mut arg.kind {
vis.visit_ident(name);
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.arena.alloc(self.expr_call_mut(span, e, args))
}

fn expr_call_lang_item_fn_mut(
pub(super) fn expr_call_lang_item_fn_mut(
&mut self,
span: Span,
lang_item: hir::LangItem,
Expand All @@ -1811,7 +1811,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.arena.alloc(self.expr_call_lang_item_fn_mut(span, lang_item, args, hir_id))
}

fn expr_lang_item_path(
pub(super) fn expr_lang_item_path(
&mut self,
span: Span,
lang_item: hir::LangItem,
Expand Down
Loading