From patchwork Tue Aug 31 10:10:55 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 1522517 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=nextmovesoftware.com header.i=@nextmovesoftware.com header.a=rsa-sha256 header.s=default header.b=bbLkEIWJ; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4GzNJR47tjz9sR4 for ; Tue, 31 Aug 2021 20:11:33 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E1D23857409 for ; Tue, 31 Aug 2021 10:11:31 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 25FC63858401 for ; Tue, 31 Aug 2021 10:10:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 25FC63858401 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=cv3KgcuIbNEEUp1mAIHWlkB6hmjqMxGBHP8cJA+IuHM=; b=bbLkEIWJRel7uyHOs8wKrMBvEb tIkpK26+zew8FiSiau2wDEh+fsv+txRGkFa0ERzmZtWyP2dWKKADWV5rAlyS0QdMc8dY3TMXucsoK 4/loGi462SQLALVAtsK8nIjBxnipO+ke2qTdQfucBAgotPIx6Y4B7y8pg6vsQePfTtYXDTmBAii5f ioQ69eIPzM4d7lqF7dAXCvxZBrphgtfANkgrldptwuZN1hmOwGPjLgoEG6CtegWgFM/IxDcBaTwB7 yPOXRysmhYMGMjRgbpA4CzoKm7IydaEN6qI/mOXNDZdTd3htDS69JAqmwgQBQ2GyYAqnIgakIkJ38 iZGodyCQ==; Received: from host86-163-217-120.range86-163.btcentralplus.com ([86.163.217.120]:53367 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1mL0jK-0001T9-Kc for gcc-patches@gcc.gnu.org; Tue, 31 Aug 2021 06:10:58 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] C: PR c/79412: Poison decls with error_mark_node after type mismatch Date: Tue, 31 Aug 2021 11:10:55 +0100 Message-ID: <00ea01d79e50$7d49f490$77ddddb0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdeeR5KPb8H2SD4ARamFI5THtHMpjA== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, 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: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch fixes an ICE during error-recovery regression in the C front-end. The symptom is that the middle-end's sanity checking assertions fail during gimplification when being asked to increment an array, which is non-sense. The issue is that the C-front end has detected the type mismatch and reported an error to the user, but hasn't provided any indication of this to the middle-end, simply passing bogus trees that the optimizers recognize as invalid. This appears to be a frequently reported ICE with 94730, 94731, 101036 and 101365 all marked as duplicates. I believe the correct (polite) fix is to mark the mismatched types as problematic/dubious in the front-end, when the error is spotted, so that the middle-end has a heads-up and can be a little more forgiving. This patch to c-decl.c's duplicate_decls sets (both) mismatched types to error_mark_node if they are significantly different, and we've issued an error message. Alas, this is too punitive for FUNCTION_DECLs where we store return types, parameter lists, parameter types and attributes in the type, but fortunately the middle-end is already more cautious about trusting possibly suspect function types. This fix required one minor change to the testsuite, typedef-var-2.c where after conflicting type definitions, we now no longer assume that the (first or) second definition is the correct one. This change only affects the behaviour after seen_error(), so should be relatively safe. This patch has been tested on x86_64-pc-linux-gnu with "make bootstrap" and "make -k check" with no new failures. Ok for mainline? 2020-08-31 Roger Sayle gcc/c/ChangeLog PR c/79412 * c-decl.c (duplicate_decls): On significant mismatches, mark the types of both (non-function) decls as error_mark_node, so that the middle-end can see the code is malformed. (free_attr_access_data): Don't process if the type has been set to error_mark_node. gcc/testsuite/ChangeLog PR c/79412 * gcc.dg/pr79412.c: New test case. * gcc.dg/typedef-var-2.c: Update expeted errors. Roger --- /* { dg-do compile } */ /* { dg-options "-O2" } */ int a; /* { dg-message "note: previous declaration" "previous declaration" { target *-*-* } .-1 } */ void fn1 () { a++; } int a[] = {2}; /* { dg-error "conflicting types" } */ diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c index 221a67f..52fa2ca 100644 --- a/gcc/c/c-decl.c +++ b/gcc/c/c-decl.c @@ -2957,6 +2957,17 @@ duplicate_decls (tree newdecl, tree olddecl) { /* Avoid `unused variable' and other warnings for OLDDECL. */ suppress_warning (olddecl, OPT_Wunused); + /* If the types are completely different, poison them both with + error_mark_node. */ + if (TREE_CODE (TREE_TYPE (newdecl)) != TREE_CODE (TREE_TYPE (olddecl)) + && olddecl != error_mark_node + && seen_error()) + { + if (TREE_CODE (olddecl) != FUNCTION_DECL) + TREE_TYPE (olddecl) = error_mark_node; + if (TREE_CODE (newdecl) != FUNCTION_DECL) + TREE_TYPE (newdecl) = error_mark_node; + } return false; } @@ -12209,7 +12220,7 @@ free_attr_access_data () attr_access::free_lang_data (attrs); tree fntype = TREE_TYPE (n->decl); - if (!fntype) + if (!fntype || fntype == error_mark_node) continue; tree attrs = TYPE_ATTRIBUTES (fntype); if (!attrs) diff --git a/gcc/testsuite/gcc.dg/typedef-var-2.c b/gcc/testsuite/gcc.dg/typedef-var-2.c index 716d29c..bc119a0 100644 --- a/gcc/testsuite/gcc.dg/typedef-var-2.c +++ b/gcc/testsuite/gcc.dg/typedef-var-2.c @@ -4,12 +4,13 @@ int f (void) { extern float v; - +/* { dg-message "note: previous declaration" "previous declaration" { target *-*-* } .-1 } */ return (v > 0.0f); } extern int t; +/* { dg-message "note: previous declaration" "previous declaration" { target *-*-* } .-1 } */ typedef float t; /* { dg-error "redeclared as different kind of symbol" } */ -t v = 4.5f; +t v = 4.5f; /* { dg-error "conflicting types" } */