public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gmx.de>
To: Mikael Morin <morin-mikael@orange.fr>
Cc: fortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH, v2] Fortran: error recovery for invalid types in array constructors [PR107000]
Date: Wed, 5 Oct 2022 23:40:52 +0200	[thread overview]
Message-ID: <trinity-7d605483-76d2-4fd9-8856-e95db3adad91-1665006051981@3c-app-gmx-bap28> (raw)
In-Reply-To: <d67bf978-369e-6eb1-6dc3-dfc01827f725@orange.fr>

[-- Attachment #1: Type: text/plain, Size: 1597 bytes --]

Hi Mikael,

> Gesendet: Mittwoch, 05. Oktober 2022 um 11:23 Uhr
> Von: "Mikael Morin" <morin-mikael@orange.fr>
> An: "Harald Anlauf" <anlauf@gmx.de>, "fortran" <fortran@gcc.gnu.org>, "gcc-patches" <gcc-patches@gcc.gnu.org>
> Betreff: Re: [PATCH] Fortran: error recovery for invalid types in array constructors [PR107000]

> The following does.
>
>
> diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc
> index e6e35ef3c42..2c57c796270 100644
> --- a/gcc/fortran/arith.cc
> +++ b/gcc/fortran/arith.cc
> @@ -1443,7 +1443,7 @@ reduce_binary_aa (arith (*eval) (gfc_expr *,
> gfc_expr *, gfc_expr **),
>          gfc_replace_expr (c->expr, r);
>       }
>
> -  if (c || d)
> +  if (rc == ARITH_OK && (c || d))
>       rc = ARITH_INCOMMENSURATE;
>
>     if (rc != ARITH_OK)

that's great!  It fixes several rather weird cases.  (There is at least
another PR on the incommensurate arrays, but we should not attempt to
fix everything today.)

> There is one last thing that I'm dissatisfied with.
> The handling of unknown types should be moved to reduce_binary, because
> the dispatching in reduce_binary doesn't handle EXPR_OP, so even if
> either or both operands are scalar, they are handled by the (array vs
> array) reduce_binary_aa function.  That's confusing.

Do you have an example?

Anyway, please find attached an updated patch that incorporates your
two changes and regtests fine on x86_64-pc-linux-gnu.

Even if you disagree, I think this is really a significant step
forwards... (error-recovery wise).

OK for mainline?

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr107000-v2.diff --]
[-- Type: text/x-patch, Size: 8379 bytes --]

From 1b40214b2b538ec176ff6c118770e6e1cc8796ae Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 4 Oct 2022 23:04:06 +0200
Subject: [PATCH] Fortran: error recovery for invalid types in array
 constructors [PR107000]

gcc/fortran/ChangeLog:

	PR fortran/107000
	* arith.cc (gfc_arith_error): Define error message for
	ARITH_INVALID_TYPE.
	(reduce_unary): Catch arithmetic expressions with invalid type.
	(reduce_binary_ac): Likewise.
	(reduce_binary_ca): Likewise.
	(reduce_binary_aa): Likewise.
	(eval_intrinsic): Likewise.
	(gfc_real2complex): Source expression must be of type REAL.
	* gfortran.h (enum arith): Add ARITH_INVALID_TYPE.

gcc/testsuite/ChangeLog:

	PR fortran/107000
	* gfortran.dg/pr107000.f90: New test.

Co-authored-by: Mikael Morin <mikael@gcc.gnu.org>
---
 gcc/fortran/arith.cc                   | 23 +++++++++++-
 gcc/fortran/gfortran.h                 |  2 +-
 gcc/testsuite/gfortran.dg/pr107000.f90 | 50 ++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/pr107000.f90

diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc
index d57059a375f..2c57c796270 100644
--- a/gcc/fortran/arith.cc
+++ b/gcc/fortran/arith.cc
@@ -118,6 +118,9 @@ gfc_arith_error (arith code)
     case ARITH_WRONGCONCAT:
       p = G_("Illegal type in character concatenation at %L");
       break;
+    case ARITH_INVALID_TYPE:
+      p = G_("Invalid type in arithmetic operation at %L");
+      break;

     default:
       gfc_internal_error ("gfc_arith_error(): Bad error code");
@@ -1261,6 +1264,9 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op,
   gfc_expr *r;
   arith rc;

+  if (op->expr_type == EXPR_OP && op->ts.type == BT_UNKNOWN)
+    return ARITH_INVALID_TYPE;
+
   if (op->expr_type == EXPR_CONSTANT)
     return eval (op, result);

@@ -1302,6 +1308,9 @@ reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
   gfc_expr *r;
   arith rc = ARITH_OK;

+  if (op1->expr_type == EXPR_OP && op1->ts.type == BT_UNKNOWN)
+    return ARITH_INVALID_TYPE;
+
   head = gfc_constructor_copy (op1->value.constructor);
   for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
     {
@@ -1354,6 +1363,9 @@ reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
   gfc_expr *r;
   arith rc = ARITH_OK;

+  if (op2->expr_type == EXPR_OP && op2->ts.type == BT_UNKNOWN)
+    return ARITH_INVALID_TYPE;
+
   head = gfc_constructor_copy (op2->value.constructor);
   for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c))
     {
@@ -1414,6 +1426,10 @@ reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
   if (!gfc_check_conformance (op1, op2, _("elemental binary operation")))
     return ARITH_INCOMMENSURATE;

+  if ((op1->expr_type == EXPR_OP && op1->ts.type == BT_UNKNOWN)
+      || (op2->expr_type == EXPR_OP && op2->ts.type == BT_UNKNOWN))
+    return ARITH_INVALID_TYPE;
+
   head = gfc_constructor_copy (op1->value.constructor);
   for (c = gfc_constructor_first (head),
        d = gfc_constructor_first (op2->value.constructor);
@@ -1427,7 +1443,7 @@ reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **),
 	gfc_replace_expr (c->expr, r);
     }

-  if (c || d)
+  if (rc == ARITH_OK && (c || d))
     rc = ARITH_INCOMMENSURATE;

   if (rc != ARITH_OK)
@@ -1638,6 +1654,8 @@ eval_intrinsic (gfc_intrinsic_op op,
   else
     rc = reduce_binary (eval.f3, op1, op2, &result);

+  if (rc == ARITH_INVALID_TYPE)
+    goto runtime;

   /* Something went wrong.  */
   if (op == INTRINSIC_POWER && rc == ARITH_PROHIBIT)
@@ -2238,6 +2256,9 @@ gfc_real2complex (gfc_expr *src, int kind)
   arith rc;
   bool did_warn = false;

+  if (src->ts.type != BT_REAL)
+    return NULL;
+
   result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where);

   mpc_set_fr (result->value.complex, src->value.real, GFC_MPC_RND_MODE);
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 608dda4bf55..10bb098d136 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -226,7 +226,7 @@ enum gfc_intrinsic_op
 enum arith
 { ARITH_OK = 1, ARITH_OVERFLOW, ARITH_UNDERFLOW, ARITH_NAN,
   ARITH_DIV0, ARITH_INCOMMENSURATE, ARITH_ASYMMETRIC, ARITH_PROHIBIT,
-  ARITH_WRONGCONCAT
+  ARITH_WRONGCONCAT, ARITH_INVALID_TYPE
 };

 /* Statements.  */
diff --git a/gcc/testsuite/gfortran.dg/pr107000.f90 b/gcc/testsuite/gfortran.dg/pr107000.f90
new file mode 100644
index 00000000000..30289078c57
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr107000.f90
@@ -0,0 +1,50 @@
+! { dg-do compile }
+! PR fortran/107000 - ICE in gfc_real2complex, reduce_unary, reduce_binary_*
+! Contributed by G.Steinmetz
+
+program p
+  real    :: y(1)
+  complex :: x(1)
+  x = (1.0, 2.0) * [real :: -'1']    ! { dg-error "Operand of unary numeric operator" }
+  x = (1.0, 2.0) * [complex :: +'1'] ! { dg-error "Operand of unary numeric operator" }
+  x = [complex :: -'1'] * (1.0, 2.0) ! { dg-error "Operand of unary numeric operator" }
+  y = [complex :: -'1'] * 2          ! { dg-error "Operand of unary numeric operator" }
+  y = 2 * [complex :: -'1']          ! { dg-error "Operand of unary numeric operator" }
+  y = 2 * [complex :: -(.true.)]     ! { dg-error "Operand of unary numeric operator" }
+  y = [complex :: -(.true.)] * 2     ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real ::  -'1' ]        ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real :: [-'1']]        ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real ::  +(.true.) ]   ! { dg-error "Operand of unary numeric operator" }
+  print *, - [real :: [+(.true.)]]   ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real ::  -'1' ]      ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: (-'1')]      ! { dg-error "Operand of unary numeric operator" }
+  print *, [real ::  -'1' ] * 2      ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: (-'1')] * 2      ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [integer :: -('1')]   ! { dg-error "Operand of unary numeric operator" }
+  print *, [integer :: -('1')] * 2   ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 0, (-'1')]   ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 0, (-'1')] * 2   ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 0, -'1']     ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 0, -'1'] * 2     ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 0, 1+'1']    ! { dg-error "Operands of binary numeric operator" }
+  print *, [real :: 0, 1+'1'] * 2    ! { dg-error "Operands of binary numeric operator" }
+  print *, [real :: 1, +(.true.)]    ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, -(.true.)]    ! { dg-error "Operand of unary numeric operator" }
+  print *, 2 * [real :: 1, +(.true.)]      ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, +(.true.)] * 2      ! { dg-error "Operand of unary numeric operator" }
+  print *, [1, 2] * [real :: 1, +(.true.)] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, +(.true.)] * [1, 2] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, 2] * [real :: 1, +(.true.)] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, +(.true.)] * [real :: 1, 2] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 0, -'1'] * [real :: 1, +(+(.true.))] ! { dg-error "Operand of unary numeric operator" }
+  print *, [real :: 1, [(+(.true.))]] * [real :: 0, [(-'1')]] ! { dg-error "Operand of unary numeric operator" }
+
+  ! Legal:
+  print *, 2 * [real :: 1, [2], 3]
+  print *, [real :: 1, [2], 3] * 2
+  print *, [real :: 1, [2], 3] * [real :: 1, [2], 3]
+  print *, [real :: 1, [2], 3] * [integer :: 1, [2], 3]
+  print *, [real :: 1, [2], 3] * [1, [2], 3]
+  print *, [real :: 1,  huge(2.0)] * [real :: 1,  real(1.0)]
+  print *, [real :: 1, -(huge(2.0))] * [real :: 1, +(real(1))]
+end
--
2.35.3


  reply	other threads:[~2022-10-05 21:40 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-04 21:19 [PATCH] " Harald Anlauf
2022-10-05  8:51 ` Mikael Morin
2022-10-05  9:23   ` Mikael Morin
2022-10-05 21:40     ` Harald Anlauf [this message]
2022-10-06 20:14       ` [PATCH, v2] " Mikael Morin
2022-10-06 21:36         ` Harald Anlauf
2022-10-07  8:01           ` Mikael Morin
2022-10-07 18:46             ` Harald Anlauf
2022-10-07 19:47               ` Mikael Morin
2022-10-07 20:26                 ` [PATCH, v3] " Mikael Morin
2022-10-07 21:41                   ` Harald Anlauf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=trinity-7d605483-76d2-4fd9-8856-e95db3adad91-1665006051981@3c-app-gmx-bap28 \
    --to=anlauf@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=morin-mikael@orange.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).