Repository: rust
Issue: stack overflow in ImproperCTypesVisitor::{check_type_for_ffi, check_variant_for_ffi}
Labels: I-crash,A-lints,T-compiler,C-bug,S-bug-has-test,L-improper_ctypes

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

### Code

```Rust
use std::marker::PhantomData;

#[repr(C)]
struct A<T> {
    a: *const A<A<T>>,
    p: PhantomData<T>,
}

extern "C" {
    fn f(a: *const A<()>);
}

fn main() {}
```


### 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.83.0-nightly (adaff5368 2024-09-12)
binary: rustc
commit-hash: adaff5368b0c7b328a0320a218751d65ab1bba97
commit-date: 2024-09-12
host: x86_64-unknown-linux-gnu
release: 1.83.0-nightly
LLVM version: 19.1.0
```

### Error output

```
error: rustc interrupted by SIGSEGV, printing backtrace
...
note: rustc unexpectedly overflowed its stack! this is a bug
note: maximum backtrace depth reached, frames may have been lost
note: we would appreciate a report at https://github.com/rust-lang/rust
help: you can increase rustc's stack size by setting RUST_MIN_STACK=16777216
note: backtrace dumped due to SIGSEGV! resuming signal
Segmentation fault
```

<details><summary><strong>Backtrace</strong></summary>
<p>

```
0x00007ffff61a67d4 in <rustc_middle::ty::context::CtxtInterners>::intern_ty () from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
(gdb) bt
#0  0x00007ffff61a67d4 in <rustc_middle::ty::context::CtxtInterners>::intern_ty () from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#1  0x00007ffff625be58 in <rustc_middle::ty::Ty as rustc_type_ir::fold::TypeSuperFoldable<rustc_middle::ty::context::TyCtxt>>::super_fold_with::<rustc_type_ir::binder::ArgFolder<rustc_middle::ty::context::TyCtxt>> () from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#2  0x00007ffff639b7e0 in <&rustc_middle::ty::list::RawList<(), rustc_middle::ty::generic_args::GenericArg> as rustc_type_ir::fold::TypeFoldable<rustc_middle::ty::context::TyCtxt>>::try_fold_with::<rustc_type_ir::binder::ArgFolder<rustc_middle::ty::context::TyCtxt>> ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#3  0x00007ffff625bb48 in <rustc_middle::ty::Ty as rustc_type_ir::fold::TypeSuperFoldable<rustc_middle::ty::context::TyCtxt>>::super_fold_with::<rustc_type_ir::binder::ArgFolder<rustc_middle::ty::context::TyCtxt>> () from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#4  0x00007ffff625bc31 in <rustc_middle::ty::Ty as rustc_type_ir::fold::TypeSuperFoldable<rustc_middle::ty::context::TyCtxt>>::super_fold_with::<rustc_type_ir::binder::ArgFolder<rustc_middle::ty::context::TyCtxt>> () from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#5  0x00007ffff6275014 in <rustc_middle::ty::FieldDef>::ty () from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#6  0x00007ffff50ae576 in <rustc_lint::types::ImproperCTypesVisitor>::check_variant_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#7  0x00007ffff50af1bf in <rustc_lint::types::ImproperCTypesVisitor>::check_type_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#8  0x00007ffff50ae5bc in <rustc_lint::types::ImproperCTypesVisitor>::check_variant_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#9  0x00007ffff50af1bf in <rustc_lint::types::ImproperCTypesVisitor>::check_type_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#10 0x00007ffff50ae5bc in <rustc_lint::types::ImproperCTypesVisitor>::check_variant_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#11 0x00007ffff50af1bf in <rustc_lint::types::ImproperCTypesVisitor>::check_type_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#12 0x00007ffff50ae5bc in <rustc_lint::types::ImproperCTypesVisitor>::check_variant_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
#13 0x00007ffff50af1bf in <rustc_lint::types::ImproperCTypesVisitor>::check_type_for_ffi ()
   from /home/tm/.rustup/toolchains/stage1/lib/librustc_driver-14c009390c37828b.so
...
```
</p>
</details>

<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

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

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