Skip to content

Commit a93d843

Browse files
authored
[flang] Don't warn on (0.,0.)**(nonzero noninteger) (llvm#145179)
Folding hands complex exponentiations with constant arguments off to the native libm, and on a least on host, this can produce spurious warnings about division by zero and invalid arguments. Handle the case of a zero base specially to avoid that, and also emit better warnings for the undefined 0.**0 and (0.,0.)**0 cases. And add a test for these warnings and the existing related ones.
1 parent 348002e commit a93d843

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

flang/include/flang/Evaluate/complex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ template <typename REAL_TYPE> class Complex {
4545
im_.Compare(that.im_) == Relation::Equal;
4646
}
4747

48-
constexpr bool IsZero() const { return re_.IsZero() || im_.IsZero(); }
48+
constexpr bool IsZero() const { return re_.IsZero() && im_.IsZero(); }
4949

5050
constexpr bool IsInfinite() const {
5151
return re_.IsInfinite() || im_.IsInfinite();

flang/lib/Evaluate/fold-implementation.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,14 @@ Expr<T> FoldOperation(FoldingContext &context, Power<T> &&x) {
21922192
}
21932193
return Expr<T>{Constant<T>{power.power}};
21942194
} else {
2195-
if (auto callable{GetHostRuntimeWrapper<T, T, T>("pow")}) {
2195+
if (folded->first.IsZero()) {
2196+
if (folded->second.IsZero()) {
2197+
context.messages().Say(common::UsageWarning::FoldingException,
2198+
"REAL/COMPLEX 0**0 is not defined"_warn_en_US);
2199+
} else {
2200+
return Expr<T>(Constant<T>{folded->first}); // 0. ** nonzero -> 0.
2201+
}
2202+
} else if (auto callable{GetHostRuntimeWrapper<T, T, T>("pow")}) {
21962203
return Expr<T>{
21972204
Constant<T>{(*callable)(context, folded->first, folded->second)}};
21982205
} else if (context.languageFeatures().ShouldWarn(

flang/test/Semantics/bug1046.f90

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
2+
!WARNING: INTEGER(4) 0**0 is not defined
3+
print *, 0**0
4+
!WARNING: REAL/COMPLEX 0**0 is not defined
5+
print *, 0**0.
6+
!WARNING: invalid argument on power with INTEGER exponent
7+
print *, 0.0**0
8+
!WARNING: REAL/COMPLEX 0**0 is not defined
9+
print *, 0.0**0.
10+
!WARNING: invalid argument on power with INTEGER exponent
11+
print *, (0.0, 0.0)**0
12+
!WARNING: REAL/COMPLEX 0**0 is not defined
13+
print *, (0.0, 0.0)**0.
14+
print *, (0.0, 0.0)**2.5
15+
end
16+
17+

0 commit comments

Comments
 (0)