Skip to content

Fix some duplicate node id issues #3787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ GRS_OBJS = \
rust/rust-derive-partial-eq.o \
rust/rust-derive-eq.o \
rust/rust-derive-hash.o \
rust/rust-node-id-fix-visitor.o \
rust/rust-proc-macro.o \
rust/rust-macro-invoc-lexer.o \
rust/rust-proc-macro-invoc-lexer.o \
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,11 @@ class Type : public Visitable

NodeId get_node_id () const { return node_id; }

void reset_node_id ()
{
node_id = Analysis::Mappings::get ().get_next_node_id ();
}

protected:
Type () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}

Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/ast/rust-path.h
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,11 @@ class TypePathSegment

NodeId get_node_id () const { return node_id; }

void reset_node_id ()
{
node_id = Analysis::Mappings::get ().get_next_node_id ();
}

bool is_crate_path_seg () const
{
return get_ident_segment ().is_crate_path_seg ();
Expand Down
5 changes: 5 additions & 0 deletions gcc/rust/expand/rust-derive-default.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-derive-default.h"
#include "rust-node-id-fix-visitor.h"
#include "rust-ast.h"
#include "rust-diagnostics.h"
#include "rust-path.h"
Expand Down Expand Up @@ -100,6 +101,8 @@ DeriveDefault::visit_struct (StructStruct &item)
auto name = field.get_field_name ().as_string ();
auto expr = default_call (field.get_field_type ().clone_type ());

NodeIdFixVisitor::fix (expr);

cloned_fields.emplace_back (
builder.struct_expr_field (std::move (name), std::move (expr)));
}
Expand All @@ -121,6 +124,8 @@ DeriveDefault::visit_tuple (TupleStruct &tuple_item)
{
auto type = field.get_field_type ().clone_type ();

NodeIdFixVisitor::fix (type);

defaulted_fields.emplace_back (default_call (std::move (type)));
}

Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/expand/rust-derive-eq.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-derive-eq.h"
#include "rust-node-id-fix-visitor.h"
#include "rust-ast.h"
#include "rust-expr.h"
#include "rust-item.h"
Expand All @@ -34,6 +35,8 @@ DeriveEq::go (Item &item)
{
item.accept_vis (*this);

NodeIdFixVisitor::fix (expanded);

return std::move (expanded);
}

Expand Down
58 changes: 58 additions & 0 deletions gcc/rust/expand/rust-node-id-fix-visitor.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (C) 2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-node-id-fix-visitor.h"

namespace Rust {

void
NodeIdFixVisitor::visit (AST::TypePathSegment &seg)
{
DefaultASTVisitor::visit (seg);
seg.reset_node_id ();
}

void
NodeIdFixVisitor::visit (AST::TypePathSegmentGeneric &seg)
{
DefaultASTVisitor::visit (seg);
seg.reset_node_id ();
}

void
NodeIdFixVisitor::visit (AST::TypePathSegmentFunction &seg)
{
DefaultASTVisitor::visit (seg);
seg.reset_node_id ();
}

void
NodeIdFixVisitor::visit (AST::TypePath &path)
{
DefaultASTVisitor::visit (path);
path.reset_node_id ();
}

void
NodeIdFixVisitor::visit (AST::QualifiedPathInType &path)
{
DefaultASTVisitor::visit (path);
path.reset_node_id ();
}

} // namespace Rust
52 changes: 52 additions & 0 deletions gcc/rust/expand/rust-node-id-fix-visitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (C) 2025 Free Software Foundation, Inc.

// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.

// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.

// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-ast-visitor.h"

namespace Rust {

// This class reassigns node ids to AST nodes
// It's a bit of a hack to work around cloning copying node ids

class NodeIdFixVisitor : public AST::DefaultASTVisitor
{
public:
using DefaultASTVisitor::visit;

template <typename T> static void fix (T &node)
{
NodeIdFixVisitor instance;
instance.visit (node);
}

template <typename T> void visit (std::vector<T> &nodes)
{
for (auto &ent : nodes)
visit (ent);
}

// TODO: add more visitors

void visit (AST::TypePathSegment &) override;
void visit (AST::TypePathSegmentGeneric &) override;
void visit (AST::TypePathSegmentFunction &) override;
void visit (AST::TypePath &) override;
void visit (AST::QualifiedPathInType &) override;
};

} // namespace Rust
2 changes: 0 additions & 2 deletions gcc/testsuite/rust/compile/nr2/exclude
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub_restricted_1.rs
pub_restricted_2.rs
pub_restricted_3.rs
issue-2905-2.rs
derive-default1.rs
derive-eq-invalid.rs
torture/alt_patterns1.rs
torture/name_resolve1.rs
issue-3671.rs
Expand Down