Skip to content

Commit 454ec0d

Browse files
committed
Implement availableSince.
Tag Backpack fields (mixins, signatures) to `availableSince [2,0]`. This "fixes" #4448, as fields are recognised, warned, but parsed as empty if cabal-version < 2.0 (actual cut-off is ! (>= 1.25) Also availableSince is removed from `build-tool-depends`, as we **want** to parse (and not warn) it in old Cabal files. It can be thought as added retrospectively to old specs, but old `Cabal` s don't know how to use it.
1 parent 4533308 commit 454ec0d

File tree

9 files changed

+109
-4
lines changed

9 files changed

+109
-4
lines changed

Cabal/Distribution/CabalSpecVersion.hs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ class CabalSpecVersion v where
1818
specHasCommonStanzas :: v -> HasCommonStanzas
1919

2020
data CabalSpecOld = CabalSpecOld
21+
data CabalSpecV20 = CabalSpecV20
2122
data CabalSpecV22 = CabalSpecV22
2223

2324
instance CabalSpecVersion CabalSpecOld where
2425
cabalSpecVersion = CabalSpecOld
2526
specParsec _ = parsec
27+
specKnows _ vs = vs < [1,25]
28+
specHasElif _ = NoElif
29+
specHasCommonStanzas _ = NoCommonStanzas
30+
31+
instance CabalSpecVersion CabalSpecV20 where
32+
cabalSpecVersion = CabalSpecV20
33+
specParsec _ = parsec
2634
specKnows _ vs = vs < [2,1]
2735
specHasElif _ = NoElif
2836
specHasCommonStanzas _ = NoCommonStanzas

Cabal/Distribution/FieldGrammar/Class.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ class FieldGrammar g where
9090
-- | Annotate field with since spec-version.
9191
availableSince
9292
:: [Int] -- ^ spec version
93+
-> a -- ^ default value
9394
-> g s a
9495
-> g s a
9596

Cabal/Distribution/FieldGrammar/Parsec.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,20 @@ instance CabalSpecVersion v => FieldGrammar (ParsecFieldGrammar v) where
202202
trim :: String -> String
203203
trim = dropWhile isSpace . dropWhileEnd isSpace
204204

205-
-- TODO: use versionedAvailable to drop parsing if old field.
206-
availableSince _ = id
205+
availableSince vs def p@(ParsecFG names _ _)
206+
| specKnows (cabalSpecVersion :: v) vs = p
207+
| otherwise = ParsecFG mempty mempty parser'
208+
where
209+
parser' values = do
210+
let unknownFields = Map.intersection values $ Map.fromSet (const ()) names
211+
for_ (Map.toList unknownFields) $ \(name, fields) ->
212+
for_ fields $ \(MkNamelessField pos _) ->
213+
parseWarning pos PWTUnknownField $
214+
"The field " <> show name <> " is available since Cabal " ++ show vs
215+
216+
pure def
207217

218+
-- todo we know about this field
208219
deprecatedSince (_ : _) _ grammar = grammar -- pass on non-empty version
209220
deprecatedSince _ msg (ParsecFG names prefixes parser) = ParsecFG names prefixes parser'
210221
where

Cabal/Distribution/FieldGrammar/Pretty.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,5 @@ instance FieldGrammar PrettyFieldGrammar where
6666
knownField _ = pure ()
6767
deprecatedSince [] _ _ = PrettyFG (\_ -> mempty)
6868
deprecatedSince _ _ x = x
69-
availableSince _ = id
69+
availableSince _ _ = id
7070
hiddenField _ = PrettyFG (\_ -> mempty)

Cabal/Distribution/PackageDescription/FieldGrammar.hs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ libraryFieldGrammar n = Library n
126126
<$> monoidalFieldAla "exposed-modules" (alaList' VCat MQuoted) L.exposedModules
127127
<*> monoidalFieldAla "reexported-modules" (alaList CommaVCat) L.reexportedModules
128128
<*> monoidalFieldAla "signatures" (alaList' VCat MQuoted) L.signatures
129+
^^^ availableSince [2,0] []
129130
<*> booleanFieldDef "exposed" L.libExposed True
130131
<*> blurFieldGrammar L.libBuildInfo buildInfoFieldGrammar
131132
{-# SPECIALIZE libraryFieldGrammar :: Maybe UnqualComponentName -> ParsecFieldGrammar' CabalSpecOld Library #-}
@@ -368,7 +369,11 @@ buildInfoFieldGrammar = BuildInfo
368369
<*> monoidalFieldAla "build-tools" (alaList CommaFSep) L.buildTools
369370
^^^ deprecatedSince [2,0] "Please use 'build-tool-depends' field"
370371
<*> monoidalFieldAla "build-tool-depends" (alaList CommaFSep) L.buildToolDepends
371-
^^^ availableSince [2,0]
372+
-- ^^^ availableSince [2,0] []
373+
-- here, we explicitly want to recognise build-tool-depends for all Cabal files
374+
-- as otherwise cabal new-build cannot really work.
375+
--
376+
-- I.e. we don't want trigger unknown field warning
372377
<*> monoidalFieldAla "cpp-options" (alaList' NoCommaFSep Token') L.cppOptions
373378
<*> monoidalFieldAla "asm-options" (alaList' NoCommaFSep Token') L.asmOptions
374379
<*> monoidalFieldAla "cmm-options" (alaList' NoCommaFSep Token') L.cmmOptions
@@ -408,6 +413,7 @@ buildInfoFieldGrammar = BuildInfo
408413
<*> prefixedFields "x-" L.customFieldsBI
409414
<*> monoidalFieldAla "build-depends" (alaList CommaVCat) L.targetBuildDepends
410415
<*> monoidalFieldAla "mixins" (alaList CommaVCat) L.mixins
416+
^^^ availableSince [2,0] []
411417
{-# SPECIALIZE buildInfoFieldGrammar :: ParsecFieldGrammar' CabalSpecOld BuildInfo #-}
412418
{-# SPECIALIZE buildInfoFieldGrammar :: ParsecFieldGrammar' CabalSpecV22 BuildInfo #-}
413419
{-# SPECIALIZE buildInfoFieldGrammar :: PrettyFieldGrammar' BuildInfo #-}

Cabal/Distribution/PackageDescription/Parsec.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ parseGenericPackageDescription' lexWarnings fs = do
168168
let goSections'
169169
| specVersion pd >= mkVersion [2,1] =
170170
goSections (cabalSpecVersion :: CabalSpecV22)
171+
| specVersion pd >= mkVersion [1,25] =
172+
goSections (cabalSpecVersion :: CabalSpecV20)
171173
| otherwise =
172174
goSections (cabalSpecVersion :: CabalSpecOld)
173175

Cabal/tests/ParserTests.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ regressionTests = testGroup "regressions"
115115
, regressionTest "common.cabal"
116116
, regressionTest "common2.cabal"
117117
, regressionTest "leading-comma.cabal"
118+
, regressionTest "wl-pprint-indef.cabal"
118119
]
119120

120121
regressionTest :: FilePath -> TestTree
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Name: wl-pprint-indef
2+
Version: 1.2
3+
Cabal-Version: >=1.6
4+
Synopsis: The Wadler/Leijen Pretty Printer
5+
Category: Text
6+
Description:
7+
This is a pretty printing library based on Wadler's paper "A Prettier
8+
Printer". See the haddocks for full info. This version allows the
9+
library user to declare overlapping instances of the 'Pretty' class.
10+
License: BSD3
11+
License-file: LICENSE
12+
Author: Daan Leijen
13+
Maintainer: Noam Lewis <[email protected]>
14+
Build-Type: Simple
15+
16+
Executable wl-pprint-string-example
17+
Main-is: Main.hs
18+
Hs-Source-Dirs: example-string
19+
Other-Modules: StringImpl
20+
Build-Depends: base < 5,
21+
str-string >= 0.1.0.0,
22+
wl-pprint-indef
23+
Mixins: wl-pprint-indef requires (Text.PrettyPrint.Leijen.Str as StringImpl)
24+
25+
Library
26+
Exposed-Modules: Text.PrettyPrint.Leijen
27+
Signatures: Text.PrettyPrint.Leijen.Str
28+
Mixins: str-sig requires (Str as Text.PrettyPrint.Leijen.Str)
29+
Build-Depends: base < 5,
30+
str-sig >= 0.1.0.0
31+
32+
source-repository head
33+
type: git
34+
location: [email protected]:danidiaz/wl-pprint-indef.git
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
PWarning PWTUnknownField (Position 28 3) "The field \"mixins\" is available since Cabal [2,0]"
2+
PWarning PWTUnknownField (Position 27 3) "The field \"signatures\" is available since Cabal [2,0]"
3+
PWarning PWTUnknownField (Position 27 3) "Unknown field: \"signatures\""
4+
PWarning PWTUnknownField (Position 28 3) "Unknown field: \"mixins\""
5+
PWarning PWTUnknownField (Position 23 3) "The field \"mixins\" is available since Cabal [2,0]"
6+
PWarning PWTUnknownField (Position 23 3) "Unknown field: \"mixins\""
7+
name: wl-pprint-indef
8+
version: 1.2
9+
license: BSD3
10+
license-file: LICENSE
11+
maintainer: Noam Lewis <[email protected]>
12+
author: Daan Leijen
13+
synopsis: The Wadler/Leijen Pretty Printer
14+
description:
15+
This is a pretty printing library based on Wadler's paper "A Prettier
16+
Printer". See the haddocks for full info. This version allows the
17+
library user to declare overlapping instances of the 'Pretty' class.
18+
category: Text
19+
cabal-version: >=1.6
20+
build-type: Simple
21+
22+
source-repository head
23+
type: git
24+
location: [email protected]:danidiaz/wl-pprint-indef.git
25+
26+
library
27+
exposed-modules:
28+
Text.PrettyPrint.Leijen
29+
build-depends:
30+
base <5,
31+
str-sig >=0.1.0.0
32+
33+
executable wl-pprint-string-example
34+
main-is: Main.hs
35+
scope: unknown
36+
hs-source-dirs: example-string
37+
other-modules:
38+
StringImpl
39+
build-depends:
40+
base <5,
41+
str-string >=0.1.0.0,
42+
wl-pprint-indef -any

0 commit comments

Comments
 (0)