Skip to content

Commit 1ada076

Browse files
Merge #1189
1189: Add missing `SimplePath`s location r=CohenArthur a=CohenArthur Sorry to new contributors but I think I've taken all the good first issues we opened yesterday... Closes #1179 Closes #1180 Closes #1181 Closes #1182 Necessary for #1172 Addresses #1159 Co-authored-by: Arthur Cohen <[email protected]>
2 parents af48e2a + 471cff2 commit 1ada076

File tree

5 files changed

+53
-42
lines changed

5 files changed

+53
-42
lines changed

gcc/rust/ast/rust-ast-full-test.cc

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4118,12 +4118,14 @@ AttributeParser::parse_meta_item_inner ()
41184118
return parse_path_meta_item ();
41194119
}
41204120

4121-
Identifier ident = peek_token ()->as_string ();
4121+
auto ident = peek_token ()->as_string ();
4122+
auto ident_locus = peek_token ()->get_locus ();
4123+
41224124
if (is_end_meta_item_tok (peek_token (1)->get_id ()))
41234125
{
41244126
// meta word syntax
41254127
skip_token ();
4126-
return std::unique_ptr<MetaWord> (new MetaWord (std::move (ident)));
4128+
return std::unique_ptr<MetaWord> (new MetaWord (ident, ident_locus));
41274129
}
41284130

41294131
if (peek_token (1)->get_id () == EQUAL)
@@ -4133,15 +4135,18 @@ AttributeParser::parse_meta_item_inner ()
41334135
&& is_end_meta_item_tok (peek_token (3)->get_id ()))
41344136
{
41354137
// meta name value str syntax
4136-
std::string value = peek_token (2)->as_string ();
4138+
auto &value_tok = peek_token (2);
4139+
auto value = value_tok->as_string ();
4140+
auto locus = value_tok->get_locus ();
41374141

41384142
skip_token (2);
41394143

41404144
// remove the quotes from the string value
41414145
std::string raw_value = unquote_string (std::move (value));
41424146

41434147
return std::unique_ptr<MetaNameValueStr> (
4144-
new MetaNameValueStr (std::move (ident), std::move (raw_value)));
4148+
new MetaNameValueStr (ident, ident_locus, std::move (raw_value),
4149+
locus));
41454150
}
41464151
else
41474152
{
@@ -4183,7 +4188,7 @@ AttributeParser::parse_meta_item_inner ()
41834188
if (!meta_name_value_str_items.empty ())
41844189
{
41854190
return std::unique_ptr<MetaListNameValueStr> (
4186-
new MetaListNameValueStr (std::move (ident),
4191+
new MetaListNameValueStr (ident, ident_locus,
41874192
std::move (meta_name_value_str_items)));
41884193
}
41894194

@@ -4222,7 +4227,7 @@ AttributeParser::parse_meta_item_inner ()
42224227
if (!path_items.empty ())
42234228
{
42244229
return std::unique_ptr<MetaListPaths> (
4225-
new MetaListPaths (std::move (ident), std::move (path_items)));
4230+
new MetaListPaths (ident, ident_locus, std::move (path_items)));
42264231
}
42274232

42284233
rust_error_at (Linemap::unknown_location (),
@@ -4694,11 +4699,11 @@ Attribute
46944699
MetaNameValueStr::to_attribute () const
46954700
{
46964701
LiteralExpr lit_expr (str, Literal::LitType::STRING,
4697-
PrimitiveCoreType::CORETYPE_UNKNOWN, {}, Location ());
4702+
PrimitiveCoreType::CORETYPE_UNKNOWN, {}, str_locus);
46984703
// FIXME: What location do we put here? Is the literal above supposed to have
46994704
// an empty location as well?
47004705
// Should MetaNameValueStr keep a location?
4701-
return Attribute (SimplePath::from_str (ident, Location ()),
4706+
return Attribute (SimplePath::from_str (ident, ident_locus),
47024707
std::unique_ptr<AttrInputLiteral> (
47034708
new AttrInputLiteral (std::move (lit_expr))));
47044709
}
@@ -4725,8 +4730,7 @@ MetaItemSeq::to_attribute () const
47254730
Attribute
47264731
MetaWord::to_attribute () const
47274732
{
4728-
// FIXME: How do we get a location here?
4729-
return Attribute (SimplePath::from_str (ident, Location ()), nullptr);
4733+
return Attribute (SimplePath::from_str (ident, ident_locus), nullptr);
47304734
}
47314735

47324736
Attribute
@@ -4744,8 +4748,7 @@ MetaListPaths::to_attribute () const
47444748

47454749
std::unique_ptr<AttrInputMetaItemContainer> new_seq_container (
47464750
new AttrInputMetaItemContainer (std::move (new_seq)));
4747-
// FIXME: How do we get a location here?
4748-
return Attribute (SimplePath::from_str (ident, Location ()),
4751+
return Attribute (SimplePath::from_str (ident, ident_locus),
47494752
std::move (new_seq_container));
47504753
}
47514754

@@ -4760,8 +4763,7 @@ MetaListNameValueStr::to_attribute () const
47604763

47614764
std::unique_ptr<AttrInputMetaItemContainer> new_seq_container (
47624765
new AttrInputMetaItemContainer (std::move (new_seq)));
4763-
// FIXME: How do we get a location here?
4764-
return Attribute (SimplePath::from_str (ident, Location ()),
4766+
return Attribute (SimplePath::from_str (ident, ident_locus),
47654767
std::move (new_seq_container));
47664768
}
47674769

gcc/rust/ast/rust-item.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,23 +666,25 @@ struct Visibility
666666
return Visibility (PUB, SimplePath::create_empty ());
667667
}
668668

669-
// Creates a public visibility with crate-relative paths or whatever.
670-
static Visibility create_crate ()
669+
// Creates a public visibility with crate-relative paths
670+
static Visibility create_crate (Location crate_tok_location)
671671
{
672-
return Visibility (PUB_CRATE, SimplePath::create_empty ());
672+
return Visibility (PUB_CRATE,
673+
SimplePath::from_str ("crate", crate_tok_location));
673674
}
674675

675-
// Creates a public visibility with self-relative paths or whatever.
676-
static Visibility create_self ()
676+
// Creates a public visibility with self-relative paths
677+
static Visibility create_self (Location self_tok_location)
677678
{
678-
return Visibility (PUB_SELF, SimplePath::create_empty ());
679+
return Visibility (PUB_SELF,
680+
SimplePath::from_str ("self", self_tok_location));
679681
}
680682

681-
// Creates a public visibility with parent module-relative paths or
682-
// whatever.
683-
static Visibility create_super ()
683+
// Creates a public visibility with parent module-relative paths
684+
static Visibility create_super (Location super_tok_location)
684685
{
685-
return Visibility (PUB_SUPER, SimplePath::create_empty ());
686+
return Visibility (PUB_SUPER,
687+
SimplePath::from_str ("super", super_tok_location));
686688
}
687689

688690
// Creates a private visibility

gcc/rust/ast/rust-macro.h

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,12 @@ class MetaItemSeq : public MetaItem
759759
class MetaWord : public MetaItem
760760
{
761761
Identifier ident;
762+
Location ident_locus;
762763

763764
public:
764-
MetaWord (Identifier ident) : ident (std::move (ident)) {}
765+
MetaWord (Identifier ident, Location ident_locus)
766+
: ident (std::move (ident)), ident_locus (ident_locus)
767+
{}
765768

766769
std::string as_string () const override { return ident; }
767770

@@ -783,12 +786,17 @@ class MetaWord : public MetaItem
783786
class MetaNameValueStr : public MetaItem
784787
{
785788
Identifier ident;
789+
Location ident_locus;
790+
786791
// NOTE: str stored without quotes
787792
std::string str;
793+
Location str_locus;
788794

789795
public:
790-
MetaNameValueStr (Identifier ident, std::string str)
791-
: ident (std::move (ident)), str (std::move (str))
796+
MetaNameValueStr (Identifier ident, Location ident_locus, std::string str,
797+
Location str_locus)
798+
: ident (std::move (ident)), ident_locus (ident_locus),
799+
str (std::move (str)), str_locus (str_locus)
792800
{}
793801

794802
std::string as_string () const override
@@ -821,11 +829,14 @@ class MetaNameValueStr : public MetaItem
821829
class MetaListPaths : public MetaItem
822830
{
823831
Identifier ident;
832+
Location ident_locus;
824833
std::vector<SimplePath> paths;
825834

826835
public:
827-
MetaListPaths (Identifier ident, std::vector<SimplePath> paths)
828-
: ident (std::move (ident)), paths (std::move (paths))
836+
MetaListPaths (Identifier ident, Location ident_locus,
837+
std::vector<SimplePath> paths)
838+
: ident (std::move (ident)), ident_locus (ident_locus),
839+
paths (std::move (paths))
829840
{}
830841

831842
std::string as_string () const override;
@@ -852,13 +863,14 @@ class MetaListPaths : public MetaItem
852863
class MetaListNameValueStr : public MetaItem
853864
{
854865
Identifier ident;
866+
Location ident_locus;
855867
std::vector<MetaNameValueStr> strs;
856868

857-
// FIXME add location info
858-
859869
public:
860-
MetaListNameValueStr (Identifier ident, std::vector<MetaNameValueStr> strs)
861-
: ident (std::move (ident)), strs (std::move (strs))
870+
MetaListNameValueStr (Identifier ident, Location ident_locus,
871+
std::vector<MetaNameValueStr> strs)
872+
: ident (std::move (ident)), ident_locus (ident_locus),
873+
strs (std::move (strs))
862874
{}
863875

864876
std::string as_string () const override;

gcc/rust/hir/rust-ast-lower.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,8 @@ translate_visibility (const AST::Visibility &vis)
4343
case AST::Visibility::PRIV:
4444
case AST::Visibility::PUB_SELF:
4545
return Visibility (Visibility::VisType::PRIVATE);
46-
// Desugar pub(crate) into pub(in crate) and so on
47-
// FIXME: How do we get a location for the SimplePath here?
4846
case AST::Visibility::PUB_CRATE:
49-
return Visibility (Visibility::PUBLIC,
50-
AST::SimplePath::from_str ("crate", Location ()));
5147
case AST::Visibility::PUB_SUPER:
52-
return Visibility (Visibility::PUBLIC,
53-
AST::SimplePath::from_str ("super", Location ()));
5448
case AST::Visibility::PUB_IN_PATH:
5549
return Visibility (Visibility::VisType::PUBLIC, vis.get_path ());
5650
break;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,6 +2136,7 @@ Parser<ManagedTokenSource>::parse_visibility ()
21362136
lexer.skip_token ();
21372137

21382138
const_TokenPtr t = lexer.peek_token ();
2139+
auto path_loc = t->get_locus ();
21392140

21402141
switch (t->get_id ())
21412142
{
@@ -2144,19 +2145,19 @@ Parser<ManagedTokenSource>::parse_visibility ()
21442145

21452146
skip_token (RIGHT_PAREN);
21462147

2147-
return AST::Visibility::create_crate ();
2148+
return AST::Visibility::create_crate (path_loc);
21482149
case SELF:
21492150
lexer.skip_token ();
21502151

21512152
skip_token (RIGHT_PAREN);
21522153

2153-
return AST::Visibility::create_self ();
2154+
return AST::Visibility::create_self (path_loc);
21542155
case SUPER:
21552156
lexer.skip_token ();
21562157

21572158
skip_token (RIGHT_PAREN);
21582159

2159-
return AST::Visibility::create_super ();
2160+
return AST::Visibility::create_super (path_loc);
21602161
case IN: {
21612162
lexer.skip_token ();
21622163

0 commit comments

Comments
 (0)