Skip to content

Commit

Permalink
hir-dump: Fix more segfaults in the HIR dump
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* hir/rust-hir-dump.cc: Check unique_ptr members are present before
	visiting them.
	* hir/tree/rust-hir-path.h: Add `has_{type, trait}` methods to
	QualifiedPathInType.
  • Loading branch information
CohenArthur committed Dec 21, 2024
1 parent b5c354d commit e27ac1f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
28 changes: 24 additions & 4 deletions gcc/rust/hir/rust-hir-dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ Dump::do_functionparam (FunctionParam &e)
void
Dump::do_pathpattern (PathPattern &e)
{
if (e.get_path_kind () == PathPattern::Kind::LangItem)
{
put_field ("segments", "#[lang = \""
+ LangItem::ToString (e.get_lang_item_kind ())
+ "\"]");
return;
}

std::string str = "";

for (const auto &segment : e.get_segments ())
Expand Down Expand Up @@ -402,9 +410,15 @@ void
Dump::do_qualifiedpathtype (QualifiedPathType &e)
{
do_mappings (e.get_mappings ());
visit_field ("type", e.get_type ());
if (e.has_type ())
visit_field ("type", e.get_type ());
else
put_field ("type", "none");

visit_field ("trait", e.get_trait ());
if (e.has_trait ())
visit_field ("trait", e.get_trait ());
else
put_field ("trait", "none");
}

void
Expand Down Expand Up @@ -521,7 +535,10 @@ Dump::do_traitfunctiondecl (TraitFunctionDecl &e)
else
put_field ("function_params", "empty");

visit_field ("return_type", e.get_return_type ());
if (e.has_return_type ())
visit_field ("return_type", e.get_return_type ());
else
put_field ("return_type", "none");

if (e.has_where_clause ())
put_field ("where_clause", e.get_where_clause ().as_string ());
Expand Down Expand Up @@ -1295,7 +1312,10 @@ Dump::visit (BreakExpr &e)
else
put_field ("label", "none");

visit_field ("break_expr ", e.get_expr ());
if (e.has_break_expr ())
visit_field ("break_expr ", e.get_expr ());
else
put_field ("break_expr", "none");

end ("BreakExpr");
}
Expand Down
9 changes: 8 additions & 1 deletion gcc/rust/hir/tree/rust-hir-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -641,13 +641,20 @@ class QualifiedPathType

Analysis::NodeMapping get_mappings () const { return mappings; }

bool has_type () { return type != nullptr; }
bool has_trait () { return trait != nullptr; }

Type &get_type ()
{
rust_assert (type);
return *type;
}

TypePath &get_trait () { return *trait; }
TypePath &get_trait ()
{
rust_assert (trait);
return *trait;
}

bool trait_has_generic_args () const;

Expand Down

0 comments on commit e27ac1f

Please sign in to comment.