Skip to content

Commit adf8940

Browse files
committed
Remove redundant error types
Some error types in the parser had the exact same meaning and could be grouped under one same type. gcc/rust/ChangeLog: * ast/rust-ast.cc (AttributeParser::parse_meta_item_lit): * expand/rust-macro-builtins-helpers.cc (parse_single_string_literal): * expand/rust-macro-expand.cc (MacroExpander::match_fragment): * parse/rust-cfg-parser.cc (parse_cfg_option): * parse/rust-parse-error.h (struct SimplePath): (struct DelimTokenTree): (struct Token): (struct TokenTree): (class LifetimeParam): (struct LifetimeParam): (class Lifetime): (enum class): (struct BlockExpr): * parse/rust-parse-impl-expr.hxx: * parse/rust-parse-impl-path.hxx: * parse/rust-parse-impl-ttree.hxx: * parse/rust-parse-impl.hxx: * parse/rust-parse.h: Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
1 parent 8c2fac6 commit adf8940

File tree

10 files changed

+97
-168
lines changed

10 files changed

+97
-168
lines changed

gcc/rust/ast/rust-ast.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,7 +3816,7 @@ DelimTokenTree::to_token_stream () const
38163816
std::unique_ptr<MetaItemLitExpr>
38173817
AttributeParser::parse_meta_item_lit ()
38183818
{
3819-
std::unique_ptr<LiteralExpr> lit_expr = parser->parse_literal_expr ({});
3819+
auto lit_expr = parser->parse_literal_expr ({});
38203820

38213821
// TODO: return nullptr instead?
38223822
if (!lit_expr)
@@ -3825,7 +3825,7 @@ AttributeParser::parse_meta_item_lit ()
38253825
lexer->peek_token ()->get_locus ()));
38263826

38273827
return std::unique_ptr<MetaItemLitExpr> (
3828-
new MetaItemLitExpr (std::move (*lit_expr)));
3828+
new MetaItemLitExpr (std::move (*lit_expr.value ())));
38293829
}
38303830

38313831
bool

gcc/rust/expand/rust-macro-builtins-helpers.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ parse_single_string_literal (BuiltinMacro kind,
188188

189189
if (parser.peek_current_token ()->get_id () == STRING_LITERAL)
190190
{
191-
lit_expr = parser.parse_literal_expr ();
191+
lit_expr = parser.parse_literal_expr ().value ();
192192
parser.maybe_skip_token (COMMA);
193193
if (parser.peek_current_token ()->get_id () != last_token_id)
194194
{

gcc/rust/expand/rust-macro-expand.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ MacroExpander::match_fragment (Parser<MacroInvocLexer> &parser,
427427
break;
428428

429429
case AST::MacroFragSpec::LITERAL:
430-
parser.parse_literal_expr ();
430+
std::ignore = parser.parse_literal_expr ();
431431
break;
432432

433433
case AST::MacroFragSpec::ITEM:

gcc/rust/parse/rust-cfg-parser.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ parse_cfg_option (std::string &input, std::string &key, std::string &value)
5151
{
5252
rust_assert (parser.skip_token (EQUAL));
5353

54-
auto value_expr = parser.parse_literal_expr ();
54+
auto value_expr_res = parser.parse_literal_expr ();
5555
// We had an equal sign but no value, error out
56-
if (!value_expr)
56+
if (!value_expr_res)
5757
return false;
58+
auto value_expr = std::move (value_expr_res.value ());
5859

5960
if (value_expr->get_lit_type () != AST::Literal::LitType::STRING)
6061
return false;

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

Lines changed: 22 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,6 @@ struct Attribute
5555
Attribute (Kind kind) : kind (kind) {}
5656
};
5757

58-
struct SimplePath
59-
{
60-
static tl::expected<AST::SimplePath, SimplePath> make_malformed ()
61-
{
62-
return tl::unexpected<SimplePath> (SimplePath (Kind::MALFORMED));
63-
}
64-
65-
enum class Kind
66-
{
67-
MALFORMED,
68-
} kind;
69-
70-
private:
71-
SimplePath (Kind kind) : kind (kind) {}
72-
};
73-
7458
struct AttributeBody
7559
{
7660
static tl::expected<Parse::AttributeBody, AttributeBody> make_invalid_path ()
@@ -172,81 +156,6 @@ struct AttrInput
172156
AttrInput (Kind kind) : kind (kind) {}
173157
};
174158

175-
struct DelimTokenTree
176-
{
177-
static tl::expected<AST::DelimTokenTree, DelimTokenTree>
178-
make_expected_delimiter ()
179-
{
180-
return tl::unexpected<DelimTokenTree> (
181-
DelimTokenTree (Kind::EXPECTED_DELIMITER));
182-
}
183-
184-
static tl::expected<AST::DelimTokenTree, DelimTokenTree>
185-
make_invalid_token_tree ()
186-
{
187-
return tl::unexpected<DelimTokenTree> (
188-
DelimTokenTree (Kind::INVALID_TOKEN_TREE));
189-
}
190-
191-
static tl::expected<AST::DelimTokenTree, DelimTokenTree>
192-
make_mismatched_delimiters ()
193-
{
194-
return tl::unexpected<DelimTokenTree> (
195-
DelimTokenTree (Kind::INVALID_TOKEN_TREE));
196-
}
197-
198-
enum class Kind
199-
{
200-
EXPECTED_DELIMITER,
201-
INVALID_TOKEN_TREE,
202-
MISMATCHED_DELIMITERS,
203-
} kind;
204-
205-
private:
206-
DelimTokenTree (Kind kind) : kind (kind) {}
207-
};
208-
209-
struct Token
210-
{
211-
static tl::expected<std::unique_ptr<AST::Token>, Token> make_malformed ()
212-
{
213-
return tl::unexpected<Token> (Token (Kind::MALFORMED));
214-
}
215-
216-
enum class Kind
217-
{
218-
MALFORMED,
219-
} kind;
220-
221-
private:
222-
Token (Kind kind) : kind (kind) {}
223-
};
224-
225-
struct TokenTree
226-
{
227-
static tl::expected<std::unique_ptr<AST::TokenTree>, TokenTree>
228-
make_malformed ()
229-
{
230-
return tl::unexpected<TokenTree> (TokenTree (Kind::MALFORMED));
231-
}
232-
233-
static tl::expected<std::unique_ptr<AST::TokenTree>, TokenTree>
234-
make_malformed_delimited_token_tree ()
235-
{
236-
return tl::unexpected<TokenTree> (
237-
TokenTree (Kind::MALFORMED_DELIMITED_TOKEN_TREE));
238-
}
239-
240-
enum class Kind
241-
{
242-
MALFORMED,
243-
MALFORMED_DELIMITED_TOKEN_TREE,
244-
} kind;
245-
246-
private:
247-
TokenTree (Kind kind) : kind (kind) {}
248-
};
249-
250159
struct Item
251160
{
252161
static tl::expected<std::unique_ptr<AST::Item>, Item> make_end_of_file ()
@@ -319,17 +228,26 @@ struct Visibility
319228
Visibility (Kind kind) : kind (kind) {}
320229
};
321230

322-
class LifetimeParam
231+
struct LifetimeParam
323232
{
324-
};
233+
static tl::expected<AST::LifetimeParam, LifetimeParam>
234+
make_not_a_lifetime_param ()
235+
{
236+
return tl::unexpected<LifetimeParam> (
237+
LifetimeParam (Kind::NOT_A_LIFETIME_PARAM));
238+
}
325239

326-
class Lifetime
327-
{
240+
enum class Kind
241+
{
242+
NOT_A_LIFETIME_PARAM,
243+
} kind;
244+
245+
private:
246+
LifetimeParam (Kind kind) : kind (kind) {}
328247
};
329248

330-
enum class AnonConst
249+
class Lifetime
331250
{
332-
InvalidSizeExpr,
333251
};
334252

335253
struct LoopLabel
@@ -385,21 +303,14 @@ struct Self
385303
Self (Kind kind) : kind (kind) {}
386304
};
387305

388-
struct BlockExpr
306+
// Generic intermediate AST node error used when the errors need no special
307+
// handling
308+
enum class Node
389309
{
390-
static tl::expected<std::unique_ptr<AST::BlockExpr>, BlockExpr>
391-
make_malformed ()
392-
{
393-
return tl::unexpected<BlockExpr> (BlockExpr (Kind::MALFORMED));
394-
}
395-
396-
enum class Kind
397-
{
398-
MALFORMED,
399-
} kind;
400-
401-
private:
402-
BlockExpr (Kind kind) : kind (kind) {}
310+
// Unexpected or missing token whilst parsing the node
311+
MALFORMED,
312+
// Error whilst parsing a child construct for the current node
313+
CHILD_ERROR,
403314
};
404315

405316
} // namespace Error

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

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Rust {
2626

2727
// Parses a block expression, including the curly braces at start and end.
2828
template <typename ManagedTokenSource>
29-
tl::expected<std::unique_ptr<AST::BlockExpr>, Parse::Error::BlockExpr>
29+
tl::expected<std::unique_ptr<AST::BlockExpr>, Parse::Error::Node>
3030
Parser<ManagedTokenSource>::parse_block_expr (
3131
AST::AttrVec outer_attrs, tl::optional<AST::LoopLabel> label,
3232
location_t pratt_parsed_loc)
@@ -38,7 +38,7 @@ Parser<ManagedTokenSource>::parse_block_expr (
3838
if (!skip_token (LEFT_CURLY))
3939
{
4040
skip_after_end_block ();
41-
return Parse::Error::BlockExpr::make_malformed ();
41+
return tl::unexpected (Parse::Error::Node::MALFORMED);
4242
}
4343
}
4444

@@ -55,7 +55,7 @@ Parser<ManagedTokenSource>::parse_block_expr (
5555
if (expr_or_stmt.is_error ())
5656
{
5757
skip_after_end_block ();
58-
return Parse::Error::BlockExpr::make_malformed ();
58+
return tl::unexpected (Parse::Error::Node::CHILD_ERROR);
5959
}
6060

6161
t = lexer.peek_token ();
@@ -82,7 +82,7 @@ Parser<ManagedTokenSource>::parse_block_expr (
8282
add_error (std::move (error));
8383

8484
skip_after_end_block ();
85-
return Parse::Error::BlockExpr::make_malformed ();
85+
return tl::unexpected (Parse::Error::Node::MALFORMED);
8686
}
8787

8888
// grammar allows for empty block expressions
@@ -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, Parse::Error::AnonConst>
101+
tl::expected<AST::AnonConst, Parse::Error::Node>
102102
Parser<ManagedTokenSource>::parse_anon_const ()
103103
{
104104
auto current = lexer.peek_token ();
@@ -111,15 +111,15 @@ Parser<ManagedTokenSource>::parse_anon_const ()
111111
auto expr = parse_expr ();
112112

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

116116
return AST::AnonConst (std::move (expr), locus);
117117
}
118118

119119
/* Parse a "const block", a block preceded by the `const` keyword whose
120120
* statements can be const evaluated and used in constant contexts */
121121
template <typename ManagedTokenSource>
122-
std::unique_ptr<AST::ConstBlock>
122+
tl::expected<std::unique_ptr<AST::ConstBlock>, Parse::Error::Node>
123123
Parser<ManagedTokenSource>::parse_const_block_expr (AST::AttrVec outer_attrs,
124124
location_t locus)
125125
{
@@ -130,7 +130,7 @@ Parser<ManagedTokenSource>::parse_const_block_expr (AST::AttrVec outer_attrs,
130130
add_error (Error (locus, "failed to parse inner block in const block"));
131131
skip_after_end_block ();
132132

133-
return nullptr;
133+
return tl::unexpected (Parse::Error::Node{});
134134
}
135135
auto block = std::move (block_res.value ());
136136

@@ -144,7 +144,7 @@ Parser<ManagedTokenSource>::parse_const_block_expr (AST::AttrVec outer_attrs,
144144
/* Parses a "grouped" expression (expression in parentheses), used to control
145145
* precedence. */
146146
template <typename ManagedTokenSource>
147-
std::unique_ptr<AST::GroupedExpr>
147+
tl::expected<std::unique_ptr<AST::GroupedExpr>, Parse::Error::Node>
148148
Parser<ManagedTokenSource>::parse_grouped_expr (AST::AttrVec outer_attrs)
149149
{
150150
location_t locus = lexer.peek_token ()->get_locus ();
@@ -154,17 +154,17 @@ Parser<ManagedTokenSource>::parse_grouped_expr (AST::AttrVec outer_attrs)
154154

155155
// parse required expr inside parentheses
156156
std::unique_ptr<AST::Expr> expr_in_parens = parse_expr ();
157-
if (expr_in_parens == nullptr)
157+
if (!expr_in_parens)
158158
{
159159
// skip after somewhere?
160160
// error?
161-
return nullptr;
161+
return tl::unexpected (Parse::Error::Node::CHILD_ERROR);
162162
}
163163

164164
if (!skip_token (RIGHT_PAREN))
165165
{
166166
// skip after somewhere?
167-
return nullptr;
167+
return tl::unexpected (Parse::Error::Node::MALFORMED);
168168
}
169169

170170
return std::unique_ptr<AST::GroupedExpr> (
@@ -174,7 +174,7 @@ Parser<ManagedTokenSource>::parse_grouped_expr (AST::AttrVec outer_attrs)
174174

175175
// Parses a closure expression (closure definition).
176176
template <typename ManagedTokenSource>
177-
std::unique_ptr<AST::ClosureExpr>
177+
tl::expected<std::unique_ptr<AST::ClosureExpr>, Parse::Error::Node>
178178
Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
179179
{
180180
location_t locus = lexer.peek_token ()->get_locus ();
@@ -234,7 +234,7 @@ Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
234234
t->get_token_description ()));
235235

236236
// skip somewhere?
237-
return nullptr;
237+
return tl::unexpected (Parse::Error::Node::MALFORMED);
238238
}
239239

240240
// again branch based on next token
@@ -255,7 +255,7 @@ Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
255255
add_error (std::move (error));
256256

257257
// skip somewhere?
258-
return nullptr;
258+
return tl::unexpected (Parse::Error::Node::CHILD_ERROR);
259259
}
260260

261261
// parse block expr, which is required
@@ -268,7 +268,7 @@ Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
268268
add_error (std::move (error));
269269

270270
// skip somewhere?
271-
return nullptr;
271+
return tl::unexpected (Parse::Error::Node::CHILD_ERROR);
272272
}
273273

274274
return std::unique_ptr<AST::ClosureExprInnerTyped> (
@@ -290,7 +290,7 @@ Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
290290
add_error (std::move (error));
291291

292292
// skip somewhere?
293-
return nullptr;
293+
return tl::unexpected (Parse::Error::Node::CHILD_ERROR);
294294
}
295295

296296
return std::unique_ptr<AST::ClosureExprInner> (
@@ -301,7 +301,7 @@ Parser<ManagedTokenSource>::parse_closure_expr (AST::AttrVec outer_attrs)
301301

302302
// Parses a literal token (to literal expression).
303303
template <typename ManagedTokenSource>
304-
std::unique_ptr<AST::LiteralExpr>
304+
tl::expected<std::unique_ptr<AST::LiteralExpr>, Parse::Error::Node>
305305
Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs)
306306
{
307307
// TODO: change if literal representation in lexer changes
@@ -367,7 +367,7 @@ Parser<ManagedTokenSource>::parse_literal_expr (AST::AttrVec outer_attrs)
367367
t->get_token_description ()));
368368

369369
// skip?
370-
return nullptr;
370+
return tl::unexpected (Parse::Error::Node::MALFORMED);
371371
}
372372

373373
// create literal based on stuff in switch
@@ -2268,8 +2268,14 @@ Parser<ManagedTokenSource>::null_denotation_not_path (
22682268
tok->get_token_description ()));
22692269
return nullptr;
22702270
case CONST:
2271-
return parse_const_block_expr (std::move (outer_attrs),
2272-
tok->get_locus ());
2271+
{
2272+
auto const_block
2273+
= parse_const_block_expr (std::move (outer_attrs), tok->get_locus ());
2274+
if (const_block)
2275+
return std::move (const_block.value ());
2276+
else
2277+
return nullptr;
2278+
}
22732279
default:
22742280
if (!restrictions.expr_can_be_null)
22752281
add_error (Error (tok->get_locus (),

0 commit comments

Comments
 (0)