Index: gcc/fortran/class.c
===================================================================
*** gcc/fortran/class.c	(revision 203719)
--- gcc/fortran/class.c	(working copy)
*************** along with GCC; see the file COPYING3.
*** 53,58 ****
--- 53,59 ----
  #include "coretypes.h"
  #include "gfortran.h"
  #include "constructor.h"
+ #include "target-memory.h"
  
  /* Inserts a derived type component reference in a data reference chain.
      TS: base type of the ref chain so far, in which we will pick the component
*************** gfc_find_intrinsic_vtab (gfc_typespec *t
*** 2473,2478 ****
--- 2474,2480 ----
  	      int hash;
  	      gfc_namespace *sub_ns;
  	      gfc_namespace *contained;
+ 	      gfc_expr *e;
  
  	      gfc_get_symbol (name, ns, &vtype);
  	      if (!gfc_add_flavor (&vtype->attr, FL_DERIVED, NULL,
*************** gfc_find_intrinsic_vtab (gfc_typespec *t
*** 2498,2509 ****
  	      c->ts.type = BT_INTEGER;
  	      c->ts.kind = 4;
  	      c->attr.access = ACCESS_PRIVATE;
! 	      if (ts->type == BT_CHARACTER)
! 		c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
! 						   NULL, charlen*ts->kind);
! 	      else
  		c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
! 						   NULL, ts->kind);
  
  	      /* Add component _extends.  */
  	      if (!gfc_add_component (vtype, "_extends", &c))
--- 2500,2515 ----
  	      c->ts.type = BT_INTEGER;
  	      c->ts.kind = 4;
  	      c->attr.access = ACCESS_PRIVATE;
! 
! 	      /* Build a minimal expression to make use of
! 		 target-memory.c/gfc_element_size for 'size'. */
! 	      e = gfc_get_expr ();
! 	      e->ts = *ts;
! 	      e->expr_type = EXPR_VARIABLE;
  	      c->initializer = gfc_get_int_expr (gfc_default_integer_kind,
! 						 NULL,
! 						 (int)gfc_element_size (e));
! 	      gfc_free_expr (e);
  
  	      /* Add component _extends.  */
  	      if (!gfc_add_component (vtype, "_extends", &c))
Index: gcc/testsuite/gfortran.dg/unlimited_polymorphic_12.f90
===================================================================
*** gcc/testsuite/gfortran.dg/unlimited_polymorphic_12.f90	(revision 0)
--- gcc/testsuite/gfortran.dg/unlimited_polymorphic_12.f90	(working copy)
***************
*** 0 ****
--- 1,55 ----
+ ! { dg-do run }
+ !
+ ! PR fortran/58793
+ !
+ ! Contributed by  Vladimir Fuka
+ !
+ ! Had the wrong value for the storage_size for complex
+ !
+ module m
+   use iso_fortran_env
+   implicit none
+   integer, parameter :: c1 = real_kinds(1)
+   integer, parameter :: c2 = real_kinds(2)
+   integer, parameter :: c3 = real_kinds(size(real_kinds)-1)
+   integer, parameter :: c4 = real_kinds(size(real_kinds))
+ contains
+  subroutine s(o, k)
+     class(*) :: o
+     integer :: k
+     integer :: sz
+ 
+     select case (k)
+      case (4)
+       sz = 32*2
+      case (8)
+       sz = 64*2
+      case (10,16)
+       sz = 128*2
+      case default
+        call abort()
+     end select
+ 
+     if (storage_size(o) /= sz) call abort()
+     select type (o)
+       type is (complex(c1))
+         if (storage_size(o) /= sz) call abort()
+       type is (complex(c2))
+         if (storage_size(o) /= sz) call abort()
+     end select
+     select type (o)
+       type is (complex(c3))
+         if (storage_size(o) /= sz) call abort()
+       type is (complex(c4))
+         if (storage_size(o) /= sz) call abort()
+     end select
+   end subroutine s
+ end module m
+ 
+ program p
+  use m
+  call s((1._c1, 2._c1), c1)
+  call s((1._c2, 2._c2), c2)
+  call s((1._c3, 2._c3), c3)
+  call s((1._c4, 2._c4), c4)
+ end program p
