Skip to content

Commit fcb9873

Browse files
committed
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 <pierre-emmanuel.patry@embecosm.com>
1 parent e830d3c commit fcb9873

File tree

4 files changed

+115
-75
lines changed

4 files changed

+115
-75
lines changed

gcc/rust/parse/rust-parse-error.h

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,72 @@ struct Visibility
319319
Visibility (Kind kind) : kind (kind) {}
320320
};
321321

322+
class LifetimeParam
323+
{
324+
};
325+
326+
class Lifetime
327+
{
328+
};
329+
330+
enum class AnonConst
331+
{
332+
InvalidSizeExpr,
333+
};
334+
335+
struct LoopLabel
336+
{
337+
static tl::expected<AST::LoopLabel, LoopLabel> make_not_loop_label ()
338+
{
339+
return tl::unexpected<LoopLabel> (LoopLabel (Kind::NOT_LOOP_LABEL));
340+
}
341+
342+
static tl::expected<AST::LoopLabel, LoopLabel> make_missing_colon ()
343+
{
344+
return tl::unexpected<LoopLabel> (LoopLabel (Kind::MISSING_COLON));
345+
}
346+
347+
enum class Kind
348+
{
349+
// Not an hard error
350+
NOT_LOOP_LABEL,
351+
// Hard error
352+
MISSING_COLON,
353+
} kind;
354+
355+
private:
356+
LoopLabel (Kind kind) : kind (kind) {}
357+
};
358+
359+
struct Self
360+
{
361+
static tl::expected<std::unique_ptr<AST::Param>, Self>
362+
make_self_raw_pointer ()
363+
{
364+
return tl::unexpected<Self> (Self (Kind::SELF_RAW_PTR));
365+
}
366+
367+
static tl::expected<std::unique_ptr<AST::Param>, Self> make_not_self ()
368+
{
369+
return tl::unexpected<Self> (Self (Kind::NOT_SELF));
370+
}
371+
372+
static tl::expected<std::unique_ptr<AST::Param>, Self> make_parsing_error ()
373+
{
374+
return tl::unexpected<Self> (Self (Kind::PARSING));
375+
}
376+
377+
enum class Kind
378+
{
379+
SELF_RAW_PTR,
380+
PARSING,
381+
NOT_SELF,
382+
} kind;
383+
384+
private:
385+
Self (Kind kind) : kind (kind) {}
386+
};
387+
322388
} // namespace Error
323389
} // namespace Parse
324390
} // namespace Rust

gcc/rust/parse/rust-parse-impl-expr.hxx

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Parser<ManagedTokenSource>::parse_block_expr (
9898
/* Parse an anonymous const expression. This can be a regular const expression
9999
* or an underscore for deferred const inference */
100100
template <typename ManagedTokenSource>
101-
tl::expected<AST::AnonConst, AnonConstError>
101+
tl::expected<AST::AnonConst, Parse::Error::AnonConst>
102102
Parser<ManagedTokenSource>::parse_anon_const ()
103103
{
104104
auto current = lexer.peek_token ();
@@ -111,7 +111,7 @@ Parser<ManagedTokenSource>::parse_anon_const ()
111111
auto expr = parse_expr ();
112112

113113
if (!expr)
114-
return tl::make_unexpected (AnonConstError::InvalidSizeExpr);
114+
return tl::make_unexpected (Parse::Error::AnonConst::InvalidSizeExpr);
115115

116116
return AST::AnonConst (std::move (expr), locus);
117117
}
@@ -1106,33 +1106,34 @@ std::unique_ptr<AST::Expr>
11061106
Parser<ManagedTokenSource>::parse_labelled_loop_expr (const_TokenPtr tok,
11071107
AST::AttrVec outer_attrs)
11081108
{
1109-
/* TODO: decide whether it should not work if there is no label, or parse it
1110-
* with no label at the moment, I will make it not work with no label
1111-
* because that's the implication. */
1112-
1113-
if (tok->get_id () != LIFETIME)
1114-
{
1115-
Error error (tok->get_locus (),
1116-
"expected lifetime in labelled loop expr (to parse loop "
1117-
"label) - found %qs",
1118-
tok->get_token_description ());
1119-
add_error (std::move (error));
1120-
1121-
// skip?
1122-
return nullptr;
1123-
}
1124-
11251109
// parse loop label (required)
1126-
// TODO: Convert this return type to tl::expected instead of tl::optional
11271110
auto parsed_label = parse_loop_label (tok);
11281111
if (!parsed_label)
11291112
{
1130-
Error error (lexer.peek_token ()->get_locus (),
1131-
"failed to parse loop label in labelled loop expr");
1132-
add_error (std::move (error));
1113+
/* TODO: decide whether it should not work if there is no label, or parse
1114+
* it with no label at the moment, I will make it not work with no label
1115+
* because that's the implication. */
11331116

1134-
// skip?
1135-
return nullptr;
1117+
if (parsed_label.error ().kind
1118+
== Parse::Error::LoopLabel::Kind::NOT_LOOP_LABEL)
1119+
{
1120+
Error error (tok->get_locus (),
1121+
"expected lifetime in labelled loop expr (to parse loop "
1122+
"label) - found %qs",
1123+
tok->get_token_description ());
1124+
add_error (std::move (error));
1125+
return nullptr;
1126+
}
1127+
1128+
else
1129+
{
1130+
Error error (lexer.peek_token ()->get_locus (),
1131+
"failed to parse loop label in labelled loop expr");
1132+
add_error (std::move (error));
1133+
1134+
// skip?
1135+
return nullptr;
1136+
}
11361137
}
11371138

11381139
auto label = parsed_label

gcc/rust/parse/rust-parse-impl.hxx

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ Parser<ManagedTokenSource>::parse_function (AST::Visibility vis,
15881588
auto initial_param = parse_self_param ();
15891589

15901590
if (!initial_param.has_value ()
1591-
&& initial_param.error () != ParseSelfError::NOT_SELF)
1591+
&& initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
15921592
return nullptr;
15931593

15941594
if (initial_param.has_value () && lexer.peek_token ()->get_id () == COMMA)
@@ -2138,7 +2138,7 @@ Parser<ManagedTokenSource>::parse_non_ptr_sequence (
21382138

21392139
/* Parses a single lifetime generic parameter (not including comma). */
21402140
template <typename ManagedTokenSource>
2141-
tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
2141+
tl::expected<AST::LifetimeParam, Parse::Error::LifetimeParam>
21422142
Parser<ManagedTokenSource>::parse_lifetime_param ()
21432143
{
21442144
// parse outer attributes, which are optional and may not exist
@@ -2149,7 +2149,7 @@ Parser<ManagedTokenSource>::parse_lifetime_param ()
21492149
if (lifetime_tok->get_id () != LIFETIME)
21502150
{
21512151
// if lifetime is missing, must not be a lifetime param, so return error
2152-
return tl::make_unexpected<ParseLifetimeParamError> ({});
2152+
return tl::make_unexpected<Parse::Error::LifetimeParam> ({});
21532153
}
21542154
lexer.skip_token ();
21552155
AST::Lifetime lifetime (AST::Lifetime::NAMED, lifetime_tok->get_str (),
@@ -2827,7 +2827,7 @@ Parser<ManagedTokenSource>::parse_lifetime_bounds (EndTokenPred is_end_token)
28272827
/* Parses a lifetime token (named, 'static, or '_). Also handles lifetime not
28282828
* existing. */
28292829
template <typename ManagedTokenSource>
2830-
tl::expected<AST::Lifetime, ParseLifetimeError>
2830+
tl::expected<AST::Lifetime, Parse::Error::Lifetime>
28312831
Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
28322832
{
28332833
const_TokenPtr lifetime_tok = lexer.peek_token ();
@@ -2839,7 +2839,7 @@ Parser<ManagedTokenSource>::parse_lifetime (bool allow_elided)
28392839
}
28402840
else
28412841
{
2842-
return tl::make_unexpected<ParseLifetimeError> ({});
2842+
return tl::make_unexpected<Parse::Error::Lifetime> ({});
28432843
}
28442844
}
28452845
lexer.skip_token ();
@@ -4286,7 +4286,7 @@ Parser<ManagedTokenSource>::parse_inherent_impl_function_or_method (
42864286
auto initial_param = parse_self_param ();
42874287

42884288
if (!initial_param.has_value ()
4289-
&& initial_param.error () != ParseSelfError::NOT_SELF)
4289+
&& initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
42904290
return nullptr;
42914291

42924292
/* FIXME: ensure that self param doesn't accidently consume tokens for a
@@ -4487,7 +4487,7 @@ Parser<ManagedTokenSource>::parse_trait_impl_function_or_method (
44874487
auto initial_param = parse_self_param ();
44884488

44894489
if (!initial_param.has_value ()
4490-
&& initial_param.error () != ParseSelfError::NOT_SELF)
4490+
&& initial_param.error ().kind != Parse::Error::Self::Kind::NOT_SELF)
44914491
return nullptr;
44924492

44934493
// FIXME: ensure that self param doesn't accidently consume tokens for a
@@ -5132,7 +5132,7 @@ Parser<ManagedTokenSource>::parse_generic_args_binding ()
51325132

51335133
// Parses a self param. Also handles self param not existing.
51345134
template <typename ManagedTokenSource>
5135-
tl::expected<std::unique_ptr<AST::Param>, ParseSelfError>
5135+
tl::expected<std::unique_ptr<AST::Param>, Parse::Error::Self>
51365136
Parser<ManagedTokenSource>::parse_self_param ()
51375137
{
51385138
bool has_reference = false;
@@ -5156,7 +5156,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
51565156
{
51575157
rust_error_at (lexer.peek_token ()->get_locus (),
51585158
"cannot pass %<self%> by raw pointer");
5159-
return tl::make_unexpected (ParseSelfError::SELF_PTR);
5159+
return Parse::Error::Self::make_self_raw_pointer ();
51605160
}
51615161
}
51625162

@@ -5177,7 +5177,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
51775177
is_self = true;
51785178

51795179
if (!is_self)
5180-
return tl::make_unexpected (ParseSelfError::NOT_SELF);
5180+
return Parse::Error::Self::make_not_self ();
51815181

51825182
// test if self is a reference parameter
51835183
if (lexer.peek_token ()->get_id () == AMP)
@@ -5200,7 +5200,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
52005200
add_error (std::move (error));
52015201

52025202
// skip after somewhere?
5203-
return tl::make_unexpected (ParseSelfError::PARSING);
5203+
return Parse::Error::Self::make_parsing_error ();
52045204
}
52055205
}
52065206
}
@@ -5218,7 +5218,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
52185218
if (self_tok->get_id () != SELF)
52195219
{
52205220
// skip after somewhere?
5221-
return tl::make_unexpected (ParseSelfError::NOT_SELF);
5221+
return Parse::Error::Self::make_not_self ();
52225222
}
52235223
lexer.skip_token ();
52245224

@@ -5237,7 +5237,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
52375237
add_error (std::move (error));
52385238

52395239
// skip after somewhere?
5240-
return tl::make_unexpected (ParseSelfError::PARSING);
5240+
return Parse::Error::Self::make_parsing_error ();
52415241
}
52425242
}
52435243

@@ -5250,7 +5250,7 @@ Parser<ManagedTokenSource>::parse_self_param ()
52505250
add_error (std::move (error));
52515251

52525252
// skip after somewhere?
5253-
return tl::make_unexpected (ParseSelfError::PARSING);
5253+
return Parse::Error::Self::make_parsing_error ();
52545254
}
52555255

52565256
if (has_reference)
@@ -5379,15 +5379,14 @@ Parser<ManagedTokenSource>::parse_expr_stmt (AST::AttrVec outer_attrs,
53795379

53805380
// Parses a loop label used in loop expressions.
53815381
template <typename ManagedTokenSource>
5382-
tl::expected<AST::LoopLabel, ParseLoopLabelError>
5382+
tl::expected<AST::LoopLabel, Parse::Error::LoopLabel>
53835383
Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
53845384
{
53855385
// parse lifetime - if doesn't exist, assume no label
53865386
if (tok->get_id () != LIFETIME)
53875387
{
53885388
// not necessarily an error
5389-
return tl::unexpected<ParseLoopLabelError> (
5390-
ParseLoopLabelError::NOT_LOOP_LABEL);
5389+
return Parse::Error::LoopLabel::make_not_loop_label ();
53915390
}
53925391
/* FIXME: check for named lifetime requirement here? or check in semantic
53935392
* analysis phase? */
@@ -5396,11 +5395,10 @@ Parser<ManagedTokenSource>::parse_loop_label (const_TokenPtr tok)
53965395
if (!skip_token (COLON))
53975396
{
53985397
// skip somewhere?
5399-
return tl::unexpected<ParseLoopLabelError> (
5400-
ParseLoopLabelError::MISSING_COLON);
5398+
Parse::Error::LoopLabel::make_missing_colon ();
54015399
}
54025400

5403-
return tl::expected<AST::LoopLabel, ParseLoopLabelError> (
5401+
return tl::expected<AST::LoopLabel, Parse::Error::LoopLabel> (
54045402
AST::LoopLabel (std::move (label), tok->get_locus ()));
54055403
}
54065404

gcc/rust/parse/rust-parse.h

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,6 @@ along with GCC; see the file COPYING3. If not see
2929

3030
namespace Rust {
3131

32-
class ParseLifetimeParamError
33-
{
34-
};
35-
36-
class ParseLifetimeError
37-
{
38-
};
39-
40-
enum class AnonConstError
41-
{
42-
InvalidSizeExpr,
43-
};
44-
45-
enum class ParseLoopLabelError
46-
{
47-
NOT_LOOP_LABEL,
48-
MISSING_COLON,
49-
};
50-
51-
enum class ParseSelfError
52-
{
53-
SELF_PTR,
54-
PARSING,
55-
NOT_SELF,
56-
};
57-
5832
// Left binding powers of operations.
5933
enum binding_powers
6034
{
@@ -275,7 +249,7 @@ template <typename ManagedTokenSource> class Parser
275249
tl::optional<AST::LoopLabel> = tl::nullopt,
276250
location_t pratt_parsed_loc = UNKNOWN_LOCATION);
277251

278-
tl::expected<AST::AnonConst, AnonConstError> parse_anon_const ();
252+
tl::expected<AST::AnonConst, Parse::Error::AnonConst> parse_anon_const ();
279253

280254
std::unique_ptr<AST::ConstBlock>
281255
parse_const_block_expr (AST::AttrVec outer_attrs = AST::AttrVec (),
@@ -420,7 +394,7 @@ template <typename ManagedTokenSource> class Parser
420394
ParseFunction parsing_function, EndTokenPred is_end_token,
421395
std::string error_msg = "failed to parse generic param in generic params")
422396
-> std::vector<decltype (parsing_function ())>;
423-
tl::expected<AST::LifetimeParam, ParseLifetimeParamError>
397+
tl::expected<AST::LifetimeParam, Parse::Error::LifetimeParam>
424398
parse_lifetime_param ();
425399
std::vector<std::unique_ptr<AST::TypeParam>> parse_type_params ();
426400
template <typename EndTokenPred>
@@ -450,7 +424,7 @@ template <typename ManagedTokenSource> class Parser
450424
std::vector<AST::Lifetime> parse_lifetime_bounds ();
451425
template <typename EndTokenPred>
452426
std::vector<AST::Lifetime> parse_lifetime_bounds (EndTokenPred is_end_token);
453-
tl::expected<AST::Lifetime, ParseLifetimeError>
427+
tl::expected<AST::Lifetime, Parse::Error::Lifetime>
454428
parse_lifetime (bool allow_elided);
455429
AST::Lifetime lifetime_from_token (const_TokenPtr tok);
456430
std::unique_ptr<AST::ExternalTypeItem>
@@ -486,7 +460,8 @@ template <typename ManagedTokenSource> class Parser
486460
std::unique_ptr<AST::ConstantItem>
487461
parse_trait_const (AST::AttrVec outer_attrs);
488462

489-
tl::expected<std::unique_ptr<AST::Param>, ParseSelfError> parse_self_param ();
463+
tl::expected<std::unique_ptr<AST::Param>, Parse::Error::Self>
464+
parse_self_param ();
490465

491466
std::unique_ptr<AST::Impl> parse_impl (AST::Visibility vis,
492467
AST::AttrVec outer_attrs);
@@ -753,7 +728,7 @@ template <typename ManagedTokenSource> class Parser
753728
std::unique_ptr<AST::Expr> parse_labelled_loop_expr (const_TokenPtr tok,
754729
AST::AttrVec outer_attrs
755730
= AST::AttrVec ());
756-
tl::expected<AST::LoopLabel, ParseLoopLabelError>
731+
tl::expected<AST::LoopLabel, Parse::Error::LoopLabel>
757732
parse_loop_label (const_TokenPtr tok);
758733
std::unique_ptr<AST::AsyncBlockExpr>
759734
parse_async_block_expr (AST::AttrVec outer_attrs = AST::AttrVec ());

0 commit comments

Comments
 (0)