c++: Fix ICE with using and virtual function. [PR95719]
authorJason Merrill <jason@redhat.com>
Wed, 24 Jun 2020 01:25:21 +0000 (21:25 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 24 Jun 2020 19:59:20 +0000 (15:59 -0400)
conversion_path points to the base where we found the using-declaration, not
where the function is actually a member; look up the actual base.  And then
maybe look back to the derived class if the base is primary.

gcc/cp/ChangeLog:

PR c++/95719
* call.c (build_over_call): Look up the overrider in base_binfo.
* class.c (lookup_vfn_in_binfo): Look through BINFO_PRIMARY_P.

gcc/testsuite/ChangeLog:

PR c++/95719
* g++.dg/tree-ssa/final4.C: New test.

gcc/cp/call.c
gcc/cp/class.c
gcc/testsuite/g++.dg/tree-ssa/final4.C [new file with mode: 0644]

index 2b39a37..fe68fda 100644 (file)
@@ -8694,7 +8694,11 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
       if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
          && resolves_to_fixed_type_p (arg))
        {
-         fn = lookup_vfn_in_binfo (DECL_VINDEX (fn), cand->conversion_path);
+         tree binfo = cand->conversion_path;
+         if (BINFO_TYPE (binfo) != DECL_CONTEXT (fn))
+           binfo = lookup_base (binfo, DECL_CONTEXT (fn), ba_unique,
+                                NULL, complain);
+         fn = lookup_vfn_in_binfo (DECL_VINDEX (fn), binfo);
          flags |= LOOKUP_NONVIRTUAL;
        }
 
index 629d27d..94a9585 100644 (file)
@@ -2455,6 +2455,10 @@ lookup_vfn_in_binfo (tree idx, tree binfo)
   int ix = tree_to_shwi (idx);
   if (TARGET_VTABLE_USES_DESCRIPTORS)
     ix /= MAX (TARGET_VTABLE_USES_DESCRIPTORS, 1);
+  while (BINFO_PRIMARY_P (binfo))
+    /* BINFO_VIRTUALS in a primary base isn't accurate, find the derived
+       class that actually owns the vtable.  */
+    binfo = BINFO_INHERITANCE_CHAIN (binfo);
   tree virtuals = BINFO_VIRTUALS (binfo);
   return TREE_VALUE (chain_index (ix, virtuals));
 }
diff --git a/gcc/testsuite/g++.dg/tree-ssa/final4.C b/gcc/testsuite/g++.dg/tree-ssa/final4.C
new file mode 100644 (file)
index 0000000..387d4bb
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/95719
+// { dg-do compile { target c++11 } }
+// { dg-additional-options -fdump-tree-gimple }
+// { dg-final { scan-tree-dump "S2::f" "gimple" } }
+
+struct S1 { virtual ~S1(); };
+struct S2 {
+    virtual ~S2();
+    virtual void f();
+};
+struct S3 final: S1, S2 { using S2::f; };
+void g(S3 & s) { s.f(); }