Patchwork C++ PATCH for c++/54359 ('this' in trailing-return-type with out-of-class method defn)

login
register
mail settings
Submitter Jason Merrill
Date March 17, 2013, 1:41 a.m.
Message ID <51451F3C.1050709@redhat.com>
Download mbox | patch
Permalink /patch/228261/
State New
Headers show

Comments

Jason Merrill - March 17, 2013, 1:41 a.m.
We were handling 'this' in a trailing-return-type properly for in-class 
declarations of member functions, but not for an out-of-class 
definition, where "member_p" isn't set.

Tested x86_64-pc-linux-gnu, applying to trunk.

Patch

commit 24c28e89e76e2deb3d3940b0461c322774afef26
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Mar 11 06:15:26 2013 -0400

    	PR c++/54359
    	* parser.c (cp_parser_direct_declarator): Fix late return
    	for out-of-class defn of member function.

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 5add0c4..b0df636 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -16366,6 +16366,8 @@  cp_parser_direct_declarator (cp_parser* parser,
 		  tree exception_specification;
 		  tree late_return;
 		  tree attrs;
+		  bool memfn = (member_p || (pushed_scope
+					     && CLASS_TYPE_P (pushed_scope)));
 
 		  is_declarator = true;
 
@@ -16382,7 +16384,7 @@  cp_parser_direct_declarator (cp_parser* parser,
 		  attrs = cp_parser_std_attribute_spec_seq (parser);
 
 		  late_return = (cp_parser_late_return_type_opt
-				 (parser, member_p ? cv_quals : -1));
+				 (parser, memfn ? cv_quals : -1));
 
 		  /* Parse the virt-specifier-seq.  */
 		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
diff --git a/gcc/testsuite/g++.dg/cpp0x/trailing8.C b/gcc/testsuite/g++.dg/cpp0x/trailing8.C
new file mode 100644
index 0000000..304845e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/trailing8.C
@@ -0,0 +1,25 @@ 
+// PR c++/54359
+// { dg-require-effective-target c++11 }
+
+int& ref(int& x) { return x; }
+const int& ref(const int& x) { return x; }
+
+class A {
+    int x;
+    int f() const;
+    auto test1() const -> decltype(this);
+    auto test2() const -> decltype(ref(x));
+    auto test3() const -> decltype(f());
+};
+
+auto A::test1() const -> decltype(this) {
+    return this;
+}
+
+auto A::test2() const -> decltype(ref(x)) {
+    return ref(x);
+}
+
+auto A::test3() const -> decltype(f()) {
+    return f();
+}