]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fortran: error recovery on duplicate declaration of class variable [PR95710]
authorHarald Anlauf <anlauf@gmx.de>
Fri, 22 Sep 2023 19:06:00 +0000 (21:06 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Sat, 23 Sep 2023 17:21:19 +0000 (19:21 +0200)
gcc/fortran/ChangeLog:

PR fortran/95710
* class.cc (gfc_build_class_symbol): Do not try to build class
container for invalid typespec.
* resolve.cc (resolve_fl_var_and_proc): Prevent NULL pointer
dereference.
(resolve_symbol): Likewise.

gcc/testsuite/ChangeLog:

PR fortran/95710
* gfortran.dg/pr95710.f90: New test.

gcc/fortran/class.cc
gcc/fortran/resolve.cc
gcc/testsuite/gfortran.dg/pr95710.f90 [new file with mode: 0644]

index 9d0c802b8676ac7feeefc777133770e2e02a4a06..5c43b77dba384fed06246c3481c8513c4bf9e135 100644 (file)
@@ -647,6 +647,10 @@ gfc_build_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
 
   gcc_assert (as);
 
+  /* We cannot build the class container now.  */
+  if (attr->class_ok && (!ts->u.derived || !ts->u.derived->components))
+    return false;
+
   /* Class container has already been built with same name.  */
   if (attr->class_ok
       && ts->u.derived->components->attr.dimension >= attr->dimension
index 1042b8c18e8eba741ddcb0f817920f5510cdd9ac..861f69ac20fd79405283416dd6a09d5e1e9de191 100644 (file)
@@ -13326,6 +13326,7 @@ resolve_fl_var_and_proc (gfc_symbol *sym, int mp_flag)
          && sym->ts.u.derived
          && !sym->attr.select_type_temporary
          && !UNLIMITED_POLY (sym)
+         && CLASS_DATA (sym)
          && CLASS_DATA (sym)->ts.u.derived
          && !gfc_type_is_extensible (CLASS_DATA (sym)->ts.u.derived))
        {
@@ -16068,7 +16069,8 @@ resolve_symbol (gfc_symbol *sym)
       specification_expr = saved_specification_expr;
     }
 
-  if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
+  if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived
+      && CLASS_DATA (sym))
     {
       as = CLASS_DATA (sym)->as;
       class_attr = CLASS_DATA (sym)->attr;
diff --git a/gcc/testsuite/gfortran.dg/pr95710.f90 b/gcc/testsuite/gfortran.dg/pr95710.f90
new file mode 100644 (file)
index 0000000..566c38d
--- /dev/null
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! PR fortran/95710 - ICE on duplicate declaration of class variable
+! Contributed by G.Steinmetz
+
+module m
+  interface
+     module function s()
+     end
+  end interface
+end
+submodule(m) m2
+contains
+  module function s()
+    class(*), allocatable :: x
+    class(*), allocatable :: x ! { dg-error "Unclassifiable statement" }
+  end
+end