From: Pierre-Emmanuel Patry Date: Wed, 10 Dec 2025 12:38:24 +0000 (+0100) Subject: gccrs: Move old parser error classes to error header X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ab9a220c151692690939bef0ad5612912d7175e;p=thirdparty%2Fgcc.git gccrs: Move old parser error classes to error header A parser error header with Parse::Error namespace has recently been introduced. Move some old parser error classes to this namespace. gcc/rust/ChangeLog: * parse/rust-parse.h (class ParseLifetimeParamError): Move error from here ... (class ParseLifetimeError): Likewise. (enum class): Likewise. * parse/rust-parse-error.h (class LifetimeParam): ... to here. here. (class Lifetime): Likewise. (enum class): Likewise. (struct LoopLabel): Likewise and make it a full struct with ctors. (struct Self): Likewise. * parse/rust-parse-impl-expr.hxx: Make error point to new namespace. * parse/rust-parse-impl.hxx: Likewise. Signed-off-by: Pierre-Emmanuel Patry --- diff --git a/gcc/rust/parse/rust-parse-error.h b/gcc/rust/parse/rust-parse-error.h index c480ad761ae..06497a77008 100644 --- a/gcc/rust/parse/rust-parse-error.h +++ b/gcc/rust/parse/rust-parse-error.h @@ -319,6 +319,72 @@ private: Visibility (Kind kind) : kind (kind) {} }; +class LifetimeParam +{ +}; + +class Lifetime +{ +}; + +enum class AnonConst +{ + InvalidSizeExpr, +}; + +struct LoopLabel +{ + static tl::expected make_not_loop_label () + { + return tl::unexpected (LoopLabel (Kind::NOT_LOOP_LABEL)); + } + + static tl::expected make_missing_colon () + { + return tl::unexpected (LoopLabel (Kind::MISSING_COLON)); + } + + enum class Kind + { + // Not an hard error + NOT_LOOP_LABEL, + // Hard error + MISSING_COLON, + } kind; + +private: + LoopLabel (Kind kind) : kind (kind) {} +}; + +struct Self +{ + static tl::expected, Self> + make_self_raw_pointer () + { + return tl::unexpected (Self (Kind::SELF_RAW_PTR)); + } + + static tl::expected, Self> make_not_self () + { + return tl::unexpected (Self (Kind::NOT_SELF)); + } + + static tl::expected, Self> make_parsing_error () + { + return tl::unexpected (Self (Kind::PARSING)); + } + + enum class Kind + { + SELF_RAW_PTR, + PARSING, + NOT_SELF, + } kind; + +private: + Self (Kind kind) : kind (kind) {} +}; + } // namespace Error } // namespace Parse } // namespace Rust diff --git a/gcc/rust/parse/rust-parse-impl-expr.hxx b/gcc/rust/parse/rust-parse-impl-expr.hxx index 43124b72558..958b303abf5 100644 --- a/gcc/rust/parse/rust-parse-impl-expr.hxx +++ b/gcc/rust/parse/rust-parse-impl-expr.hxx @@ -98,7 +98,7 @@ Parser::parse_block_expr ( /* Parse an anonymous const expression. This can be a regular const expression * or an underscore for deferred const inference */ template -tl::expected +tl::expected Parser::parse_anon_const () { auto current = lexer.peek_token (); @@ -111,7 +111,7 @@ Parser::parse_anon_const () auto expr = parse_expr (); if (!expr) - return tl::make_unexpected (AnonConstError::InvalidSizeExpr); + return tl::make_unexpected (Parse::Error::AnonConst::InvalidSizeExpr); return AST::AnonConst (std::move (expr), locus); } @@ -1106,33 +1106,34 @@ std::unique_ptr Parser::parse_labelled_loop_expr (const_TokenPtr tok, AST::AttrVec outer_attrs) { - /* TODO: decide whether it should not work if there is no label, or parse it - * with no label at the moment, I will make it not work with no label - * because that's the implication. */ - - if (tok->get_id () != LIFETIME) - { - Error error (tok->get_locus (), - "expected lifetime in labelled loop expr (to parse loop " - "label) - found %qs", - tok->get_token_description ()); - add_error (std::move (error)); - - // skip? - return nullptr; - } - // parse loop label (required) - // TODO: Convert this return type to tl::expected instead of tl::optional auto parsed_label = parse_loop_label (tok); if (!parsed_label) { - Error error (lexer.peek_token ()->get_locus (), - "failed to parse loop label in labelled loop expr"); - add_error (std::move (error)); + /* TODO: decide whether it should not work if there is no label, or parse + * it with no label at the moment, I will make it not work with no label + * because that's the implication. */ - // skip? - return nullptr; + if (parsed_label.error ().kind + == Parse::Error::LoopLabel::Kind::NOT_LOOP_LABEL) + { + Error error (tok->get_locus (), + "expected lifetime in labelled loop expr (to parse loop " + "label) - found %qs", + tok->get_token_description ()); + add_error (std::move (error)); + return nullptr; + } + + else + { + Error error (lexer.peek_token ()->get_locus (), + "failed to parse loop label in labelled loop expr"); + add_error (std::move (error)); + + // skip? + return nullptr; + } } auto label = parsed_label diff --git a/gcc/rust/parse/rust-parse-impl.hxx b/gcc/rust/parse/rust-parse-impl.hxx index 01091c9f33d..9f307717bbc 100644 --- a/gcc/rust/parse/rust-parse-impl.hxx +++ b/gcc/rust/parse/rust-parse-impl.hxx @@ -1588,7 +1588,7 @@ Parser::parse_function (AST::Visibility vis, auto initial_param = parse_self_param (); if (!initial_param.has_value () - && initial_param.error () != ParseSelfError::NOT_SELF) + && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF) return nullptr; if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA) @@ -2138,7 +2138,7 @@ Parser::parse_non_ptr_sequence ( /* Parses a single lifetime generic parameter (not including comma). */ template -tl::expected +tl::expected Parser::parse_lifetime_param () { // parse outer attributes, which are optional and may not exist @@ -2149,7 +2149,7 @@ Parser::parse_lifetime_param () if (lifetime_tok->get_id () != LIFETIME) { // if lifetime is missing, must not be a lifetime param, so return error - return tl::make_unexpected ({}); + return tl::make_unexpected ({}); } lexer.skip_token (); AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (), @@ -2827,7 +2827,7 @@ Parser::parse_lifetime_bounds (EndTokenPred is_end_token) /* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not * existing. */ template -tl::expected +tl::expected Parser::parse_lifetime (bool allow_elided) { const_TokenPtr lifetime_tok = lexer.peek_token (); @@ -2839,7 +2839,7 @@ Parser::parse_lifetime (bool allow_elided) } else { - return tl::make_unexpected ({}); + return tl::make_unexpected ({}); } } lexer.skip_token (); @@ -4286,7 +4286,7 @@ Parser::parse_inherent_impl_function_or_method ( auto initial_param = parse_self_param (); if (!initial_param.has_value () - && initial_param.error () != ParseSelfError::NOT_SELF) + && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF) return nullptr; /* FIXME: ensure that self param doesn't accidently consume tokens for a @@ -4487,7 +4487,7 @@ Parser::parse_trait_impl_function_or_method ( auto initial_param = parse_self_param (); if (!initial_param.has_value () - && initial_param.error () != ParseSelfError::NOT_SELF) + && initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF) return nullptr; // FIXME: ensure that self param doesn't accidently consume tokens for a @@ -5132,7 +5132,7 @@ Parser::parse_generic_args_binding () // Parses a self param. Also handles self param not existing. template -tl::expected, ParseSelfError> +tl::expected, Parse::Error::Self> Parser::parse_self_param () { bool has_reference = false; @@ -5156,7 +5156,7 @@ Parser::parse_self_param () { rust_error_at (lexer.peek_token ()->get_locus (), "cannot pass % by raw pointer"); - return tl::make_unexpected (ParseSelfError::SELF_PTR); + return Parse::Error::Self::make_self_raw_pointer (); } } @@ -5177,7 +5177,7 @@ Parser::parse_self_param () is_self = true; if (!is_self) - return tl::make_unexpected (ParseSelfError::NOT_SELF); + return Parse::Error::Self::make_not_self (); // test if self is a reference parameter if (lexer.peek_token ()->get_id () == AMP) @@ -5200,7 +5200,7 @@ Parser::parse_self_param () add_error (std::move (error)); // skip after somewhere? - return tl::make_unexpected (ParseSelfError::PARSING); + return Parse::Error::Self::make_parsing_error (); } } } @@ -5218,7 +5218,7 @@ Parser::parse_self_param () if (self_tok->get_id () != SELF) { // skip after somewhere? - return tl::make_unexpected (ParseSelfError::NOT_SELF); + return Parse::Error::Self::make_not_self (); } lexer.skip_token (); @@ -5237,7 +5237,7 @@ Parser::parse_self_param () add_error (std::move (error)); // skip after somewhere? - return tl::make_unexpected (ParseSelfError::PARSING); + return Parse::Error::Self::make_parsing_error (); } } @@ -5250,7 +5250,7 @@ Parser::parse_self_param () add_error (std::move (error)); // skip after somewhere? - return tl::make_unexpected (ParseSelfError::PARSING); + return Parse::Error::Self::make_parsing_error (); } if (has_reference) @@ -5379,15 +5379,14 @@ Parser::parse_expr_stmt (AST::AttrVec outer_attrs, // Parses a loop label used in loop expressions. template -tl::expected +tl::expected Parser::parse_loop_label (const_TokenPtr tok) { // parse lifetime - if doesn't exist, assume no label if (tok->get_id () != LIFETIME) { // not necessarily an error - return tl::unexpected ( - ParseLoopLabelError::NOT_LOOP_LABEL); + return Parse::Error::LoopLabel::make_not_loop_label (); } /* FIXME: check for named lifetime requirement here? or check in semantic * analysis phase? */ @@ -5396,11 +5395,10 @@ Parser::parse_loop_label (const_TokenPtr tok) if (!skip_token (COLON)) { // skip somewhere? - return tl::unexpected ( - ParseLoopLabelError::MISSING_COLON); + Parse::Error::LoopLabel::make_missing_colon (); } - return tl::expected ( + return tl::expected ( AST::LoopLabel (std::move (label), tok->get_locus ())); } diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 9e6d4bc215d..95745a60ce1 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -29,32 +29,6 @@ along with GCC; see the file COPYING3. If not see namespace Rust { -class ParseLifetimeParamError -{ -}; - -class ParseLifetimeError -{ -}; - -enum class AnonConstError -{ - InvalidSizeExpr, -}; - -enum class ParseLoopLabelError -{ - NOT_LOOP_LABEL, - MISSING_COLON, -}; - -enum class ParseSelfError -{ - SELF_PTR, - PARSING, - NOT_SELF, -}; - // Left binding powers of operations. enum binding_powers { @@ -275,7 +249,7 @@ public: tl::optional = tl::nullopt, location_t pratt_parsed_loc = UNKNOWN_LOCATION); - tl::expected parse_anon_const (); + tl::expected parse_anon_const (); std::unique_ptr parse_const_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (), @@ -420,7 +394,7 @@ private: ParseFunction parsing_function, EndTokenPred is_end_token, std::string error_msg = "failed to parse generic param in generic params") -> std::vector; - tl::expected + tl::expected parse_lifetime_param (); std::vector> parse_type_params (); template @@ -450,7 +424,7 @@ private: std::vector parse_lifetime_bounds (); template std::vector parse_lifetime_bounds (EndTokenPred is_end_token); - tl::expected + tl::expected parse_lifetime (bool allow_elided); AST::Lifetime lifetime_from_token (const_TokenPtr tok); std::unique_ptr @@ -486,7 +460,8 @@ private: std::unique_ptr parse_trait_const (AST::AttrVec outer_attrs); - tl::expected, ParseSelfError> parse_self_param (); + tl::expected, Parse::Error::Self> + parse_self_param (); std::unique_ptr parse_impl (AST::Visibility vis, AST::AttrVec outer_attrs); @@ -752,7 +727,7 @@ private: std::unique_ptr parse_labelled_loop_expr (const_TokenPtr tok, AST::AttrVec outer_attrs = AST::AttrVec ()); - tl::expected + tl::expected parse_loop_label (const_TokenPtr tok); std::unique_ptr parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());