From patchwork Tue Apr 7 18:50:48 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2 via Gcc-patches" X-Patchwork-Id: 1267608 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@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=jq2Y9sTU; 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 48xc1Z6g99z9sP7 for ; Wed, 8 Apr 2020 04:50:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C453B3877034; Tue, 7 Apr 2020 18:50:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C453B3877034 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1586285453; bh=8oxOA4lOvCGcw4vm1zTCTmkaYPx9EiLiW3TXVPh/1hI=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=jq2Y9sTUOnoJQSCGjYyn5mopBVgj4XGGhcBroJvF2j2Fqq8CGEfYY9EqK0EC8SqAM lbWWUdor00T52D98fdKSdj48JrSd1iEtx8eFdPlzPa/uE3BNcX89e9pxfiRNB9dVK2 dzd1Xq9V6RZcSnvRx/fV9AhklUL34gnL7hPDti0w= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-qk1-x732.google.com (mail-qk1-x732.google.com [IPv6:2607:f8b0:4864:20::732]) by sourceware.org (Postfix) with ESMTPS id 600E5385DC34 for ; Tue, 7 Apr 2020 18:50:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 600E5385DC34 Received: by mail-qk1-x732.google.com with SMTP id z15so480035qki.7 for ; Tue, 07 Apr 2020 11:50:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language; bh=8oxOA4lOvCGcw4vm1zTCTmkaYPx9EiLiW3TXVPh/1hI=; b=B8bywfouQqlZZ8h193qJIDjQT4NxnU9q4vyKBDBraSvrXHbVYxRPVpz2Jvca7NZiec 8XDFjwU9vX6raNvlsTlsPbnbxeyHOuT3Fq/LShFWWVkcF4FgQYiYnrTR/JjQ39j1hJ91 tR9aYYiqxMip/biHeL23jFXHHk2n9XdqQepWyjMCNN0LDcbwv80lHHQ4REcDiQ47WCf1 D/2UIrBCxS1prH9y0tUGePXdu5O+F5btPSERsMdYVUr1wG5Q4tqS6lzxtZaWgsKXdsLw VlKK7YpDtCOL2pMHYWIe2bU5u4qnWlsLwvBW+IhdeYyMigMAJv7fP4yMZYLDMnbMtuzC rrAg== X-Gm-Message-State: AGi0PubYWHowEyT9ygv16V4/LOEhKXUvsvvGDsAZyWNxU42gRnXDUz8y YB2PbDX4HIGVTjqP1T356V8= X-Google-Smtp-Source: APiQypLi8CzyUhd0nBUmtycP/gfpzAB8tE/SPZQNrEN6j6P8XW35vxgkRVt8dFBb5nz7WGniJMU1LQ== X-Received: by 2002:a37:ccb:: with SMTP id 194mr3767755qkm.476.1586285449960; Tue, 07 Apr 2020 11:50:49 -0700 (PDT) Received: from [192.168.0.41] (97-118-122-145.hlrn.qwest.net. [97.118.122.145]) by smtp.gmail.com with ESMTPSA id r15sm17113727qkm.53.2020.04.07.11.50.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 07 Apr 2020 11:50:49 -0700 (PDT) To: gcc-patches , Jason Merrill Subject: [PATCH] reject scalar array initialization with nullptr [PR94510] Message-ID: <50c22816-dd24-4ceb-6620-04c168642427@gmail.com> Date: Tue, 7 Apr 2020 12:50:48 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-20.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , X-Patchwork-Original-From: Martin Sebor via Gcc-patches From: "Li, Pan2 via Gcc-patches" Reply-To: Martin Sebor Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Among the numerous regressions introduced by the change committed to GCC 9 to allow string literals as template arguments is a failure to recognize the C++ nullptr and GCC's __null constants as pointers. For one, I didn't realize that nullptr, being a null pointer constant, doesn't have a pointer type, and two, I didn't think of __null (which is a special integer constant that NULL sometimes expands to). The attached patch adjusts the special handling of trailing zero initializers in reshape_init_array_1 to recognize both kinds of constants and avoid treating them as zeros of the array integer element type. This restores the expected diagnostics when either constant is used in the initializer list. Martin PR c++/94510 - nullptr_t implicitly cast to zero twice in std::array gcc/cp/ChangeLog: PR c++/94510 * decl.c (reshape_init_array_1): Exclude mismatches with all kinds of pointers. gcc/testsuite/ChangeLog: PR c++/94510 * g++.dg/init/array57.C: New test. * g++.dg/init/array58.C: New test. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index a127734af69..692c8ed73f4 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -6041,9 +6041,14 @@ reshape_init_array_1 (tree elt_type, tree max_index, reshape_iter *d, TREE_CONSTANT (new_init) = false; /* Pointers initialized to strings must be treated as non-zero - even if the string is empty. */ + even if the string is empty. Handle all kinds of pointers, + including std::nullptr and GCC's __nullptr, neither of which + has a pointer type. */ tree init_type = TREE_TYPE (elt_init); - if (POINTER_TYPE_P (elt_type) != POINTER_TYPE_P (init_type) + bool init_is_ptr = (POINTER_TYPE_P (init_type) + || NULLPTR_TYPE_P (init_type) + || null_node_p (elt_init)); + if (POINTER_TYPE_P (elt_type) != init_is_ptr || !type_initializer_zero_p (elt_type, elt_init)) last_nonzero = index; diff --git a/gcc/testsuite/g++.dg/init/array57.C b/gcc/testsuite/g++.dg/init/array57.C new file mode 100644 index 00000000000..fdd7e76eb18 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array57.C @@ -0,0 +1,15 @@ +/* PR c++/94510 - nullptr_t implicitly cast to zero twice in std::array + { dg-do compile } */ + +int ia1[2] = { (void*)0 }; // { dg-error "invalid conversion from 'void\\\*'" } +int ia2[2] = { (void*)0, 0 }; // { dg-error "invalid conversion from 'void\\\*'" } +int ia3[] = { (void*)0, 0 }; // { dg-error "invalid conversion from 'void\\\*'" } + +int ia4[2] = { __null }; // { dg-warning "\\\[-Wconversion-null" } +int ia5[2] = { __null, 0 }; // { dg-warning "\\\[-Wconversion-null" } +int ia6[] = { __null, 0 }; // { dg-warning "\\\[-Wconversion-null" } + + +const char ca1[2] = { (char*)0, 0 }; // { dg-error "invalid conversion from 'char\\\*'" } + +const char ca2[2] = { __null, 0 }; // { dg-warning "\\\[-Wconversion-null" } diff --git a/gcc/testsuite/g++.dg/init/array58.C b/gcc/testsuite/g++.dg/init/array58.C new file mode 100644 index 00000000000..655e08fa600 --- /dev/null +++ b/gcc/testsuite/g++.dg/init/array58.C @@ -0,0 +1,21 @@ +/* PR c++/94510 - nullptr_t implicitly cast to zero twice in std::array + { dg-do compile { target c++11 } } */ + +namespace std { +typedef __typeof__ (nullptr) nullptr_t; +} + +int ia1[2] = { nullptr }; // { dg-error "cannot convert 'std::nullptr_t' to 'int'" } +int ia2[2] = { nullptr, 0 }; // { dg-error "cannot convert 'std::nullptr_t' to 'int'" } +int ia3[] = { nullptr, 0 }; // { dg-error "cannot convert 'std::nullptr_t' to 'int'" } + +int ia4[2] = { (std::nullptr_t)0 }; // { dg-error "cannot convert 'std::nullptr_t' to 'int'" } +int ia5[2] = { (std::nullptr_t)0, 0 }; // { dg-error "cannot convert 'std::nullptr_t' to 'int'" } +int ia6[] = { (std::nullptr_t)0, 0 }; // { dg-error "cannot convert 'std::nullptr_t' to 'int'" } + + +const char ca1[2] = { nullptr, 0 }; // { dg-error "cannot convert 'std::nullptr_t' to 'const char'" } + +const char ca2[2] = { (char*)nullptr, 0 };// { dg-error "invalid conversion from 'char\\\*' to 'char'" } + +const char ca3[2] = { std::nullptr_t () };// { dg-error "cannot convert 'std::nullptr_t'" }