llvm-project/flang/test/Semantics/allocate11.f90

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

162 lines
6.8 KiB
Fortran
Raw Normal View History

! RUN: %S/test_errors.sh %s %t %f18
! Check for semantic errors in ALLOCATE statements
! TODO: Function Pointer in allocate and derived types!
! Rules I should know when working with coarrays and derived type:
! C736: If EXTENDS appears and the type being defined has a coarray ultimate
! component, its parent type shall have a coarray ultimate component.
! C746: (R737) If a coarray-spec appears, it shall be a deferred-coshape-spec-list
! and the component shall have the ALLOCATABLE attribute.
! C747: If a coarray-spec appears, the component shall not be of type C_PTR or
! C_FUNPTR from the intrinsic module ISO_C_BINDING (18.2), or of type TEAM_TYPE from the
! intrinsic module ISO_FORTRAN_ENV (16.10.2).
! C748: A data component whose type has a coarray ultimate component shall be a
! nonpointer nonallocatable scalar and shall not be a coarray.
! 7.5.4.3 Coarray components
! 7.5.6 Final subroutines: C786
! C825 An entity whose type has a coarray ultimate component shall be a
! nonpointer nonallocatable scalar, shall not be a coarray, and shall not be a function result.
! C826 A coarray or an object with a coarray ultimate component shall be an
! associate name, a dummy argument, or have the ALLOCATABLE or SAVE attribute.
subroutine C937(var)
! Type-spec shall not specify a type that has a coarray ultimate component.
type A
real, allocatable :: x[:]
end type
type B
type(A) y
[flang] New implementation for checks for constraints C741 through C750 Summary: Most of these checks were already implemented, and I just added references to them to the code and tests. Also, much of this code was already reviewed in the old flang/f18 GitHub repository, but I didn't get to merge it before we switched repositories. I implemented the check for C747 to not allow coarray components in derived types that are of type C_PTR, C_FUNPTR, or type TEAM_TYPE. I implemented the check for C748 that requires a data component whose type has a coarray ultimate component to be a nonpointer, nonallocatable scalar and not be a coarray. I implemented the check for C750 that adds additional restrictions to the bounds expressions of a derived type component that's an array. These bounds expressions are sepcification expressions as defined in 10.1.11. There was already code in lib/Evaluate/check-expression.cpp to check semantics for specification expressions, but it did not check for the extra requirements of C750. C750 prohibits specification functions, the intrinsic functions ALLOCATED, ASSOCIATED, EXTENDS_TYPE_OF, PRESENT, and SAME_TYPE_AS. It also requires every specification inquiry reference to be a constant expression, and requires that the value of the bound not depend on the value of a variable. To implement these additional checks, I added code to the intrinsic proc table to get the intrinsic class of a procedure. I also added an enumeration to distinguish between specification expressions for derived type component bounds versus for type parameters. I then changed the code to pass an enumeration value to "CheckSpecificationExpr()" to indicate that the expression was a bounds expression and used this value to determine whether to emit an error message when violations of C750 are found. I changed the implementation of IsPureProcedure() to handle statement functions and changed some references in the code that tested for the PURE attribute to call IsPureProcedure(). I also fixed some unrelated tests that got new errors when I implemented these new checks. Reviewers: tskeith, DavidTruby, sscalpone Subscribers: jfb, llvm-commits Tags: #llvm, #flang Differential Revision: https://reviews.llvm.org/D79263
2020-05-02 04:00:28 +08:00
!ERROR: A component with a POINTER or ALLOCATABLE attribute may not be of a type with a coarray ultimate component (named 'y%x')
type(B), pointer :: forward
real :: u
end type
type C
type(B) z
end type
type D
[flang] New implementation for checks for constraints C741 through C750 Summary: Most of these checks were already implemented, and I just added references to them to the code and tests. Also, much of this code was already reviewed in the old flang/f18 GitHub repository, but I didn't get to merge it before we switched repositories. I implemented the check for C747 to not allow coarray components in derived types that are of type C_PTR, C_FUNPTR, or type TEAM_TYPE. I implemented the check for C748 that requires a data component whose type has a coarray ultimate component to be a nonpointer, nonallocatable scalar and not be a coarray. I implemented the check for C750 that adds additional restrictions to the bounds expressions of a derived type component that's an array. These bounds expressions are sepcification expressions as defined in 10.1.11. There was already code in lib/Evaluate/check-expression.cpp to check semantics for specification expressions, but it did not check for the extra requirements of C750. C750 prohibits specification functions, the intrinsic functions ALLOCATED, ASSOCIATED, EXTENDS_TYPE_OF, PRESENT, and SAME_TYPE_AS. It also requires every specification inquiry reference to be a constant expression, and requires that the value of the bound not depend on the value of a variable. To implement these additional checks, I added code to the intrinsic proc table to get the intrinsic class of a procedure. I also added an enumeration to distinguish between specification expressions for derived type component bounds versus for type parameters. I then changed the code to pass an enumeration value to "CheckSpecificationExpr()" to indicate that the expression was a bounds expression and used this value to determine whether to emit an error message when violations of C750 are found. I changed the implementation of IsPureProcedure() to handle statement functions and changed some references in the code that tested for the PURE attribute to call IsPureProcedure(). I also fixed some unrelated tests that got new errors when I implemented these new checks. Reviewers: tskeith, DavidTruby, sscalpone Subscribers: jfb, llvm-commits Tags: #llvm, #flang Differential Revision: https://reviews.llvm.org/D79263
2020-05-02 04:00:28 +08:00
!ERROR: A component with a POINTER or ALLOCATABLE attribute may not be of a type with a coarray ultimate component (named 'x')
type(A), pointer :: potential
end type
class(*), allocatable :: var
! unlimited polymorphic is the ONLY way to get an allocatable/pointer 'var' that can be
! allocated with a type-spec T that has coarray ultimate component without
! violating other rules than C937.
! Rationale:
! C934 => var must be type compatible with T.
! => var type is T, a type P extended by T, or unlimited polymorphic
! C825 => var cannot be of type T.
! C736 => all parent types P of T must have a coarray ultimate component
! => var cannot be of type P (C825)
! => if var can be defined, it can only be unlimited polymorphic
! Also, as per C826 or C852, var can only be an allocatable, not a pointer
! OK, x is not an ultimate component
allocate(D:: var)
!ERROR: Type-spec in ALLOCATE must not specify a type with a coarray ultimate component
allocate(A:: var)
!ERROR: Type-spec in ALLOCATE must not specify a type with a coarray ultimate component
allocate(B:: var)
!ERROR: Type-spec in ALLOCATE must not specify a type with a coarray ultimate component
allocate(C:: var)
end subroutine
!TODO: type extending team_type !? subcomponents !?
subroutine C938_C947(var2, ptr, ptr2, fptr, my_team, srca)
! If an allocate-object is a coarray, type-spec shall not specify type C_PTR or
! C_FUNPTR from the intrinsic module ISO_C_BINDING, or type TEAM_TYPE from the intrinsic module
! ISO_FORTRAN_ENV.
use ISO_FORTRAN_ENV
use ISO_C_BINDING
type A(k, l)
integer, kind :: k
integer, len :: l
real(kind=k) x(l,l)
end type
! Again, I do not see any other way to violate this rule and not others without
! having var being an unlimited polymorphic.
! Suppose var of type P and T, the type in type-spec
! Per C934, P must be compatible with T. P cannot be a forbidden type per C824.
! Per C728 and 7.5.7.1, P cannot extend a c_ptr or _c_funptr. hence, P has to be
! unlimited polymorphic or a type that extends TEAM_TYPE.
class(*), allocatable :: var[:], var2(:)[:]
class(*), allocatable :: varok, varok2(:)
Type(C_PTR) :: ptr, ptr2(2:10)
Type(C_FUNPTR) fptr
Type(TEAM_TYPE) my_team
Type(A(4, 10)) :: srca
! Valid constructs
allocate(real:: var[5:*])
allocate(A(4, 10):: var[5:*])
allocate(TEAM_TYPE:: varok, varok2(2))
allocate(C_PTR:: varok, varok2(2))
allocate(C_FUNPTR:: varok, varok2(2))
!ERROR: Type-Spec in ALLOCATE must not be TEAM_TYPE from ISO_FORTRAN_ENV when an allocatable object is a coarray
allocate(TEAM_TYPE:: var[5:*])
!ERROR: Type-Spec in ALLOCATE must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(C_PTR:: varok, var[5:*])
!ERROR: Type-Spec in ALLOCATE must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(C_FUNPTR:: var[5:*])
!ERROR: Type-Spec in ALLOCATE must not be TEAM_TYPE from ISO_FORTRAN_ENV when an allocatable object is a coarray
allocate(TEAM_TYPE:: var2(2)[5:*])
!ERROR: Type-Spec in ALLOCATE must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(C_PTR:: var2(2)[5:*])
!ERROR: Type-Spec in ALLOCATE must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(C_FUNPTR:: varok2(2), var2(2)[5:*])
! C947: The declared type of source-expr shall not be C_PTR or C_FUNPTR from the
! intrinsic module ISO_C_BINDING, or TEAM_TYPE from the intrinsic module
! ISO_FORTRAN_ENV, if an allocateobject is a coarray.
!
! ! Valid constructs
allocate(var[5:*], SOURCE=cos(0.5_4))
allocate(var[5:*], MOLD=srca)
allocate(varok, varok2(2), SOURCE=ptr)
allocate(varok2, MOLD=ptr2)
allocate(varok, varok2(2), SOURCE=my_team)
allocate(varok, varok2(2), MOLD=fptr)
!ERROR: SOURCE or MOLD expression type must not be TEAM_TYPE from ISO_FORTRAN_ENV when an allocatable object is a coarray
allocate(var[5:*], SOURCE=my_team)
!ERROR: SOURCE or MOLD expression type must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(var[5:*], SOURCE=ptr)
!ERROR: SOURCE or MOLD expression type must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(varok, var[5:*], MOLD=ptr2(1))
!ERROR: SOURCE or MOLD expression type must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(var[5:*], MOLD=fptr)
!ERROR: SOURCE or MOLD expression type must not be TEAM_TYPE from ISO_FORTRAN_ENV when an allocatable object is a coarray
allocate(var2(2)[5:*], MOLD=my_team)
!ERROR: SOURCE or MOLD expression type must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(var2(2)[5:*], MOLD=ptr)
!ERROR: SOURCE or MOLD expression type must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(var2(2)[5:*], SOURCE=ptr2)
!ERROR: SOURCE or MOLD expression type must not be C_PTR or C_FUNPTR from ISO_C_BINDING when an allocatable object is a coarray
allocate(varok2(2), var2(2)[5:*], SOURCE=fptr)
end subroutine