From patchwork Mon Aug 5 07:32:43 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Franciszek Witt X-Patchwork-Id: 1968915 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=MIBCeslR; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.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 ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wcp822gY7z1yZl for ; Mon, 5 Aug 2024 17:33:22 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7AD7B385DDE4 for ; Mon, 5 Aug 2024 07:33:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x535.google.com (mail-ed1-x535.google.com [IPv6:2a00:1450:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id A72BC385841E for ; Mon, 5 Aug 2024 07:32:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A72BC385841E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A72BC385841E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::535 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722843179; cv=none; b=pWxF54wGru5t7Ythud4jDYcGTqjEYVUi7LuyeX7tFie9xJQMR+1BrVyPlVX5A2X03JoBRG8M4Z2ogJ8a8pNGyIZYfd6GHDjxJ34fnomKBp1/9XDgw79baJ+sqPyfQCP85wGl3t3LJoSDh0Fe0dqo9gNs/6B11ncfJR1WNXqNh6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722843179; c=relaxed/simple; bh=4JX+pvs2XTYZH2haF8oBUNvt9NbtLn7UzrNa+jFUxwM=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=mYxqqkuE/UKKibDSoHeEEcrpqU9irz5evEuidYwKbFlApPsdC/0Qfly7iTnsrba0bGA2AiYKzmEJzHUDVZNoOOc8MVUG3pW5St6u0/YCGYXsGCxw+4ewkdPsLqgJXnfLUggTX3JoDymAZ+1at7Z1n8lcjHsnEKgWYyu4e6esvgU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x535.google.com with SMTP id 4fb4d7f45d1cf-5b7b6a30454so4926188a12.2 for ; Mon, 05 Aug 2024 00:32:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722843175; x=1723447975; darn=gcc.gnu.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=AcY1RhN3phUsxnsgk6BfhJcu2Ov7WBvCr5NOYYqN5Ek=; b=MIBCeslRBiVkpAgiMajYIeiuHy5N+Req7fiwJbwQiXCL2n+2uUDbj5YF7ECdqazkJm iMoYG/X+krBs23XdBE75MELfUmstxM/HMbJDZjjVFALC/We9YePcHVRYwNB4qXPacpXv Uc699qXlLsUu889TPx17z5cXSRlEHlQDSRNAzhh6GPB4TT05Hxa3S2KJddTyVMmE6hRx 5uxjCfyiJ6BRfI5fSe5LV7uOhiPBxw/j+jbHzp3nWxUKXXYoT0gSWkUGdv2MbMe2RpfN 2sVPj1FlJbgTuCzZLzt/N21qoQ1BWIIf1oPMDeCPVOet+lVO3+RpL3wRLUdnCYyFfY6/ +mJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722843175; x=1723447975; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=AcY1RhN3phUsxnsgk6BfhJcu2Ov7WBvCr5NOYYqN5Ek=; b=fywe45XO5BIQHDSnyyzg/wnQiNUNekuYRtJbQwNo5UD0u3M+RbKQi2owjHOHiHRfcy xZFK7GgPfiMY/bk8b+eNEH9H7ZC6CN0qEH9NX+b2vUcEfWDqHY4OxoczahmjbFgaQno0 Ia4B9FNUZ/+/d7Hsa0t30be28m3jEMEoX71c73PsGm0eZApObj60vEJf7I248dVokHZM baSQ1Sq5dKHMInEhczM4puCDcgE5BERvR8E1+2Z3UIlLzTqN7VZz4shSKxKrjVG2KYgX ypZG6yO8ksIYBtcw7y2plQR6WDs+ZbO1AwHDhfeiY1ew5JBuX4YaFnyC1ZbA4rul+00V TuRQ== X-Gm-Message-State: AOJu0Yxc2vVvHWA+zd++CANnVvrDrqweY7GXK4tBsBexcBJlh9KB6A3j tSO4Ti8J9C3BVZyj+M0HwkBS5TQPmVkwWmrwNdHeHAXclair3eaBIHLPNxWISWo+dJxfr9uDQul qs+WpCRuveQwmrKJdY/S38xRtkyreLh8k X-Google-Smtp-Source: AGHT+IHcNEVNRbt+G588jXpiOnk7L2Ze3BEL3JBod4IQFaYi9ViOZZonE8btjKk97RQC/NdRUc0//dnLSXyQjcFto24= X-Received: by 2002:a05:6402:14d0:b0:5b5:2048:ba8d with SMTP id 4fb4d7f45d1cf-5b7f3ad6ea8mr7514368a12.16.1722843174866; Mon, 05 Aug 2024 00:32:54 -0700 (PDT) MIME-Version: 1.0 From: Franciszek Witt Date: Mon, 5 Aug 2024 09:32:43 +0200 Message-ID: Subject: [PATCH v3] Improve bad error message with stray semicolon in initializer (and related) [PR101232] To: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, HTML_MESSAGE, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 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 Hi, could someone review this patch? This is built on top of the v2 from https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101232 the only difference is fix for error59.C I have tested it on x86_64 Ubuntu 22 machine. Regards Franciszek --- Author: Franciszek Witt Date: Mon Aug 5 09:00:35 2024 +0200 c++: [PR101232] PR c++/101232 gcc/cp/ChangeLog: * parser.cc (cp_parser_postfix_expression): Commit to the parse in case we know its either a cast or invalid syntax. (cp_parser_braced_list): Add a heuristic to inform about missing comma or operator. gcc/testsuite/ChangeLog: * g++.dg/parse/error59.C: Change the test so the new error message is accepted. * g++.dg/cpp0x/initlist-err1.C: New test. * g++.dg/cpp0x/initlist-err2.C: New test. * g++.dg/cpp0x/initlist-err3.C: New test. --- diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 1dd0efaf963..2e0ce1c6ddb 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -7881,8 +7881,13 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p, --parser->prevent_constrained_type_specifiers; /* Parse the cast itself. */ if (!cp_parser_error_occurred (parser)) - postfix_expression - = cp_parser_functional_cast (parser, type); + { + if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) + /* This can only be a cast. */ + cp_parser_commit_to_topmost_tentative_parse (parser); + postfix_expression + = cp_parser_functional_cast (parser, type); + } /* If that worked, we're done. */ if (cp_parser_parse_definitely (parser)) break; @@ -26350,8 +26355,19 @@ cp_parser_braced_list (cp_parser *parser, bool *non_constant_p /*=nullptr*/) else if (non_constant_p) *non_constant_p = false; /* Now, there should be a trailing `}'. */ - location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location; - braces.require_close (parser); + cp_token * token = cp_lexer_peek_token (parser->lexer); + location_t finish_loc = token->location; + if (!braces.require_close (parser)) + { + /* This is just a heuristic. */ + if (token->type != CPP_SEMICOLON) + { + inform (finish_loc, + "probably missing a comma or an operator before"); + if (cp_parser_skip_to_closing_brace (parser)) + cp_lexer_consume_token (parser->lexer); + } + } TREE_TYPE (initializer) = init_list_type_node; recompute_constructor_flags (initializer); diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-err1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-err1.C new file mode 100644 index 00000000000..6ea8afb3273 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-err1.C @@ -0,0 +1,11 @@ +// PR c++/101232 +// { dg-do compile { target c++11 } } + +struct X { + int a; + int b; +}; + +void f() { + auto x = X{ 1, 2; }; // { dg-error "21:" } +} // { dg-prune-output "expected declaration" } diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-err2.C b/gcc/testsuite/g++.dg/cpp0x/initlist-err2.C new file mode 100644 index 00000000000..227f519dc19 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-err2.C @@ -0,0 +1,11 @@ +// PR c++/101232 +// { dg-do compile { target c++11 } } + +struct X { + int a; + int b; +}; + +void f() { + auto x = X{ 1 2 }; // { dg-error "19:.*probably" } +} diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-err3.C b/gcc/testsuite/g++.dg/cpp0x/initlist-err3.C new file mode 100644 index 00000000000..b77ec9bf4e9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-err3.C @@ -0,0 +1,11 @@ +// PR c++/101232 +// { dg-do compile { target c++11 } } + +struct X { + int a; + int b; +}; + +void f() { + auto x = X{ 1, {2 }; // { dg-error "expected.*before" } +} diff --git a/gcc/testsuite/g++.dg/parse/error59.C b/gcc/testsuite/g++.dg/parse/error59.C index 2c44e210366..d782c9b1616 100644 --- a/gcc/testsuite/g++.dg/parse/error59.C +++ b/gcc/testsuite/g++.dg/parse/error59.C @@ -3,4 +3,4 @@ void foo() { (struct {}x){}; // { dg-error "" } -} +} // { dg-excess-errors "" }