Skip to content

Commit d17e0aa

Browse files
bors[bot]antego
andauthored
Merge #1116
1116: Move `cfg!()` macro to builtins. Fixes #1039 r=CohenArthur a=antego Fixes #1039 Hey team, I need help understanding why the test fails. Compilation succeeds, all the existing tests pass. However the test that I've added fails with the error: ``` FAIL: rust/compile/cfg_macro.rs (test for excess errors) Excess errors: /Users/anton/Documents/projects/gcc2/gccrs/gcc/testsuite/rust/compile/cfg_macro.rs:17:8: fatal error: Failed to lower expr: [MacroInvocation: outer attributes: none cfg!((A)) has semicolon: false] compilation terminated. ``` I tried to understand what's happening using a debugger. The only thing that I understood is that the `MacroBuiltin::cfg` function runs. Appreciate any feedback. Thank you. Co-authored-by: antego <antego@users.noreply.github.com>
2 parents d36a3c5 + a9c0649 commit d17e0aa

File tree

5 files changed

+40
-49
lines changed

5 files changed

+40
-49
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,4 +376,40 @@ MacroBuiltin::env (Location invoc_locus, AST::MacroInvocData &invoc)
376376
return AST::ASTFragment ({node});
377377
}
378378

379+
AST::ASTFragment
380+
MacroBuiltin::cfg (Location invoc_locus, AST::MacroInvocData &invoc)
381+
{
382+
// only parse if not already parsed
383+
if (!invoc.is_parsed ())
384+
{
385+
std::unique_ptr<AST::AttrInputMetaItemContainer> converted_input (
386+
invoc.get_delim_tok_tree ().parse_to_meta_item ());
387+
388+
if (converted_input == nullptr)
389+
{
390+
rust_debug ("DEBUG: failed to parse macro to meta item");
391+
// TODO: do something now? is this an actual error?
392+
}
393+
else
394+
{
395+
std::vector<std::unique_ptr<AST::MetaItemInner>> meta_items (
396+
std::move (converted_input->get_items ()));
397+
invoc.set_meta_item_output (std::move (meta_items));
398+
}
399+
}
400+
401+
/* TODO: assuming that cfg! macros can only have one meta item inner, like cfg
402+
* attributes */
403+
if (invoc.get_meta_items ().size () != 1)
404+
return AST::ASTFragment::create_error ();
405+
406+
bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (
407+
Session::get_instance ());
408+
auto literal_exp = AST::SingleASTNode (std::unique_ptr<AST::Expr> (
409+
new AST::LiteralExpr (result ? "true" : "false", AST::Literal::BOOL,
410+
PrimitiveCoreType::CORETYPE_BOOL, {}, invoc_locus)));
411+
412+
return AST::ASTFragment ({literal_exp});
413+
}
414+
379415
} // namespace Rust

gcc/rust/expand/rust-macro-builtins.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class MacroBuiltin
9292

9393
static AST::ASTFragment env (Location invoc_locus,
9494
AST::MacroInvocData &invoc);
95+
96+
static AST::ASTFragment cfg (Location invoc_locus,
97+
AST::MacroInvocData &invoc);
9598
};
9699
} // namespace Rust
97100

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

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -25,50 +25,6 @@
2525
#include "rust-attribute-visitor.h"
2626

2727
namespace Rust {
28-
void
29-
MacroExpander::parse_macro_to_meta_item (AST::MacroInvocData &invoc)
30-
{
31-
// only parse if not already parsed
32-
if (invoc.is_parsed ())
33-
return;
34-
35-
std::unique_ptr<AST::AttrInputMetaItemContainer> converted_input (
36-
invoc.get_delim_tok_tree ().parse_to_meta_item ());
37-
38-
if (converted_input == nullptr)
39-
{
40-
rust_debug ("DEBUG: failed to parse macro to meta item");
41-
// TODO: do something now? is this an actual error?
42-
}
43-
else
44-
{
45-
std::vector<std::unique_ptr<AST::MetaItemInner>> meta_items (
46-
std::move (converted_input->get_items ()));
47-
invoc.set_meta_item_output (std::move (meta_items));
48-
}
49-
}
50-
51-
AST::Literal
52-
MacroExpander::expand_cfg_macro (AST::MacroInvocData &invoc)
53-
{
54-
// only allow on cfg macros
55-
if (invoc.get_path () != "cfg")
56-
return AST::Literal::create_error ();
57-
58-
parse_macro_to_meta_item (invoc);
59-
60-
/* TODO: assuming that cfg! macros can only have one meta item inner, like cfg
61-
* attributes */
62-
if (invoc.get_meta_items ().size () != 1)
63-
return AST::Literal::create_error ();
64-
65-
bool result = invoc.get_meta_items ()[0]->check_cfg_predicate (session);
66-
if (result)
67-
return AST::Literal ("true", AST::Literal::BOOL, CORETYPE_BOOL);
68-
else
69-
return AST::Literal ("false", AST::Literal::BOOL, CORETYPE_BOOL);
70-
}
71-
7228
AST::ASTFragment
7329
MacroExpander::expand_decl_macro (Location invoc_locus,
7430
AST::MacroInvocData &invoc,

gcc/rust/expand/rust-macro-expand.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -226,11 +226,6 @@ struct MacroExpander
226226
bool fails_cfg (const AST::AttrVec &attr) const;
227227
bool fails_cfg_with_expand (AST::AttrVec &attrs) const;
228228

229-
// Expand the data of a cfg! macro.
230-
void parse_macro_to_meta_item (AST::MacroInvocData &invoc);
231-
// Get the literal representation of a cfg! macro.
232-
AST::Literal expand_cfg_macro (AST::MacroInvocData &invoc);
233-
234229
bool depth_exceeds_recursion_limit () const;
235230

236231
bool try_match_rule (AST::MacroRule &match_rule,

gcc/rust/util/rust-hir-map.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,7 @@ Mappings::insert_macro_def (AST::MacroRulesDefinition *macro)
756756
{"compile_error", MacroBuiltin::compile_error},
757757
{"concat", MacroBuiltin::concat},
758758
{"env", MacroBuiltin::env},
759+
{"cfg", MacroBuiltin::cfg},
759760
};
760761

761762
auto builtin = builtin_macros.find (macro->get_rule_name ());

0 commit comments

Comments
 (0)