Skip to content

Commit

Permalink
name resolution on Some fails
Browse files Browse the repository at this point in the history
  • Loading branch information
liamnaddell committed Jan 5, 2025
1 parent 6528955 commit c24d5fc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
43 changes: 34 additions & 9 deletions gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,16 +661,27 @@ class LangItemPath : public Path
LangItem::Kind kind;
NodeId node_id;
location_t locus;
//TODO: Comment
//TODO: Fix all constructors!!
std::vector<PathExprSegment> path_segment;

LangItemPath (LangItem::Kind kind, NodeId node_id, location_t locus)
: kind (kind), node_id (node_id), locus (locus)
{}
{
//TODO: 0,clean
PathExprSegment sgm(LangItem::ToString(kind), 0);
path_segment.push_back(sgm);
}

public:
explicit LangItemPath (LangItem::Kind kind, location_t locus)
: kind (kind), node_id (Analysis::Mappings::get ().get_next_node_id ()),
locus (locus)
{}
{
//TODO: 0,clean
PathExprSegment sgm(LangItem::ToString(kind), 0);
path_segment.push_back(sgm);
}

Path::Kind get_path_kind () const override { return Path::Kind::LangItem; }

Expand All @@ -687,6 +698,10 @@ class LangItemPath : public Path

NodeId get_node_id () const override { return node_id; }
location_t get_locus () const override { return locus; }
std::vector<PathExprSegment> &get_segments ()
{
return path_segment;
}
};

/* AST node representing a path-in-expression pattern (path that allows
Expand Down Expand Up @@ -763,6 +778,7 @@ class PathInExpression : public Pattern, public ExprWithoutBlock
// Returns whether path in expression is in an error state.
bool is_error () const
{
//TODO: Look at later!
if (path->get_path_kind () == Path::Kind::Regular)
return !static_cast<RegularPath &> (*path).has_segments ();

Expand All @@ -773,8 +789,10 @@ class PathInExpression : public Pattern, public ExprWithoutBlock
* arguments). Otherwise returns an empty SimplePath. */
SimplePath as_simple_path () const
{
//TODO: Do this fixup / cleanme
// FIXME: Cleanup
if (path->get_path_kind () == Path::Kind::Regular)
if (path->get_path_kind () == Path::Kind::Regular ||
path->get_path_kind() == Path::Kind::LangItem)
return static_cast<RegularPath &> (*path).convert_to_simple_path (
has_opening_scope_resolution);
else
Expand Down Expand Up @@ -808,16 +826,17 @@ class PathInExpression : public Pattern, public ExprWithoutBlock

PathExprSegment &get_final_segment ()
{
if (path->get_path_kind () == Path::Kind::Regular)
if (path->get_path_kind () == Path::Kind::Regular ||
path->get_path_kind() == Path::Kind::LangItem)
return static_cast<RegularPath &> (*path).get_segments ().back ();

// lang item segment?
rust_unreachable ();
}

const PathExprSegment &get_final_segment () const
{
if (path->get_path_kind () == Path::Kind::Regular)
if (path->get_path_kind () == Path::Kind::Regular ||
path->get_path_kind() == Path::Kind::LangItem)
return static_cast<RegularPath &> (*path).get_segments ().back ();

// lang item segment?
Expand All @@ -826,23 +845,29 @@ class PathInExpression : public Pattern, public ExprWithoutBlock

const std::vector<PathExprSegment> &get_segments () const
{
if (path->get_path_kind () == Path::Kind::Regular)
if (path->get_path_kind () == Path::Kind::Regular)
return static_cast<RegularPath &> (*path).get_segments ();

if (path->get_path_kind() == Path::Kind::LangItem)
return (static_cast<LangItemPath &> (*path)).get_segments ();

rust_unreachable ();
}

std::vector<PathExprSegment> &get_segments ()
{
if (path->get_path_kind () == Path::Kind::Regular)
if (path->get_path_kind () == Path::Kind::Regular)
return static_cast<RegularPath &> (*path).get_segments ();

if (path->get_path_kind() == Path::Kind::LangItem)
return (static_cast<LangItemPath &> (*path)).get_segments ();
rust_unreachable ();
}

bool is_single_segment () const
{
if (path->get_path_kind () == Path::Kind::Regular)
if (path->get_path_kind () == Path::Kind::Regular ||
path->get_path_kind() == Path::Kind::LangItem)
return static_cast<RegularPath &> (*path).get_segments ().size () == 1;

rust_unreachable ();
Expand Down
10 changes: 8 additions & 2 deletions gcc/rust/expand/rust-macro-builtins-utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,10 @@ MacroBuiltin::option_env_handler (location_t invoc_locus,

if (env_value == nullptr)
{
//TODO: Make_Unique
std::unique_ptr<AST::Expr> none_expr
= std::unique_ptr<AST::Expr> (new AST::PathInExpression (
b.path_in_expression ({"core", "option", "Option", "None"})));
= std::unique_ptr<AST::Expr> (new AST::PathInExpression
(LangItem::Kind::OPTION_NONE,{},invoc_locus));

auto node = AST::SingleASTNode (std::move (none_expr));
std::vector<AST::SingleASTNode> nodes;
Expand All @@ -290,10 +291,15 @@ MacroBuiltin::option_env_handler (location_t invoc_locus,
std::vector<std::unique_ptr<AST::Expr>> args;
args.push_back (b.literal_string (env_value));

/*
std::unique_ptr<AST::Expr> some_expr
= b.call (std::unique_ptr<AST::Expr> (new AST::PathInExpression (
b.path_in_expression ({"core", "option", "Option", "Some"}))),
std::move (args));
*/
std::unique_ptr<AST::Expr> some_expr
= b.call (std::unique_ptr<AST::Expr> (new AST::PathInExpression
(LangItem::Kind::OPTION_SOME,{},invoc_locus)),std::move(args));

auto node = AST::SingleASTNode (std::move (some_expr));

Expand Down
1 change: 1 addition & 0 deletions gcc/rust/resolve/rust-early-name-resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ EarlyNameResolver::visit (AST::ConstGenericParam &)
void
EarlyNameResolver::visit (AST::PathInExpression &path)
{

for (auto &segment : path.get_segments ())
if (segment.has_generic_args ())
resolve_generic_args (segment.get_generic_args ());
Expand Down

0 comments on commit c24d5fc

Please sign in to comment.