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

"panicked at 'bad span: .: `'" when formatting tuple.0.0` #4355

Closed
yvt opened this issue Jul 28, 2020 · 8 comments · Fixed by #4469
Closed

"panicked at 'bad span: .: `'" when formatting tuple.0.0` #4355

yvt opened this issue Jul 28, 2020 · 8 comments · Fixed by #4469
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@yvt
Copy link

yvt commented Jul 28, 2020

Describe the bug

The tuple.0.0 syntax, whose support was recently merged to rustc (rust-lang/rust#71322), causes rustfmt to panic.

To Reproduce

fn main() {
    let _ = ((1,),).0.0;
}

Playground Link

thread 'main' panicked at 'bad span: `.`: ``', src/tools/rustfmt/src/source_map.rs:52:13
Backtrace
stack backtrace:
   0: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
   1: core::fmt::write
   2: std::io::Write::write_fmt
   3: std::panicking::default_hook::{{closure}}
   4: std::panicking::default_hook
   5: std::panicking::rust_panic_with_hook
   6: rust_begin_unwind
   7: std::panicking::begin_panic_fmt
   8: <rustfmt_nightly::visitor::SnippetProvider as rustfmt_nightly::source_map::SpanUtils>::span_before::{{closure}}
   9: <rustfmt_nightly::visitor::SnippetProvider as rustfmt_nightly::source_map::SpanUtils>::span_before
  10: rustfmt_nightly::chains::rewrite_chain
  11: rustfmt_nightly::expr::format_expr
  12: rustfmt_nightly::expr::rewrite_assign_rhs_with
  13: rustfmt_nightly::items::<impl rustfmt_nightly::rewrite::Rewrite for rustc_ap_rustc_ast::ast::Local>::rewrite
  14: rustfmt_nightly::stmt::format_stmt
  15: rustfmt_nightly::visitor::FmtVisitor::walk_stmts
  16: rustfmt_nightly::visitor::FmtVisitor::visit_block
  17: rustfmt_nightly::visitor::FmtVisitor::visit_fn
  18: rustfmt_nightly::visitor::FmtVisitor::visit_item
  19: rustfmt_nightly::reorder::<impl rustfmt_nightly::visitor::FmtVisitor>::visit_items_with_reordering
  20: rustfmt_nightly::visitor::FmtVisitor::walk_mod_items
  21: rustfmt_nightly::visitor::FmtVisitor::format_separate_mod
  22: rustfmt_nightly::formatting::format_project
  23: scoped_tls::ScopedKey<T>::set
  24: rustc_ap_rustc_ast::attr::with_session_globals
  25: rustfmt_nightly::Session<T>::format
  26: rustfmt::format_and_emit_report
  27: rustfmt_nightly::Session<T>::override_config
  28: rustfmt::execute
  29: rustfmt::main
  30: std::rt::lang_start::{{closure}}
  31: std::rt::lang_start_internal
  32: main

Expected behavior

rustfmt completes without panicking.

Meta

  • rustfmt version: rustfmt 1.4.19-nightly (cef1c0d5 2020-07-21)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: rustfmt +nightly-2020-07-27 filename.rs
@yvt yvt added the bug Panic, non-idempotency, invalid code, etc. label Jul 28, 2020
@ayazhafiz
Copy link
Contributor

I think the rustc AST may be broken. I just ran ().0 and ().0.0 through the parser; the former parses to a field access of form Field("()", "0"). The latter parses to Field("().0.0", "0.0"), when it should be Field("()", "0.0") (since "0.0" is being treated as an identifier, not an access chain? also strange).

Anyway, I think the AST would have to be fixed because rustfmt relies on it to deconstruct the chain that should be formatted.

@zeroflaw
Copy link

Can confirm I just hit this issue as well.

@topecongiro
Copy link
Contributor

It seems that spans related to nested tuples are imprecise.

  • Each tuple nested index contains an entire span of indexes (e.g., the span of 0 and 1 of 0.1 is 0.1, not 0 nor 1)
  • The span of the tuple expression contains nested indexes

@JakkuSakura
Copy link

I have the following code which compiles.

struct Baz{val: i32}
struct Bar(Baz);
struct Foo(Bar);

fn main() {
    let x = Foo(Bar(Baz{val:1}));
    x.0.0.val;
}
cargo fmt
thread 'main' panicked at 'bad span: `.`: ``', src/tools/rustfmt/src/source_map.rs:52:13
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

On another machine, rustfmt gives
image

rust-lang/rust#71322
intellij-rust/intellij-rust#6154
intellij-rust/intellij-rust#6029

@petrochenkov
Copy link
Contributor

This should be fixed in rust-lang/rust#77774, but needs confirmation.

bors added a commit to rust-lang-ci/rust that referenced this issue Oct 11, 2020
rustc_parse: More precise spans for `tuple.0.0`

This should help with rust-lang/rustfmt#4355, but I haven't verified, cc `@calebcartwright.`
@calebcartwright calebcartwright removed the blocked Blocked on rustc, an RFC, etc. label Oct 14, 2020
@calebcartwright calebcartwright pinned this issue Oct 26, 2020
@mk12
Copy link

mk12 commented Nov 6, 2020

This doesn't seem to be fixed. The same playground link in the original post still panics on rustfmt.

When I run locally on the repro example:

fn main() {
    let _ = ((1,),).0.0;
}

With nightly on linux:

$ rustfmt +nightly --version
rustfmt 1.4.22-nightly (97d0301 2020-10-04)

I get this:

$ env RUST_BACKTRACE=1 rustfmt +nightly repro.rs
thread 'main' panicked at 'bad span: `.`: ``', src/tools/rustfmt/src/source_map.rs:52:13
stack backtrace:
   0: rust_begin_unwind
             at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:483:5
   1: std::panicking::begin_panic_fmt
             at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:437:5
   2: <rustfmt_nightly::visitor::SnippetProvider as rustfmt_nightly::source_map::SpanUtils>::span_before::{{closure}}
   3: rustfmt_nightly::chains::rewrite_chain
   4: rustfmt_nightly::expr::format_expr
   5: rustfmt_nightly::expr::rewrite_assign_rhs_with
   6: rustfmt_nightly::items::<impl rustfmt_nightly::rewrite::Rewrite for rustc_ap_rustc_ast::ast::Local>::rewrite
   7: rustfmt_nightly::stmt::format_stmt
   8: rustfmt_nightly::visitor::FmtVisitor::walk_stmts
   9: rustfmt_nightly::visitor::FmtVisitor::visit_block
  10: rustfmt_nightly::visitor::FmtVisitor::visit_fn
  11: rustfmt_nightly::visitor::FmtVisitor::visit_item
  12: rustfmt_nightly::reorder::<impl rustfmt_nightly::visitor::FmtVisitor>::visit_items_with_reordering
  13: rustfmt_nightly::visitor::FmtVisitor::format_separate_mod
  14: rustfmt_nightly::formatting::format_project
  15: scoped_tls::ScopedKey<T>::set
  16: rustfmt_nightly::Session<T>::format
  17: rustfmt::format_and_emit_report
  18: rustfmt_nightly::Session<T>::override_config
  19: rustfmt::execute
  20: rustfmt::main
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

@calebcartwright
Copy link
Member

@mk12 - this is fixed but we haven't been able to get it released yet due to various blockers upstream

@calebcartwright
Copy link
Member

calebcartwright commented Nov 9, 2020

For those tracking here, this is fixed from v1.4.23 onward, and v1.4.24 is available in the latest nightly.

However, please note that for historical reasons (that predate the semi-recent rustc changes) rustfmt formats these with a space inserted to prevent syntax errors (#1063). Due to rustfmt's stability guarantee, we cannot change that default behavior as it would break existing formatting. If you find that space to be problematic, then you can opt into the v2 formatting by adding version = "Two" to the rustfmt config file or use the work around with ()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants