Bug 68153 - ICE for intrinsic reshape with negative dim in effective shape
Summary: ICE for intrinsic reshape with negative dim in effective shape
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 5.2.1
: P3 normal
Target Milestone: ---
Assignee: kargls
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-10-29 18:25 UTC by Gerhard Steinmetz
Modified: 2015-11-08 19:01 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-10-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Gerhard Steinmetz 2015-10-29 18:25:16 UTC
This code with negative dimension(s) in shape sh :

$ cat z1.f90
program p
   integer, parameter :: sh(2) = [2, -2]
   integer, parameter :: a(2,2) = reshape([1, 2, 3, 4], sh)
   print *, a
end

$ gfortran -g -O0 -Wall -fcheck=all z1.f90
f951: internal compiler error: in gfc_simplify_reshape, at fortran/simplify.c:5091

---

$ cat z4.f90
program p
   integer, parameter :: sh(2) = -2
   integer, parameter :: a(2,2) = reshape([1, 2, 3, 4], sh)
   print *, a
end

$ gfortran -g -O0 -Wall -fcheck=all z4.f90
f951: internal compiler error: in gfc_simplify_reshape, at fortran/simplify.c:5091
Comment 1 Gerhard Steinmetz 2015-10-29 18:26:10 UTC
Detected, but pointing to the line before :

$ cat z3.f90
program p
   integer, parameter :: sh(2) = [2, 2]
   integer, parameter :: a(2,2) = reshape([1, 2, 3, 4], -sh)
   print *, a
end

$ gfortran -g -O0 -Wall -fcheck=all z3.f90
z3.f90:2:34:

    integer, parameter :: sh(2) = [2, 2]
                                  1
Error: 'shape' argument of 'reshape' intrinsic at (1) has negative element (-2)
Comment 2 Dominique d'Humieres 2015-10-29 22:39:03 UTC
Confirmed from 4.8 up to trunk (6.0). Note that the error emitted for the code in comment 1 is quite misleading.
Comment 3 kargls 2015-10-30 06:26:51 UTC
This one is interesting.  gfc_check_reshape isn't checking
'sh' because it thinks 'sh' is a variable instead of a
named constant.  So, when gfc_simplify_reshape is called,
it runs into an gcc_assert that the element is greater
than zero because gfc_check_reshape should have emitted
an error.  If the gcc_assert is removed, an error message
is generated.
Comment 4 kargls 2015-11-01 20:04:08 UTC
I have a patch.
Comment 5 kargls 2015-11-07 20:18:49 UTC
Author: kargl
Date: Sat Nov  7 20:18:17 2015
New Revision: 229939

URL: https://gcc.gnu.org/viewcvs?rev=229939&root=gcc&view=rev
Log:
2015-11-07  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/68153
	* check.c (gfc_check_reshape): Improve check for valid SHAPE argument.

2015-11-07  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/68153
	* gfortran.dg/pr68153.f90: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/pr68153.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/check.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 kargls 2015-11-08 17:54:08 UTC
Author: kargl
Date: Sun Nov  8 17:53:36 2015
New Revision: 229958

URL: https://gcc.gnu.org/viewcvs?rev=229958&root=gcc&view=rev
Log:
2015-11-08  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/68153
	* check.c (gfc_check_reshape): Improve check for valid SHAPE argument.

2015-11-08  Steven G. Kargl  <kargl@gcc.gnu.org>

	PR fortran/68153
	* gfortran.dg/pr68153.f90: New test.

Added:
    branches/gcc-5-branch/gcc/testsuite/gfortran.dg/pr68153.f90
Modified:
    branches/gcc-5-branch/gcc/fortran/ChangeLog
    branches/gcc-5-branch/gcc/fortran/check.c
    branches/gcc-5-branch/gcc/testsuite/ChangeLog
Comment 7 kargls 2015-11-08 19:01:45 UTC
Fixed on trunk and 5-branch.  Thanks for the bug report.