From 247d130a98de369409acf56454c5daec6b9dea0f Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Sun, 27 Aug 2023 11:26:05 +0200 Subject: [PATCH 1/6] Fix regression for FS0725 --- src/Compiler/Checking/CheckPatterns.fs | 26 +- .../UnionCasePatternMatchingErrors.fs | 274 +++++++++++++++++- 2 files changed, 274 insertions(+), 26 deletions(-) diff --git a/src/Compiler/Checking/CheckPatterns.fs b/src/Compiler/Checking/CheckPatterns.fs index 4b40ccf1804..67b4c5d4266 100644 --- a/src/Compiler/Checking/CheckPatterns.fs +++ b/src/Compiler/Checking/CheckPatterns.fs @@ -595,18 +595,25 @@ and TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags patEnv ty (m let mkf, argTys, argNames = ApplyUnionCaseOrExn m cenv env ty item let numArgTys = argTys.Length + let warnOnUnionWithNoData = + g.langVersion.SupportsFeature(LanguageFeature.MatchNotAllowedForUnionCaseWithNoData) let args, extraPatternsFromNames = match args with | SynArgPats.Pats args -> - if g.langVersion.SupportsFeature(LanguageFeature.MatchNotAllowedForUnionCaseWithNoData) then + if warnOnUnionWithNoData then match args with - | [ SynPat.Wild _ ] | [ SynPat.Named _ ] when argNames.IsEmpty -> + | [ SynPat.Wild m ] | [ SynPat.Named(range = m) ] when argNames.IsEmpty -> + // Here we only care about the cases where the user has written: + // | Case _ -> ... + // | Case name -> ... + // let myDiscardedArgFunc(Case _) = ...""" + // let myDiscardedArgFunc(Case name) = ...""" + // This needs to be a waring because it was a valid pattern in version 7.0 and earlier and we don't want to break existing code. + // The rest of the cases will still be reported as FS0725 warning(Error(FSComp.SR.matchNotAllowedForUnionCaseWithNoData(), m)) - args, [] - | _ -> args, [] - else - args, [] + | _ -> () + args, [] | SynArgPats.NamePatPairs (pairs, m, _) -> // rewrite patterns from the form (name-N = pat-N; ...) to (..._, pat-N, _...) // so type T = Case of name: int * value: int @@ -664,12 +671,11 @@ and TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags patEnv ty (m | [SynPatErrorSkip(SynPat.Wild _ as e) | SynPatErrorSkip(SynPat.Paren(SynPatErrorSkip(SynPat.Wild _ as e), _))] -> List.replicate numArgTys e, [] | args when numArgTys = 0 -> - if g.langVersion.SupportsFeature(LanguageFeature.MatchNotAllowedForUnionCaseWithNoData) then - [], args - else + match args with + | [ SynPat.Named _ ] when warnOnUnionWithNoData -> [], args + | _ -> errorR (Error (FSComp.SR.tcUnionCaseDoesNotTakeArguments (), m)) [], args - | arg :: rest when numArgTys = 1 -> if numArgTys = 1 && not (List.isEmpty rest) then errorR (Error (FSComp.SR.tcUnionCaseRequiresOneArgument (), m)) diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs index c6671c81893..e4ec45ee0e3 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs @@ -85,9 +85,8 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 9, Col 7, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") - - + |> withSingleDiagnostic (Warning 3548, Line 9, Col 9, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") + [] let ``Union Pattern discard allowed for union case that takes no data with Lang version 7`` () = FSharp """ @@ -132,7 +131,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 9, Col 7, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 9, Col 9, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern discard not allowed for union case that takes no data with Lang preview`` () = @@ -153,7 +152,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 12, Col 7, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 12, Col 9, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern function discard not allowed for union case that takes no data with Lang preview`` () = @@ -174,7 +173,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 12, Col 7, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 12, Col 9, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern discard allowed for union case that takes no data with Lang version 7`` () = @@ -215,7 +214,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 12, Col 7, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 12, Col 9, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Multiple pattern discards not allowed for union case that takes no data with Lang preview`` () = @@ -241,8 +240,8 @@ let myVal = |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 3548, Line 16, Col 7, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 17, Col 20, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 16, Col 9, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 17, Col 22, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") ] [] @@ -269,8 +268,8 @@ let myVal = |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 3548, Line 16, Col 7, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 17, Col 20, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 16, Col 9, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 17, Col 22, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") ] [] @@ -283,6 +282,18 @@ let myDiscardedArgFunc(A _) = 5+5""" |> withLangVersion70 |> typecheck |> shouldSucceed + +[] +let ``Pattern discard not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang preview`` () = + FSharp """ +module Tests +type MyWrapper = A + +let myDiscardedArgFunc(A _) = 5+5""" + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Warning 3548, Line 5, Col 26, Line 5, Col 27, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern named not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang 7`` () = @@ -294,9 +305,54 @@ let myFunc(A a) = 5+5""" |> withLangVersion70 |> typecheck |> shouldFail - |> withDiagnostics [ - (Error 725, Line 5, Col 12, Line 5, Col 15, "This union case does not take arguments") - ] + |> withSingleDiagnostic (Error 725, Line 5, Col 12, Line 5, Col 15, "This union case does not take arguments") + +[] +let ``Pattern named not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang preview`` () = + FSharp """ +module Tests +type MyWrapper = A + +let myFunc(A a) = 5+5""" + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Warning 3548, Line 5, Col 14, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") + +[] +let ``Pattern named not allowed for unions cases with no data and using them as a deconstruct syntax in functions Lang 7`` () = + FSharp """ +module Tests + let myDiscardedArgFunc(None x y) = None""" + |> withLangVersion70 + |> withOptions ["--nowarn:25"] + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 3, Col 28, Line 3, Col 36, "This union case does not take arguments") + +[] +let ``Pattern named not allowed for unions cases with no data and using them as a deconstruct syntax in functions Lang preview`` () = + FSharp """ +module Tests + let myDiscardedArgFunc(None x y) = None""" + |> withLangVersionPreview + |> withOptions ["--nowarn:25"] + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 3, Col 28, Line 3, Col 36, "This union case does not take arguments") + +[] +let ``Pattern discard or named are not allowed for single-case union case that takes no data with Lang 7`` () = + FSharp """ +module Tests +type MyWrapper = A + +let myFunc(A a) = 5+5 +let myDiscardedArgFunc(A _) = 5+5""" + |> withLangVersion70 + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 5, Col 12, Line 5, Col 15, "This union case does not take arguments") [] let ``Pattern discard or named are not allowed for single-case union case that takes no data with Lang preview`` () = @@ -310,6 +366,192 @@ let myDiscardedArgFunc(A _) = 5+5""" |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 3548, Line 5, Col 12, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 6, Col 24, Line 6, Col 27, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 5, Col 14, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 6, Col 26, Line 6, Col 27, "Pattern discard is not allowed for union case that takes no data.") + ] + +[] +let ``Pattern discard not allowed union case does not take discarded arguments with Lang 7`` () = + FSharp """ +module Tests + type A = | A + let a = A + match a with + | A _ _ -> ()""" + |> withLangVersion70 + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 6, Col 7, Line 6, Col 12, "This union case does not take arguments") + +[] +let ``Pattern discard not allowed union case does not take discarded arguments with Lang preview`` () = + FSharp """ +module Tests + type A = | A + let a = A + match a with + | A _ _ -> ()""" + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 6, Col 7, Line 6, Col 12, "This union case does not take arguments") + +[] +let ``Pattern named not allowed union case does not take arguments with Lang 7`` () = + FSharp """ +module Tests + match None with + | None x y -> () + | Some _ -> ()""" + |> withLangVersion70 + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 15, "This union case does not take arguments") + +[] +let ``Pattern named not allowed union case does not take arguments with Lang preview`` () = + FSharp """ +module Tests + match None with + | None x y -> () + | Some _ -> ()""" + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 15, "This union case does not take arguments") + +[] +let ``Pattern named not allowed union case does not take arguments wrapped with parens with Lang preview`` () = + FSharp """ +module Tests + match None with + | None (x, y) -> () + | Some _ -> ()""" + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 18, "This union case does not take arguments") + +[] +let ``Pattern named not allowed union case does not take arguments wrapped with parens with Lang 7`` () = + FSharp """ +module Tests + match None with + | None (x, y) -> () + | Some _ -> ()""" + |> withLangVersion70 + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 18, "This union case does not take arguments") + +[] +let ``Pattern named not allowed union case does not take any arguments with Lang 7`` () = + FSharp """ +module Tests + type A = { X: int } + type B = B of int + match None with + | None 1 -> () + + match None with + | None (1, 2) -> () + + match None with + | None [] -> () + + match None with + | None [||] -> () + + match None with + | None { X = 1 } -> () + + match None with + | None (B 1) -> () + + match None with + | None (x, y) -> () + + match None with + | None false -> () + + match None with + | None _ -> () + + type C = | C + + let myDiscardedArgFunc(C _) = () + + let myDiscardedArgFunc2(C c) = () + """ + |> withLangVersion70 + |> withOptions ["--nowarn:25"] + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 725, Line 6, Col 7, Line 6, Col 13, "This union case does not take arguments") + (Error 725, Line 9, Col 7, Line 9, Col 18, "This union case does not take arguments") + (Error 725, Line 12, Col 7, Line 12, Col 14, "This union case does not take arguments") + (Error 725, Line 15, Col 7, Line 15, Col 16, "This union case does not take arguments") + (Error 725, Line 18, Col 7, Line 18, Col 21, "This union case does not take arguments") + (Error 725, Line 21, Col 7, Line 21, Col 17, "This union case does not take arguments") + (Error 725, Line 24, Col 7, Line 24, Col 18, "This union case does not take arguments") + (Error 725, Line 27, Col 7, Line 27, Col 17, "This union case does not take arguments") + (Error 725, Line 36, Col 29, Line 36, Col 32, "This union case does not take arguments") + ] + +[] +let ``Pattern named not allowed union case does not take any arguments with Lang preview`` () = + FSharp """ +module Tests + type A = { X: int } + type B = | B of int + match None with + | None 1 -> () + + match None with + | None (1, 2) -> () + + match None with + | None [] -> () + + match None with + | None [||] -> () + + match None with + | None { X = 1 } -> () + + match None with + | None (B 1) -> () + + match None with + | None (x, y) -> () + + match None with + | None false -> () + + match None with + | None _ -> () + + type C = | C + + let myDiscardedArgFunc(C _) = () + + let myDiscardedArgFunc2(C c) = () + """ + |> withLangVersionPreview + |> withOptions ["--nowarn:25"] + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Error 725, Line 6, Col 7, Line 6, Col 13, "This union case does not take arguments") + (Error 725, Line 9, Col 7, Line 9, Col 18, "This union case does not take arguments") + (Error 725, Line 12, Col 7, Line 12, Col 14, "This union case does not take arguments") + (Error 725, Line 15, Col 7, Line 15, Col 16, "This union case does not take arguments") + (Error 725, Line 18, Col 7, Line 18, Col 21, "This union case does not take arguments") + (Error 725, Line 21, Col 7, Line 21, Col 17, "This union case does not take arguments") + (Error 725, Line 24, Col 7, Line 24, Col 18, "This union case does not take arguments") + (Error 725, Line 27, Col 7, Line 27, Col 17, "This union case does not take arguments") + (Warning 3548, Line 30, Col 12, Line 30, Col 13, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 34, Col 30, Line 34, Col 31, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 36, Col 31, Line 36, Col 32, "Pattern discard is not allowed for union case that takes no data.") ] \ No newline at end of file From 49bfdce86aa997e06a3552b2268fdcde06ec5118 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Sun, 27 Aug 2023 14:59:01 +0200 Subject: [PATCH 2/6] more tests --- .../ErrorMessages/UnionCasePatternMatchingErrors.fs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs index e4ec45ee0e3..23ecf1358c9 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs @@ -482,6 +482,9 @@ module Tests let myDiscardedArgFunc(C _) = () let myDiscardedArgFunc2(C c) = () + + match None with + | None x -> () """ |> withLangVersion70 |> withOptions ["--nowarn:25"] @@ -497,6 +500,7 @@ module Tests (Error 725, Line 24, Col 7, Line 24, Col 18, "This union case does not take arguments") (Error 725, Line 27, Col 7, Line 27, Col 17, "This union case does not take arguments") (Error 725, Line 36, Col 29, Line 36, Col 32, "This union case does not take arguments") + (Error 725, Line 39, Col 7, Line 39, Col 13, "This union case does not take arguments") ] [] @@ -537,6 +541,9 @@ module Tests let myDiscardedArgFunc(C _) = () let myDiscardedArgFunc2(C c) = () + + match None with + | None x -> () """ |> withLangVersionPreview |> withOptions ["--nowarn:25"] @@ -554,4 +561,5 @@ module Tests (Warning 3548, Line 30, Col 12, Line 30, Col 13, "Pattern discard is not allowed for union case that takes no data.") (Warning 3548, Line 34, Col 30, Line 34, Col 31, "Pattern discard is not allowed for union case that takes no data.") (Warning 3548, Line 36, Col 31, Line 36, Col 32, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 39, Col 12, Line 39, Col 13, "Pattern discard is not allowed for union case that takes no data.") ] \ No newline at end of file From 5a448218455038b33ed78c71ed955a9bda8539d1 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Sun, 27 Aug 2023 18:29:54 +0200 Subject: [PATCH 3/6] more tests and fix ranges --- src/Compiler/Checking/CheckPatterns.fs | 2 +- .../UnionCasePatternMatchingErrors.fs | 84 +++++++++++++++---- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/src/Compiler/Checking/CheckPatterns.fs b/src/Compiler/Checking/CheckPatterns.fs index 67b4c5d4266..e326268b940 100644 --- a/src/Compiler/Checking/CheckPatterns.fs +++ b/src/Compiler/Checking/CheckPatterns.fs @@ -603,7 +603,7 @@ and TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags patEnv ty (m | SynArgPats.Pats args -> if warnOnUnionWithNoData then match args with - | [ SynPat.Wild m ] | [ SynPat.Named(range = m) ] when argNames.IsEmpty -> + | [ SynPat.Wild _ ] | [ SynPat.Named _ ] when argNames.IsEmpty -> // Here we only care about the cases where the user has written: // | Case _ -> ... // | Case name -> ... diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs index 23ecf1358c9..9ffcc39bdbc 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs @@ -85,7 +85,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 9, Col 9, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 9, Col 7, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Union Pattern discard allowed for union case that takes no data with Lang version 7`` () = @@ -131,7 +131,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 9, Col 9, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 9, Col 7, Line 9, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern discard not allowed for union case that takes no data with Lang preview`` () = @@ -152,7 +152,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 12, Col 9, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 12, Col 7, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern function discard not allowed for union case that takes no data with Lang preview`` () = @@ -173,7 +173,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 12, Col 9, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 12, Col 7, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern discard allowed for union case that takes no data with Lang version 7`` () = @@ -214,7 +214,7 @@ let myVal = |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 12, Col 9, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 12, Col 7, Line 12, Col 10, "Pattern discard is not allowed for union case that takes no data.") [] let ``Multiple pattern discards not allowed for union case that takes no data with Lang preview`` () = @@ -240,8 +240,8 @@ let myVal = |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 3548, Line 16, Col 9, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 17, Col 22, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 16, Col 7, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 17, Col 20, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") ] [] @@ -268,8 +268,8 @@ let myVal = |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 3548, Line 16, Col 9, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 17, Col 22, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 16, Col 7, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 17, Col 20, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") ] [] @@ -283,6 +283,30 @@ let myDiscardedArgFunc(A _) = 5+5""" |> typecheck |> shouldSucceed +[] +let ``Pattern discard 2 allowed for single-case unions when using them as a deconstruct syntax in functions with Lang 7`` () = + FSharp """ +module Tests +type MyWrapper = A + +let myDiscardedArgFunc(A __) = 5+5""" + |> withLangVersion70 + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 5, Col 24, Line 5, Col 28, "This union case does not take arguments") + +[] +let ``Pattern discard 2 allowed for single-case unions when using them as a deconstruct syntax in functions with Lang preview`` () = + FSharp """ +module Tests +type MyWrapper = A + +let myDiscardedArgFunc(A __) = 5+5""" + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Warning 3548, Line 5, Col 24, Line 5, Col 28, "Pattern discard is not allowed for union case that takes no data.") + [] let ``Pattern discard not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang preview`` () = FSharp """ @@ -293,7 +317,7 @@ let myDiscardedArgFunc(A _) = 5+5""" |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 5, Col 26, Line 5, Col 27, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 5, Col 24, Line 5, Col 27, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern named not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang 7`` () = @@ -317,7 +341,7 @@ let myFunc(A a) = 5+5""" |> withLangVersionPreview |> typecheck |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 5, Col 14, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") + |> withSingleDiagnostic (Warning 3548, Line 5, Col 12, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") [] let ``Pattern named not allowed for unions cases with no data and using them as a deconstruct syntax in functions Lang 7`` () = @@ -366,10 +390,36 @@ let myDiscardedArgFunc(A _) = 5+5""" |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 3548, Line 5, Col 14, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 6, Col 26, Line 6, Col 27, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 5, Col 12, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 6, Col 24, Line 6, Col 27, "Pattern discard is not allowed for union case that takes no data.") ] +[] +let ``Pattern discard not allowed union case does not take discarded 2 arguments with Lang 7`` () = + FSharp """ +module Tests + type A = | A + let a = A + match a with + | A __ -> ()""" + |> withLangVersion70 + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Error 725, Line 6, Col 7, Line 6, Col 11, "This union case does not take arguments") + +[] +let ``Pattern discard not allowed union case does not take discarded 2 arguments with Lang preview`` () = + FSharp """ +module Tests + type A = | A + let a = A + match a with + | A __ -> ()""" + |> withLangVersionPreview + |> typecheck + |> shouldFail + |> withSingleDiagnostic (Warning 3548, Line 6, Col 7, Line 6, Col 11, "Pattern discard is not allowed for union case that takes no data.") + [] let ``Pattern discard not allowed union case does not take discarded arguments with Lang 7`` () = FSharp """ @@ -558,8 +608,8 @@ module Tests (Error 725, Line 21, Col 7, Line 21, Col 17, "This union case does not take arguments") (Error 725, Line 24, Col 7, Line 24, Col 18, "This union case does not take arguments") (Error 725, Line 27, Col 7, Line 27, Col 17, "This union case does not take arguments") - (Warning 3548, Line 30, Col 12, Line 30, Col 13, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 34, Col 30, Line 34, Col 31, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 36, Col 31, Line 36, Col 32, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 39, Col 12, Line 39, Col 13, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 30, Col 7, Line 30, Col 13, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 34, Col 28, Line 34, Col 31, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 36, Col 29, Line 36, Col 32, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 39, Col 7, Line 39, Col 13, "Pattern discard is not allowed for union case that takes no data.") ] \ No newline at end of file From 58508ccee026f54b769610890c03f59a7b490ae5 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 28 Aug 2023 11:00:42 +0200 Subject: [PATCH 4/6] Improve testing --- .../E_UnionCaseTakesNoArguments.fs | 59 +++ .../UnionCasePatternMatchingErrors.fs | 390 ++++-------------- 2 files changed, 136 insertions(+), 313 deletions(-) create mode 100644 tests/FSharp.Compiler.ComponentTests/ErrorMessages/E_UnionCaseTakesNoArguments.fs diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/E_UnionCaseTakesNoArguments.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/E_UnionCaseTakesNoArguments.fs new file mode 100644 index 00000000000..d918dddde03 --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/E_UnionCaseTakesNoArguments.fs @@ -0,0 +1,59 @@ +type A = { X: int } + +type B = | B of int + +type C = | C + +match None with +| None 1 -> () + +match None with +| None (1, 2) -> () + +match None with +| None [] -> () + +match None with +| None [||] -> () + +match None with +| None { X = 1 } -> () + +match None with +| None (B 1) -> () + +match None with +| None (x, y) -> () + +match None with +| None false -> () + +match None with +| None _ -> () // Wildcard pattern raises a warning in F# 8.0 + +match None with +| None x -> () + +match None with +| None (x, y) -> () +| Some _ -> () + +match None with +| None x y -> () +| Some _ -> () + +let c = C + +match c with +| C _ _ -> () + +match c with +| C __ -> () + +let myDiscardedArgFunc(C _) = () // Wildcard pattern raises a warning in F# 8.0 + +let myDiscardedArgFunc2(C c) = () + +let myDiscardedArgFunc3(C __) = 5+5 + +let myDiscardedArgFunc(None x y) = None \ No newline at end of file diff --git a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs index 9ffcc39bdbc..9c259417acb 100644 --- a/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs +++ b/tests/FSharp.Compiler.ComponentTests/ErrorMessages/UnionCasePatternMatchingErrors.fs @@ -1,5 +1,6 @@ module ErrorMessages.UnionCasePatternMatchingErrors +open FSharp.Test open Xunit open FSharp.Test.Compiler @@ -245,7 +246,7 @@ let myVal = ] [] -let ``Multiple function pattern discards is not allowed for union case that takes no data with Lang preview`` () = +let ``Multiple pattern discards not allowed for union case that takes no data with Lang 7`` () = FSharp """ module Tests type U = @@ -260,356 +261,119 @@ let a : U = A let d : V = D let myVal = - function + match a, d with | A _, D -> 15 | B (x, _, _), D _ -> 16 | C _, _ -> 17""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withDiagnostics [ - (Warning 3548, Line 16, Col 7, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 17, Col 20, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") - ] - -[] -let ``Pattern discard allowed for single-case unions when using them as a deconstruct syntax in functions with Lang 7`` () = - FSharp """ -module Tests -type MyWrapper = A - -let myDiscardedArgFunc(A _) = 5+5""" |> withLangVersion70 |> typecheck |> shouldSucceed [] -let ``Pattern discard 2 allowed for single-case unions when using them as a deconstruct syntax in functions with Lang 7`` () = - FSharp """ -module Tests -type MyWrapper = A - -let myDiscardedArgFunc(A __) = 5+5""" - |> withLangVersion70 - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 5, Col 24, Line 5, Col 28, "This union case does not take arguments") - -[] -let ``Pattern discard 2 allowed for single-case unions when using them as a deconstruct syntax in functions with Lang preview`` () = - FSharp """ -module Tests -type MyWrapper = A - -let myDiscardedArgFunc(A __) = 5+5""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 5, Col 24, Line 5, Col 28, "Pattern discard is not allowed for union case that takes no data.") - -[] -let ``Pattern discard not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang preview`` () = - FSharp """ -module Tests -type MyWrapper = A - -let myDiscardedArgFunc(A _) = 5+5""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 5, Col 24, Line 5, Col 27, "Pattern discard is not allowed for union case that takes no data.") - -[] -let ``Pattern named not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang 7`` () = +let ``Multiple function pattern discards is not allowed for union case that takes no data with Lang preview`` () = FSharp """ module Tests -type MyWrapper = A - -let myFunc(A a) = 5+5""" - |> withLangVersion70 - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 5, Col 12, Line 5, Col 15, "This union case does not take arguments") +type U = + | A + | B of int * int * int + | C of int * int * int -[] -let ``Pattern named not allowed for single-case unions when using them as a deconstruct syntax in functions with Lang preview`` () = - FSharp """ -module Tests -type MyWrapper = A - -let myFunc(A a) = 5+5""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 5, Col 12, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") +type V = + | D -[] -let ``Pattern named not allowed for unions cases with no data and using them as a deconstruct syntax in functions Lang 7`` () = - FSharp """ -module Tests - let myDiscardedArgFunc(None x y) = None""" - |> withLangVersion70 - |> withOptions ["--nowarn:25"] - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 3, Col 28, Line 3, Col 36, "This union case does not take arguments") - -[] -let ``Pattern named not allowed for unions cases with no data and using them as a deconstruct syntax in functions Lang preview`` () = - FSharp """ -module Tests - let myDiscardedArgFunc(None x y) = None""" - |> withLangVersionPreview - |> withOptions ["--nowarn:25"] - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 3, Col 28, Line 3, Col 36, "This union case does not take arguments") +let a : U = A +let d : V = D -[] -let ``Pattern discard or named are not allowed for single-case union case that takes no data with Lang 7`` () = - FSharp """ -module Tests -type MyWrapper = A - -let myFunc(A a) = 5+5 -let myDiscardedArgFunc(A _) = 5+5""" - |> withLangVersion70 - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 5, Col 12, Line 5, Col 15, "This union case does not take arguments") - -[] -let ``Pattern discard or named are not allowed for single-case union case that takes no data with Lang preview`` () = - FSharp """ -module Tests -type MyWrapper = A - -let myFunc(A a) = 5+5 -let myDiscardedArgFunc(A _) = 5+5""" +let myVal = + function + | A _, D -> 15 + | B (x, _, _), D _ -> 16 + | C _, _ -> 17""" |> withLangVersionPreview |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 3548, Line 5, Col 12, Line 5, Col 15, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 6, Col 24, Line 6, Col 27, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 16, Col 7, Line 16, Col 10, "Pattern discard is not allowed for union case that takes no data.") + (Warning 3548, Line 17, Col 20, Line 17, Col 23, "Pattern discard is not allowed for union case that takes no data.") ] [] -let ``Pattern discard not allowed union case does not take discarded 2 arguments with Lang 7`` () = - FSharp """ -module Tests - type A = | A - let a = A - match a with - | A __ -> ()""" - |> withLangVersion70 - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 6, Col 7, Line 6, Col 11, "This union case does not take arguments") - -[] -let ``Pattern discard not allowed union case does not take discarded 2 arguments with Lang preview`` () = - FSharp """ -module Tests - type A = | A - let a = A - match a with - | A __ -> ()""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Warning 3548, Line 6, Col 7, Line 6, Col 11, "Pattern discard is not allowed for union case that takes no data.") - -[] -let ``Pattern discard not allowed union case does not take discarded arguments with Lang 7`` () = - FSharp """ -module Tests - type A = | A - let a = A - match a with - | A _ _ -> ()""" - |> withLangVersion70 - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 6, Col 7, Line 6, Col 12, "This union case does not take arguments") - -[] -let ``Pattern discard not allowed union case does not take discarded arguments with Lang preview`` () = +let ``Multiple function pattern discards is not allowed for union case that takes no data with Lang 7`` () = FSharp """ module Tests - type A = | A - let a = A - match a with - | A _ _ -> ()""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 6, Col 7, Line 6, Col 12, "This union case does not take arguments") - -[] -let ``Pattern named not allowed union case does not take arguments with Lang 7`` () = - FSharp """ -module Tests - match None with - | None x y -> () - | Some _ -> ()""" - |> withLangVersion70 - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 15, "This union case does not take arguments") +type U = + | A + | B of int * int * int + | C of int * int * int -[] -let ``Pattern named not allowed union case does not take arguments with Lang preview`` () = - FSharp """ -module Tests - match None with - | None x y -> () - | Some _ -> ()""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 15, "This union case does not take arguments") +type V = + | D -[] -let ``Pattern named not allowed union case does not take arguments wrapped with parens with Lang preview`` () = - FSharp """ -module Tests - match None with - | None (x, y) -> () - | Some _ -> ()""" - |> withLangVersionPreview - |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 18, "This union case does not take arguments") +let a : U = A + +let d : V = D -[] -let ``Pattern named not allowed union case does not take arguments wrapped with parens with Lang 7`` () = - FSharp """ -module Tests - match None with - | None (x, y) -> () - | Some _ -> ()""" +let myVal = + function + | A _, D -> 15 + | B (x, _, _), D _ -> 16 + | C _, _ -> 17""" |> withLangVersion70 |> typecheck - |> shouldFail - |> withSingleDiagnostic (Error 725, Line 4, Col 7, Line 4, Col 18, "This union case does not take arguments") - -[] -let ``Pattern named not allowed union case does not take any arguments with Lang 7`` () = - FSharp """ -module Tests - type A = { X: int } - type B = B of int - match None with - | None 1 -> () - - match None with - | None (1, 2) -> () - - match None with - | None [] -> () - - match None with - | None [||] -> () - - match None with - | None { X = 1 } -> () - - match None with - | None (B 1) -> () - - match None with - | None (x, y) -> () - - match None with - | None false -> () - - match None with - | None _ -> () - - type C = | C - - let myDiscardedArgFunc(C _) = () - - let myDiscardedArgFunc2(C c) = () + |> shouldSucceed - match None with - | None x -> () - """ +[] +let ``Pattern named not allowed union case does not take any arguments with Lang 7`` compilation = + compilation + |> asFs |> withLangVersion70 |> withOptions ["--nowarn:25"] |> typecheck |> shouldFail |> withDiagnostics [ - (Error 725, Line 6, Col 7, Line 6, Col 13, "This union case does not take arguments") - (Error 725, Line 9, Col 7, Line 9, Col 18, "This union case does not take arguments") - (Error 725, Line 12, Col 7, Line 12, Col 14, "This union case does not take arguments") - (Error 725, Line 15, Col 7, Line 15, Col 16, "This union case does not take arguments") - (Error 725, Line 18, Col 7, Line 18, Col 21, "This union case does not take arguments") - (Error 725, Line 21, Col 7, Line 21, Col 17, "This union case does not take arguments") - (Error 725, Line 24, Col 7, Line 24, Col 18, "This union case does not take arguments") - (Error 725, Line 27, Col 7, Line 27, Col 17, "This union case does not take arguments") - (Error 725, Line 36, Col 29, Line 36, Col 32, "This union case does not take arguments") - (Error 725, Line 39, Col 7, Line 39, Col 13, "This union case does not take arguments") + (Error 725, Line 8, Col 3, Line 8, Col 9, "This union case does not take arguments"); + (Error 725, Line 11, Col 3, Line 11, Col 14, "This union case does not take arguments") + (Error 725, Line 14, Col 3, Line 14, Col 10, "This union case does not take arguments") + (Error 725, Line 17, Col 3, Line 17, Col 12, "This union case does not take arguments") + (Error 725, Line 20, Col 3, Line 20, Col 17, "This union case does not take arguments") + (Error 725, Line 23, Col 3, Line 23, Col 13, "This union case does not take arguments") + (Error 725, Line 26, Col 3, Line 26, Col 14, "This union case does not take arguments") + (Error 725, Line 29, Col 3, Line 29, Col 13, "This union case does not take arguments") + (Error 725, Line 35, Col 3, Line 35, Col 9, "This union case does not take arguments") + (Error 725, Line 38, Col 3, Line 38, Col 14, "This union case does not take arguments") + (Error 725, Line 42, Col 3, Line 42, Col 11, "This union case does not take arguments") + (Error 725, Line 48, Col 3, Line 48, Col 8, "This union case does not take arguments") + (Error 725, Line 51, Col 3, Line 51, Col 7, "This union case does not take arguments") + (Error 725, Line 55, Col 25, Line 55, Col 28, "This union case does not take arguments") + (Error 725, Line 57, Col 25, Line 57, Col 29, "This union case does not take arguments") + (Error 725, Line 59, Col 24, Line 59, Col 32, "This union case does not take arguments") ] -[] -let ``Pattern named not allowed union case does not take any arguments with Lang preview`` () = - FSharp """ -module Tests - type A = { X: int } - type B = | B of int - match None with - | None 1 -> () - - match None with - | None (1, 2) -> () - - match None with - | None [] -> () - - match None with - | None [||] -> () - - match None with - | None { X = 1 } -> () - - match None with - | None (B 1) -> () - - match None with - | None (x, y) -> () - - match None with - | None false -> () - - match None with - | None _ -> () - - type C = | C - - let myDiscardedArgFunc(C _) = () - - let myDiscardedArgFunc2(C c) = () - - match None with - | None x -> () - """ +[] +let ``Pattern named not allowed union case does not take any arguments with Lang preview`` compilation = + compilation + |> asFs |> withLangVersionPreview |> withOptions ["--nowarn:25"] |> typecheck |> shouldFail |> withDiagnostics [ - (Error 725, Line 6, Col 7, Line 6, Col 13, "This union case does not take arguments") - (Error 725, Line 9, Col 7, Line 9, Col 18, "This union case does not take arguments") - (Error 725, Line 12, Col 7, Line 12, Col 14, "This union case does not take arguments") - (Error 725, Line 15, Col 7, Line 15, Col 16, "This union case does not take arguments") - (Error 725, Line 18, Col 7, Line 18, Col 21, "This union case does not take arguments") - (Error 725, Line 21, Col 7, Line 21, Col 17, "This union case does not take arguments") - (Error 725, Line 24, Col 7, Line 24, Col 18, "This union case does not take arguments") - (Error 725, Line 27, Col 7, Line 27, Col 17, "This union case does not take arguments") - (Warning 3548, Line 30, Col 7, Line 30, Col 13, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 34, Col 28, Line 34, Col 31, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 36, Col 29, Line 36, Col 32, "Pattern discard is not allowed for union case that takes no data.") - (Warning 3548, Line 39, Col 7, Line 39, Col 13, "Pattern discard is not allowed for union case that takes no data.") + (Error 725, Line 8, Col 3, Line 8, Col 9, "This union case does not take arguments") + (Error 725, Line 11, Col 3, Line 11, Col 14, "This union case does not take arguments") + (Error 725, Line 14, Col 3, Line 14, Col 10, "This union case does not take arguments") + (Error 725, Line 17, Col 3, Line 17, Col 12, "This union case does not take arguments") + (Error 725, Line 20, Col 3, Line 20, Col 17, "This union case does not take arguments") + (Error 725, Line 23, Col 3, Line 23, Col 13, "This union case does not take arguments") + (Error 725, Line 26, Col 3, Line 26, Col 14, "This union case does not take arguments") + (Error 725, Line 29, Col 3, Line 29, Col 13, "This union case does not take arguments") + (Warning 3548, Line 32, Col 3, Line 32, Col 9, "Pattern discard is not allowed for union case that takes no data.") + (Error 725, Line 35, Col 3, Line 35, Col 9, "This union case does not take arguments") + (Error 725, Line 38, Col 3, Line 38, Col 14, "This union case does not take arguments") + (Error 725, Line 42, Col 3, Line 42, Col 11, "This union case does not take arguments") + (Error 725, Line 48, Col 3, Line 48, Col 8, "This union case does not take arguments") + (Error 725, Line 51, Col 3, Line 51, Col 7, "This union case does not take arguments") + (Warning 3548, Line 53, Col 24, Line 53, Col 27, "Pattern discard is not allowed for union case that takes no data.") + (Error 725, Line 55, Col 25, Line 55, Col 28, "This union case does not take arguments") + (Error 725, Line 57, Col 25, Line 57, Col 29, "This union case does not take arguments") + (Error 725, Line 59, Col 24, Line 59, Col 32, "This union case does not take arguments") ] \ No newline at end of file From db35143ff6e3d475bae09c5ff581e89fe99411de Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 28 Aug 2023 11:08:55 +0200 Subject: [PATCH 5/6] Only warn for the wirdcard, not for named or others --- src/Compiler/Checking/CheckPatterns.fs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Compiler/Checking/CheckPatterns.fs b/src/Compiler/Checking/CheckPatterns.fs index e326268b940..252066f8681 100644 --- a/src/Compiler/Checking/CheckPatterns.fs +++ b/src/Compiler/Checking/CheckPatterns.fs @@ -603,12 +603,10 @@ and TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags patEnv ty (m | SynArgPats.Pats args -> if warnOnUnionWithNoData then match args with - | [ SynPat.Wild _ ] | [ SynPat.Named _ ] when argNames.IsEmpty -> - // Here we only care about the cases where the user has written: + | [ SynPat.Wild _ ] when argNames.IsEmpty -> + // Here we only care about the cases where the user has written the wildcard pattern explicitly // | Case _ -> ... - // | Case name -> ... // let myDiscardedArgFunc(Case _) = ...""" - // let myDiscardedArgFunc(Case name) = ...""" // This needs to be a waring because it was a valid pattern in version 7.0 and earlier and we don't want to break existing code. // The rest of the cases will still be reported as FS0725 warning(Error(FSComp.SR.matchNotAllowedForUnionCaseWithNoData(), m)) @@ -671,11 +669,8 @@ and TcPatLongIdentUnionCaseOrExnCase warnOnUpper cenv env ad vFlags patEnv ty (m | [SynPatErrorSkip(SynPat.Wild _ as e) | SynPatErrorSkip(SynPat.Paren(SynPatErrorSkip(SynPat.Wild _ as e), _))] -> List.replicate numArgTys e, [] | args when numArgTys = 0 -> - match args with - | [ SynPat.Named _ ] when warnOnUnionWithNoData -> [], args - | _ -> - errorR (Error (FSComp.SR.tcUnionCaseDoesNotTakeArguments (), m)) - [], args + errorR (Error (FSComp.SR.tcUnionCaseDoesNotTakeArguments (), m)) + [], args | arg :: rest when numArgTys = 1 -> if numArgTys = 1 && not (List.isEmpty rest) then errorR (Error (FSComp.SR.tcUnionCaseRequiresOneArgument (), m)) From c6e2ac12656a3e4d2562e5ab5c40dffe82cc2164 Mon Sep 17 00:00:00 2001 From: Edgar Gonzalez Date: Mon, 28 Aug 2023 11:25:29 +0200 Subject: [PATCH 6/6] Update quick fixe --- ...luousCaptureForUnionCaseWithNoDataTests.fs | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/RemoveSuperfluousCaptureForUnionCaseWithNoDataTests.fs b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/RemoveSuperfluousCaptureForUnionCaseWithNoDataTests.fs index 6ea555d1aac..1fb2ce2cc3f 100644 --- a/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/RemoveSuperfluousCaptureForUnionCaseWithNoDataTests.fs +++ b/vsintegration/tests/FSharp.Editor.Tests/CodeFixes/RemoveSuperfluousCaptureForUnionCaseWithNoDataTests.fs @@ -10,18 +10,15 @@ open CodeFixTestFramework let private codeFix = RemoveSuperfluousCaptureForUnionCaseWithNoDataCodeFixProvider() -[] -[] -[] -[] -let ``Fixes FS3548 - DUs`` caseValue = +[] +let ``Fixes FS3548 - DUs`` () = let code = - $""" + """ type Type = | A | B of int let f x = match x with - | A {caseValue} -> 42 + | A _ -> 42 | B number -> number """ @@ -44,18 +41,40 @@ let f x = Assert.Equal(expected, actual) -[] -[] -[] -[] -let ``Fixes FS3548 - marker types`` caseValue = +[] +let ``Fixes FS3548 - discarded argument in function`` () = let code = - $""" + """ +type C = | C + +let myDiscardedArgFunc(C _) = () +""" + + let expected = + Some + { + Message = "Remove unused binding" + FixedCode = + """ +type C = | C + +let myDiscardedArgFunc(C) = () +""" + } + + let actual = codeFix |> tryFix code (WithOption "--langversion:preview") + + Assert.Equal(expected, actual) + +[] +let ``Fixes FS3548 - marker types`` () = + let code = + """ type Type = Type let f x = match x with - | Type {caseValue} -> () + | Type _ -> () """ let expected =