From 77df05d0714b405eed6a7285abdc8bd90cf37ae1 Mon Sep 17 00:00:00 2001
From: Matthew Pickering <matthewtpickering@gmail.com>
Date: Mon, 10 Mar 2025 12:52:05 +0000
Subject: [PATCH] Take into account all flags when computing iface_hash

The "interface hash" should contain a hash of everything in the
interface file. We are not doing that yet but at least a start is to
include a hash of everything in `mi_self_recomp`, rather than just
`mi_src_hash` and `mi_usages`.

In particular, this fixes #25837, a bug where we should recompile a
`dyn_hi` file but fail to do so.
---
 compiler/GHC/Iface/Recomp.hs                     | 10 +++-------
 .../driver/dynamicToo/dynamicToo001/Makefile     | 16 ++++++++++++++++
 .../dynamicToo/dynamicToo001/T25837.stdout       |  1 +
 .../dynamicToo/dynamicToo001/T25837Module.hs     |  6 ++++++
 .../tests/driver/dynamicToo/dynamicToo001/test.T |  1 +
 5 files changed, 27 insertions(+), 7 deletions(-)
 create mode 100644 testsuite/tests/driver/dynamicToo/dynamicToo001/T25837.stdout
 create mode 100644 testsuite/tests/driver/dynamicToo/dynamicToo001/T25837Module.hs

diff --git a/compiler/GHC/Iface/Recomp.hs b/compiler/GHC/Iface/Recomp.hs
index e4a7a34b40bd..56704c34d0a4 100644
--- a/compiler/GHC/Iface/Recomp.hs
+++ b/compiler/GHC/Iface/Recomp.hs
@@ -1301,18 +1301,14 @@ addFingerprints hsc_env iface0
 
    -- The interface hash depends on:
    --   - the ABI hash, plus
-   --   - the source file hash,
+   --   - the things which can affect whether a module is recompiled
    --   - the module level annotations,
-   --   - usages
    --   - deps (home and external packages, dependent files)
-   --   - hpc
    iface_hash <- computeFingerprint putNameLiterally
                         (mod_hash,
-                         mi_src_hash iface0,
                          ann_fn (mkVarOccFS (fsLit "module")),  -- See mkIfaceAnnCache
-                         mi_usages iface0,
-                         sorted_deps,
-                         mi_hpc iface0)
+                         mi_self_recomp_info iface0,
+                         sorted_deps )
 
    let
     final_iface_exts = ModIfaceBackend
diff --git a/testsuite/tests/driver/dynamicToo/dynamicToo001/Makefile b/testsuite/tests/driver/dynamicToo/dynamicToo001/Makefile
index ee0d464440d9..2aa72e15cd07 100644
--- a/testsuite/tests/driver/dynamicToo/dynamicToo001/Makefile
+++ b/testsuite/tests/driver/dynamicToo/dynamicToo001/Makefile
@@ -48,4 +48,20 @@ dynamicToo001MakeB:
 	cp B1.hs B.hs
 	"$(TEST_HC)" $(TEST_HC_OPTS) -O  C.hs -dynamic-too
 
+.PHONY: T25837
+# Test that -dynamic-too notices when interface files were compiled with different flags
+T25837:
+	# First compile without -dynamic but with -O0
+	"$(TEST_HC)" $(TEST_HC_OPTS) -O0 -c T25837Module.hs
+
+	# Then compile with -dynamic but with -ticky
+	"$(TEST_HC)" $(TEST_HC_OPTS) $(TEST_HC_OPTS_DYN) -ticky -c T25837Module.hs
+
+	# Now try to compile with -dynamic-too, should detect the mismatch and recompile
+	"$(TEST_HC)" $(TEST_HC_OPTS) -v1 -dynamic-too --make T25837Module.hs
+
+	# Verify that both interfaces now match (optional check)
+	$(call checkExists,T25837Module.hi)
+	$(call checkExists,T25837Module.dyn_hi)
+
 
diff --git a/testsuite/tests/driver/dynamicToo/dynamicToo001/T25837.stdout b/testsuite/tests/driver/dynamicToo/dynamicToo001/T25837.stdout
new file mode 100644
index 000000000000..096ff581deb6
--- /dev/null
+++ b/testsuite/tests/driver/dynamicToo/dynamicToo001/T25837.stdout
@@ -0,0 +1 @@
+[1 of 1] Compiling T25837Module     ( T25837Module.hs, T25837Module.o, T25837Module.dyn_o ) [Mismatched dynamic interface file]
diff --git a/testsuite/tests/driver/dynamicToo/dynamicToo001/T25837Module.hs b/testsuite/tests/driver/dynamicToo/dynamicToo001/T25837Module.hs
new file mode 100644
index 000000000000..243b8f2194cf
--- /dev/null
+++ b/testsuite/tests/driver/dynamicToo/dynamicToo001/T25837Module.hs
@@ -0,0 +1,6 @@
+module T25837Module where
+
+data MyType = MyType Int
+
+myValue :: MyType
+myValue = MyType 42
\ No newline at end of file
diff --git a/testsuite/tests/driver/dynamicToo/dynamicToo001/test.T b/testsuite/tests/driver/dynamicToo/dynamicToo001/test.T
index 2c9636bc89da..58539df19ebf 100644
--- a/testsuite/tests/driver/dynamicToo/dynamicToo001/test.T
+++ b/testsuite/tests/driver/dynamicToo/dynamicToo001/test.T
@@ -5,3 +5,4 @@ opts = [ extra_files(['A.hs', 'B1.hs', 'B2.hs', 'C.hs']),
 test('dynamicToo001', [opts], makefile_test, [])
 test('dynamicToo001MakeA', [opts], makefile_test, [])
 test('dynamicToo001MakeB', [opts], makefile_test, [])
+test('T25837', [opts], makefile_test, [])
-- 
GitLab