From c8246d961ff4f8807c28fe07862a3c99f32a0f21 Mon Sep 17 00:00:00 2001 From: Liam Naddell Date: Tue, 17 Dec 2024 11:48:03 -0500 Subject: [PATCH] Fix NR2.0 compiler ICE caused by Generics in Enums gcc/rust/ChangeLog: * resolve/rust-late-name-resolver-2.0.cc: Change the late name resolver to enter proper lexical scope during typechecking * resolve/rust-late-name-resolver-2.0.h: Add needed prototype to header * resolve/rust-toplevel-name-resolver-2.0.cc: Add generic parameters to enum's scoped RIB to allow for proper name resolution on types. gcc/testsuite/ChangeLog: * rust/compile/issue-3307.rs: Add small test for generics+enums combination for NR2.0 Signed-off-by: Liam Naddell --- gcc/rust/resolve/rust-late-name-resolver-2.0.cc | 7 +++++++ gcc/rust/resolve/rust-late-name-resolver-2.0.h | 1 + gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc | 9 +++++++++ gcc/testsuite/rust/compile/issue-3307.rs | 9 +++++++++ 4 files changed, 26 insertions(+) create mode 100644 gcc/testsuite/rust/compile/issue-3307.rs diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc index f2245c987397..605120386316 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.cc @@ -294,6 +294,13 @@ Late::visit (AST::StructStruct &s) ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis); } +void +Late::visit (AST::Enum &s) +{ + auto s_vis = [this, &s] () { AST::DefaultASTVisitor::visit (s); }; + ctx.scoped (Rib::Kind::Item, s.get_node_id (), s_vis); +} + void Late::visit (AST::StructExprStruct &s) { diff --git a/gcc/rust/resolve/rust-late-name-resolver-2.0.h b/gcc/rust/resolve/rust-late-name-resolver-2.0.h index 98cf09205bf4..7260f99507d1 100644 --- a/gcc/rust/resolve/rust-late-name-resolver-2.0.h +++ b/gcc/rust/resolve/rust-late-name-resolver-2.0.h @@ -52,6 +52,7 @@ class Late : public DefaultResolver void visit (AST::StructExprStructBase &) override; void visit (AST::StructExprStructFields &) override; void visit (AST::StructStruct &) override; + void visit (AST::Enum &) override; void visit (AST::GenericArgs &) override; void visit (AST::GenericArg &); diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc index 01c5d26f4c15..8fa4809d7158 100644 --- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc +++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc @@ -332,6 +332,15 @@ TopLevel::visit (AST::EnumItemDiscriminant &variant) void TopLevel::visit (AST::Enum &enum_item) { + auto generic_vis = [this, &enum_item] () { + for (auto &g : enum_item.get_generic_params ()) + { + g->accept_vis (*this); + } + }; + + ctx.scoped (Rib::Kind::Item, enum_item.get_node_id (), generic_vis); + insert_or_error_out (enum_item.get_identifier (), enum_item, Namespace::Types); diff --git a/gcc/testsuite/rust/compile/issue-3307.rs b/gcc/testsuite/rust/compile/issue-3307.rs new file mode 100644 index 000000000000..cc376faeb9dc --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-3307.rs @@ -0,0 +1,9 @@ +#[lang = "sized"] +trait Sized {} + +pub enum ROption { + RSome(T), + RNone, +} + +fn main() {}