From patchwork Wed Jan 5 03:21:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandra Loosemore X-Patchwork-Id: 49542 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B31C3858437 for ; Wed, 5 Jan 2022 03:21:55 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa4.mentor.iphmx.com (esa4.mentor.iphmx.com [68.232.137.252]) by sourceware.org (Postfix) with ESMTPS id E1D3B3858C2C; Wed, 5 Jan 2022 03:21:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E1D3B3858C2C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com IronPort-SDR: 5M6lro+DnE4ogmJLS1gpHIU2WQrzgsg9mwK/sOZ6zGnnfcWyMrunKKKcUqqzZHel8qddU8EIDs Ym+0+8j+uUsvv7mC1q1WmJ/NY1X7u67DER99ank64Z0TwGR/xnd7brCBPqjuTuJCvAvW7NCdy8 v6JybIg2tEYcMViDgjO4n01BPydbsJn7mf2DvJP+4s35XRbsqvxHSD4rpfJ4FKl+4DMH2+micP RuQEHQgUa+V6IC+L+VorCM8vp4Ui+xYQHX/L5Upm8a+7+GjPRc6QfydCcXOsUUlxafVEx2tuub JSd90E2yWQ7DgDHfdpS1xayh X-IronPort-AV: E=Sophos;i="5.88,262,1635235200"; d="scan'208";a="70393454" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa4.mentor.iphmx.com with ESMTP; 04 Jan 2022 19:21:28 -0800 IronPort-SDR: +SPU2qaxNsT6QsYy3MgXOGaNA7IAyDcFxkkZttgdtiqm+Tzzw1cPtu3O3wd96/he0vzKoIDhUb fgryhew1cCLx/ueGnsL/R2kklswBM5OLaqCQvBtlKCQMHjFKVd8kYfEcWY/BeyPRNDiQZyEdCw HSRqYaiKxDXN7q8Co8Y6PXcis/4dcNFMvDeTSZllsAig//yFtwq+Yv1/uwPOxdF2+gFOoTgWLH wGEU8x0A17FgWiMxokpGczlNMsuyOpIAJN/MECHygN8FkCpluRJEAlZsP4geIcPkwZrjUsTrpH wXQ= To: "fortran@gcc.gnu.org" From: Sandra Loosemore Subject: [PATCH] Fortran: Fix ICE caused by missing error for untyped symbol [PR103258] Message-ID: <77c049b9-998c-ad9a-d3ff-8593b581e1f6@codesourcery.com> Date: Tue, 4 Jan 2022 20:21:25 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 Content-Language: en-US X-ClientProxiedBy: SVR-ORW-MBX-05.mgc.mentorg.com (147.34.90.205) To svr-orw-mbx-03.mgc.mentorg.com (147.34.90.203) X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: "gcc-patches@gcc.gnu.org" Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" This patch fixes an ICE that appeared after I checked in my patch for PR101337 back in November, which made the resolve phase try harder to check all operands/arguments for errors instead of giving up after the first one, but it's actually a bug that existed before that and was only revealed by that earlier patch. The problem is that the parse phase is doing early resolution to try to constant-fold a character length expression. It's throwing away the error(s) if it fails, but in the test case for this issue it was leaving behind some state indicating that the error had already been diagnosed so it wasn't getting caught again during the "real" resolution phase either. Every bit of code touched by this seems kind of hacky to me -- the different mechanisms for suppressing/ignoring errors, the magic bit in the symbol attributes, the part that tries to constant-fold an expression that might not actually be a constant, etc. But, this is the least hacky fix I could come up with. :-P It fixes the test case from the issue and does not cause any regressions elsewhere in the gfortran testsuite. OK to check in? -Sandra commit ea7deef7dad4239435374884713a187ae8faa4eb Author: Sandra Loosemore Date: Tue Jan 4 18:18:13 2022 -0800 Fortran: Fix ICE caused by missing error for untyped symbol [PR103258] The bit on a symbol to mark that it had already been diagnosed as lacking a type was getting set even when the error was suppressed or discarded, specifically when doing early resolution on a character length expression to see if it can be constant-folded. Explicitly suppress errors before doing that, then check whether they are suppressed before setting the bit. 2022-01-04 Sandra Loosemore PR fortran/103258 gcc/fortran/ * decl.c (gfc_match_char_spec): Suppress errors around call to gfc_reduce_init_expr. * error.c (gfc_query_suppress_errors): New. * gfortran.h (gfc_query_suppress_errors): Declare. * symbol.c (gfc_set_default_type): Check gfc_query_suppress_errors. gcc/testsuite/ * gfortran.dg/pr103258.f90: New. diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c index 4e510cc..c846923 100644 --- a/gcc/fortran/decl.c +++ b/gcc/fortran/decl.c @@ -3609,7 +3609,9 @@ done: gfc_current_ns = gfc_get_namespace (NULL, 0); e = gfc_copy_expr (len); + gfc_push_suppress_errors (); gfc_reduce_init_expr (e); + gfc_pop_suppress_errors (); if (e->expr_type == EXPR_CONSTANT) { gfc_replace_expr (len, e); diff --git a/gcc/fortran/error.c b/gcc/fortran/error.c index be2eb93..e95c083 100644 --- a/gcc/fortran/error.c +++ b/gcc/fortran/error.c @@ -83,6 +83,15 @@ gfc_pop_suppress_errors (void) } +/* Query whether errors are suppressed. */ + +bool +gfc_query_suppress_errors (void) +{ + return suppress_errors > 0; +} + + /* Determine terminal width (for trimming source lines in output). */ static int diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index d01a9dc..3b791a4 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -1083,6 +1083,7 @@ typedef struct void gfc_push_suppress_errors (void); void gfc_pop_suppress_errors (void); +bool gfc_query_suppress_errors (void); /* Character length structures hold the expression that gives the diff --git a/gcc/fortran/symbol.c b/gcc/fortran/symbol.c index 0385595..1a4b022 100644 --- a/gcc/fortran/symbol.c +++ b/gcc/fortran/symbol.c @@ -299,7 +299,7 @@ gfc_set_default_type (gfc_symbol *sym, int error_flag, gfc_namespace *ns) if (ts->type == BT_UNKNOWN) { - if (error_flag && !sym->attr.untyped) + if (error_flag && !sym->attr.untyped && !gfc_query_suppress_errors ()) { const char *guessed = lookup_symbol_fuzzy (sym->name, sym); if (guessed) diff --git a/gcc/testsuite/gfortran.dg/pr103258.f90 b/gcc/testsuite/gfortran.dg/pr103258.f90 new file mode 100644 index 0000000..4521fcd --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr103258.f90 @@ -0,0 +1,14 @@ +! { dg-do compile} +! { dg-additional-options "-Wno-pedantic" } +! +! Test from PR103258. This used to ICE due to incorrectly marking the +! no-implicit-type error for n and m in the character length expression +! as already diagnosed during early resolution, when in fact errors are +! ignored in that parsing context. We now expect the errors to be diagnosed +! at the point of the first use of each symbol. + +subroutine s(n) ! { dg-error "Symbol 'n' .*has no IMPLICIT type" } +implicit none +character(n+m) :: c ! { dg-error "Symbol 'm' .*has no IMPLICIT type" } +entry e(m) +end