]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Fix ICE on parsing trait object missing dyn keyword
authorPhilip Herron <herron.philip@googlemail.com>
Fri, 24 Mar 2023 22:17:36 +0000 (22:17 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:21:14 +0000 (18:21 +0100)
Trait objects are not required to use the 'dyn' keyword though it is
depreciated in later editions/version of Rustc. This patch handles the case
when we query the type for an HIR::Item which happens to be an HIR::Trait
and return a trait object or error.

Fixes #2037

gcc/rust/ChangeLog:

* typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit): return a TraitObject

gcc/testsuite/ChangeLog:

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

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

index 571e86c7369229b1b355a1abb4fb5811f216f30f..2116a9bdabc5273a96ab95d6d4f7c3c54e7cd8f3 100644 (file)
@@ -456,7 +456,17 @@ TypeCheckItem::visit (HIR::Module &module)
 void
 TypeCheckItem::visit (HIR::Trait &trait)
 {
-  TraitResolver::Resolve (trait);
+  TraitReference *trait_ref = TraitResolver::Resolve (trait);
+  if (trait_ref->is_error ())
+    {
+      infered = new TyTy::ErrorType (trait.get_mappings ().get_hirid ());
+      return;
+    }
+
+  RustIdent ident{CanonicalPath::create_empty (), trait.get_locus ()};
+  infered = new TyTy::DynamicObjectType (
+    trait.get_mappings ().get_hirid (), ident,
+    {TyTy::TypeBoundPredicate (*trait_ref, trait.get_locus ())});
 }
 
 void
diff --git a/gcc/testsuite/rust/compile/issue-2037.rs b/gcc/testsuite/rust/compile/issue-2037.rs
new file mode 100644 (file)
index 0000000..ec27a0d
--- /dev/null
@@ -0,0 +1,13 @@
+trait Foo {
+    fn bar(&mut self, other: &mut Foo);
+}
+
+struct Baz;
+
+impl Foo for Baz {
+    fn bar(&mut self, other: &Foo) {}
+    // { dg-error "expected" "" { target *-*-* } .-1 }
+    // { dg-error "method .bar. has an incompatible type for trait .Foo." "" { target *-*-* } .-2 }
+}
+
+fn main() {}