fortran-wringer-tests icon indicating copy to clipboard operation
fortran-wringer-tests copied to clipboard

Contiguous pointer to type component

Open ivan-pi opened this issue 4 months ago • 4 comments

For the following program

integer, parameter :: sp = kind(1.0e0)
type :: d1
  real(sp) :: val
end type
type(d1), target :: b(5)
real(sp), pointer, contiguous :: c(:)
b%val = 1.0_sp
c => b%val
end

flang, nvfortran, and nagfor accept it.

gfortran raises an error:

/app/example.f90:8:5:

    8 | c => b%val
      |     1
Error: Assignment to contiguous pointer from non-contiguous target at (1)

ifx raises an error:

/app/example.f90(8): error #8377: An array is not contiguous if any part other than final has non-zero rank.   [VAL]
c => b%val
-------^
/app/example.f90(8): error #8371: If pointer is declared CONTIGUOUS, target must be contiguous as well.   [VAL]
c => b%val
-------^
compilation aborted for /app/example.f90 (code 1)

ivan-pi avatar Aug 12 '25 14:08 ivan-pi

Here's a small variation where we lie about the contiguity:

integer, parameter :: sp = kind(1.0e0)
type :: d1
  real(sp) :: x, y
end type
type(d1), target :: b(5)
real(sp), pointer, contiguous :: c(:)
b%x = 1.0_sp
c => b%x
print *, c
end

flang prints:

 1. 1. 1. 1. 1.

nvfortran prints:

    1.000000        0.000000        1.000000        0.000000     
    1.000000

whereas nagfor says:

> nagfor test_pointer.f90
NAG Fortran Compiler Release 7.2(Shin-Urayasu) Build 7203
Error: test_pointer.f90, line 8: Pointer assignment of noncontiguous target B%X to CONTIGUOUS pointer C
[NAG Fortran Compiler error termination, 1 error]

ivan-pi avatar Aug 12 '25 14:08 ivan-pi

Interesting; thanks for the examples. You'll get a warning out of flang-new -pedantic on your second example.

klausler avatar Aug 12 '25 14:08 klausler

Thanks for the tip. At least dropping the contiguous attribute appears to work consistently everywhere (in both cases).

Is -pedantic supposed to trigger the false positive (?) in the following nested case:

integer, parameter :: sp = kind(1.0e0)
type :: d1
  real(sp) :: x
end type
type :: d2
   type(d1) :: x
end type

type(d2), target :: b(5)
real(sp), pointer, contiguous :: c(:)
b%x%x = 1.0_sp
c => b%x%x
print *, c
end

ivan-pi avatar Aug 12 '25 14:08 ivan-pi

Here's a PR (https://github.com/llvm/llvm-project/pull/153222) for flang-new that emits a hard error when appropriate, and doesn't emit a false positive warning.

klausler avatar Aug 12 '25 16:08 klausler