From 18422c9c386ca49f8427cdd26a882749603d6da8 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 20 Sep 2024 17:38:14 +0100 Subject: [PATCH] rust: fix ICE during name resolution for impls on unit-types The canonical paths need to support unit-types which are technically a TupleType with no fields. This handles this case and adds an unreachable. Fixes #3036 gcc/rust/ChangeLog: * resolve/rust-ast-resolve-type.cc (ResolveTypeToCanonicalPath::visit): add unit-type catch * resolve/rust-ast-resolve-type.h: likewise gcc/testsuite/ChangeLog: * rust/compile/nr2/exclude: nr2 cant handle this * rust/compile/issue-3036.rs: New test. Signed-off-by: Philip Herron --- gcc/rust/resolve/rust-ast-resolve-type.cc | 9 +++++++++ gcc/rust/resolve/rust-ast-resolve-type.h | 2 ++ gcc/testsuite/rust/compile/issue-3036.rs | 14 ++++++++++++++ gcc/testsuite/rust/compile/nr2/exclude | 1 + 4 files changed, 26 insertions(+) create mode 100644 gcc/testsuite/rust/compile/issue-3036.rs diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc index e93515811ae4..15ae8db9389e 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.cc +++ b/gcc/rust/resolve/rust-ast-resolve-type.cc @@ -507,6 +507,15 @@ ResolveTypeToCanonicalPath::visit (AST::NeverType &type) result = CanonicalPath::new_seg (type.get_node_id (), "!"); } +void +ResolveTypeToCanonicalPath::visit (AST::TupleType &type) +{ + if (!type.is_unit_type ()) + rust_unreachable (); + + result = CanonicalPath::new_seg (type.get_node_id (), "()"); +} + ResolveTypeToCanonicalPath::ResolveTypeToCanonicalPath () : ResolverBase (), result (CanonicalPath::create_empty ()) {} diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h b/gcc/rust/resolve/rust-ast-resolve-type.h index 1df0f2605cd1..50dd890c4e31 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.h +++ b/gcc/rust/resolve/rust-ast-resolve-type.h @@ -248,6 +248,8 @@ class ResolveTypeToCanonicalPath : public ResolverBase void visit (AST::NeverType &type) override; + void visit (AST::TupleType &type) override; + private: ResolveTypeToCanonicalPath (); diff --git a/gcc/testsuite/rust/compile/issue-3036.rs b/gcc/testsuite/rust/compile/issue-3036.rs new file mode 100644 index 000000000000..4418ccc04cb9 --- /dev/null +++ b/gcc/testsuite/rust/compile/issue-3036.rs @@ -0,0 +1,14 @@ +#[lang = "sized"] +trait Sized {} + +#[stable(feature = "rust1", since = "1.0.0")] +pub trait Default: Sized { + #[stable(feature = "rust1", since = "1.0.0")] + fn default() -> Self; +} + +impl Default for () { + fn default() -> () { + () + } +} diff --git a/gcc/testsuite/rust/compile/nr2/exclude b/gcc/testsuite/rust/compile/nr2/exclude index bfb51fd7fee9..50781e56b85e 100644 --- a/gcc/testsuite/rust/compile/nr2/exclude +++ b/gcc/testsuite/rust/compile/nr2/exclude @@ -252,3 +252,4 @@ issue-3082.rs issue-3139-1.rs issue-3139-2.rs issue-3139-3.rs +issue-3036.rs