Skip to content

Commit d21534f

Browse files
authored
[flang][volatile] Get volatility of designators from base instead of component symbol (#138611)
The standard says in [8.5.20 VOLATILE attribute]: If an object has the VOLATILE attribute, then all of its sub-objects also have the VOLATILE attribute. This code takes this into account and uses the volatility of the base of the designator instead of that of the component. In fact, fields in a structure are not allowed to have the volatile attribute. So given the code, `A%B => t`, symbol `B` could never directly have the volatile attribute, and the volatility of `A` indicates the volatility of `B`. This PR should address [the comments](#132486 (comment)) on this PR #132486
1 parent 82982d7 commit d21534f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

flang/lib/Semantics/pointer-assignment.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,6 @@ bool PointerAssignmentChecker::Check(const evaluate::Designator<T> &d) {
329329
" shape"_err_en_US;
330330
} else if (rhsType->corank() > 0 &&
331331
(isVolatile_ != last->attrs().test(Attr::VOLATILE))) { // C1020
332-
// TODO: what if A is VOLATILE in A%B%C? need a better test here
333332
if (isVolatile_) {
334333
msg = "Pointer may not be VOLATILE when target is a"
335334
" non-VOLATILE coarray"_err_en_US;
@@ -569,6 +568,12 @@ bool CheckPointerAssignment(SemanticsContext &context, const SomeExpr &lhs,
569568
return false; // error was reported
570569
}
571570
PointerAssignmentChecker checker{context, scope, *pointer};
571+
const Symbol *base{GetFirstSymbol(lhs)};
572+
if (base) {
573+
// 8.5.20(4) If an object has the VOLATILE attribute, then all of its
574+
// subobjects also have the VOLATILE attribute.
575+
checker.set_isVolatile(base->attrs().test(Attr::VOLATILE));
576+
}
572577
checker.set_isBoundsRemapping(isBoundsRemapping);
573578
checker.set_isAssumedRank(isAssumedRank);
574579
bool lhsOk{checker.CheckLeftHandSide(lhs)};

flang/test/Semantics/assign02.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ module m1
88
type t2
99
sequence
1010
real :: t2Field
11+
real, pointer :: t2FieldPtr
1112
end type
1213
type t3
1314
type(t2) :: t3Field
15+
type(t2), pointer :: t3FieldPtr
1416
end type
1517
contains
1618

@@ -198,6 +200,14 @@ subroutine s13
198200
q2 => y%t3Field
199201
!OK:
200202
q3 => y
203+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
204+
p3%t3FieldPtr => y%t3Field
205+
!ERROR: VOLATILE target associated with non-VOLATILE pointer
206+
p3%t3FieldPtr%t2FieldPtr => y%t3Field%t2Field
207+
!OK
208+
q3%t3FieldPtr => y%t3Field
209+
!OK
210+
q3%t3FieldPtr%t2FieldPtr => y%t3Field%t2Field
201211
end
202212
end
203213

0 commit comments

Comments
 (0)