b/gcc/testsuite/gfortran.dg/inquire_18.f90
new file mode 100644
@@ -0,0 +1,11 @@
+! { dg-do run }
+! PR84412 Wrong "Inquire statement identifies an internal file" error
+program bug
+ implicit none
+ integer :: i
+ character(len=1) :: s
+ write (s,'(i1)') 0
+ open(newUnit=i,file='inquire_18.txt',status='unknown')
+ inquire(unit=i)
+ close(i, status="delete")
+end program bug
@@ -3993,6 +3993,10 @@ finalize_transfer (st_parameter_dt *dtp)
if (dtp->u.p.unit_is_internal)
{
+ /* The unit structure may be reused later so clear the
+ internal unit kind. */
+ dtp->u.p.current_unit->internal_unit_kind = 0;
+
fbuf_destroy (dtp->u.p.current_unit);
if (dtp->u.p.current_unit
&& (dtp->u.p.current_unit->child_dtio == 0)