Sasha Bogicevic pushed to branch wip/21101 at Glasgow Haskell Compiler / GHC

Commits:

12 changed files:

Changes:

  • compiler/GHC/Rename/Pat.hs
    ... ... @@ -874,7 +874,7 @@ rnHsRecFields ctxt mk_arg (HsRecFields { rec_flds = flds, rec_dotdot = dotdot })
    874 874
                ; checkErr dd_flag (needFlagDotDot ctxt)
    
    875 875
                ; (rdr_env, lcl_env) <- getRdrEnvs
    
    876 876
                ; conInfo <- lookupConstructorInfo qcon
    
    877
    -           ; when (conFieldInfo conInfo == ConHasPositionalArgs) (addErr (TcRnIllegalWildcardsInConstructor con))
    
    877
    +           ; when (conFieldInfo conInfo == ConHasPositionalArgs) (addErr (TcRnIllegalWildcardsInConstructor (toRecordFieldPart ctxt) con))
    
    878 878
                ; let present_flds = mkOccSet $ map rdrNameOcc (getFieldRdrs flds)
    
    879 879
     
    
    880 880
                        -- For constructor uses (but not patterns)
    

  • compiler/GHC/Tc/Errors/Ppr.hs
    ... ... @@ -357,18 +357,12 @@ instance Diagnostic TcRnMessage where
    357 357
           -> mkSimpleDecorated $ vcat [text "Illegal view pattern: " <+> ppr pat]
    
    358 358
         TcRnCharLiteralOutOfRange c
    
    359 359
           -> mkSimpleDecorated $ text "character literal out of range: '\\" <> char c  <> char '\''
    
    360
    -    TcRnIllegalWildcardsInConstructor con
    
    361
    -      -> mkSimpleDecorated $
    
    362
    -           vcat [ text "The data constructor"
    
    363
    -                  <+> quotes (ppr con)
    
    364
    -                  <+> text "does not have named record fields, so a pattern match"
    
    365
    -                  <+> quotes (ppr con)
    
    366
    -                  <+> text " { .. } is incorrect."
    
    367
    -                , text "Possible fixes:"
    
    368
    -                , nest 2 (text "* Replace the pattern '" <+> quotes (ppr con) <+> text "'{ .. }' with" <+> quotes (ppr con))
    
    369
    -                , nest 2 (text "* Replace the pattern '" <+> quotes (ppr con) <+> text "'{ .. }' with" <+> quotes (ppr con) <+> text "{}")
    
    370
    -                , nest 4 (text "This version works even if you add/remove fields to " <+> quotes (ppr con) <+> text "later")
    
    371
    -                ]
    
    360
    +    TcRnIllegalWildcardsInConstructor ctx con
    
    361
    +      -> mkSimpleDecorated $
    
    362
    +           text "The data constructor" <+> quotes (ppr con)
    
    363
    +             <+> text "does not have named record fields, so the record"
    
    364
    +             <+> pprRecordFieldPart ctx
    
    365
    +             <+> quotes (ppr con <> text "{..}") <+> text "is invalid."
    
    372 366
         TcRnIgnoringAnnotations anns
    
    373 367
           -> mkSimpleDecorated $
    
    374 368
                text "Ignoring ANN annotation" <> plural anns <> comma
    
    ... ... @@ -2797,8 +2791,10 @@ instance Diagnostic TcRnMessage where
    2797 2791
           -> [suggestExtension LangExt.ViewPatterns]
    
    2798 2792
         TcRnCharLiteralOutOfRange{}
    
    2799 2793
           -> noHints
    
    2800
    -    TcRnIllegalWildcardsInConstructor{}
    
    2801
    -      -> noHints
    
    2794
    +    TcRnIllegalWildcardsInConstructor ctx con
    
    2795
    +      -> case ctx of
    
    2796
    +           RecordFieldPattern{} -> [SuggestEmptyRecordBraces con]
    
    2797
    +           _                    -> [SuggestExplicitConstructorArguments con]
    
    2802 2798
         TcRnIgnoringAnnotations{}
    
    2803 2799
           -> noHints
    
    2804 2800
         TcRnAnnotationInSafeHaskell
    

  • compiler/GHC/Tc/Errors/Types.hs
    ... ... @@ -817,17 +817,32 @@ data TcRnMessage where
    817 817
       TcRnNegativeNumTypeLiteral :: IntegralLit GhcRn -> TcRnMessage
    
    818 818
     
    
    819 819
       {-| TcRnIllegalWildcardsInConstructor is an error that occurs whenever
    
    820
    -      the record wildcards '..' are used inside a constructor without labeled fields.
    
    820
    +      the record wildcards '..' are used with a constructor whose fields are
    
    821
    +      positional (unlabelled). The 'RecordFieldPart' field records whether
    
    822
    +      the wildcards occurred in a record construction (an expression) or in
    
    823
    +      a record pattern, so that the message and its suggested fixes can be
    
    824
    +      worded accordingly. Constructors with no fields at all do not trigger
    
    825
    +      this error: since GHC proposal 496 ("Nullary record wildcards"),
    
    826
    +      @C {..}@ is legal for nullary constructors.
    
    821 827
     
    
    822
    -      Examples(s): None
    
    828
    +      Example(s):
    
    829
    +
    
    830
    +        data D = D Int Bool
    
    831
    +
    
    832
    +        f :: D -> ()
    
    833
    +        f D{..} = ()      -- record pattern
    
    834
    +
    
    835
    +        g :: D
    
    836
    +        g = D{..}         -- record construction
    
    823 837
     
    
    824 838
          Test cases:
    
    825 839
            rename/should_fail/T9815.hs
    
    826 840
            rename/should_fail/T9815b.hs
    
    827 841
            rename/should_fail/T9815ghci.hs
    
    828 842
            rename/should_fail/T9815bghci.hs
    
    843
    +       rename/should_fail/T21101.hs
    
    829 844
       -}
    
    830
    -  TcRnIllegalWildcardsInConstructor :: !Name -> TcRnMessage
    
    845
    +  TcRnIllegalWildcardsInConstructor :: !RecordFieldPart -> !Name -> TcRnMessage
    
    831 846
     
    
    832 847
       {-| TcRnIgnoringAnnotations is a warning that occurs when the source code
    
    833 848
           contains annotation pragmas but the platform in use does not support an
    

  • compiler/GHC/Types/Hint.hs
    ... ... @@ -548,6 +548,22 @@ data GhcHint
    548 548
       | SuggestUpgradeForSemaphoreVersionMismatch !SemaphoreUpgradeTarget !Int
    
    549 549
         -- ^ The 'Int' is the required protocol version.
    
    550 550
     
    
    551
    +  {-| Suggest replacing a record wildcard pattern @C {..}@ with @C {}@,
    
    552
    +      which matches a constructor without binding its fields.
    
    553
    +
    
    554
    +      Triggered by 'GHC.Tc.Errors.Types.TcRnIllegalWildcardsInConstructor'
    
    555
    +      in a record pattern.
    
    556
    +  -}
    
    557
    +  | SuggestEmptyRecordBraces !Name
    
    558
    +
    
    559
    +  {-| Suggest applying a constructor directly to its arguments instead
    
    560
    +      of record syntax, for constructors without labelled fields.
    
    561
    +
    
    562
    +      Triggered by 'GHC.Tc.Errors.Types.TcRnIllegalWildcardsInConstructor'
    
    563
    +      in a record construction.
    
    564
    +  -}
    
    565
    +  | SuggestExplicitConstructorArguments !Name
    
    566
    +
    
    551 567
     -- | What the user should upgrade to resolve an @-jsem@ semaphore
    
    552 568
     --   protocol version mismatch.
    
    553 569
     data SemaphoreUpgradeTarget
    

  • compiler/GHC/Types/Hint/Ppr.hs
    ... ... @@ -345,6 +345,11 @@ instance Outputable GhcHint where
    345 345
                      text "The jobserver uses a newer semaphore protocol than this GHC."
    
    346 346
                   $$ (text "Upgrade GHC to a version that supports semaphore protocol v"
    
    347 347
                       <> int required <> text " to resolve this.")
    
    348
    +    SuggestEmptyRecordBraces con
    
    349
    +      -> text "Use" <+> quotes (ppr con <> text "{}") <+> text "instead,"
    
    350
    +         <+> text "which matches" <+> quotes (ppr con) <+> text "regardless of its fields"
    
    351
    +    SuggestExplicitConstructorArguments con
    
    352
    +      -> text "Apply" <+> quotes (ppr con) <+> text "to its arguments instead"
    
    348 353
     
    
    349 354
     perhapsAsPat :: SDoc
    
    350 355
     perhapsAsPat = text "Perhaps you meant an as-pattern, which must not be surrounded by whitespace"
    

  • testsuite/tests/rename/should_fail/T21101.hs
    1
    +{-# LANGUAGE RecordWildCards #-}
    
    2
    +module T21101 where
    
    3
    +
    
    4
    +data D = D Int Bool
    
    5
    +
    
    6
    +f :: D -> ()
    
    7
    +f D{..} = ()

  • testsuite/tests/rename/should_fail/T21101.stderr
    1
    +T21101.hs:7:3: error: [GHC-47217]
    
    2
    +    The data constructor ‘D’ does not have named record fields, so the record pattern ‘D{..}’ is invalid.
    
    3
    +    Suggested fix:
    
    4
    +      Use ‘D{}’ instead, which matches ‘D’ regardless of its fields
    
    5
    +

  • testsuite/tests/rename/should_fail/T9815.stderr
    1
    -
    
    2 1
     T9815.hs:6:13: error: [GHC-47217]
    
    3
    -    Illegal `{..}' notation for constructor ‘N’
    
    4
    -      Record wildcards may not be used for constructors with unlabelled fields.
    
    5
    -      Possible fix: Remove the `{..}' and add a match for each field of the constructor.
    2
    +    The data constructor ‘N’ does not have named record fields, so the record construction ‘N{..}’ is invalid.
    
    3
    +    Suggested fix: Apply ‘N’ to its arguments instead
    
    4
    +

  • testsuite/tests/rename/should_fail/T9815b.stderr
    1
    -
    
    2 1
     T9815.hs:6:13: error: [GHC-47217]
    
    3
    -    Illegal `{..}' notation for constructor ‘N’
    
    4
    -      Record wildcards may not be used for constructors with unlabelled fields.
    
    5
    -      Possible fix: Remove the `{..}' and add a match for each field of the constructor.
    2
    +    The data constructor ‘N’ does not have named record fields, so the record construction ‘N{..}’ is invalid.
    
    3
    +    Suggested fix: Apply ‘N’ to its arguments instead
    
    4
    +

  • testsuite/tests/rename/should_fail/T9815bghci.stderr
    1
    +<interactive>:5:7: error: [GHC-47217]
    
    2
    +    The data constructor ‘Arg’ does not have named record fields, so the record construction ‘Arg{..}’ is invalid.
    
    3
    +    Suggested fix: Apply ‘Arg’ to its arguments instead
    
    1 4
     
    2
    -<interactive>:5:7: [GHC-47217]
    
    3
    -    Illegal `{..}' notation for constructor ‘Arg’
    
    4
    -      Record wildcards may not be used for constructors with unlabelled fields.
    
    5
    -      Possible fix: Remove the `{..}' and add a match for each field of the constructor.

  • testsuite/tests/rename/should_fail/T9815ghci.stderr
    1
    +<interactive>:3:7: error: [GHC-47217]
    
    2
    +    The data constructor ‘Data.Semigroup.Arg’ does not have named record fields, so the record construction ‘Data.Semigroup.Arg{..}’ is invalid.
    
    3
    +    Suggested fix: Apply ‘Data.Semigroup.Arg’ to its arguments instead
    
    1 4
     
    2
    -<interactive>:3:7: [GHC-47217]
    
    3
    -    Illegal `{..}' notation for constructor ‘Data.Semigroup.Arg’
    
    4
    -      Record wildcards may not be used for constructors with unlabelled fields.
    
    5
    -      Possible fix: Remove the `{..}' and add a match for each field of the constructor.

  • testsuite/tests/rename/should_fail/all.T
    ... ... @@ -186,6 +186,7 @@ test('T18138', normal, compile_fail, [''])
    186 186
     test('T20147', normal, compile_fail, [''])
    
    187 187
     test('RnEmptyStatementGroup1', normal, compile_fail, [''])
    
    188 188
     test('RnImplicitBindInMdoNotation', normal, compile_fail, [''])
    
    189
    +test('T21101', normal, compile_fail, [''])
    
    189 190
     test('T21605a', normal, compile_fail, [''])
    
    190 191
     test('T21605b', normal, compile_fail, [''])
    
    191 192
     test('T21605c', normal, compile_fail, [''])