Skip to content

Commit e73d51d

Browse files
authored
[flang] ASSOCIATE/SELECT TYPE entities aren't pointer/allocatable (#99364)
Fix what seems to be a regression in semantics in definability checking: the construct entities of ASSOCIATE and SELECT TYPE constructs are never pointers or allocatables, even when their selectors are so. SELECT RANK construct entities, however, can be pointers or allocatables.
1 parent 433e09c commit e73d51d

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

flang/lib/Semantics/definable.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,10 @@ static std::optional<parser::Message> WhyNotDefinableBase(parser::CharBlock at,
178178
static std::optional<parser::Message> WhyNotDefinableLast(parser::CharBlock at,
179179
const Scope &scope, DefinabilityFlags flags, const Symbol &original) {
180180
const Symbol &ultimate{original.GetUltimate()};
181-
if (const auto *association{ultimate.detailsIf<AssocEntityDetails>()}) {
181+
if (const auto *association{ultimate.detailsIf<AssocEntityDetails>()};
182+
association &&
183+
(association->rank().has_value() ||
184+
!flags.test(DefinabilityFlag::PointerDefinition))) {
182185
if (auto dataRef{
183186
evaluate::ExtractDataRef(*association->expr(), true, true)}) {
184187
return WhyNotDefinableLast(at, scope, flags, dataRef->GetLastSymbol());

flang/test/Semantics/associate03.f90

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
2+
! A construct entity does not have the POINTER or ALLOCATABLE attribute,
3+
! except in SELECT RANK.
4+
5+
subroutine test(up,ua,rp,ra)
6+
class(*), pointer :: up
7+
class(*), allocatable :: ua
8+
real, pointer :: rp(..)
9+
real, allocatable :: ra(..)
10+
real, target :: x
11+
real, pointer :: p
12+
real, allocatable :: a
13+
associate (s => p)
14+
!ERROR: The left-hand side of a pointer assignment is not definable
15+
!BECAUSE: 's' is not a pointer
16+
s => x
17+
!ERROR: Entity in ALLOCATE statement must have the ALLOCATABLE or POINTER attribute
18+
allocate(s)
19+
!ERROR: Name in DEALLOCATE statement must have the ALLOCATABLE or POINTER attribute
20+
deallocate(s)
21+
!ERROR: 's' may not appear in NULLIFY
22+
!BECAUSE: 's' is not a pointer
23+
nullify(s)
24+
end associate
25+
select type(s => up)
26+
type is (real)
27+
!ERROR: The left-hand side of a pointer assignment is not definable
28+
!BECAUSE: 's' is not a pointer
29+
s => x
30+
!ERROR: Entity in ALLOCATE statement must have the ALLOCATABLE or POINTER attribute
31+
allocate(s)
32+
!ERROR: Name in DEALLOCATE statement must have the ALLOCATABLE or POINTER attribute
33+
deallocate(s)
34+
!ERROR: 's' may not appear in NULLIFY
35+
!BECAUSE: 's' is not a pointer
36+
nullify(s)
37+
end select
38+
select rank(s => rp)
39+
rank(0)
40+
s => x ! ok
41+
allocate(s) ! ok
42+
deallocate(s) ! ok
43+
nullify(s) ! ok
44+
!ERROR: RANK (*) cannot be used when selector is POINTER or ALLOCATABLE
45+
rank(*)
46+
rank default
47+
!ERROR: The left-hand side of a pointer assignment must not be an assumed-rank dummy argument
48+
!ERROR: pointer 's' associated with object 'x' with incompatible type or shape
49+
s => x
50+
!ERROR: An assumed-rank dummy argument may not appear in an ALLOCATE statement
51+
allocate(s)
52+
deallocate(s) ! ok
53+
nullify(s) ! ok
54+
end select
55+
associate (s => a)
56+
!ERROR: Entity in ALLOCATE statement must have the ALLOCATABLE or POINTER attribute
57+
allocate(s)
58+
!ERROR: Name in DEALLOCATE statement must have the ALLOCATABLE or POINTER attribute
59+
deallocate(s)
60+
end associate
61+
select type(s => ua)
62+
type is (real)
63+
!ERROR: Entity in ALLOCATE statement must have the ALLOCATABLE or POINTER attribute
64+
allocate(s)
65+
!ERROR: Name in DEALLOCATE statement must have the ALLOCATABLE or POINTER attribute
66+
deallocate(s)
67+
end select
68+
select rank(s => ra)
69+
rank(0)
70+
allocate(s) ! ok
71+
deallocate(s) ! ok
72+
!ERROR: RANK (*) cannot be used when selector is POINTER or ALLOCATABLE
73+
rank(*)
74+
rank default
75+
!ERROR: An assumed-rank dummy argument may not appear in an ALLOCATE statement
76+
allocate(s)
77+
deallocate(s) ! ok
78+
end select
79+
end

0 commit comments

Comments
 (0)