diff --git a/.github/workflows/quick-jobs.yml b/.github/workflows/quick-jobs.yml
index f304ec9fa000c6bf69cd6fd09565e7d3459bf94c..0a84f2f2529bbc7945b0bf426abeda086070e50a 100644
--- a/.github/workflows/quick-jobs.yml
+++ b/.github/workflows/quick-jobs.yml
@@ -37,7 +37,7 @@ jobs:
       - name: Update Hackage index
         run: cabal v2-update
       - name: Install alex
-        run: cabal v2-install alex
+        run: cabal v2-install alex --constraint='alex ==3.2.6'
       - uses: actions/checkout@v2
       - name: Regenerate files
         run: |
diff --git a/Cabal/Distribution/Fields/Lexer.hs b/Cabal/Distribution/Fields/Lexer.hs
index 9116ce331cab2ba46027b5ab235d81a1d512073c..6be64ae08b2377da13465d1ccbe58e0916572043 100644
--- a/Cabal/Distribution/Fields/Lexer.hs
+++ b/Cabal/Distribution/Fields/Lexer.hs
@@ -372,6 +372,7 @@ uncheckedShiftL# = shiftL#
 #endif
 
 {-# INLINE alexIndexInt16OffAddr #-}
+alexIndexInt16OffAddr :: AlexAddr -> Int# -> Int#
 alexIndexInt16OffAddr (AlexA# arr) off =
 #ifdef WORDS_BIGENDIAN
   narrow16Int# i
@@ -381,10 +382,14 @@ alexIndexInt16OffAddr (AlexA# arr) off =
         low  = int2Word# (ord# (indexCharOffAddr# arr off'))
         off' = off *# 2#
 #else
-  indexInt16OffAddr# arr off
+#if __GLASGOW_HASKELL__ >= 901
+  int16ToInt#
+#endif
+    (indexInt16OffAddr# arr off)
 #endif
 
 {-# INLINE alexIndexInt32OffAddr #-}
+alexIndexInt32OffAddr :: AlexAddr -> Int# -> Int#
 alexIndexInt32OffAddr (AlexA# arr) off =
 #ifdef WORDS_BIGENDIAN
   narrow32Int# i
@@ -398,7 +403,10 @@ alexIndexInt32OffAddr (AlexA# arr) off =
    b0   = int2Word# (ord# (indexCharOffAddr# arr off'))
    off' = off *# 4#
 #else
-  indexInt32OffAddr# arr off
+#if __GLASGOW_HASKELL__ >= 901
+  int32ToInt#
+#endif
+    (indexInt32OffAddr# arr off)
 #endif
 
 #if __GLASGOW_HASKELL__ < 503
diff --git a/Cabal/Distribution/Simple/Build/PathsModule.hs b/Cabal/Distribution/Simple/Build/PathsModule.hs
index 5e660e8d655a73589fed27754d263a6d256232e3..21db5625329d7cfec20789347d77951d71f05fa6 100644
--- a/Cabal/Distribution/Simple/Build/PathsModule.hs
+++ b/Cabal/Distribution/Simple/Build/PathsModule.hs
@@ -64,8 +64,9 @@ generatePathsModule pkg_descr lbi clbi =
         | otherwise =
           "{-# OPTIONS_GHC -fffi #-}\n"
 
-       warning_pragmas =
-        "{-# OPTIONS_GHC -fno-warn-missing-import-lists #-}\n"
+       warning_pragmas = unlines $
+        [ "{-# OPTIONS_GHC -fno-warn-missing-import-lists #-}" ] ++
+        [ "{-# OPTIONS_GHC -Wno-missing-safe-haskell-mode #-}" | supports_missing_safehaskell ]
 
        foreign_imports
         | absolute = ""
@@ -250,6 +251,7 @@ generatePathsModule pkg_descr lbi clbi =
         supports_cpp = supports_language_pragma
         supports_rebindable_syntax= ghc_newer_than (mkVersion [7,0,1])
         supports_language_pragma = ghc_newer_than (mkVersion [6,6,1])
+        supports_missing_safehaskell = ghc_newer_than (mkVersion [8,10,1])
 
         ghc_newer_than minVersion =
           case compilerCompatVersion GHC (compiler lbi) of
diff --git a/boot/ci-quick-jobs.template.yml b/boot/ci-quick-jobs.template.yml
index f304ec9fa000c6bf69cd6fd09565e7d3459bf94c..0a84f2f2529bbc7945b0bf426abeda086070e50a 100644
--- a/boot/ci-quick-jobs.template.yml
+++ b/boot/ci-quick-jobs.template.yml
@@ -37,7 +37,7 @@ jobs:
       - name: Update Hackage index
         run: cabal v2-update
       - name: Install alex
-        run: cabal v2-install alex
+        run: cabal v2-install alex --constraint='alex ==3.2.6'
       - uses: actions/checkout@v2
       - name: Regenerate files
         run: |
diff --git a/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/my.cabal b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/my.cabal
new file mode 100644
index 0000000000000000000000000000000000000000..355f5819120f275ed69521ce32c837e676760ee9
--- /dev/null
+++ b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/my.cabal
@@ -0,0 +1,19 @@
+cabal-version: 2.2
+name: PathsModule
+version: 0.1
+license: BSD-3-Clause
+author: Oleg Grenrus
+stability: stable
+category: PackageTests
+build-type: Simple
+
+description:
+    Check that the generated paths module compiles.
+
+Library
+    exposed-modules: Paths_PathsModule
+    build-depends: base
+    default-language: Haskell2010
+
+    if impl(ghc >= 8.10)
+      ghc-options: -Winferred-safe-imports -Wmissing-safe-haskell-mode -Werror
diff --git a/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.cabal.out b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.cabal.out
new file mode 100644
index 0000000000000000000000000000000000000000..8d21f201844e858c12a8ef2d9375533a0cec4c08
--- /dev/null
+++ b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.cabal.out
@@ -0,0 +1,5 @@
+# Setup configure
+Configuring PathsModule-0.1...
+# Setup build
+Preprocessing library for PathsModule-0.1..
+Building library for PathsModule-0.1..
diff --git a/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.out b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.out
new file mode 100644
index 0000000000000000000000000000000000000000..8d21f201844e858c12a8ef2d9375533a0cec4c08
--- /dev/null
+++ b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.out
@@ -0,0 +1,5 @@
+# Setup configure
+Configuring PathsModule-0.1...
+# Setup build
+Preprocessing library for PathsModule-0.1..
+Building library for PathsModule-0.1..
diff --git a/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.test.hs b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.test.hs
new file mode 100644
index 0000000000000000000000000000000000000000..7c55afb42cadf2ed9b8c10c028f83a3b65d3abe4
--- /dev/null
+++ b/cabal-testsuite/PackageTests/PathsModule/MissingSafeHaskellMode/setup.test.hs
@@ -0,0 +1,3 @@
+import Test.Cabal.Prelude
+-- Test that Paths module is generated and available for libraries.
+main = setupAndCabalTest $ setup_build []