PR c++/89831 - error with qualified-id in const member function.
authorJason Merrill <jason@redhat.com>
Wed, 27 Mar 2019 20:39:19 +0000 (16:39 -0400)
committerJason Merrill <jason@gcc.gnu.org>
Wed, 27 Mar 2019 20:39:19 +0000 (16:39 -0400)
Since the fix for 15272 we were remembering the wrong function to use at
instantiation time, because the type of the SCOPE_REF didn't reflect the
cv-quals of 'this'.  Conveniently, we can fix this by simplifying the code.

* semantics.c (finish_non_static_data_member): Use object cv-quals
in scoped case, too.

From-SVN: r269977

gcc/cp/ChangeLog
gcc/cp/semantics.c
gcc/testsuite/g++.dg/template/scope6.C [new file with mode: 0644]

index bdd5211..9803ef9 100644 (file)
@@ -1,5 +1,9 @@
 2019-03-27  Jason Merrill  <jason@redhat.com>
 
+       PR c++/89831 - error with qualified-id in const member function.
+       * semantics.c (finish_non_static_data_member): Use object cv-quals
+       in scoped case, too.
+
        PR c++/89421 - ICE with lambda in template parameter list.
        * parser.c (cp_parser_lambda_expression): Also reject a lambda in a
        template parameter list before C++20.
index fea2696..a08a2a5 100644 (file)
@@ -1861,7 +1861,7 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
 
   if (current_class_ptr)
     TREE_USED (current_class_ptr) = 1;
-  if (processing_template_decl && !qualifying_scope)
+  if (processing_template_decl)
     {
       tree type = TREE_TYPE (decl);
 
@@ -1882,17 +1882,16 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope)
          type = cp_build_qualified_type (type, quals);
        }
 
-      ret = (convert_from_reference
-             (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
+      if (qualifying_scope)
+       /* Wrap this in a SCOPE_REF for now.  */
+       ret = build_qualified_name (type, qualifying_scope, decl,
+                                   /*template_p=*/false);
+      else
+       ret = (convert_from_reference
+              (build_min (COMPONENT_REF, type, object, decl, NULL_TREE)));
     }
   /* If PROCESSING_TEMPLATE_DECL is nonzero here, then
-     QUALIFYING_SCOPE is also non-null.  Wrap this in a SCOPE_REF
-     for now.  */
-  else if (processing_template_decl)
-    ret = build_qualified_name (TREE_TYPE (decl),
-                               qualifying_scope,
-                               decl,
-                               /*template_p=*/false);
+     QUALIFYING_SCOPE is also non-null.  */
   else
     {
       tree access_type = TREE_TYPE (object);
diff --git a/gcc/testsuite/g++.dg/template/scope6.C b/gcc/testsuite/g++.dg/template/scope6.C
new file mode 100644 (file)
index 0000000..6d28fb2
--- /dev/null
@@ -0,0 +1,17 @@
+// PR c++/89831
+
+struct Q { 
+    int operator[](int i) { return 0; }
+    int operator[](int i) const { return 0; }
+};
+
+struct Base {
+    Q x;
+};
+struct X : public Base {
+    template <typename T>
+    void f(T) const {
+        int q = Base::x[0];
+    }   
+};
+int main() { X().f(3); }