From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by sourceware.org (Postfix) with ESMTPS id 874373858432; Mon, 13 May 2024 20:27:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 874373858432 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 874373858432 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.227.15.15 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715632042; cv=none; b=C+yX1vonzPOaZJZjZnY3gKBfHTuwWiLa99bwLXQd3Myl376L3J6BHA3Csm6eJIlLp3DdectTRaj/bNbyVHAc25BdXul8A1heUan9Iui9kZZtxA9l38SOju9DnDVAs529/AA1qPKhIl4OOl7nDN0FTJkZb4MoAnzuNpi3w8uwPOU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715632042; c=relaxed/simple; bh=si25mJUAYpCrv4gQIIRKeHPnZQ7XpqS8Hms3G1o18gY=; h=DKIM-Signature:MIME-Version:Message-ID:From:To:Subject:Date; b=RwnHig5le+ghHUS6Ob9rE0GYyJzyBp7gKCMnqFGcYnrFXE5oyqE1lx6hAhAqjNz3u3Ec2FP+/WC7alW/NmrB7LtgP4ZOsqUyIztDmdMR1K3GmDOdS+u46z5whUiEsp8+x5X/I4p4/V/aEdQOh0XnP8wHF+s+Jnrtd+l6+OxXH0I= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1715632038; x=1716236838; i=anlauf@gmx.de; bh=rsxR2XEuHziz+aXhtGHUoI3zR+IMjiHkVMXOfthL978=; h=X-UI-Sender-Class:MIME-Version:Message-ID:From:To:Subject: Content-Type:Date:cc:content-transfer-encoding:content-type:date: from:message-id:mime-version:reply-to:subject:to; b=QTQANl86WrdXaxjDjMpDb0QFKp0M9ZlMAEt95lRqE621QVYdaOU96F0JIXaYPAQd nJZ6fRwqdbT0Za9le4KPEM4DkWGnA9awre4klj3UGhqGVw4cAXNCwRHXesE9K8DQh LQ77p1PW696ixCPEoDgl0DO0FJg4NNe7H3IIuMKViuCWuTvgrz6bxj7YfqmQUhUgV K++6gfi1owsWZRn94gJ1rvN6a0x2EzmNYlcWqEzh9Skq0uZD5lzhX6U4o6dhUAZ70 T4lMzvLv4psGMDn1DFeSfHALCf4eVKWoyEcpvaXSvgQixdkkYVLOdO6uJ1R5s89bT koE2bI2ecR+zdSdcog== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.84.116] ([93.207.84.116]) by web-mail.gmx.net (3c-app-gmx-bs23.server.lan [172.19.170.75]) (via HTTP); Mon, 13 May 2024 22:27:18 +0200 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: fix bounds check for assignment, class component [PR86100] Content-Type: multipart/mixed; boundary=kenitram-dd1db141-d908-4d65-a6ce-d674666232c7 Date: Mon, 13 May 2024 22:27:18 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:ibGew9CFeEg4YDgNO8kVmMZ+cIC2CyUy9Kg6at+y0GwuQXLdYuhvLIMKBjJFSeFstjuZN yW51wfjCc7OZ/pP9453Mse8SK+Qbp63E+6vH8168/h3vZ0P26FtjP0iyLEeDESY8gR+aGCAsoJMw aefQus/1gKZfkwTgIPJG+lF0MUW1ukd5H4xxwdz9RUUHNvyruCxDgigbM/ET9wSmVJuMnZSIhbhD FN/9Svh0weoKqMspaFM/VtRQ6sBS3YJwE8ovxR1ceuHlwpfmTyQvwsLUnrEYmFZ1gCBB3TXK2C2d vc= UI-OutboundReport: notjunk:1;M01:P0:GXoL2EDNX5g=;nxoC5KoWeVk95+26JUCx3LQX3TX KVX95AwrM3YiELvR42AOwXPGZRgMcTw8B0/jUUQhA1eICULAkoUzr8aejRrU/u/X7gsB2p9Gs HdSKoPLfcxWXZ1OMmjb6PfBrQDwTdVU34SaTLnM4ogjOqZUp+DBgSpNHozOZ+ycFhwLavbuqf fdGZ7Q3v3sPF0iE/K2mQu7ez+fvuP1D803rLI+BRtin0HUPdtN3ojMBE1tx8SFdc4gb1uWV4q 4mYQm0nRNOqXgFKPiln3QfhbB2D0qEPlAyX+3R3XEoxJDtZyi7tnPW28R2zoA/jlSTbV0nB5p E5Rx73i0L7nvgDRDmbFc8+2YQ9NWWs0ZFtxoziL2sXYiojnYKwQQX+nQkKXyBaNTK3cBblfMQ GSv4ETQ0nuxJfduu9wKdEPigQYThU5AUwJWcPYjXesEDx0N4tunTyaI+sRj6Qun7v4kOSWnZA Fq5/T3Gg3EuAfwP0BimVkqgarux5APrFY2KNO3KaPSUez3ElItbLMW+OzNGsPLUz3y1x6qXoD aA4uqdsXQ+Q/TVI0IbRzWdb06Mh0qY4uXtj0Yh1Mkuncp8kbGfM0bsgpWHzTxlcQiQUU/0W7p Pn8P5tRWtggYq+kw9jyZ3MsbFAyAmCfixPkGMNWly7cfeQArPqYQlS56nx3rQkUrjrNkpWeaB rUBFmIMGuYSDRpmHx/zfo1jd6oSDfuxJSX3vTBAqNGsDXXi0Pt9CRnPuqG4Ums0= X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --kenitram-dd1db141-d908-4d65-a6ce-d674666232c7 Content-Type: text/plain; charset=UTF-8 Dear all, the attached patch does two things: - it fixes a bogus array bounds check when deep-copying a class component of a derived type and the class component has rank > 1, the reason being that the previous code compared the full size of one side with the size of the first dimension of the other - the bounds-check error message that was generated e.g. by an allocate statement with conflicting sizes in the allocation and the source-expr will now use an improved abbreviated name pointing to the component involved, which was introduced in 14-development. What I could not resolve: a deep copy may still create no useful array name in the error message (which I am now unable to trigger). If someone sees how to extract it reliably from the tree, please let me know. Regtested on x86_64-pc-linux-gnu. OK for mainline? I would like to backport this to 14-branch after a decent delay. Thanks, Harald --kenitram-dd1db141-d908-4d65-a6ce-d674666232c7 Content-Type: text/x-patch Content-Disposition: attachment; filename=pr86100.diff Content-Transfer-Encoding: quoted-printable =46rom e187285dfd83da2f69cfd50854c701744dc8acc5 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Mon, 13 May 2024 22:06:33 +0200 Subject: [PATCH] Fortran: fix bounds check for assignment, class component [PR86100] gcc/fortran/ChangeLog: PR fortran/86100 * trans-array.cc (gfc_conv_ss_startstride): Use abridged_ref_name to generate a more user-friendly name for bounds-check messages. * trans-expr.cc (gfc_copy_class_to_class): Fix bounds check for rank>1 by looping over the dimensions. gcc/testsuite/ChangeLog: PR fortran/86100 * gfortran.dg/bounds_check_25.f90: New test. =2D-- gcc/fortran/trans-array.cc | 7 +++- gcc/fortran/trans-expr.cc | 40 ++++++++++--------- gcc/testsuite/gfortran.dg/bounds_check_25.f90 | 32 +++++++++++++++ 3 files changed, 60 insertions(+), 19 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/bounds_check_25.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index c5b56f4e273..eec62c296ff 100644 =2D-- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -4911,6 +4911,7 @@ done: gfc_expr *expr; locus *expr_loc; const char *expr_name; + char *ref_name =3D NULL; ss_info =3D ss->info; if (ss_info->type !=3D GFC_SS_SECTION) @@ -4922,7 +4923,10 @@ done: expr =3D ss_info->expr; expr_loc =3D &expr->where; - expr_name =3D expr->symtree->name; + if (expr->ref) + expr_name =3D ref_name =3D abridged_ref_name (expr, NULL); + else + expr_name =3D expr->symtree->name; gfc_start_block (&inner); @@ -5134,6 +5138,7 @@ done: gfc_add_expr_to_block (&block, tmp); + free (ref_name); } tmp =3D gfc_finish_block (&block); diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index e315e2d3370..dfc5b8e9b4a 100644 =2D-- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -1520,7 +1520,6 @@ gfc_copy_class_to_class (tree from, tree to, tree ne= lems, bool unlimited) stmtblock_t body; stmtblock_t ifbody; gfc_loopinfo loop; - tree orig_nelems =3D nelems; /* Needed for bounds check. */ gfc_init_block (&body); tmp =3D fold_build2_loc (input_location, MINUS_EXPR, @@ -1552,27 +1551,32 @@ gfc_copy_class_to_class (tree from, tree to, tree = nelems, bool unlimited) /* Add bounds check. */ if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc) { - char *msg; const char *name =3D "<>"; - tree from_len; + int dim, rank; if (DECL_P (to)) - name =3D (const char *)(DECL_NAME (to)->identifier.id.str); - - from_len =3D gfc_conv_descriptor_size (from_data, 1); - from_len =3D fold_convert (TREE_TYPE (orig_nelems), from_len); - tmp =3D fold_build2_loc (input_location, NE_EXPR, - logical_type_node, from_len, orig_nelems); - msg =3D xasprintf ("Array bound mismatch for dimension %d " - "of array '%s' (%%ld/%%ld)", - 1, name); - - gfc_trans_runtime_check (true, false, tmp, &body, - &gfc_current_locus, msg, - fold_convert (long_integer_type_node, orig_nelems), - fold_convert (long_integer_type_node, from_len)); + name =3D IDENTIFIER_POINTER (DECL_NAME (to)); - free (msg); + rank =3D GFC_TYPE_ARRAY_RANK (TREE_TYPE (from_data)); + for (dim =3D 1; dim <=3D rank; dim++) + { + tree from_len, to_len, cond; + char *msg; + + from_len =3D gfc_conv_descriptor_size (from_data, dim); + from_len =3D fold_convert (long_integer_type_node, from_len); + to_len =3D gfc_conv_descriptor_size (to_data, dim); + to_len =3D fold_convert (long_integer_type_node, to_len); + msg =3D xasprintf ("Array bound mismatch for dimension %d " + "of array '%s' (%%ld/%%ld)", + dim, name); + cond =3D fold_build2_loc (input_location, NE_EXPR, + logical_type_node, from_len, to_len); + gfc_trans_runtime_check (true, false, cond, &body, + &gfc_current_locus, msg, + to_len, from_len); + free (msg); + } } tmp =3D build_call_vec (fcn_type, fcn, args); diff --git a/gcc/testsuite/gfortran.dg/bounds_check_25.f90 b/gcc/testsuite= /gfortran.dg/bounds_check_25.f90 new file mode 100644 index 00000000000..cc2247597f9 =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/bounds_check_25.f90 @@ -0,0 +1,32 @@ +! { dg-do run } +! { dg-additional-options "-fcheck=3Dbounds -fdump-tree-original" } +! +! PR fortran/86100 - bogus bounds check with assignment, class component + +program p + implicit none + type any_matrix + class(*), allocatable :: m(:,:) + end type any_matrix + type(any_matrix) :: a, b + allocate (a%m, source=3Dreshape([3,5],shape=3D[1,2])) + + ! The following assignment did create a bogus bounds violation: + b =3D a ! Line 15 + if (any (shape (b%m) /=3D shape (a%m))) stop 1 + +contains + + ! Verify improved array name in array name + subroutine bla () + type(any_matrix) :: c, d + allocate (real :: c%m(3,5)) + allocate (d%m(7,9),source=3Dc%m) ! Line 24 + end subroutine bla +end + +! { dg-final { scan-tree-dump-times "line 15 .* bound mismatch for dimens= ion 1 of array .'.*.'" 1 "original" } } +! { dg-final { scan-tree-dump-times "line 15 .* bound mismatch for dimens= ion 2 of array .'.*.'" 1 "original" } } + +! { dg-final { scan-tree-dump-times "line 24 .* bound mismatch for dimens= ion 1 of array .'d%%m.'" 1 "original" } } +! { dg-final { scan-tree-dump-times "line 24 .* bound mismatch for dimens= ion 2 of array .'d%%m.'" 1 "original" } } =2D- 2.35.3 --kenitram-dd1db141-d908-4d65-a6ce-d674666232c7--