Skip to content

Invalid syntax and crash on unit struct with where-clause #3194

Closed
@dtolnay

Description

@dtolnay

Against the current master branch, 4e2f741:

$ echo "mod m { struct S where A: B; }" | cargo run --bin rustfmt

mod m {
    struct S
    mod m { struct S
    where
        A: B;
}

The output is not valid Rust syntax.

If the input is in two files, rustfmt crashes. I am hitting this in dtolnay/syn#536.

src/lib.rs

mod m;

src/m.rs

struct A where B: C;
$ RUST_BACKTRACE=1 cargo run --bin rustfmt -- ../path/to/src/lib.rs 

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', libcore/option.rs:355:21
stack backtrace:
   9: <core::option::Option<T>>::unwrap
  10: rustfmt_nightly::rewrite::RewriteContext::snippet
             at src/rewrite.rs:56
  11: rustfmt_nightly::comment::rewrite_missing_comment
             at src/comment.rs:823
  12: rustfmt_nightly::items::rewrite_comments_before_after_where
             at src/items.rs:2731
  13: rustfmt_nightly::items::rewrite_where_clause_rfc_style
             at src/items.rs:2528
  14: rustfmt_nightly::items::rewrite_where_clause
             at src/items.rs:2630
  15: rustfmt_nightly::items::format_generics
             at src/items.rs:2786
  16: rustfmt_nightly::items::format_unit_struct
             at src/items.rs:1183
  17: rustfmt_nightly::items::format_struct
             at src/items.rs:991
  18: rustfmt_nightly::items::<impl rustfmt_nightly::visitor::FmtVisitor<'a>>::visit_struct
             at src/items.rs:438
  19: rustfmt_nightly::visitor::FmtVisitor::visit_item
             at src/visitor.rs:399
  20: rustfmt_nightly::reorder::<impl rustfmt_nightly::visitor::FmtVisitor<'a>>::visit_items_with_reordering
             at src/reorder.rs:281
  21: rustfmt_nightly::visitor::FmtVisitor::walk_mod_items
             at src/visitor.rs:697
  22: rustfmt_nightly::visitor::FmtVisitor::format_separate_mod
             at src/visitor.rs:781
  23: <rustfmt_nightly::formatting::FormatContext<'a, T>>::format_file
             at ./src/formatting.rs:150
  24: rustfmt_nightly::formatting::format_project
             at ./src/formatting.rs:92
  25: rustfmt_nightly::formatting::<impl rustfmt_nightly::Session<'b, T>>::format_input_inner::{{closure}}
             at ./src/formatting.rs:47
  26: <scoped_tls::ScopedKey<T>>::set
             at scoped-tls-0.1.2/src/lib.rs:155
  27: syntax::with_globals::{{closure}}
             at rustc-ap-syntax-290.0.0/lib.rs:123
  28: <scoped_tls::ScopedKey<T>>::set
             at scoped-tls-0.1.2/src/lib.rs:155
  29: syntax::with_globals
             at rustc-ap-syntax-290.0.0/lib.rs:122
  30: rustfmt_nightly::formatting::<impl rustfmt_nightly::Session<'b, T>>::format_input_inner
             at ./src/formatting.rs:31
  31: <rustfmt_nightly::Session<'b, T>>::format
             at ./src/lib.rs:531
  32: rustfmt::format_and_emit_report
             at src/bin/main.rs:300
  33: rustfmt::format::{{closure}}
             at src/bin/main.rs:272
  34: <rustfmt_nightly::Session<'b, T>>::override_config
             at ./src/lib.rs:539
  35: rustfmt::format
             at src/bin/main.rs:271
  36: rustfmt::execute
             at src/bin/main.rs:199
  37: rustfmt::main
             at src/bin/main.rs:36

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions