Skip to content

Commit f89d2be

Browse files
authored
[flang] Prioritize DATA object error messages a little better (llvm#66258)
When a DATA statement object is not valid, there's a number of possible reasons. Emit an error message for the most egregious violation, so that an unlucky user doesn't fix something easy (due to a less-severe error message masking one that is worse) and then run into something that might be more serious.
1 parent 47669af commit f89d2be

File tree

2 files changed

+72
-10
lines changed

2 files changed

+72
-10
lines changed

flang/lib/Semantics/check-data.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,24 @@ class DataVarChecker : public evaluate::AllTraverse<DataVarChecker, true> {
5858
const Scope &scope{context_.FindScope(source_)};
5959
bool isFirstSymbol{isFirstSymbol_};
6060
isFirstSymbol_ = false;
61-
if (const char *whyNot{IsAutomatic(symbol) ? "Automatic variable"
62-
: IsDummy(symbol) ? "Dummy argument"
63-
: IsFunctionResult(symbol) ? "Function result"
64-
: IsAllocatable(symbol) ? "Allocatable"
61+
// Ordered so that most egregious errors are first
62+
if (const char *whyNot{IsProcedure(symbol) && !IsPointer(symbol)
63+
? "Procedure"
64+
: isFirstSymbol && IsHostAssociated(symbol, scope)
65+
? "Host-associated object"
66+
: isFirstSymbol && IsUseAssociated(symbol, scope)
67+
? "USE-associated object"
68+
: IsDummy(symbol) ? "Dummy argument"
69+
: IsFunctionResult(symbol) ? "Function result"
70+
: IsAutomatic(symbol) ? "Automatic variable"
71+
: IsAllocatable(symbol) ? "Allocatable"
6572
: IsInitialized(symbol, true /*ignore DATA*/,
6673
true /*ignore allocatable components*/,
6774
true /*ignore uninitialized pointer components*/)
6875
? "Default-initialized"
69-
: IsProcedure(symbol) && !IsPointer(symbol) ? "Procedure"
70-
// remaining checks don't apply to components
71-
: !isFirstSymbol ? nullptr
72-
: IsHostAssociated(symbol, scope) ? "Host-associated object"
73-
: IsUseAssociated(symbol, scope) ? "USE-associated object"
7476
: symbol.has<AssocEntityDetails>() ? "Construct association"
75-
: IsPointer(symbol) && (hasComponent_ || hasSubscript_)
77+
: isFirstSymbol && IsPointer(symbol) &&
78+
(hasComponent_ || hasSubscript_)
7679
? "Target of pointer"
7780
: nullptr}) {
7881
context_.Say(source_,

flang/test/Semantics/data18.f90

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1
2+
! Test error message priorities for DATA problems
3+
module m
4+
integer useAlloc
5+
allocatable useAlloc
6+
integer, pointer :: usePtr(:)
7+
contains
8+
subroutine useProc
9+
end
10+
end
11+
function f(hostDummy, hostProc) result(hostResult)
12+
integer hostDummy, hostResult
13+
external hostProc
14+
integer hostAuto(hostDummy)
15+
integer, allocatable :: hostAlloc
16+
integer :: hostInit = 1
17+
integer, pointer :: hostPtr(:)
18+
contains
19+
subroutine test(innerDummy, innerProc)
20+
use m
21+
external innerProc
22+
integer innerAuto(innerDummy)
23+
integer, allocatable :: innerAlloc
24+
integer :: innerInit = 1
25+
integer, pointer :: innerPtr(:)
26+
!ERROR: Procedure 'useproc' must not be initialized in a DATA statement
27+
data useProc/0/
28+
!ERROR: Procedure 'hostproc' must not be initialized in a DATA statement
29+
data hostProc/0/
30+
!ERROR: Procedure 'innerproc' must not be initialized in a DATA statement
31+
data innerProc/0/
32+
!ERROR: Host-associated object 'hostdummy' must not be initialized in a DATA statement
33+
data hostDummy/1/
34+
!ERROR: Host-associated object 'hostresult' must not be initialized in a DATA statement
35+
data hostResult/1/
36+
!ERROR: Host-associated object 'hostauto' must not be initialized in a DATA statement
37+
data hostAuto/1/
38+
!ERROR: Host-associated object 'hostalloc' must not be initialized in a DATA statement
39+
data hostAlloc/1/
40+
!ERROR: Host-associated object 'hostinit' must not be initialized in a DATA statement
41+
data hostInit/1/
42+
!ERROR: Host-associated object 'hostptr' must not be initialized in a DATA statement
43+
data hostPtr(1)/1/
44+
!ERROR: USE-associated object 'usealloc' must not be initialized in a DATA statement
45+
data useAlloc/1/
46+
!ERROR: USE-associated object 'useptr' must not be initialized in a DATA statement
47+
data usePtr(1)/1/
48+
!ERROR: Dummy argument 'innerdummy' must not be initialized in a DATA statement
49+
data innerDummy/1/
50+
!ERROR: Automatic variable 'innerauto' must not be initialized in a DATA statement
51+
data innerAuto/1/
52+
!ERROR: Allocatable 'inneralloc' must not be initialized in a DATA statement
53+
data innerAlloc/1/
54+
!ERROR: Default-initialized 'innerinit' must not be initialized in a DATA statement
55+
data innerInit/1/
56+
!ERROR: Target of pointer 'innerptr' must not be initialized in a DATA statement
57+
data innerptr(1)/1/
58+
end
59+
end

0 commit comments

Comments
 (0)