public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [pushed] c++: result location and explicit inst [PR108496]
@ 2023-01-23 19:09 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2023-01-23 19:09 UTC (permalink / raw)
  To: gcc-patches

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

-- 8< --

In r13-4469 we started to build the RESULT_DECL in grokdeclarator, while we
still know the location of the return type.  But in this testcase, we hit
that code again when parsing the explicit instantiation, and clobber the
DECL_RESULT that was previously used in parsing the function.  So, only set
DECL_RESULT if it isn't already set.

	PR c++/108496

gcc/cp/ChangeLog:

	* decl.cc (grokdeclarator): Check whether DECL_RESULT is already
	set.

gcc/testsuite/ChangeLog:

	* g++.dg/template/explicit-instantiation5.C: New test.
---
 gcc/cp/decl.cc                                    |  6 +++++-
 .../g++.dg/template/explicit-instantiation5.C     | 15 +++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/template/explicit-instantiation5.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 75ddf8016b5..d606b31d7a7 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -14776,7 +14776,9 @@ grokdeclarator (const cp_declarator *declarator,
       {
 	/* If we saw a return type, record its location.  */
 	location_t loc = declspecs->locations[ds_type_spec];
-	if (loc != UNKNOWN_LOCATION)
+	if (loc == UNKNOWN_LOCATION)
+	  /* Build DECL_RESULT in start_preparsed_function.  */;
+	else if (!DECL_RESULT (decl))
 	  {
 	    tree restype = TREE_TYPE (TREE_TYPE (decl));
 	    tree resdecl = build_decl (loc, RESULT_DECL, 0, restype);
@@ -14784,6 +14786,8 @@ grokdeclarator (const cp_declarator *declarator,
 	    DECL_IGNORED_P (resdecl) = 1;
 	    DECL_RESULT (decl) = resdecl;
 	  }
+	else if (funcdef_flag)
+	  DECL_SOURCE_LOCATION (DECL_RESULT (decl)) = loc;
       }
 
     /* Record constancy and volatility on the DECL itself .  There's
diff --git a/gcc/testsuite/g++.dg/template/explicit-instantiation5.C b/gcc/testsuite/g++.dg/template/explicit-instantiation5.C
new file mode 100644
index 00000000000..7bc007c8107
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/explicit-instantiation5.C
@@ -0,0 +1,15 @@
+// PR c++/108496
+
+struct S { long a, b, c; } s;
+
+template <int, typename>
+S foo (S);
+
+template <>
+S
+foo <0, long> (S)
+{
+  return s;
+}
+
+template S foo <0, long> (S);

base-commit: 208c6678c25bd9a11e6c5911a4c123cb6b7f3d6e
-- 
2.31.1


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

only message in thread, other threads:[~2023-01-23 19:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-23 19:09 [pushed] c++: result location and explicit inst [PR108496] Jason Merrill

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).