Describe the bug
The tuple.0.0 syntax, whose support was recently merged to rustc (https://github.com/rust-lang/rust/pull/71322), causes rustfmt to panic.
To Reproduce
fn main() {
let _ = ((1,),).0.0;
}
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 1.4.19-nightly (cef1c0d5 2020-07-21)rustfmt +nightly-2020-07-27 filename.rsI 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.
Can confirm I just hit this issue as well.
It seems that spans related to nested tuples are imprecise.
0 and 1 of 0.1 is 0.1, not 0 nor 1)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

https://github.com/rust-lang/rust/pull/71322
https://github.com/intellij-rust/intellij-rust/issues/6154
https://github.com/intellij-rust/intellij-rust/issues/6029
This should be fixed in https://github.com/rust-lang/rust/pull/77774, but needs confirmation.
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.
@mk12 - this is fixed but we haven't been able to get it released yet due to various blockers upstream
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 ()