[Git][ghc/ghc][wip/T24824] Pmc: Improve warning messages of -Wincomplete-record-selectors

Sebastian Graf (@sgraf812) gitlab at gitlab.haskell.org
Thu Aug 22 15:14:26 UTC 2024



Sebastian Graf pushed to branch wip/T24824 at Glasgow Haskell Compiler / GHC


Commits:
f92cc9a8 by Sebastian Graf at 2024-08-22T17:14:17+02:00
Pmc: Improve warning messages of -Wincomplete-record-selectors

... as suggested by Adam Gundry in !12685.

- - - - -


12 changed files:

- compiler/GHC/HsToCore/Errors/Ppr.hs
- compiler/GHC/HsToCore/Errors/Types.hs
- compiler/GHC/HsToCore/Expr.hs
- compiler/GHC/HsToCore/Pmc.hs
- compiler/GHC/Tc/Errors/Ppr.hs
- compiler/GHC/Tc/Errors/Types.hs
- compiler/GHC/Tc/Instance/Class.hs
- testsuite/tests/pmcheck/should_compile/DsIncompleteRecSel1.stderr
- testsuite/tests/pmcheck/should_compile/DsIncompleteRecSel2.stderr
- testsuite/tests/pmcheck/should_compile/DsIncompleteRecSel3.stderr
- testsuite/tests/pmcheck/should_compile/T24891.stderr
- testsuite/tests/typecheck/should_compile/TcIncompleteRecSel.stderr


Changes:

=====================================
compiler/GHC/HsToCore/Errors/Ppr.hs
=====================================
@@ -207,10 +207,12 @@ instance Diagnostic DsMessage where
                           <+> text "for"<+> quotes (ppr lhs_id)
                           <+> text "might fire first")
                 ]
-    DsIncompleteRecordSelector name cons_wo_field not_full_examples -> mkSimpleDecorated $
-      text "The application of the record field" <+> quotes (ppr name)
-      <+> text "may fail for the following constructors:"
-      <+> vcat (map ppr cons_wo_field ++ [text "..." | not_full_examples])
+    DsIncompleteRecordSelector name cons maxCons -> mkSimpleDecorated $
+      hang (text "Selecting the record field" <+> quotes (ppr name)
+              <+> text "may fail for the following constructors:")
+           2
+           (hsep $ punctuate comma $
+            map ppr (take maxCons cons) ++ [ text "..." | lengthExceeds cons maxCons ])
 
   diagnosticReason = \case
     DsUnknownMessage m          -> diagnosticReason m


=====================================
compiler/GHC/HsToCore/Errors/Types.hs
=====================================
@@ -163,7 +163,7 @@ data DsMessage
        DsIncompleteRecSel2
        DsIncompleteRecSel3
   -}
-  | DsIncompleteRecordSelector !Name ![ConLike] !Bool
+  | DsIncompleteRecordSelector !Name ![ConLike] !Int
 
   deriving Generic
 


=====================================
compiler/GHC/HsToCore/Expr.hs
=====================================
@@ -271,17 +271,12 @@ dsExpr (HsRecSel _ (FieldOcc id _))
        ; suppress_here <- getSuppressIncompleteRecSelsDs
          -- See (5) of Note [Detecting incomplete record selectors] in GHC.HsToCore.Pmc
        ; unless suppress_here $ do
-           cons_trimmed <- trim_cons cons_wo_field
+           dflags <- getDynFlags
+           let maxCons = maxUncoveredPatterns dflags
            -- See (4) of Note [Detecting incomplete record selectors] in GHC.HsToCore.Pmc
            unless (null cons_wo_field) $
-             diagnosticDs $ DsIncompleteRecordSelector name cons_trimmed (cons_trimmed /= cons_wo_field)
+             diagnosticDs $ DsIncompleteRecordSelector name cons_wo_field maxCons
        ; dsHsVar id }
-  where
-    trim_cons :: [ConLike] -> DsM [ConLike]
-    trim_cons cons_wo_field = do
-      dflags <- getDynFlags
-      let maxConstructors = maxUncoveredPatterns dflags
-      return $ take maxConstructors cons_wo_field
 
 
 dsExpr (HsUnboundVar (HER ref _ _) _)  = dsEvTerm =<< readMutVar ref


=====================================
compiler/GHC/HsToCore/Pmc.hs
=====================================
@@ -385,13 +385,11 @@ pmcRecSel sel_id arg
           sel_name = varName sel_id
           warn_incomplete arg_id uncov_nablas = do
             dflags <- getDynFlags
-            let maxConstructors = maxUncoveredPatterns dflags
-            unc_examples <- getNFirstUncovered MinimalCover [arg_id] (maxConstructors + 1) uncov_nablas
+            let maxPatterns = maxUncoveredPatterns dflags
+            unc_examples <- getNFirstUncovered MinimalCover [arg_id] (maxPatterns + 1) uncov_nablas
             let cons = [con | unc_example <- unc_examples
                       , Just (PACA (PmAltConLike con) _ _) <- [lookupSolution unc_example arg_id]]
-                not_full_examples = length cons == (maxConstructors + 1)
-                cons' = take maxConstructors cons
-            diagnosticDs $ DsIncompleteRecordSelector sel_name cons' not_full_examples
+            diagnosticDs $ DsIncompleteRecordSelector sel_name cons maxPatterns
 
 pmcRecSel _ _ = return ()
 


=====================================
compiler/GHC/Tc/Errors/Ppr.hs
=====================================
@@ -1492,9 +1492,12 @@ instance Diagnostic TcRnMessage where
     TcRnPartialFieldSelector fld -> mkSimpleDecorated $
       sep [text "Use of partial record field selector" <> colon,
            nest 2 $ quotes (ppr (occName fld))]
-    TcRnHasFieldResolvedIncomplete name -> mkSimpleDecorated $
-      text "The invocation of `getField` on the record field" <+> quotes (ppr name)
-      <+> text "may produce an error since it is not defined for all data constructors"
+    TcRnHasFieldResolvedIncomplete name cons maxCons -> mkSimpleDecorated $
+      hang (text "Selecting the record field" <+> quotes (ppr name)
+              <+> text "may fail for the following constructors:")
+           2
+           (hsep $ punctuate comma $
+            map ppr (take maxCons cons) ++ [ text "..." | lengthExceeds cons maxCons ])
     TcRnBadFieldAnnotation n con reason -> mkSimpleDecorated $
       hang (pprBadFieldAnnotationReason reason)
          2 (text "on the" <+> speakNth n


=====================================
compiler/GHC/Tc/Errors/Types.hs
=====================================
@@ -3514,7 +3514,7 @@ data TcRnMessage where
      Test cases:
        TcIncompleteRecSel
   -}
-  TcRnHasFieldResolvedIncomplete :: !Name -> TcRnMessage
+  TcRnHasFieldResolvedIncomplete :: !Name -> ![ConLike] -> !Int -> TcRnMessage
 
   {-| TcRnBadFieldAnnotation is an error/warning group indicating that a
     strictness/unpack related data type field annotation is invalid.


=====================================
compiler/GHC/Tc/Instance/Class.hs
=====================================
@@ -1289,10 +1289,12 @@ matchHasField dflags short_cut clas tys
                      then do { -- See Note [Unused name reporting and HasField]
                                addUsedGRE AllDeprecationWarnings gre
                              ; keepAlive name
+                             ; let maxCons = maxUncoveredPatterns dflags
+                             ; let (_, fallible_cons) = sel_cons (idDetails sel_id)
                              ; suppress <- getSuppressIncompleteRecSelsTc
                                -- See (7) of Note [Detecting incomplete record selectors] in GHC.HsToCore.Pmc
-                             ; unless (null (snd $ sel_cons $ idDetails sel_id) || suppress) $ do
-                                 addDiagnostic $ TcRnHasFieldResolvedIncomplete name
+                             ; unless (null fallible_cons || suppress) $ do
+                                 addDiagnostic $ TcRnHasFieldResolvedIncomplete name fallible_cons maxCons
                              ; return OneInst { cir_new_theta   = theta
                                               , cir_mk_ev       = mk_ev
                                               , cir_canonical   = True


=====================================
testsuite/tests/pmcheck/should_compile/DsIncompleteRecSel1.stderr
=====================================
@@ -1,3 +1,4 @@
-
 DsIncompleteRecSel1.hs:8:5: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘x’ may fail for the following constructors: T2
+    Selecting the record field ‘x’ may fail for the following constructors:
+      T2
+


=====================================
testsuite/tests/pmcheck/should_compile/DsIncompleteRecSel2.stderr
=====================================
@@ -1,6 +1,8 @@
-
 DsIncompleteRecSel2.hs:22:8: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘x’ may fail for the following constructors: T4
+    Selecting the record field ‘x’ may fail for the following constructors:
+      T4
 
 DsIncompleteRecSel2.hs:28:19: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘x’ may fail for the following constructors: P
+    Selecting the record field ‘x’ may fail for the following constructors:
+      P
+


=====================================
testsuite/tests/pmcheck/should_compile/DsIncompleteRecSel3.stderr
=====================================
@@ -1,80 +1,48 @@
-
 DsIncompleteRecSel3.hs:29:7: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘x’ may fail for the following constructors: T2
+    Selecting the record field ‘x’ may fail for the following constructors:
+      T2
 
 DsIncompleteRecSel3.hs:34:7: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:13: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:19: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:25: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:31: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:37: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:43: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:49: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:55: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:34:61: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
 
 DsIncompleteRecSel3.hs:37:5: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘y’ may fail for the following constructors: G2
-                                                                                     G3
-                                                                                     G4
-                                                                                     G5
-                                                                                     ...
+    Selecting the record field ‘y’ may fail for the following constructors:
+      G2, G3, G4, G5, ...
+


=====================================
testsuite/tests/pmcheck/should_compile/T24891.stderr
=====================================
@@ -1,9 +1,12 @@
 T24891.hs:13:7: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘sel’ may fail for the following constructors: T3
+    Selecting the record field ‘sel’ may fail for the following constructors:
+      T3
 
 T24891.hs:25:12: warning: [GHC-86894] [-Wincomplete-record-selectors]
-    The invocation of `getField` on the record field ‘sel2’ may produce an error since it is not defined for all data constructors
+    Selecting the record field ‘sel2’ may fail for the following constructors:
+      No
 
 T24891.hs:30:16: warning: [GHC-17335] [-Wincomplete-record-selectors]
-    The application of the record field ‘sel3’ may fail for the following constructors: No2
+    Selecting the record field ‘sel3’ may fail for the following constructors:
+      No2
 


=====================================
testsuite/tests/typecheck/should_compile/TcIncompleteRecSel.stderr
=====================================
@@ -1,3 +1,4 @@
-
 TcIncompleteRecSel.hs:16:5: warning: [GHC-86894] [-Wincomplete-record-selectors]
-    The invocation of `getField` on the record field ‘x’ may produce an error since it is not defined for all data constructors
+    Selecting the record field ‘x’ may fail for the following constructors:
+      T2
+



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f92cc9a869a4f25c72ea4c03c2c06efee169a236

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/f92cc9a869a4f25c72ea4c03c2c06efee169a236
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20240822/39d9bf6d/attachment-0001.html>


More information about the ghc-commits mailing list