Skip to content

Commit c3def59

Browse files
authored
[flang] Fix bogus error about duplicate binding names (#89786)
Don't call SetBindNameOn() from DeclareUnknownEntity() unless there is an explicit BIND(C) attribute. Fixes #89439 and #89558.
1 parent 29c98e5 commit c3def59

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

flang/lib/Semantics/resolve-names.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,7 +1779,6 @@ void AttrsVisitor::SetBindNameOn(Symbol &symbol) {
17791779
!symbol.attrs().test(Attr::BIND_C)) {
17801780
return;
17811781
}
1782-
17831782
std::optional<std::string> label{
17841783
evaluate::GetScalarConstantValue<evaluate::Ascii>(bindName_)};
17851784
// 18.9.2(2): discard leading and trailing blanks
@@ -1798,16 +1797,18 @@ void AttrsVisitor::SetBindNameOn(Symbol &symbol) {
17981797
} else {
17991798
label = symbol.name().ToString();
18001799
}
1801-
// Check if a symbol has two Bind names.
1800+
// Checks whether a symbol has two Bind names.
18021801
std::string oldBindName;
1803-
if (symbol.GetBindName()) {
1804-
oldBindName = *symbol.GetBindName();
1802+
if (const auto *bindName{symbol.GetBindName()}) {
1803+
oldBindName = *bindName;
18051804
}
18061805
symbol.SetBindName(std::move(*label));
18071806
if (!oldBindName.empty()) {
18081807
if (const std::string * newBindName{symbol.GetBindName()}) {
18091808
if (oldBindName != *newBindName) {
1810-
Say(symbol.name(), "The entity '%s' has multiple BIND names"_err_en_US);
1809+
Say(symbol.name(),
1810+
"The entity '%s' has multiple BIND names ('%s' and '%s')"_err_en_US,
1811+
symbol.name(), oldBindName, *newBindName);
18111812
}
18121813
}
18131814
}
@@ -4986,7 +4987,9 @@ Symbol &DeclarationVisitor::DeclareUnknownEntity(
49864987
if (symbol.attrs().test(Attr::EXTERNAL)) {
49874988
ConvertToProcEntity(symbol);
49884989
}
4989-
SetBindNameOn(symbol);
4990+
if (attrs.test(Attr::BIND_C)) {
4991+
SetBindNameOn(symbol);
4992+
}
49904993
return symbol;
49914994
}
49924995
}

flang/test/Semantics/declarations03.f90

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module m
1919
common /blk4/ w
2020
bind(c, name="cc") :: t2, /blk4/
2121

22-
!ERROR: The entity 'blk5' has multiple BIND names
22+
!ERROR: The entity 'blk5' has multiple BIND names ('dd' and 'ee')
2323
common /blk5/ i
2424
bind(c, name="dd") :: /blk5/
2525
bind(c, name="ee") :: /blk5/
@@ -29,7 +29,7 @@ module m
2929
bind(c, name="ff") :: /blk6/
3030
bind(c, name="ff") :: /blk7/
3131

32-
!ERROR: The entity 's1' has multiple BIND names
32+
!ERROR: The entity 's1' has multiple BIND names ('gg' and 'hh')
3333
integer :: s1
3434
bind(c, name="gg") :: s1
3535
!ERROR: BIND_C attribute was already specified on 's1'
@@ -40,12 +40,12 @@ module m
4040
bind(c, name="ii") :: s2
4141
bind(c, name="ii") :: s3
4242

43-
!ERROR: The entity 's4' has multiple BIND names
43+
!ERROR: The entity 's4' has multiple BIND names ('ss1' and 'jj')
4444
integer, bind(c, name="ss1") :: s4
4545
!ERROR: BIND_C attribute was already specified on 's4'
4646
bind(c, name="jj") :: s4
4747

48-
!ERROR: The entity 's5' has multiple BIND names
48+
!ERROR: The entity 's5' has multiple BIND names ('kk' and 'ss2')
4949
bind(c, name="kk") :: s5
5050
!ERROR: BIND_C attribute was already specified on 's5'
5151
integer, bind(c, name="ss2") :: s5
@@ -72,3 +72,8 @@ module b
7272
!ERROR: Two entities have the same global name 'int'
7373
integer, bind(c, name="int") :: i
7474
end module
75+
76+
module c
77+
bind(c, name = "AAA") a
78+
integer aaa ! ensure no bogus error about multiple binding names
79+
end module

0 commit comments

Comments
 (0)