Skip to content

format_code_in_doc_comments causes panic for empty code blocks.  #5234

@andrewgazelka

Description

@andrewgazelka

format_code_in_doc_comments causes panic for empty code blocks. For instance:

/// This breaks rustfmt
/// ```
///
/// ```
fn tester() {}

Reproducing

thread 'main' panicked at 'begin <= end (12 <= 11) when slicing `fn main() {}
`', src/tools/rustfmt/src/lib.rs:399:47
stack backtrace:
   0: _rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::str::slice_error_fail
   3: rustfmt_nightly::format_code_block
   4: rustfmt_nightly::comment::rewrite_comment_inner
   5: rustfmt_nightly::comment::identify_comment
   6: rustfmt_nightly::attr::rewrite_initial_doc_comments
   7: <[rustc_ast::ast::Attribute] as rustfmt_nightly::rewrite::Rewrite>::rewrite
   8: <rustfmt_nightly::visitor::FmtVisitor>::visit_attrs
   9: <rustfmt_nightly::visitor::FmtVisitor>::visit_item
  10: <rustfmt_nightly::visitor::FmtVisitor>::visit_items_with_reordering
  11: <rustfmt_nightly::visitor::FmtVisitor>::format_separate_mod
  12: rustfmt_nightly::formatting::format_project::<rustfmt_nightly::Session<std::io::stdio::Stdout>>
  13: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::with::<<rustfmt_nightly::Session<std::io::stdio::Stdout>>::format_input_inner::{closure#0}, core::result::Result<rustfmt_nightly::FormatReport, rustfmt_nightly::ErrorKind>>
  14: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_span::create_session_if_not_set_then<core::result::Result<rustfmt_nightly::FormatReport, rustfmt_nightly::ErrorKind>, <rustfmt_nightly::Session<std::io::stdio::Stdout>>::format_input_inner::{closure#0}>::{closure#0}, core::result::Result<rustfmt_nightly::FormatReport, rustfmt_nightly::ErrorKind>>
  15: <rustfmt_nightly::Session<std::io::stdio::Stdout>>::format
  16: rustfmt::format_and_emit_report::<std::io::stdio::Stdout>
  17: <rustfmt_nightly::Session<std::io::stdio::Stdout>>::override_config::<rustfmt::format::{closure#0}, ()>
  18: rustfmt::execute
  19: rustfmt::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    a-commentsbugPanic, non-idempotency, invalid code, etc.only-with-optionrequires a non-default option value to reproducep-medium

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions