Skip to content

Commit

Permalink
Enable LangVersion on 'Add missing case for underscore in for _ feature
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinRansom committed Jun 3, 2019
1 parent 522fc42 commit 218ca54
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 18 deletions.
5 changes: 5 additions & 0 deletions FSharpBuild.Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<Import Project="eng\targets\NGenBinaries.targets" />
<Import Project="FSharp.Profiles.props" />

<!-- LanguageVersion -->
<PropertyGroup>
<DefineConstants Condition="'$(LangVer)' != '' and '$(LangVer)' >= '4.7'">$(DefineConstants);LANGVERSION_ATLEAST_4_7</DefineConstants>
</PropertyGroup>

<Target Name="CopyAndSubstituteTextFiles"
Inputs="@(CopyAndSubstituteText)"
Outputs="@(CopyAndSubstituteText->'$(IntermediateOutputPath)%(Filename)%(Extension)')"
Expand Down
6 changes: 2 additions & 4 deletions src/fsharp/LanguageFeatures.fs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type LanguageFeature =
| LanguageVersion46 = 0
| LanguageVersion47 = 1
| SingleUnderscorePattern = 2
| Nullness = 1000
| ScriptingPackageManagement = 1001
| WildCardInForLoop = 3

/// LanguageVersion management
type LanguageVersion (specifiedVersion) =
Expand All @@ -44,9 +43,8 @@ type LanguageVersion (specifiedVersion) =
// Add new LanguageVersions here ...
LanguageFeature.LanguageVersion47, 4.7m
LanguageFeature.LanguageVersion46, 4.6m
LanguageFeature.Nullness, previewVersion
LanguageFeature.ScriptingPackageManagement, previewVersion
LanguageFeature.SingleUnderscorePattern, previewVersion
LanguageFeature.WildCardInForLoop, previewVersion

// Add new LanguageFeatures here ...
|]
Expand Down
3 changes: 1 addition & 2 deletions src/fsharp/LanguageFeatures.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ type LanguageFeature =
| LanguageVersion46 = 0
| LanguageVersion47 = 1
| SingleUnderscorePattern = 2
| Nullness = 1000
| ScriptingPackageManagement = 1001
| WildCardInForLoop = 3

/// LanguageVersion management
type LanguageVersion =
Expand Down
9 changes: 5 additions & 4 deletions src/fsharp/pars.fsy
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,11 @@ let mkDefnBindings (mWhole,BindingSetPreAttrs(_,isRec,isUse,declsPreAttrs,_bindi
let attrDecls = if not (isNil freeAttrs) then [ SynModuleDecl.Attributes (freeAttrs,attrsm) ] else []
attrDecls @ letDecls

let idOfPat m p =
let idOfPat (parseState:IParseState) m p =
match p with
| SynPat.Wild r -> mkSynId r "_"
| SynPat.Named (SynPat.Wild _,id,false,_,_) -> id
| SynPat.Wild r when parseState.LexBuffer.SupportsFeature LanguageFeature.WildCardInForLoop ->
mkSynId r "_"
| SynPat.Named (SynPat.Wild _,id,false,_,_) -> id
| SynPat.LongIdent(LongIdentWithDots([id],_),_,None, SynConstructorArgs.Pats [], None,_) -> id
| _ -> raiseParseErrorAt m (FSComp.SR.parsIntegerForLoopRequiresSimpleIdentifier())

Expand Down Expand Up @@ -3992,7 +3993,7 @@ forLoopBinder:

forLoopRange:
| parenPattern EQUALS declExpr forLoopDirection declExpr
{ idOfPat (rhs parseState 1) $1,$3,$4,$5 }
{ idOfPat parseState (rhs parseState 1) $1,$3,$4,$5 }

| parenPattern EQUALS rangeSequenceExpr
{ raiseParseErrorAt (rhs parseState 2) (FSComp.SR.parsUnexpectedSymbolEqualsInsteadOfIn()) }
Expand Down
12 changes: 5 additions & 7 deletions tests/fsharp/core/forexpression/test.fsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#if TESTS_AS_APP
module Core_forexpression
module Core_forexpression_47
#endif

let failures = ref false
Expand Down Expand Up @@ -110,20 +110,20 @@ let sumOverRange () =
let sumOverString () =
let mutable sum = 0
for i in testString do
#if NETCOREAPP
sum <- sum + ((int (i :?> char)) - (int '0'))
#else
sum <- sum + ((int i) - (int '0'))
#endif
sum

#if LANGVERSION_ATLEAST_4_7
// usingWildcard counts using a wildcard in a for loop
let usingWildcard () =
let mutable sum = 0
for _ = 0 to count do
sum <- sum + 1
sum

do test "wildCard" (expectedWildCard = usingWildcard () )
#endif

let arraySum = sumOverArray ()
let seqSum = sumOverSeq ()
let dictionarySum = sumOverDictionary ()
Expand All @@ -132,7 +132,6 @@ let listSum = sumOverList ()
let ilistSum = sumOverIList ()
let rangeSum = sumOverRange ()
let stringSum = sumOverString ()
let wildCard = usingWildcard ()

do test "arraySum" (expectedArraySum = arraySum )
do test "seqSum" (expectedArraySum = seqSum )
Expand All @@ -142,7 +141,6 @@ do test "listSum" (expectedArraySum = listSum )
do test "ilistSum" (expectedArraySum = ilistSum )
do test "rangeSum" (expectedRangeSum = rangeSum )
do test "stringSum" (expectedStringSum = stringSum )
do test "wildCard" (expectedWildCard = wildCard )

#if TESTS_AS_APP
let RUN() = !failures
Expand Down
41 changes: 41 additions & 0 deletions tests/fsharp/core/forexpression/version46/test.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//<Expects id="FS0525" status="error" span="(21,9)">An integer for loop must use a simple identifier</Expects>
module Global

let failures = ref []

let report_failure (s : string) =
stderr.Write" NO: "
stderr.WriteLine s
failures := !failures @ [s]

let test (s : string) b =
stderr.Write(s)
if b then stderr.WriteLine " OK"
else report_failure (s)

let check s b1 b2 = test s (b1 = b2)

// usingWildcard counts using a wildcard in a for loop
let usingWildcard () =
let mutable sum = 0
for _ = 0 to count do
sum <- sum + 1

printfn "usingWildcards expected to produce sum of 4 : sum='%d'"sum

do test "wildCard" (4 = usingWildcard () )

#if TESTS_AS_APP
let RUN() = !failures
#else
let aa =
match !failures with
| [] ->
stdout.WriteLine "Test Passed"
System.IO.File.WriteAllText("test.ok","ok")
exit 0
| _ ->
stdout.WriteLine "Test Failed"
exit 1
#endif

43 changes: 43 additions & 0 deletions tests/fsharp/core/forexpression/version47/test.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//<Expects status="success">usingWildcards expected to produce sum of 4 : sum='4'</Expects>
#if TESTS_AS_APP
module Core_forexpression_47
#endif

let failures = ref []

let report_failure (s : string) =
stderr.Write" NO: "
stderr.WriteLine s
failures := !failures @ [s]

let test (s : string) b =
stderr.Write(s)
if b then stderr.WriteLine " OK"
else report_failure (s)

let check s b1 b2 = test s (b1 = b2)

// usingWildcard counts using a wildcard in a for loop
let usingWildcard () =
let mutable sum = 0
for _ = 0 to count do
sum <- sum + 1

printfn "usingWildcards expected to produce sum of 4 : sum='%d'"sum

do test "wildCard" (4 = usingWildcard () )

#if TESTS_AS_APP
let RUN() = !failures
#else
let aa =
match !failures with
| [] ->
stdout.WriteLine "Test Passed"
System.IO.File.WriteAllText("test.ok","ok")
exit 0
| _ ->
stdout.WriteLine "Test Failed"
exit 1
#endif

2 changes: 1 addition & 1 deletion tests/fsharp/single-test.fs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ let singleTestBuildAndRunCore cfg copyFiles p languageVersion =
result <- true
finally
if result <> false then
Directory.Delete(directory, true)
try Directory.Delete(directory, true) with _ -> ()
else
printfn "Configuration: %s" cfg.Directory
printfn "Directory: %s" directory
Expand Down

0 comments on commit 218ca54

Please sign in to comment.