public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] gccrs: Support fully qualified calls to trait items from CallExpr's
@ 2023-04-06 21:38 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:38 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:92b137048583c2c22c585097119dba6a5fe06228

commit 92b137048583c2c22c585097119dba6a5fe06228
Author: Philip Herron <herron.philip@googlemail.com>
Date:   Tue Apr 4 15:40:26 2023 +0100

    gccrs: Support fully qualified calls to trait items from CallExpr's
    
    Fixes #2070
    
    gcc/rust/ChangeLog:
    
            * backend/rust-compile-resolve-path.cc (HIRCompileBase::query_compile):
            reuse resolve_method_address to monomorphize the call if required
            * typecheck/rust-hir-type-check-path.cc (TypeCheckExpr::resolve_segments):
            allow matching against unimplemented trait item bounds
    
    gcc/testsuite/ChangeLog:
    
            * rust/compile/issue-2070.rs: New test.
    
    Signed-off-by: Philip Herron <herron.philip@googlemail.com>

Diff:
---
 gcc/rust/backend/rust-compile-resolve-path.cc  | 16 ++++++++++++++++
 gcc/rust/typecheck/rust-hir-type-check-path.cc |  8 +++-----
 gcc/testsuite/rust/compile/issue-2070.rs       | 22 ++++++++++++++++++++++
 3 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc
index b7f1069a6f3..48141419051 100644
--- a/gcc/rust/backend/rust-compile-resolve-path.cc
+++ b/gcc/rust/backend/rust-compile-resolve-path.cc
@@ -186,6 +186,7 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
     = ctx->get_mappings ()->lookup_hir_extern_item (ref, &parent_block);
   bool is_hir_item = resolved_item != nullptr;
   bool is_hir_extern_item = resolved_extern_item != nullptr;
+  bool is_fn = lookup->get_kind () == TyTy::TypeKind::FNDEF;
   if (is_hir_item)
     {
       if (!lookup->has_subsititions_defined ())
@@ -206,6 +207,21 @@ HIRCompileBase::query_compile (HirId ref, TyTy::BaseType *lookup,
     }
   else
     {
+      if (is_fn)
+	{
+	  TyTy::FnType *fn = static_cast<TyTy::FnType *> (lookup);
+	  TyTy::BaseType *receiver = nullptr;
+
+	  if (fn->is_method ())
+	    {
+	      receiver = fn->get_self_type ();
+	      receiver = receiver->destructure ();
+
+	      return resolve_method_address (fn, ref, receiver, final_segment,
+					     mappings, expr_locus);
+	    }
+	}
+
       HirId parent_impl_id = UNKNOWN_HIRID;
       HIR::ImplItem *resolved_item
 	= ctx->get_mappings ()->lookup_hir_implitem (ref, &parent_impl_id);
diff --git a/gcc/rust/typecheck/rust-hir-type-check-path.cc b/gcc/rust/typecheck/rust-hir-type-check-path.cc
index 68ff995f108..230da61c3bd 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-path.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-path.cc
@@ -312,21 +312,19 @@ TypeCheckExpr::resolve_segments (NodeId root_resolved_node_id,
   for (size_t i = offset; i < segments.size (); i++)
     {
       HIR::PathExprSegment &seg = segments.at (i);
-
-      bool probe_bounds = true;
       bool probe_impls = !reciever_is_generic;
-      bool ignore_mandatory_trait_items = !reciever_is_generic;
 
       // probe the path is done in two parts one where we search impls if no
       // candidate is found then we search extensions from traits
       auto candidates
 	= PathProbeType::Probe (prev_segment, seg.get_segment (), probe_impls,
-				false, ignore_mandatory_trait_items);
+				false, true /*ignore_mandatory_trait_items*/);
       if (candidates.size () == 0)
 	{
 	  candidates
 	    = PathProbeType::Probe (prev_segment, seg.get_segment (), false,
-				    probe_bounds, ignore_mandatory_trait_items);
+				    true /*probe_bounds*/,
+				    false /*ignore_mandatory_trait_items*/);
 
 	  if (candidates.size () == 0)
 	    {
diff --git a/gcc/testsuite/rust/compile/issue-2070.rs b/gcc/testsuite/rust/compile/issue-2070.rs
new file mode 100644
index 00000000000..46f35209f4d
--- /dev/null
+++ b/gcc/testsuite/rust/compile/issue-2070.rs
@@ -0,0 +1,22 @@
+trait Foo {
+    fn get(self) -> i32;
+}
+
+struct Bar(i32);
+impl Foo for Bar {
+    fn get(self) -> i32 {
+        self.0
+    }
+}
+
+fn type_bound_test<T: Foo>(a: T) -> i32 {
+    Foo::get(a)
+}
+
+fn main() {
+    let a;
+    a = Bar(456);
+
+    let b;
+    b = type_bound_test(a);
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:38 [gcc/devel/rust/master] gccrs: Support fully qualified calls to trait items from CallExpr's Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).