From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id 588993858295 for ; Tue, 30 Jan 2024 12:10:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 588993858295 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 588993858295 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::331 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616658; cv=none; b=pPOJqV1IjLjPAMtO/uAQff7rnFwXs8VpjJDRaWbSDVqFcA1+FkPh9EfoC5Qvo61QFyH7jMKxLlneWZ97ukcqJ13nBIrHIvYb0suN4sm2XFkHen2aqQB3Gbw0PcpjbXe2D/LYW/c7T8Dj4Jm9uFtgfkBKG8PTDZ2djcZw5mf2q5M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1706616658; c=relaxed/simple; bh=2db6XbycPDK1uxdrDo9crMoNgodRprwZXaTGc77PcZs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UcWf22d260NFfo1cfQhSAqKxTSIkFukrpT8sHY+XkaXtJNdS0q0LasDI9ULNuP+iCeONw9gYdt6yhmdfnRi+9EHCB2wEPFCxnI2rn5KOYrqSD3naeP20GARLTKEzSc0VCdnTVTTrYhv3gpIM4xnQ2TBHigWDOH0oi96rHeT3OzE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x331.google.com with SMTP id 5b1f17b1804b1-40eacb6067dso52372335e9.1 for ; Tue, 30 Jan 2024 04:10:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1706616652; x=1707221452; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=7xoPxrkkfSFHEMZEenGxVGmEmD0lTBQk2GncYUzypyM=; b=QPSn+QOhX9fFDnwdebW0SswHW5UMp6+TBKRpFaQfN1VFKCJSzmb/rjvZ6D4W6uGJBG q8Qeka3uxhTHcqFFVN9uplQ3AWu6vJM2J1Hh8WDDwjR6qloks6SozRo/hsf+DXQAcnky 7U5/GGYnEYAqIvNw3pS0V506qwgat+ypKxIFBH7bwEGXAzBocwHXCH9oJCWVvLUJllvk dLCqWfcNjpcvYWmfnyAvbvfEkg8lTV7bBS+9qOxMPYrOzUBjLOpd3DrSfrfktBKCyb0w x8vqU28eQiCTUdUctH8TEGz0XxfIzLQUgRjhGvDvN7wmvxItvUcDiThHuZOnlqGT09z8 LYxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706616652; x=1707221452; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=7xoPxrkkfSFHEMZEenGxVGmEmD0lTBQk2GncYUzypyM=; b=ZKq+Pq6G48u9rCQKjuNOye9uohyGcYxpC0KT24YiibQp0KN2kmFdH0XItRPKwIzkn0 a4dmnOW7mUT86m9DXP63SFd117GL9QM5QH2nFusEQom7El1dr7f832sWgl2A5KM8R+Ae KGdt2vibOfVfBoApYVqff09k751rZJHQRT5AmZ2KweYov1yP+j+SfaqE7wu3E/QMc1FC Iyb5FEDixgM616Db7uBSJ+B0uKrruZbS7A//ttDfHug8AH775g0Gdczcov0y/Ig/zowh PYV5TIjEglR9DREEiA92wHemEexnNQEceHabLVvJOpXstW9teJQbm+rEelEd90/dOgTw XKMg== X-Gm-Message-State: AOJu0YwBpy1Co7/4xaAa+2AKCKqI7FjRJFEPYELVJSc+zrb/Xu5PBuZ7 LA9uWd9oA+rdXif8z6KEnr74ToF5P/iwzwRs8P+4rqFAytfWnLdgTXfjXaP3A4oSe4uHMeGDPFJ uYg== X-Google-Smtp-Source: AGHT+IEvoJP8CmGfdgFriMko/6lJGJ5zVX1TsvDvwRzzAPixKk9CI9rEObjp5ZvpHngfrgItkdu50g== X-Received: by 2002:a05:600c:4754:b0:40e:a39e:461f with SMTP id w20-20020a05600c475400b0040ea39e461fmr7890395wmo.38.1706616651989; Tue, 30 Jan 2024 04:10:51 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id f9-20020a056000036900b00339307d9d31sm10569894wrf.112.2024.01.30.04.10.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jan 2024 04:10:51 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Pierre-Emmanuel Patry Subject: [COMMITTED 004/101] gccrs: Report self parameter parsing error kind Date: Tue, 30 Jan 2024 13:06:20 +0100 Message-ID: <20240130121026.807464-7-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240130121026.807464-2-arthur.cohen@embecosm.com> References: <20240130121026.807464-2-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Pierre-Emmanuel Patry Self parameter parsing errors may come from different situations, which should not be handled in the same way. It is now possible to differentiate a missing self parameter from a self pointer or a parsing error. gcc/rust/ChangeLog: * parse/rust-parse-impl.h (Parser::parse_function): Early return on unrecoverable errors. (Parser::parse_trait_item): Likewise. (Parser::parse_self_param): Update return type. * parse/rust-parse.h (enum ParseSelfError): Add enumeration to describe different self parameter parsing errors. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/parse/rust-parse-impl.h | 72 ++++++++++++++++++++------------ gcc/rust/parse/rust-parse.h | 12 +++++- 2 files changed, 56 insertions(+), 28 deletions(-) diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 1ebe1ed442c..de17412c3b6 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -2880,8 +2880,13 @@ Parser::parse_function (AST::Visibility vis, return nullptr; } - std::unique_ptr initial_param = parse_self_param (); - if (initial_param != nullptr) + auto initial_param = parse_self_param (); + + if (!initial_param.has_value () + && initial_param.error () != ParseSelfError::NOT_SELF) + return nullptr; + + if (initial_param.has_value ()) skip_token (COMMA); // parse function parameters (only if next token isn't right paren) @@ -2891,9 +2896,9 @@ Parser::parse_function (AST::Visibility vis, function_params = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; }); - if (initial_param != nullptr) + if (initial_param.has_value ()) function_params.insert (function_params.begin (), - std::move (initial_param)); + std::move (*initial_param)); if (!skip_token (RIGHT_PAREN)) { @@ -5063,13 +5068,15 @@ Parser::parse_trait_item () /* now for function vs method disambiguation - method has opening * "self" param */ - std::unique_ptr initial_param = parse_self_param (); + auto initial_param = parse_self_param (); + if (!initial_param.has_value () && initial_param.error () != NOT_SELF) + return nullptr; /* FIXME: ensure that self param doesn't accidently consume tokens for * a function */ bool is_method = false; - if (initial_param != nullptr) + if (initial_param.has_value ()) { - if (initial_param->is_self ()) + if ((*initial_param)->is_self ()) is_method = true; /* skip comma so function and method regular params can be parsed @@ -5089,9 +5096,9 @@ Parser::parse_trait_item () return nullptr; } - if (initial_param != nullptr) + if (initial_param.has_value ()) function_params.insert (function_params.begin (), - std::move (initial_param)); + std::move (*initial_param)); // parse return type (optional) std::unique_ptr return_type = parse_function_return_type (); @@ -5609,14 +5616,18 @@ Parser::parse_inherent_impl_function_or_method ( // now for function vs method disambiguation - method has opening "self" // param - std::unique_ptr initial_param = parse_self_param (); + auto initial_param = parse_self_param (); + + if (!initial_param.has_value () && initial_param.error () != NOT_SELF) + return nullptr; + /* FIXME: ensure that self param doesn't accidently consume tokens for a * function one idea is to lookahead up to 4 tokens to see whether self is * one of them */ bool is_method = false; - if (initial_param != nullptr) + if (initial_param.has_value ()) { - if (initial_param->is_self ()) + if ((*initial_param)->is_self ()) is_method = true; /* skip comma so function and method regular params can be parsed in @@ -5629,9 +5640,9 @@ Parser::parse_inherent_impl_function_or_method ( std::vector> function_params = parse_function_params ([] (TokenId id) { return id == RIGHT_PAREN; }); - if (initial_param != nullptr) + if (initial_param.has_value ()) function_params.insert (function_params.begin (), - std::move (initial_param)); + std::move (*initial_param)); if (!skip_token (RIGHT_PAREN)) { @@ -5817,13 +5828,17 @@ Parser::parse_trait_impl_function_or_method ( // now for function vs method disambiguation - method has opening "self" // param - std::unique_ptr initial_param = parse_self_param (); + auto initial_param = parse_self_param (); + + if (!initial_param.has_value () && initial_param.error () != NOT_SELF) + return nullptr; + // FIXME: ensure that self param doesn't accidently consume tokens for a // function bool is_method = false; - if (initial_param != nullptr) + if (initial_param.has_value ()) { - if (initial_param->is_self ()) + if ((*initial_param)->is_self ()) is_method = true; // skip comma so function and method regular params can be parsed in @@ -5861,9 +5876,9 @@ Parser::parse_trait_impl_function_or_method ( } } - if (initial_param != nullptr) + if (initial_param.has_value ()) function_params.insert (function_params.begin (), - std::move (initial_param)); + std::move (*initial_param)); // DEBUG rust_debug ("successfully parsed function params in function or method " @@ -7112,7 +7127,7 @@ Parser::parse_qualified_path_in_type () // Parses a self param. Also handles self param not existing. template -std::unique_ptr +tl::expected, ParseSelfError> Parser::parse_self_param () { bool has_reference = false; @@ -7133,8 +7148,11 @@ Parser::parse_self_param () if (lexer.peek_token (i)->get_id () != s[i]) break; if (i == s.size ()) - rust_error_at (lexer.peek_token ()->get_locus (), - "cannot pass % by raw pointer"); + { + rust_error_at (lexer.peek_token ()->get_locus (), + "cannot pass % by raw pointer"); + return tl::make_unexpected (ParseSelfError::SELF_PTR); + } } // Trying to find those patterns: @@ -7154,7 +7172,7 @@ Parser::parse_self_param () is_self = true; if (!is_self) - return nullptr; + return tl::make_unexpected (ParseSelfError::NOT_SELF); // test if self is a reference parameter if (lexer.peek_token ()->get_id () == AMP) @@ -7175,7 +7193,7 @@ Parser::parse_self_param () add_error (std::move (error)); // skip after somewhere? - return nullptr; + return tl::make_unexpected (ParseSelfError::PARSING); } } } @@ -7193,7 +7211,7 @@ Parser::parse_self_param () if (self_tok->get_id () != SELF) { // skip after somewhere? - return nullptr; + return tl::make_unexpected (ParseSelfError::NOT_SELF); } lexer.skip_token (); @@ -7212,7 +7230,7 @@ Parser::parse_self_param () add_error (std::move (error)); // skip after somewhere? - return nullptr; + return tl::make_unexpected (ParseSelfError::PARSING); } } @@ -7225,7 +7243,7 @@ Parser::parse_self_param () add_error (std::move (error)); // skip after somewhere? - return nullptr; + return tl::make_unexpected (ParseSelfError::PARSING); } if (has_reference) diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 08e6ce000f4..e873d5292cd 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -22,6 +22,8 @@ along with GCC; see the file COPYING3. If not see #include "rust-ast-full.h" #include "rust-diagnostics.h" +#include "expected.h" + namespace Rust { /* HACK: used to resolve the expression-or-statement problem at the end of a * block by allowing either to be returned (technically). Tagged union would @@ -93,6 +95,12 @@ struct ParseRestrictions bool allow_close_after_expr_stmt = false; }; +enum ParseSelfError +{ + SELF_PTR, + PARSING, + NOT_SELF, +}; // Parser implementation for gccrs. // TODO: if updated to C++20, ManagedTokenSource would be useful as a concept template class Parser @@ -335,7 +343,9 @@ private: parse_trait_type (AST::AttrVec outer_attrs, AST::Visibility); std::unique_ptr parse_trait_const (AST::AttrVec outer_attrs); - std::unique_ptr parse_self_param (); + + tl::expected, ParseSelfError> parse_self_param (); + std::unique_ptr parse_impl (AST::Visibility vis, AST::AttrVec outer_attrs); std::unique_ptr -- 2.42.1