
Marge Bot pushed to branch wip/marge_bot_batch_merge_job at Glasgow Haskell Compiler / GHC Commits: d3209b4e by Javran Cheng at 2025-05-01T09:33:33-04:00 Suppress unused do-binding if discarded variable is Any or ZonkAny. Consider example (#25895):
do { forever (return ()); blah }
where `forever :: forall a b. IO a -> IO b`. Nothing constrains `b`, so it will be instantiates with `Any` or `ZonkAny`. But we certainly don't want to complain about a discarded do-binding. Fixes #25895 - - - - - 4 changed files: - compiler/GHC/Builtin/Types.hs - compiler/GHC/HsToCore/Expr.hs - compiler/GHC/Tc/Utils/TcType.hs - testsuite/tests/printer/T17697.stderr Changes: ===================================== compiler/GHC/Builtin/Types.hs ===================================== @@ -510,6 +510,17 @@ Wrinkles: See examples in ghc-prim:GHC.Types +(Any8) Warning about unused bindings of type `Any` and `ZonkAny` are suppressed, + following the same rationale of supressing warning about the unit type. + + For example, consider (#25895): + + do { forever (return ()); blah } + + where forever :: forall a b. IO a -> IO b + Nothing constrains `b`, so it will be instantiates with `Any` or `ZonkAny`. + But we certainly don't want to complain about a discarded do-binding. + The Any tycon used to be quite magic, but we have since been able to implement it merely with an empty kind polymorphic type family. See #10886 for a bit of history. ===================================== compiler/GHC/HsToCore/Expr.hs ===================================== @@ -1243,9 +1243,13 @@ warnDiscardedDoBindings rhs m_ty elt_ty ; when (warn_unused || warn_wrong) $ do { fam_inst_envs <- dsGetFamInstEnvs ; let norm_elt_ty = topNormaliseType fam_inst_envs elt_ty - - -- Warn about discarding non-() things in 'monadic' binding - ; if warn_unused && not (isUnitTy norm_elt_ty) + supressible_ty = + isUnitTy norm_elt_ty || isAnyTy norm_elt_ty || isZonkAnyTy norm_elt_ty + -- Warn about discarding things in 'monadic' binding, + -- however few types are excluded: + -- * Unit type `()` + -- * `ZonkAny` or `Any` type see (Any8) of Note [Any types] + ; if warn_unused && not supressible_ty then diagnosticDs (DsUnusedDoBind rhs elt_ty) else ===================================== compiler/GHC/Tc/Utils/TcType.hs ===================================== @@ -88,7 +88,7 @@ module GHC.Tc.Utils.TcType ( isSigmaTy, isRhoTy, isRhoExpTy, isOverloadedTy, isFloatingPrimTy, isDoubleTy, isFloatTy, isIntTy, isWordTy, isStringTy, isIntegerTy, isNaturalTy, - isBoolTy, isUnitTy, isCharTy, + isBoolTy, isUnitTy, isAnyTy, isZonkAnyTy, isCharTy, isTauTy, isTauTyCon, tcIsTyVarTy, isPredTy, isTyVarClassPred, checkValidClsArgs, hasTyVarHead, @@ -2006,7 +2006,7 @@ isFloatTy, isDoubleTy, isFloatPrimTy, isDoublePrimTy, isIntegerTy, isNaturalTy, isIntTy, isWordTy, isBoolTy, - isUnitTy, isCharTy :: Type -> Bool + isUnitTy, isAnyTy, isZonkAnyTy, isCharTy :: Type -> Bool isFloatTy = is_tc floatTyConKey isDoubleTy = is_tc doubleTyConKey isFloatPrimTy = is_tc floatPrimTyConKey @@ -2017,6 +2017,8 @@ isIntTy = is_tc intTyConKey isWordTy = is_tc wordTyConKey isBoolTy = is_tc boolTyConKey isUnitTy = is_tc unitTyConKey +isAnyTy = is_tc anyTyConKey +isZonkAnyTy = is_tc zonkAnyTyConKey isCharTy = is_tc charTyConKey -- | Check whether the type is of the form @Any :: k@, ===================================== testsuite/tests/printer/T17697.stderr ===================================== @@ -1,8 +1,2 @@ T17697.hs:6:5: warning: [GHC-88464] [-Wdeferred-out-of-scope-variables (in -Wdefault)] Variable not in scope: threadDelay :: t0 -> IO a0 - -T17697.hs:6:5: warning: [GHC-81995] [-Wunused-do-bind (in -Wall)] - A do-notation statement discarded a result of type - ‘GHC.Internal.Types.ZonkAny 1’ - Suggested fix: Suppress this warning by saying ‘_ <- threadDelay 1’ - View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/d3209b4ec12509805c27920ed682bfba... -- View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/d3209b4ec12509805c27920ed682bfba... You're receiving this email because of your account on gitlab.haskell.org.