I've committed the attached patch.  It does two things.
(1) It adds checking for a UNIT number when a file positioning
statement contains ERR=.  (2) It passes a locus so that a
sensible error message is printed,

2018-12-11  Steven G. Kargl  <ka...@gcc.gnu.org>

        PR fortran/88249
        * gfortran.h: Update prototype for gfc_resolve_filepos().
        * io.c (gfc_resolve_filepos): Check for UNIT number if ERR= is present.
        Use passed in locus for error message.
        * resolve.c (gfc_resolve_code): Pass locus in gfc_resolve_filepos()
        call.

2018-12-11  Steven G. Kargl  <ka...@gcc.gnu.org>

        PR fortran/88249
        * gfortran.dg/pr88249.f90: New test.
-- 
Steve
Index: gcc/fortran/gfortran.h
===================================================================
--- gcc/fortran/gfortran.h	(revision 267033)
+++ gcc/fortran/gfortran.h	(working copy)
@@ -3395,7 +3395,7 @@ bool gfc_resolve_open (gfc_open *);
 void gfc_free_close (gfc_close *);
 bool gfc_resolve_close (gfc_close *);
 void gfc_free_filepos (gfc_filepos *);
-bool gfc_resolve_filepos (gfc_filepos *);
+bool gfc_resolve_filepos (gfc_filepos *, locus *);
 void gfc_free_inquire (gfc_inquire *);
 bool gfc_resolve_inquire (gfc_inquire *);
 void gfc_free_dt (gfc_dt *);
Index: gcc/fortran/io.c
===================================================================
--- gcc/fortran/io.c	(revision 267033)
+++ gcc/fortran/io.c	(working copy)
@@ -2849,21 +2849,20 @@ cleanup:
 
 
 bool
-gfc_resolve_filepos (gfc_filepos *fp)
+gfc_resolve_filepos (gfc_filepos *fp, locus *where)
 {
   RESOLVE_TAG (&tag_unit, fp->unit);
   RESOLVE_TAG (&tag_iostat, fp->iostat);
   RESOLVE_TAG (&tag_iomsg, fp->iomsg);
-  if (!gfc_reference_st_label (fp->err, ST_LABEL_TARGET))
-    return false;
 
-  if (!fp->unit && (fp->iostat || fp->iomsg))
+  if (!fp->unit && (fp->iostat || fp->iomsg || fp->err))
     {
-      locus where;
-      where = fp->iostat ? fp->iostat->where : fp->iomsg->where;
-      gfc_error ("UNIT number missing in statement at %L", &where);
+      gfc_error ("UNIT number missing in statement at %L", where);
       return false;
     }
+
+  if (!gfc_reference_st_label (fp->err, ST_LABEL_TARGET))
+    return false;
 
   if (fp->unit->expr_type == EXPR_CONSTANT
       && fp->unit->ts.type == BT_INTEGER
Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(revision 267033)
+++ gcc/fortran/resolve.c	(working copy)
@@ -11545,7 +11545,7 @@ start:
 	case EXEC_ENDFILE:
 	case EXEC_REWIND:
 	case EXEC_FLUSH:
-	  if (!gfc_resolve_filepos (code->ext.filepos))
+	  if (!gfc_resolve_filepos (code->ext.filepos, &code->loc))
 	    break;
 
 	  resolve_branch (code->ext.filepos->err, code);
Index: gcc/testsuite/gfortran.dg/pr88249.f90
===================================================================
--- gcc/testsuite/gfortran.dg/pr88249.f90	(nonexistent)
+++ gcc/testsuite/gfortran.dg/pr88249.f90	(working copy)
@@ -0,0 +1,7 @@
+! { dg-do compile }
+program p
+   backspace (err=1) ! { dg-error "UNIT number missing" }
+   endfile (err=1)   ! { dg-error "UNIT number missing" }
+   flush (err=1)     ! { dg-error "UNIT number missing" }
+   rewind (err=1)    ! { dg-error "UNIT number missing" }
+end

Reply via email to