Skip to content

[flang] Prefer non-elemental to elemental defined operator resolution #124941

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
Jan 31, 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
3 changes: 2 additions & 1 deletion flang/include/flang/Semantics/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ class ExpressionAnalyzer {
bool CheckDataRef(const DataRef &); // ditto
std::optional<Expr<SubscriptInteger>> GetSubstringBound(
const std::optional<parser::ScalarIntExpr> &);
MaybeExpr AnalyzeDefinedOp(const parser::Name &, ActualArguments &&);
MaybeExpr AnalyzeDefinedOp(
const parser::Name &, ActualArguments &&, const Symbol *&);
MaybeExpr FixMisparsedSubstring(const parser::Designator &);

struct CalleeAndArguments {
Expand Down
83 changes: 45 additions & 38 deletions flang/lib/Semantics/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2834,13 +2834,12 @@ std::pair<const Symbol *, bool> ExpressionAnalyzer::ResolveGeneric(
// Check for generic or explicit INTRINSIC of the same name in outer scopes.
// See 15.5.5.2 for details.
if (!symbol.owner().IsGlobal() && !symbol.owner().IsDerivedType()) {
for (const std::string &n : GetAllNames(context_, symbol.name())) {
if (const Symbol *outer{symbol.owner().parent().FindSymbol(n)}) {
auto pair{ResolveGeneric(*outer, actuals, adjustActuals, isSubroutine,
mightBeStructureConstructor)};
if (pair.first) {
return pair;
}
if (const Symbol *
outer{symbol.owner().parent().FindSymbol(symbol.name())}) {
auto pair{ResolveGeneric(*outer, actuals, adjustActuals, isSubroutine,
mightBeStructureConstructor)};
if (pair.first) {
return pair;
}
}
}
Expand Down Expand Up @@ -3635,13 +3634,13 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::Expr::Concat &x) {
// The Name represents a user-defined intrinsic operator.
// If the actuals match one of the specific procedures, return a function ref.
// Otherwise report the error in messages.
MaybeExpr ExpressionAnalyzer::AnalyzeDefinedOp(
const parser::Name &name, ActualArguments &&actuals) {
MaybeExpr ExpressionAnalyzer::AnalyzeDefinedOp(const parser::Name &name,
ActualArguments &&actuals, const Symbol *&symbol) {
if (auto callee{GetCalleeAndArguments(name, std::move(actuals))}) {
CHECK(std::holds_alternative<ProcedureDesignator>(callee->u));
return MakeFunctionRef(name.source,
std::move(std::get<ProcedureDesignator>(callee->u)),
std::move(callee->arguments));
auto &proc{std::get<evaluate::ProcedureDesignator>(callee->u)};
symbol = proc.GetSymbol();
return MakeFunctionRef(
name.source, std::move(proc), std::move(callee->arguments));
} else {
return std::nullopt;
}
Expand Down Expand Up @@ -4453,38 +4452,45 @@ MaybeExpr ArgumentAnalyzer::TryDefinedOp(
parser::Messages buffer;
auto restorer{context_.GetContextualMessages().SetMessages(buffer)};
const auto &scope{context_.context().FindScope(source_)};
if (Symbol *symbol{scope.FindSymbol(oprName)}) {

auto FoundOne{[&](MaybeExpr &&thisResult, const Symbol &generic,
const Symbol *resolution) {
anyPossibilities = true;
parser::Name name{symbol->name(), symbol};
if (!fatalErrors_) {
result = context_.AnalyzeDefinedOp(name, GetActuals());
}
if (result) {
inaccessible = CheckAccessibleSymbol(scope, *symbol);
if (inaccessible) {
result.reset();
if (thisResult) {
if (auto thisInaccessible{CheckAccessibleSymbol(scope, generic)}) {
inaccessible = thisInaccessible;
} else {
hit.push_back(symbol);
hitBuffer = std::move(buffer);
bool isElemental{IsElementalProcedure(DEREF(resolution))};
bool hitsAreNonElemental{
!hit.empty() && !IsElementalProcedure(DEREF(hit[0]))};
if (isElemental && hitsAreNonElemental) {
// ignore elemental resolutions in favor of a non-elemental one
} else {
if (!isElemental && !hitsAreNonElemental) {
hit.clear();
}
result = std::move(thisResult);
hit.push_back(resolution);
hitBuffer = std::move(buffer);
}
}
}
}};

if (Symbol * generic{scope.FindSymbol(oprName)}; generic && !fatalErrors_) {
parser::Name name{generic->name(), generic};
const Symbol *resultSymbol{nullptr};
MaybeExpr possibleResult{context_.AnalyzeDefinedOp(
name, ActualArguments{actuals_}, resultSymbol)};
FoundOne(std::move(possibleResult), *generic, resultSymbol);
}
for (std::size_t passIndex{0}; passIndex < actuals_.size(); ++passIndex) {
buffer.clear();
const Symbol *generic{nullptr};
if (const Symbol *binding{
FindBoundOp(oprName, passIndex, generic, false)}) {
anyPossibilities = true;
if (MaybeExpr thisResult{TryBoundOp(*binding, passIndex)}) {
if (auto thisInaccessible{
CheckAccessibleSymbol(scope, DEREF(generic))}) {
inaccessible = thisInaccessible;
} else {
result = std::move(thisResult);
hit.push_back(binding);
hitBuffer = std::move(buffer);
}
}
if (const Symbol *
binding{FindBoundOp(
oprName, passIndex, generic, /*isSubroutine=*/false)}) {
FoundOne(TryBoundOp(*binding, passIndex), DEREF(generic), binding);
}
}
}
Expand Down Expand Up @@ -4655,7 +4661,8 @@ std::optional<ProcedureRef> ArgumentAnalyzer::GetDefinedAssignmentProc() {
}
for (std::size_t i{0}; !proc && i < actuals_.size(); ++i) {
const Symbol *generic{nullptr};
if (const Symbol *binding{FindBoundOp(oprName, i, generic, true)}) {
if (const Symbol *
binding{FindBoundOp(oprName, i, generic, /*isSubroutine=*/true)}) {
if (CheckAccessibleSymbol(scope, DEREF(generic))) {
// ignore inaccessible type-bound ASSIGNMENT(=) generic
} else if (const Symbol *
Expand Down
26 changes: 26 additions & 0 deletions flang/test/Semantics/bug12477.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
!RUN: %flang_fc1 -fsyntax-only %s 2>&1 | FileCheck %s --allow-empty
!CHECK-NOT: error:
module m
type t
contains
procedure nonelemental
generic :: operator(+) => nonelemental
end type
interface operator(+)
procedure elemental
end interface
contains
type(t) elemental function elemental (a, b)
class(t), intent(in) :: a, b
elemental = t()
end
type(t) function nonelemental (a, b)
class(t), intent(in) :: a, b(:)
nonelemental = t()
end
end
program main
use m
type(t) x, y(1)
x = x + y ! ok
end
2 changes: 0 additions & 2 deletions flang/test/Semantics/resolve110.f90
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
! RUN: %python %S/test_errors.py %s %flang_fc1
! Exercise ways to define and extend non-type-bound generics
! TODO: crashes compiler (infinite recursion) when build with MSVC
! XFAIL: system-windows

module m1
type :: t1; end type
Expand Down