Skip to content

Commit 6a6c217

Browse files
Merge #932
932: Add location information to MacroRule r=CohenArthur a=CohenArthur Closes #930 This PR adds location information to the `MacroRule` structure. The location is from the beginning of the invokation pattern, so that errors look like so: ```rust test.rs:2:5: error: ... 2 | ($a:expr, $b:expr) => { a + b } | ^ ``` Co-authored-by: Arthur Cohen <[email protected]>
2 parents d81ba63 + c1e72db commit 6a6c217

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

gcc/rust/ast/rust-macro.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define RUST_AST_MACRO_H
2121

2222
#include "rust-ast.h"
23+
#include "rust-location.h"
2324

2425
namespace Rust {
2526
namespace AST {
@@ -295,26 +296,28 @@ struct MacroRule
295296
private:
296297
MacroMatcher matcher;
297298
MacroTranscriber transcriber;
298-
299-
// TODO: should store location information?
299+
Location locus;
300300

301301
public:
302-
MacroRule (MacroMatcher matcher, MacroTranscriber transcriber)
303-
: matcher (std::move (matcher)), transcriber (std::move (transcriber))
302+
MacroRule (MacroMatcher matcher, MacroTranscriber transcriber, Location locus)
303+
: matcher (std::move (matcher)), transcriber (std::move (transcriber)),
304+
locus (locus)
304305
{}
305306

306307
// Returns whether macro rule is in error state.
307308
bool is_error () const { return matcher.is_error (); }
308309

309310
// Creates an error state macro rule.
310-
static MacroRule create_error ()
311+
static MacroRule create_error (Location locus)
311312
{
312-
// FIXME: Once #928 is merged, give location to MacroMatcher
313-
return MacroRule (MacroMatcher::create_error (Location ()),
313+
return MacroRule (MacroMatcher::create_error (locus),
314314
MacroTranscriber (DelimTokenTree::create_empty (),
315-
Location ()));
315+
Location ()),
316+
locus);
316317
}
317318

319+
Location get_locus () const { return locus; }
320+
318321
std::string as_string () const;
319322
};
320323

gcc/rust/parse/rust-parse-impl.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,23 +1678,25 @@ template <typename ManagedTokenSource>
16781678
AST::MacroRule
16791679
Parser<ManagedTokenSource>::parse_macro_rule ()
16801680
{
1681+
Location locus = lexer.peek_token ()->get_locus ();
1682+
16811683
// parse macro matcher
16821684
AST::MacroMatcher matcher = parse_macro_matcher ();
16831685

16841686
if (matcher.is_error ())
1685-
return AST::MacroRule::create_error ();
1687+
return AST::MacroRule::create_error (locus);
16861688

16871689
if (!skip_token (MATCH_ARROW))
16881690
{
16891691
// skip after somewhere?
1690-
return AST::MacroRule::create_error ();
1692+
return AST::MacroRule::create_error (locus);
16911693
}
16921694

16931695
// parse transcriber (this is just a delim token tree)
16941696
Location token_tree_loc = lexer.peek_token ()->get_locus ();
16951697
AST::MacroTranscriber transcriber (parse_delim_token_tree (), token_tree_loc);
16961698

1697-
return AST::MacroRule (std::move (matcher), std::move (transcriber));
1699+
return AST::MacroRule (std::move (matcher), std::move (transcriber), locus);
16981700
}
16991701

17001702
// Parses a macro matcher (part of a macro rule definition).

0 commit comments

Comments
 (0)