Skip to content

Commit

Permalink
c++/modules: deduced return type merging [PR114795]
Browse files Browse the repository at this point in the history
When merging an imported function template specialization with an
existing one, if the existing one has an undeduced return type and the
imported one's is already deduced, we need to propagate the deduced type
since once we install the imported definition we won't get a chance to
deduce it by normal means.

So this patch makes is_matching_decl propagate the deduced return
type alongside our propagation of the exception specification.
Another option would be to propagate it later when installing the
imported definition from read_function_def, but it seems preferable
to do it sooner rather than later.

	PR c++/114795

gcc/cp/ChangeLog:

	* module.cc (trees_in::is_matching_decl): Propagate deduced
	function return type.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/auto-4_a.H: New test.
	* g++.dg/modules/auto-4_b.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
  • Loading branch information
Patrick Palka committed Apr 23, 2024
1 parent d2f05fe commit 4f9401d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
9 changes: 9 additions & 0 deletions gcc/cp/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11537,6 +11537,15 @@ trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
else if (!DEFERRED_NOEXCEPT_SPEC_P (d_spec)
&& !comp_except_specs (d_spec, e_spec, ce_type))
goto mismatch;

/* Similarly if EXISTING has an undeduced return type, but DECL's
is already deduced. */
if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
{
dump (dumper::MERGE)
&& dump ("Propagating deduced return type to %N", existing);
TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
}
}
else if (is_typedef)
{
Expand Down
14 changes: 14 additions & 0 deletions gcc/testsuite/g++.dg/modules/auto-4_a.H
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// PR c++/114795
// { dg-additional-options "-fmodule-header" }
// { dg-module-cmi {} }

template<class T>
struct A {
auto f() { return T(); }
};

template<class T>
void g() {
A<int> a;
a.f();
}
15 changes: 15 additions & 0 deletions gcc/testsuite/g++.dg/modules/auto-4_b.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// PR c++/114795
// { dg-additional-options "-fmodules-ts -fno-module-lazy" }

template<class T>
struct A {
auto f() { return T(); }
};

A<int> a;

import "auto-4_a.H";

int main() {
g<int>(); // { dg-bogus "before deduction of 'auto'" "" { target *-*-* } 0 }
}

0 comments on commit 4f9401d

Please sign in to comment.