From patchwork Mon Jun 26 00:18:13 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 780564 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3wwqQx5FvXz9s4q for ; Mon, 26 Jun 2017 10:18:32 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="H7MU7RIJ"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:from:to:cc:references:message-id:date:mime-version :in-reply-to:content-type; q=dns; s=default; b=eP38JNZPu6fi52oXu XtZ/f9GWYPOrtmjIID6XEMBu1+kRnW08L1yYEjcAvCRjNRmf1G1u806wvMTxkZZW G8Vyei7QVuD9p6LGm/rmRARJ36/SWccLRVyxulxhkFDBoe4fxt3i4mtebt5/1Xq8 +d08x3IQHFWA74Jc0bQcRAQAss= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :subject:from:to:cc:references:message-id:date:mime-version :in-reply-to:content-type; s=default; bh=hK4fOWyCJYBrZ8/gbA5RWOJ MRJw=; b=H7MU7RIJaYFW7bl3qxnUBhAAZiyaE9gjvX4u7X/f/dPWZdl1lfehDwA 3yb63DxG0DW4i42QzCiSpWTk8t3pt6KAHnVNfUIL4NIP1RYpsrIviCc9EYKbXBS7 FfM+hV+J/szAQk4dzCKVk7ZUSViXnP7W9PuIAiQ2anh84nuw+9gY= Received: (qmail 84881 invoked by alias); 26 Jun 2017 00:18:22 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 84857 invoked by uid 89); 26 Jun 2017 00:18:21 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: userp1040.oracle.com Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 26 Jun 2017 00:18:19 +0000 Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id v5Q0IG2U030621 (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 26 Jun 2017 00:18:17 GMT Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0021.oracle.com (8.13.8/8.14.4) with ESMTP id v5Q0IGmT009673 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 26 Jun 2017 00:18:16 GMT Received: from abhmp0019.oracle.com (abhmp0019.oracle.com [141.146.116.25]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id v5Q0IFjH019066; Mon, 26 Jun 2017 00:18:15 GMT Received: from [192.168.1.4] (/87.16.215.96) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sun, 25 Jun 2017 17:18:15 -0700 Subject: Re: [C++ Patch] PR 65775 ("Late-specified return type bypasses return type checks (qualified, function, array)") From: Paolo Carlini To: "gcc-patches@gcc.gnu.org" Cc: Jason Merrill References: Message-ID: <1a012c96-26ab-3fb3-ba01-5d93c508d28d@oracle.com> Date: Mon, 26 Jun 2017 02:18:13 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.1.0 MIME-Version: 1.0 In-Reply-To: X-IsSubscribed: yes ... in fact, simply moving the checks forward, past the splice_late_return_type call, appears to work fine. I'm finishing testing the below. Thanks! Paolo. ///////////////////// /cp 2017-06-25 Paolo Carlini PR c++/65775 * decl.c (grokdeclarator): Move checks on function return type after the splice_late_return_type call; if declspecs->locations[ds_type_spec] is UNKNOWN_LOCATION fall back to input_location. /testsuite 2017-06-25 Paolo Carlini PR c++/65775 * g++.dg/cpp0x/trailing14.C: New. Index: testsuite/g++.dg/cpp0x/trailing14.C =================================================================== --- testsuite/g++.dg/cpp0x/trailing14.C (revision 0) +++ testsuite/g++.dg/cpp0x/trailing14.C (working copy) @@ -0,0 +1,15 @@ +// PR c++/65775 +// { dg-do compile { target c++11 } } +// { dg-options "-Wignored-qualifiers" } + +using Qi = int const volatile; +Qi q1(); // { dg-warning "1: type qualifiers ignored" } +auto q2() -> Qi; // { dg-warning "1: type qualifiers ignored" } + +using Fi = int(); +Fi f1(); // { dg-error "1: 'f1' declared as function returning a function" } +auto f2() -> Fi; // { dg-error "1: 'f2' declared as function returning a function" } + +using Ai = int[5]; +Ai a1(); // { dg-error "1: 'a1' declared as function returning an array" } +auto a2() -> Ai; // { dg-error "1: 'a2' declared as function returning an array" } Index: cp/decl.c =================================================================== --- cp/decl.c (revision 249632) +++ cp/decl.c (working copy) @@ -9993,6 +9993,8 @@ grokdeclarator (const cp_declarator *declarator, declspecs->locations); if (typespec_loc == UNKNOWN_LOCATION) typespec_loc = declspecs->locations[ds_type_spec]; + if (typespec_loc == UNKNOWN_LOCATION) + typespec_loc = input_location; /* Look inside a declarator for the name being declared and get it as a string, for an error message. */ @@ -10825,34 +10827,8 @@ grokdeclarator (const cp_declarator *declarator, tree arg_types; int funcdecl_p; - /* Declaring a function type. - Make sure we have a valid type for the function to return. */ + /* Declaring a function type. */ - if (type_quals != TYPE_UNQUALIFIED) - { - if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)) - { - warning_at (typespec_loc, OPT_Wignored_qualifiers, "type " - "qualifiers ignored on function return type"); - } - /* We now know that the TYPE_QUALS don't apply to the - decl, but to its return type. */ - type_quals = TYPE_UNQUALIFIED; - } - - /* Error about some types functions can't return. */ - - if (TREE_CODE (type) == FUNCTION_TYPE) - { - error ("%qs declared as function returning a function", name); - return error_mark_node; - } - if (TREE_CODE (type) == ARRAY_TYPE) - { - error ("%qs declared as function returning an array", name); - return error_mark_node; - } - input_location = declspecs->locations[ds_type_spec]; abstract_virtuals_error (ACU_RETURN, type); input_location = saved_loc; @@ -10959,8 +10935,36 @@ grokdeclarator (const cp_declarator *declarator, return error_mark_node; if (late_return_type) - late_return_type_p = true; + { + late_return_type_p = true; + type_quals = cp_type_quals (type); + } + if (type_quals != TYPE_UNQUALIFIED) + { + if (SCALAR_TYPE_P (type) || VOID_TYPE_P (type)) + warning_at (typespec_loc, OPT_Wignored_qualifiers, "type " + "qualifiers ignored on function return type"); + /* We now know that the TYPE_QUALS don't apply to the + decl, but to its return type. */ + type_quals = TYPE_UNQUALIFIED; + } + + /* Error about some types functions can't return. */ + + if (TREE_CODE (type) == FUNCTION_TYPE) + { + error_at (typespec_loc, "%qs declared as function returning " + "a function", name); + return error_mark_node; + } + if (TREE_CODE (type) == ARRAY_TYPE) + { + error_at (typespec_loc, "%qs declared as function returning " + "an array", name); + return error_mark_node; + } + if (ctype == NULL_TREE && decl_context == FIELD && funcdecl_p