llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Flang] No error checking if the type specifier specified in the allocate statement is different from the declaration

Open ohno-fj opened this issue 2 years ago • 8 comments

Version of flang-new : 18.0.0(0fe86f9c518fb1296bba8d66ce495f9dfff2c435)

When allocating a character type pointer variable, an error is not checked if the type specifier specified in allocate statement is different from the declaration.
Gfortran and ifort detect compilation errors.

Specifying parameter attribute in the type declaration of the variable (nn) checks for errors. This program (snggz792_12.f90) and results are attached at the end.

The following are the test program, Flang-new, Gfortran and ifort compilation result.

snggz792_11.f90:

module m1
  integer::nn=1
!  integer,parameter::nn=1
end module m1

program main
  use m1
  character(nn),pointer::c1s
  kk=0
  allocate(character(2)::c1s,stat=kk)
  print *,kk
end program main
$ flang-new snggz792_11.f90
$
$ gfortran snggz792_11.f90
snggz792_11.f90:8:28:

    8 |   character(nn),pointer::c1s
      |                            1
Error: ‘c1s’ at (1) must have constant character length in this context
snggz792_11.f90:10:25:

   10 |   allocate(character(2)::c1s,stat=kk)
      |                         1
Error: Allocating c1s at (1) with type-spec requires the same character-length parameter as in the declaration
$
$ ifort snggz792_11.f90
snggz792_11.f90(8): error #6591: An automatic object is invalid in a main program.   [C1S]
  character(nn),pointer::c1s
-------------------------^
compilation aborted for snggz792_11.f90 (code 1)
$

snggz792_12.f90:

module m1
!  integer::nn=1
  integer,parameter::nn=1
end module m1

program main
  use m1
  character(nn),pointer::c1s
  kk=0
  allocate(character(2)::c1s,stat=kk)
  print *,kk
end program main
$ flang-new snggz792_12.f90
error: Semantic errors in snggz792_12.f90
./snggz792_12.f90:10:26: error: Character length of allocatable object in ALLOCATE must be the same as the type-spec
    allocate(character(2)::c1s,stat=kk)
                           ^^^
$

ohno-fj avatar Jan 22 '24 05:01 ohno-fj

@llvm/issue-subscribers-flang-frontend

Author: None (ohno-fj)

``` Version of flang-new : 18.0.0(0fe86f9c518fb1296bba8d66ce495f9dfff2c435) ```

When allocating a character type pointer variable, an error is not checked if the type specifier specified in allocate statement is different from the declaration.
Gfortran and ifort detect compilation errors.

Specifying parameter attribute in the type declaration of the variable (nn) checks for errors. This program (snggz792_12.f90) and results are attached at the end.

The following are the test program, Flang-new, Gfortran and ifort compilation result.

snggz792_11.f90:

module m1
  integer::nn=1
!  integer,parameter::nn=1
end module m1

program main
  use m1
  character(nn),pointer::c1s
  kk=0
  allocate(character(2)::c1s,stat=kk)
  print *,kk
end program main
$ flang-new snggz792_11.f90
$
$ gfortran snggz792_11.f90
snggz792_11.f90:8:28:

    8 |   character(nn),pointer::c1s
      |                            1
Error: ‘c1s’ at (1) must have constant character length in this context
snggz792_11.f90:10:25:

   10 |   allocate(character(2)::c1s,stat=kk)
      |                         1
Error: Allocating c1s at (1) with type-spec requires the same character-length parameter as in the declaration
$
$ ifort snggz792_11.f90
snggz792_11.f90(8): error #<!-- -->6591: An automatic object is invalid in a main program.   [C1S]
  character(nn),pointer::c1s
-------------------------^
compilation aborted for snggz792_11.f90 (code 1)
$

snggz792_12.f90:

module m1
!  integer::nn=1
  integer,parameter::nn=1
end module m1

program main
  use m1
  character(nn),pointer::c1s
  kk=0
  allocate(character(2)::c1s,stat=kk)
  print *,kk
end program main
$ flang-new snggz792_12.f90
error: Semantic errors in snggz792_12.f90
./snggz792_12.f90:10:26: error: Character length of allocatable object in ALLOCATE must be the same as the type-spec
    allocate(character(2)::c1s,stat=kk)
                           ^^^
$

llvmbot avatar Jan 22 '24 05:01 llvmbot

@llvm/issue-subscribers-bug

Author: None (ohno-fj)

``` Version of flang-new : 18.0.0(0fe86f9c518fb1296bba8d66ce495f9dfff2c435) ```

When allocating a character type pointer variable, an error is not checked if the type specifier specified in allocate statement is different from the declaration.
Gfortran and ifort detect compilation errors.

Specifying parameter attribute in the type declaration of the variable (nn) checks for errors. This program (snggz792_12.f90) and results are attached at the end.

The following are the test program, Flang-new, Gfortran and ifort compilation result.

snggz792_11.f90:

module m1
  integer::nn=1
!  integer,parameter::nn=1
end module m1

program main
  use m1
  character(nn),pointer::c1s
  kk=0
  allocate(character(2)::c1s,stat=kk)
  print *,kk
end program main
$ flang-new snggz792_11.f90
$
$ gfortran snggz792_11.f90
snggz792_11.f90:8:28:

    8 |   character(nn),pointer::c1s
      |                            1
Error: ‘c1s’ at (1) must have constant character length in this context
snggz792_11.f90:10:25:

   10 |   allocate(character(2)::c1s,stat=kk)
      |                         1
Error: Allocating c1s at (1) with type-spec requires the same character-length parameter as in the declaration
$
$ ifort snggz792_11.f90
snggz792_11.f90(8): error #<!-- -->6591: An automatic object is invalid in a main program.   [C1S]
  character(nn),pointer::c1s
-------------------------^
compilation aborted for snggz792_11.f90 (code 1)
$

snggz792_12.f90:

module m1
!  integer::nn=1
  integer,parameter::nn=1
end module m1

program main
  use m1
  character(nn),pointer::c1s
  kk=0
  allocate(character(2)::c1s,stat=kk)
  print *,kk
end program main
$ flang-new snggz792_12.f90
error: Semantic errors in snggz792_12.f90
./snggz792_12.f90:10:26: error: Character length of allocatable object in ALLOCATE must be the same as the type-spec
    allocate(character(2)::c1s,stat=kk)
                           ^^^
$

llvmbot avatar Jan 22 '24 07:01 llvmbot

@ceseo are you still going to fix this bug?

klausler avatar Jun 05 '24 21:06 klausler

@klausler sorry, I was on vacation in the past weeks.

Yes, I was working on it today. However, I was looking at your initial implementation and I was wondering if this was intended behavior. So it's a real bug?

ceseo avatar Jun 05 '24 22:06 ceseo

I don't know; when you assigned this issue to yourself, I assumed that you would investigate, and haven't analyzed it myself.

klausler avatar Jun 06 '24 16:06 klausler

OK, I'll keep investigating. Fix is trivial, but it's breaking some tests.

ceseo avatar Jun 06 '24 20:06 ceseo

@ohno-fj I was discussing this problem with @klausler . The situation described here is not actually a bug. The compiler should emit an error only when the character length values are distinct constants, which is not the case for c1s in your test.

ceseo avatar Jun 19 '24 19:06 ceseo

@ceseo, thank you for your comment.

I understand the above. In the second program (snggz792_12.f90), nn is declared as a constant with the parameter attribute attached, so it outputs an error when compiled. It works exactly as you commented.

Could you close this issue ?

ohno-fj avatar Jun 20 '24 01:06 ohno-fj