Skip to content

[flang] Permit unused USE association of subprogram name #134009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion flang/include/flang/Support/Fortran-features.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
CompatibleDeclarationsFromDistinctModules,
NullActualForDefaultIntentAllocatable)
NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram)

using LanguageFeatures = EnumSet<LanguageFeature, LanguageFeature_enumSize>;
using UsageWarnings = EnumSet<UsageWarning, UsageWarning_enumSize>;
Expand Down
23 changes: 20 additions & 3 deletions flang/lib/Semantics/resolve-names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ class ScopeHandler : public ImplicitRulesVisitor {
void NotePossibleBadForwardRef(const parser::Name &);
std::optional<SourceName> HadForwardRef(const Symbol &) const;
bool CheckPossibleBadForwardRef(const Symbol &);
bool ConvertToUseError(Symbol &, const SourceName &, const Symbol &used);

bool inSpecificationPart_{false};
bool deferImplicitTyping_{false};
Expand Down Expand Up @@ -3335,7 +3336,7 @@ ModuleVisitor::SymbolRename ModuleVisitor::AddUse(

// symbol must be either a Use or a Generic formed by merging two uses.
// Convert it to a UseError with this additional location.
static bool ConvertToUseError(
bool ScopeHandler::ConvertToUseError(
Symbol &symbol, const SourceName &location, const Symbol &used) {
if (auto *ued{symbol.detailsIf<UseErrorDetails>()}) {
ued->add_occurrence(location, used);
Expand All @@ -3353,9 +3354,25 @@ static bool ConvertToUseError(
symbol.set_details(
UseErrorDetails{*useDetails}.add_occurrence(location, used));
return true;
} else {
return false;
}
if (const auto *hostAssocDetails{symbol.detailsIf<HostAssocDetails>()};
hostAssocDetails && hostAssocDetails->symbol().has<SubprogramDetails>() &&
&symbol.owner() == &currScope() &&
&hostAssocDetails->symbol() == currScope().symbol()) {
// Handle USE-association of procedure FOO into function/subroutine FOO,
// replacing its place-holding HostAssocDetails symbol.
context().Warn(common::UsageWarning::UseAssociationIntoSameNameSubprogram,
location,
"'%s' is use-associated into a subprogram of the same name"_port_en_US,
used.name());
SourceName created{context().GetTempName(currScope())};
Symbol &tmpUse{MakeSymbol(created, Attrs(), UseDetails{location, used})};
UseErrorDetails useError{tmpUse.get<UseDetails>()};
useError.add_occurrence(location, hostAssocDetails->symbol());
symbol.set_details(std::move(useError));
return true;
}
return false;
}

// Two ultimate symbols are distinct, but they have the same name and come
Expand Down
24 changes: 14 additions & 10 deletions flang/lib/Semantics/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1733,16 +1733,20 @@ bool HadUseError(
at, "Reference to '%s' is ambiguous"_err_en_US, symbol->name())};
for (const auto &[location, sym] : details->occurrences()) {
const Symbol &ultimate{sym->GetUltimate()};
auto &attachment{
msg.Attach(location, "'%s' was use-associated from module '%s'"_en_US,
at, sym->owner().GetName().value())};
if (&*sym != &ultimate) {
// For incompatible definitions where one comes from a hermetic
// module file's incorporated dependences and the other from another
// module of the same name.
attachment.Attach(ultimate.name(),
"ultimately from '%s' in module '%s'"_en_US, ultimate.name(),
ultimate.owner().GetName().value());
if (sym->owner().IsModule()) {
auto &attachment{msg.Attach(location,
"'%s' was use-associated from module '%s'"_en_US, at,
sym->owner().GetName().value())};
if (&*sym != &ultimate) {
// For incompatible definitions where one comes from a hermetic
// module file's incorporated dependences and the other from another
// module of the same name.
attachment.Attach(ultimate.name(),
"ultimately from '%s' in module '%s'"_en_US, ultimate.name(),
ultimate.owner().GetName().value());
}
} else {
msg.Attach(sym->name(), "declared here"_en_US);
}
}
context.SetError(*symbol);
Expand Down
1 change: 1 addition & 0 deletions flang/lib/Support/Fortran-features.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ LanguageFeatureControl::LanguageFeatureControl() {
warnUsage_.set(UsageWarning::UselessIomsg);
warnUsage_.set(UsageWarning::UnsignedLiteralTruncation);
warnUsage_.set(UsageWarning::NullActualForDefaultIntentAllocatable);
warnUsage_.set(UsageWarning::UseAssociationIntoSameNameSubprogram);
// New warnings, on by default
warnLanguage_.set(LanguageFeature::SavedLocalInSpecExpr);
warnLanguage_.set(LanguageFeature::NullActualForAllocatable);
Expand Down
8 changes: 6 additions & 2 deletions flang/test/Semantics/resolve18.f90
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ subroutine s(i)
end module

subroutine foo
!ERROR: Cannot use-associate 'foo'; it is already declared in this scope
!PORTABILITY: 'foo' is use-associated into a subprogram of the same name
use m1
!ERROR: Reference to 'foo' is ambiguous
call foo
end

subroutine bar
!ERROR: Cannot use-associate 'bar'; it is already declared in this scope
!PORTABILITY: 'foo' is use-associated into a subprogram of the same name
use m1, bar => foo
!ERROR: Reference to 'bar' is ambiguous
call bar
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the following example? Should this now compile?

subroutine buz
  use m1

contains
  subroutine foo

  end subroutine foo
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it still doesn't. It only applies to USE-associated names that conflict with the name of the subprogram into whose scope the USE association occurs.

end

!OK to use-associate a type with the same name as a generic
Expand Down