]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE during method resolution
authorPhilip Herron <herron.philip@googlemail.com>
Thu, 20 Apr 2023 11:44:31 +0000 (12:44 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:34:13 +0000 (18:34 +0100)
We were missing a check for trait item selection to ensure they are
actually methods and remove assertion to check if the trait item is a
function this is a valid error check not an assertion.

Fixes #2139

gcc/rust/ChangeLog:

* typecheck/rust-hir-dot-operator.cc (MethodResolver::select): verify it is a method

gcc/testsuite/ChangeLog:

* rust/compile/issue-2139.rs: New test.

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/typecheck/rust-hir-dot-operator.cc
gcc/testsuite/rust/compile/issue-2139.rs [new file with mode: 0644]

index d782da8960a67083d2c1a78507192a19c6a8f2d5..251cf8a02e5824cdcc90ca9b284f8aa7f4b5afee 100644 (file)
@@ -240,8 +240,12 @@ MethodResolver::select (TyTy::BaseType &receiver)
 
     const HIR::Trait *trait = trait_ref->get_hir_trait_ref ();
     HIR::TraitItem *item = item_ref->get_hir_trait_item ();
-    rust_assert (item->get_item_kind () == HIR::TraitItem::TraitItemKind::FUNC);
+    if (item->get_item_kind () != HIR::TraitItem::TraitItemKind::FUNC)
+      return true;
+
     HIR::TraitItemFunc *func = static_cast<HIR::TraitItemFunc *> (item);
+    if (!func->get_decl ().is_method ())
+      return true;
 
     TyTy::BaseType *ty = item_ref->get_tyty ();
     rust_assert (ty->get_kind () == TyTy::TypeKind::FNDEF);
diff --git a/gcc/testsuite/rust/compile/issue-2139.rs b/gcc/testsuite/rust/compile/issue-2139.rs
new file mode 100644 (file)
index 0000000..780ed58
--- /dev/null
@@ -0,0 +1,16 @@
+pub trait Foo {
+    fn foo();
+}
+
+impl Foo for u16 {
+    fn foo() {
+        <u16 as Foo>::foo()
+    }
+}
+
+fn main() {
+    let a: u16 = 123;
+    a.foo();
+    // { dg-error "failed to resolve method for .foo." "" { target *-*-* } .-1 }
+    // { dg-error "failed to type resolve expression" "" { target *-*-* } .-2 }
+}