Skip to content

Commit aace1e1

Browse files
authored
[flang] Improve error message with declaration (#87294)
When a program attempts to use a non-object entity as the base of a component reference or type parameter inquiry, the message is somewhat uninformative and the position of the entity's declaration will not reflect any updates made to the symbol during name resolution. Includes some NFC C++17 style clean-up on some code noticed while debugging (missing mandatory braces).
1 parent af61d08 commit aace1e1

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

flang/include/flang/Parser/char-block.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,17 +132,20 @@ class CharBlock {
132132
// "memcmp" in glibc has "nonnull" attributes on the input pointers.
133133
// Avoid passing null pointers, since it would result in an undefined
134134
// behavior.
135-
if (size() == 0)
135+
if (size() == 0) {
136136
return that.size() == 0 ? 0 : -1;
137-
if (that.size() == 0)
137+
} else if (that.size() == 0) {
138138
return 1;
139-
std::size_t bytes{std::min(size(), that.size())};
140-
int cmp{std::memcmp(static_cast<const void *>(begin()),
141-
static_cast<const void *>(that.begin()), bytes)};
142-
if (cmp != 0) {
143-
return cmp;
139+
} else {
140+
std::size_t bytes{std::min(size(), that.size())};
141+
int cmp{std::memcmp(static_cast<const void *>(begin()),
142+
static_cast<const void *>(that.begin()), bytes)};
143+
if (cmp != 0) {
144+
return cmp;
145+
} else {
146+
return size() < that.size() ? -1 : size() > that.size();
147+
}
144148
}
145-
return size() < that.size() ? -1 : size() > that.size();
146149
}
147150

148151
int Compare(const char *that) const {

flang/lib/Semantics/resolve-names.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,7 +2253,7 @@ void ScopeHandler::SayWithDecl(
22532253
const parser::Name &name, Symbol &symbol, MessageFixedText &&msg) {
22542254
bool isFatal{msg.IsFatal()};
22552255
Say(name, std::move(msg), symbol.name())
2256-
.Attach(Message{name.source,
2256+
.Attach(Message{symbol.name(),
22572257
symbol.test(Symbol::Flag::Implicit)
22582258
? "Implicit declaration of '%s'"_en_US
22592259
: "Declaration of '%s'"_en_US,
@@ -7843,7 +7843,7 @@ const parser::Name *DeclarationVisitor::FindComponent(
78437843
auto &symbol{base->symbol->GetUltimate()};
78447844
if (!symbol.has<AssocEntityDetails>() && !ConvertToObjectEntity(symbol)) {
78457845
SayWithDecl(*base, symbol,
7846-
"'%s' is an invalid base for a component reference"_err_en_US);
7846+
"'%s' is not an object and may not be used as the base of a component reference or type parameter inquiry"_err_en_US);
78477847
return nullptr;
78487848
}
78497849
auto *type{symbol.GetType()};

flang/test/Semantics/resolve21.f90

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ subroutine s1
1616
external :: w
1717
!ERROR: 'z' is not an object of derived type; it is implicitly typed
1818
i = z%i
19-
!ERROR: 's1' is an invalid base for a component reference
19+
!ERROR: 's1' is not an object and may not be used as the base of a component reference or type parameter inquiry
2020
i = s1%i
2121
!ERROR: 'j' is not an object of derived type
2222
i = j%i
2323
!ERROR: Component 'j' not found in derived type 't'
2424
i = x%j
25-
!ERROR: 'v' is an invalid base for a component reference
25+
!ERROR: 'v' is not an object and may not be used as the base of a component reference or type parameter inquiry
2626
i = v%i
27-
!ERROR: 'w' is an invalid base for a component reference
27+
!ERROR: 'w' is not an object and may not be used as the base of a component reference or type parameter inquiry
2828
i = w%i
2929
i = x%i !OK
3030
end subroutine

0 commit comments

Comments
 (0)