diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 6958388e454..25ffbd9eb5a 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -12650,7 +12650,9 @@ trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
 
       /* Similarly if EXISTING has an undeduced return type, but DECL's
 	 is already deduced.  */
-      if (undeduced_auto_decl (existing) && !undeduced_auto_decl (decl))
+      bool e_undeduced = undeduced_auto_decl (existing);
+      bool d_undeduced = undeduced_auto_decl (decl);
+      if (e_undeduced && !d_undeduced)
 	{
 	  dump (dumper::MERGE)
 	    && dump ("Propagating deduced return type to %N", existing);
@@ -12659,6 +12661,8 @@ trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
 	  DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
 	  TREE_TYPE (existing) = change_return_type (TREE_TYPE (d_type), e_type);
 	}
+      else if (d_undeduced && !e_undeduced)
+	/* EXISTING was deduced, leave it alone.  */;
       else if (type_uses_auto (d_ret)
 	       && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
 	{
diff --git a/gcc/testsuite/g++.dg/modules/auto-8_a.H b/gcc/testsuite/g++.dg/modules/auto-8_a.H
new file mode 100644
index 00000000000..a0a5cacd995
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/auto-8_a.H
@@ -0,0 +1,7 @@
+// PR c++/124735
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+struct S {
+  auto foo();
+};
diff --git a/gcc/testsuite/g++.dg/modules/auto-8_b.C b/gcc/testsuite/g++.dg/modules/auto-8_b.C
new file mode 100644
index 00000000000..bd749228a04
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/auto-8_b.C
@@ -0,0 +1,15 @@
+// PR c++/124735
+// { dg-additional-options "-fmodules" }
+
+struct S {
+  auto foo();
+};
+
+auto S::foo() { return 123; }
+auto pfn = &S::foo;
+
+import "auto-8_a.H";
+
+int main() {
+  return S{}.foo() != 123;
+}
