Repository: rust
Issue: ICE on trying to fmt an ast!
Labels: I-ICE,A-macros,T-compiler,A-incr-comp,C-bug,A-proc-macros

Apparently if you ask rustc to print an AST during a macro expansion it can get mad!

### Code

The last diff looked like:

```diff
 fn sniff_ast(ast: &syn::DeriveInput) -> bool {
     // This was the latest change:
-    eprintln!("{:?}", ast);
+    eprintln!("{:#?}", ast);
     // a bunch of other code amounting to
     traverse_ast_for_answer(ast)
 }
```


### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.71.1 (eb26296b5 2023-08-03)
binary: rustc
commit-hash: eb26296b556cef10fb713a38f3d16b9886080f26
commit-date: 2023-08-03
host: x86_64-unknown-linux-gnu
release: 1.71.1
LLVM version: 16.0.5
```

### Error output

```
   Compiling pgrx-pg-sys v0.10.0-beta.3 (~/tcdi/pgrx/pgrx-pg-sys)
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_expand/src/placeholders.rs:199:45
stack backtrace:
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
   Compiling pgrx-pg-sys v0.10.0-beta.3 (~/tcdi/pgrx/pgrx-pg-sys)
thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', compiler/rustc_expand/src/placeholders.rs:199:45
stack backtrace:
   0:     0x7fe5d3ba8071 - std::backtrace_rs::backtrace::libunwind::trace::h782cc21a5acaf6cb
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7fe5d3ba8071 - std::backtrace_rs::backtrace::trace_unsynchronized::hc579eb24ab204515
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fe5d3ba8071 - std::sys_common::backtrace::_print_fmt::h7223525cfdbacda2
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/sys_common/backtrace.rs:65:5
   3:     0x7fe5d3ba8071 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hbd7d55b7108d2ab8
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7fe5d3c07d9f - core::fmt::rt::Argument::fmt::hb4f4a02b9bd9dd49
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/fmt/rt.rs:138:9
   5:     0x7fe5d3c07d9f - core::fmt::write::h6d54cd7c9e155ec5
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/fmt/mod.rs:1094:21
   6:     0x7fe5d3b9b311 - std::io::Write::write_fmt::h6a453a71c692f63b
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/io/mod.rs:1713:15
   7:     0x7fe5d3ba7e85 - std::sys_common::backtrace::_print::h1cbaa8b42678f928
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7fe5d3ba7e85 - std::sys_common::backtrace::print::h4ddf81241a51b337
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7fe5d3baab47 - std::panicking::default_hook::{{closure}}::hff91f1f484ade5cd
  10:     0x7fe5d3baa934 - std::panicking::default_hook::h21f14afd59f7aef9
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/panicking.rs:288:9
  11:     0x7fe5d6d350cb - <rustc_driver_impl[a4629e012e48ffc1]::install_ice_hook::{closure#0} as core[95ee2d8017598cc7]::ops::function::FnOnce<(&core[95ee2d8017598cc7]::panic::panic_info::PanicInfo,)>>::call_once::{shim:vtable#0}
  12:     0x7fe5d3bab28d - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::ha1f76bdae3c27ab6
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/alloc/src/boxed.rs:1999:9
  13:     0x7fe5d3bab28d - std::panicking::rust_panic_with_hook::h45f66047b14c555c
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/panicking.rs:709:13
  14:     0x7fe5d3baafe1 - std::panicking::begin_panic_handler::{{closure}}::h49d1a88ef0908eb4
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/panicking.rs:595:13
  15:     0x7fe5d3ba84a6 - std::sys_common::backtrace::__rust_end_short_backtrace::hccebf9e57f8cc425
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/sys_common/backtrace.rs:151:18
  16:     0x7fe5d3baad72 - rust_begin_unwind
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/panicking.rs:593:5
  17:     0x7fe5d3c04023 - core::panicking::panic_fmt::h54ec9d0e3180a83d
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/panicking.rs:67:14
  18:     0x7fe5d3c040b3 - core::panicking::panic::h65157a6ac7f1357a
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/core/src/panicking.rs:117:5
  19:     0x7fe5d5c39afd - <rustc_expand[5bffa61e16ac72eb]::placeholders::PlaceholderExpander as rustc_ast[abe0d17221ed21f0]::mut_visit::MutVisitor>::flat_map_item
  20:     0x7fe5d5c394f9 - <smallvec[9e5e2309b9d6649b]::SmallVec<[rustc_ast[abe0d17221ed21f0]::ptr::P<rustc_ast[abe0d17221ed21f0]::ast::Item>; 1usize]> as rustc_data_structures[8548742d45a93e9b]::flat_map_in_place::FlatMapInPlace<rustc_ast[abe0d17221ed21f0]::ptr::P<rustc_ast[abe0d17221ed21f0]::ast::Item>>>::flat_map_in_place::<<rustc_expand[5bffa61e16ac72eb]::expand::AstFragment>::mut_visit_with<rustc_expand[5bffa61e16ac72eb]::placeholders::PlaceholderExpander>::{closure#2}, smallvec[9e5e2309b9d6649b]::SmallVec<[rustc_ast[abe0d17221ed21f0]::ptr::P<rustc_ast[abe0d17221ed21f0]::ast::Item>; 1usize]>>
  21:     0x7fe5d5a1dcac - <rustc_expand[5bffa61e16ac72eb]::expand::MacroExpander>::fully_expand_fragment
  22:     0x7fe5d5da586e - <rustc_expand[5bffa61e16ac72eb]::expand::MacroExpander>::expand_crate
  23:     0x7fe5d5da4b40 - <rustc_session[b7d9e6f930bc0bce]::session::Session>::time::<rustc_ast[abe0d17221ed21f0]::ast::Crate, rustc_interface[c65fde630b50e8]::passes::configure_and_expand::{closure#1}>
  24:     0x7fe5d5d57add - rustc_interface[c65fde630b50e8]::passes::resolver_for_lowering
  25:     0x7fe5d624892a - rustc_query_impl[6e478c483b869c25]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[6e478c483b869c25]::query_impl::resolver_for_lowering::dynamic_query::{closure#2}::{closure#0}, rustc_middle[c682e143f9d710c7]::query::erase::Erased<[u8; 8usize]>>
  26:     0x7fe5d6248919 - <rustc_query_impl[6e478c483b869c25]::query_impl::resolver_for_lowering::dynamic_query::{closure#2} as core[95ee2d8017598cc7]::ops::function::FnOnce<(rustc_middle[c682e143f9d710c7]::ty::context::TyCtxt, ())>>::call_once
  27:     0x7fe5d6502b2d - rustc_query_system[93b6edfdc601e81a]::query::plumbing::try_execute_query::<rustc_query_impl[6e478c483b869c25]::DynamicConfig<rustc_query_system[93b6edfdc601e81a]::query::caches::SingleCache<rustc_middle[c682e143f9d710c7]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[6e478c483b869c25]::plumbing::QueryCtxt, true>
  28:     0x7fe5d66c7540 - rustc_query_impl[6e478c483b869c25]::query_impl::resolver_for_lowering::get_query_incr::__rust_end_short_backtrace
  29:     0x7fe5d5f90520 - <rustc_interface[c65fde630b50e8]::queries::QueryResult<&rustc_middle[c682e143f9d710c7]::ty::context::GlobalCtxt>>::enter::<&rustc_data_structures[8548742d45a93e9b]::steal::Steal<(rustc_middle[c682e143f9d710c7]::ty::ResolverAstLowering, alloc[dfb0c3e97fe8b89b]::rc::Rc<rustc_ast[abe0d17221ed21f0]::ast::Crate>)>, rustc_driver_impl[a4629e012e48ffc1]::run_compiler::{closure#1}::{closure#2}::{closure#2}>
  30:     0x7fe5d5f8f397 - <rustc_interface[c65fde630b50e8]::interface::Compiler>::enter::<rustc_driver_impl[a4629e012e48ffc1]::run_compiler::{closure#1}::{closure#2}, core[95ee2d8017598cc7]::result::Result<core[95ee2d8017598cc7]::option::Option<rustc_interface[c65fde630b50e8]::queries::Linker>, rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>>
  31:     0x7fe5d5f8d5c4 - rustc_span[c2ff3b0962b898e5]::set_source_map::<core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>, rustc_interface[c65fde630b50e8]::interface::run_compiler<core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>, rustc_driver_impl[a4629e012e48ffc1]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  32:     0x7fe5d5f8d1a1 - <scoped_tls[24938f349c8d16f0]::ScopedKey<rustc_span[c2ff3b0962b898e5]::SessionGlobals>>::set::<rustc_interface[c65fde630b50e8]::interface::run_compiler<core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>, rustc_driver_impl[a4629e012e48ffc1]::run_compiler::{closure#1}>::{closure#0}, core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>>
  33:     0x7fe5d5f8c796 - std[edecf719d62c0e6b]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[c65fde630b50e8]::util::run_in_thread_pool_with_globals<rustc_interface[c65fde630b50e8]::interface::run_compiler<core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>, rustc_driver_impl[a4629e012e48ffc1]::run_compiler::{closure#1}>::{closure#0}, core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>>
  34:     0x7fe5d65e44c5 - <<std[edecf719d62c0e6b]::thread::Builder>::spawn_unchecked_<rustc_interface[c65fde630b50e8]::util::run_in_thread_pool_with_globals<rustc_interface[c65fde630b50e8]::interface::run_compiler<core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>, rustc_driver_impl[a4629e012e48ffc1]::run_compiler::{closure#1}>::{closure#0}, core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[95ee2d8017598cc7]::result::Result<(), rustc_span[c2ff3b0962b898e5]::ErrorGuaranteed>>::{closure#1} as core[95ee2d8017598cc7]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  35:     0x7fe5d3bb57b5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::he3e5dbdfabe0b668
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/alloc/src/boxed.rs:1985:9
  36:     0x7fe5d3bb57b5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h246f7c7964633611
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/alloc/src/boxed.rs:1985:9
  37:     0x7fe5d3bb57b5 - std::sys::unix::thread::Thread::new::thread_start::hadf9e3501ff0df23
                               at /rustc/eb26296b556cef10fb713a38f3d16b9886080f26/library/std/src/sys/unix/thread.rs:108:17
  38:     0x7fe5d38a99eb - <unknown>
  39:     0x7fe5d392e23c - <unknown>
  40:                0x0 - <unknown>

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.71.1 (eb26296b5 2023-08-03) running on x86_64-unknown-linux-gnu

note: compiler flags: --crate-type lib -C embed-bitcode=no -C debuginfo=line-tables-only -C incremental=[REDACTED]

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [resolver_for_lowering] getting the resolver for lowering
end of query stack
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x31046f3)[0x7fe5d6d426f3]
/usr/lib/libc.so.6(+0x3e710)[0x7fe5d385b710]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x1e16abb)[0x7fe5d5a54abb]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x1ffbc32)[0x7fe5d5c39c32]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(_RNvXs_NtCs7THNWrOn2o9_12rustc_expand12placeholdersNtB4_19PlaceholderExpanderNtNtCseKU1s2neXrU_9rustc_ast9mut_visit10MutVisitor13flat_map_item+0x3c2)[0x7fe5d5c39bb2]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x1ffb4f9)[0x7fe5d5c394f9]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(_RNvMs1_NtCs7THNWrOn2o9_12rustc_expand6expandNtB5_13MacroExpander21fully_expand_fragment+0x356c)[0x7fe5d5a1dcac]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(_RNvMs1_NtCs7THNWrOn2o9_12rustc_expand6expandNtB5_13MacroExpander12expand_crate+0x28e)[0x7fe5d5da586e]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x2166b40)[0x7fe5d5da4b40]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(_RNvNtCs47JDGWQFpQ_15rustc_interface6passes21resolver_for_lowering+0x5dd)[0x7fe5d5d57add]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x260a92a)[0x7fe5d624892a]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x260a919)[0x7fe5d6248919]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x28c4b2d)[0x7fe5d6502b2d]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x2a89540)[0x7fe5d66c7540]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x2352520)[0x7fe5d5f90520]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x2351397)[0x7fe5d5f8f397]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x234f5c4)[0x7fe5d5f8d5c4]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x234f1a1)[0x7fe5d5f8d1a1]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x234e796)[0x7fe5d5f8c796]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/librustc_driver-92234e9dc706375c.so(+0x29a64c5)[0x7fe5d65e44c5]
~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/libstd-7b9f6349d87c69a1.so(rust_metadata_std_edecf719d62c0e6b+0x1017b5)[0x7fe5d3bb57b5]
/usr/lib/libc.so.6(+0x8c9eb)[0x7fe5d38a99eb]
/usr/lib/libc.so.6(+0x11123c)[0x7fe5d392e23c]
error: could not compile `pgrx-pg-sys` (lib)

Caused by:
  process didn't exit successfully: `~/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/bin/rustc --crate-name pgrx_pg_sys --edition=2021 pgrx-pg-sys/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=line-tables-only --cfg 'feature="default"' --cfg 'feature="pg13"' -C metadata=48bd8cb881db5e3b -C extra-filename=-48bd8cb881db5e3b --out-dir ~/tcdi/pgrx/target/debug/deps -C incremental=~/tcdi/pgrx/target/debug/incremental -L dependency=~/tcdi/pgrx/target/debug/deps --extern libc=~/tcdi/pgrx/target/debug/deps/liblibc-e1d28c711bc2da07.rmeta --extern memoffset=~/tcdi/pgrx/target/debug/deps/libmemoffset-8301fcdb496dd68d.rmeta --extern pgrx_macros=~/tcdi/pgrx/target/debug/deps/libpgrx_macros-2aa717b9800894e6.so --extern pgrx_sql_entity_graph=~/tcdi/pgrx/target/debug/deps/libpgrx_sql_entity_graph-f44c013618a3386a.rmeta --extern serde=~/tcdi/pgrx/target/debug/deps/libserde-ea1936b4229d11ae.rmeta --extern sptr=~/tcdi/pgrx/target/debug/deps/libsptr-7713ded1ab1e3145.rmeta` (signal: 11, SIGSEGV: invalid memory reference)
```

</p>
</details>


<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"vincenzopalazzo"}$$TRIAGEBOT_ASSIGN_DATA_END -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->