afe or safe-infer ON
safeFlagCheck cmdl dflags =
    case safeLanguageOn dflags of
        True -> (dflags', warns)

        -- throw error if -fpackage-trust by itself with no safe haskell flag
        False | not cmdl && packageTrustOn dflags
              -> (gopt_unset dflags' Opt_PackageTrust,
                  [L (pkgTrustOnLoc dflags') $
                      "-fpackage-trust ignored;" ++
                      " must be specified with a Safe Haskell flag"]
                  )

        False | null warns && safeInfOk
              -> (dflags', [])

              | otherwise
              -> (dflags' { safeHaskell = Sf_None }, [])
                -- Have we inferred Unsafe?
                -- See Note [HscMain . Safe Haskell Inference]
    where
        -- TODO: Can we do better than this for inference?
        safeInfOk = not $ xopt Opt_OverlappingInstances dflags

        (dflags', warns) = foldl check_method (dflags, []) unsafeFlags

        check_method (df, warns) (str,loc,test,fix)
            | test df   = (apFix fix df, warns ++ safeFailure (loc dflags) str)
            | otherwise = (df, warns)

        apFix f = if safeInferOn dflags then id else f

        safeFailure loc str
           = [L loc $ str ++ " is not allowed in Safe Haskell; ignoring " ++ str]

{- **********************************************************************
%*                                                                      *
                DynFlags specifications
%*                                                                      *
%********************************************************************* -}

-- | All dynamic flags option strings. These are the user facing strings for
-- enabling and disabling options.
all
safeFlagCheck cmdl dflags =
    case safeLanguageOn dflags of
        True -> (dflags', warns)

        -- throw error if -fpackage-trust by itself with no safe haskell flag
        False | not cmdl && packageTrustOn dflags
              -> (gopt_unset dflags' Opt_PackageTrust,
                  [L (pkgTrustOnLoc dflags') $
                      "-fpackage-trust ignored;" ++
                      " must be specified with a Safe Haskell flag"]
                  )

        False | null warns && safeInfOk
              -> (dflags', [])

              | otherwise
              -> (dflags' { safeHaskell = Sf_None }, [])
                -- Have we inferred Unsafe?
                -- See Note [HscMain . Safe Haskell Inference]
    where
        -- TODO: Can we do better than this for inference?
        safeInfOk = not $ xopt Opt_OverlappingInstances dflags

        (dflags', warns) = foldl check_method (dflags, []) unsafeFlags

        check_method (df, warns) (str,loc,test,fix)
            | test df   = (apFix fix df, warns ++ safeFailure (loc dflags) str)
            | otherwise = (df, warns)

        apFix f = if safeInferOn dflags then id else f

        safeFailure loc str
           = [L loc $ str ++ " is not allowed in Safe Haskell; ignoring " ++ str]

{- **********************************************************************
%*                                                                      *
                DynFlags specifications
%*                                                                      *
%********************************************************************* -}

-- | All dynamic flags option strings. These are the user facing strings for
-- enabling and disabling options.
safeFlagCheck cmdl dflags =
    case safeLanguageOn dflags of
        True -> (dflags', warns)

        -- throw error if -fpackage-trust by itself with no safe haskell flag
        False | not cmdl && packageTrustOn dflags
              -> (gopt_unset dflags' Opt_PackageTrust,
                  [L (pkgTrustOnLoc dflags') $
                      "-fpackage-trust ignored;" ++
                      " must be specified with a Safe Haskell flag"]
                  )

        False | null warns && safeInfOk
              -> (dflags', [])

              | otherwise
              -> (dflags' { safeHaskell = Sf_None }, [])
                -- Have we inferred Unsafe?
                -- See Note [HscMain . Safe Haskell Inference]
    where
        -- TODO: Can we do better than this for inference?
        safeInfOk = not $ xopt Opt_OverlappingInstances dflags

        (dflags', warns) = foldl check_method (dflags, []) unsafeFlags

        check_method (df, warns) (str,loc,test,fix)
            | test df   = (apFix fix df, warns ++ safeFailure (loc dflags) str)
            | otherwise = (df, warns)

        apFix f = if safeInferOn dflags then id else f

        safeFailure loc str
           = [L loc $ str ++ " is not allowed in Safe Haskell; ignoring " ++ str]

{- **********************************************************************
%*                                                                      *
                DynFlags specifications
%*                                                                      *
%********************************************************************* -}

-- | All dynamic flags option strings. These are the user facing strings for
-- enabling and disabling options.