diff --git a/docs/release-notes/.FSharp.Compiler.Service/11.0.0.md b/docs/release-notes/.FSharp.Compiler.Service/11.0.0.md index 41acf4e54ce..5543558087c 100644 --- a/docs/release-notes/.FSharp.Compiler.Service/11.0.0.md +++ b/docs/release-notes/.FSharp.Compiler.Service/11.0.0.md @@ -4,7 +4,8 @@ * Fix excessive StackGuard thread jumping ([PR #18971](https://github.com/dotnet/fsharp/pull/18971)) * Checking: Fix checking nested fields for records and anonymous ([PR #18964](https://github.com/dotnet/fsharp/pull/18964)) * Fix name is bound multiple times is not reported in 'as' pattern ([PR #18984](https://github.com/dotnet/fsharp/pull/18984)) -* Type relations cache: handle potentially "infinite" types ([PR #19010](https://github.com/dotnet/fsharp/pull/19010)) +* Fix: warn FS0049 on upper union case label. ([PR #19003](https://github.com/dotnet/fsharp/pull/19003)) +* Type relations cache: handle potentially "infinite" types ([PR #19010](https://github.com/dotnet/fsharp/pull/19010)) ### Added diff --git a/src/Compiler/Checking/CheckPatterns.fs b/src/Compiler/Checking/CheckPatterns.fs index eb61edf7eeb..1b456438f62 100644 --- a/src/Compiler/Checking/CheckPatterns.fs +++ b/src/Compiler/Checking/CheckPatterns.fs @@ -401,15 +401,18 @@ and TcPatNamedAs warnOnUpper cenv env valReprInfo vFlags patEnv ty synInnerPat i phase2, acc and TcPatUnnamedAs warnOnUpper cenv env vFlags patEnv ty pat1 pat2 m = - let pats = [pat1; pat2] - let warnOnUpper = + // Type-check pat1 with the original warnOnUpper flag (to warn on uppercase identifiers) + let pat1R, patEnv1 = TcPat warnOnUpper cenv env None vFlags patEnv ty pat1 + + // For pat2 (the binding variable like UppercaseIdentifier as Foo), suppress uppercase warnings if the feature is enabled + let warnOnUpperForPat2 = if cenv.g.langVersion.SupportsFeature(LanguageFeature.DontWarnOnUppercaseIdentifiersInBindingPatterns) then AllIdsOK else warnOnUpper - - let patsR, patEnvR = TcPatterns warnOnUpper cenv env vFlags patEnv (List.map (fun _ -> ty) pats) pats - let phase2 values = TPat_conjs(List.map (fun f -> f values) patsR, m) + + let pat2R, patEnvR = TcPat warnOnUpperForPat2 cenv env None vFlags patEnv1 ty pat2 + let phase2 values = TPat_conjs([pat1R values; pat2R values], m) phase2, patEnvR and TcPatNamed warnOnUpper cenv env vFlags patEnv id ty isMemberThis vis valReprInfo m = diff --git a/src/Compiler/SyntaxTree/SyntaxTree.fs b/src/Compiler/SyntaxTree/SyntaxTree.fs index c41e483e27a..c41f2704c5f 100644 --- a/src/Compiler/SyntaxTree/SyntaxTree.fs +++ b/src/Compiler/SyntaxTree/SyntaxTree.fs @@ -1057,7 +1057,7 @@ type SynMatchClause = member this.IsTrueMatchClause = let (SynMatchClause(trivia = trivia)) = this - trivia.BarRange.IsSome && trivia.ArrowRange.IsSome + trivia.ArrowRange.IsSome member this.Range = match this with diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/BindingExpressions/BindingExpressions.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/BindingExpressions/BindingExpressions.fs index 017e5b8a61f..f5324a634f6 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/BindingExpressions/BindingExpressions.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/BindingExpressions/BindingExpressions.fs @@ -198,4 +198,10 @@ module BindingExpressions = |> asExe |> withOptions ["--test:ErrorRanges"] |> typecheck - |> shouldSucceed + |> shouldFail + |> withDiagnostics [ + (Warning 49, Line 61, Col 6, Line 61, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 122, Col 6, Line 122, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 125, Col 6, Line 125, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 128, Col 6, Line 128, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + ] diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/Union.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/Union.fs index 6aec715af30..18117c0e7f6 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/Union.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/Union.fs @@ -211,45 +211,92 @@ but here has type (Warning 26, Line 8, Col 7, Line 8, Col 55, "This rule will never be matched") ] - [] - let ``Union - UpperUnionCasePattern_fs - --test:ErrorRanges`` compilation = + [] + let ``Union - UpperUnionCasePattern01_fs LangVersion90 - --test:ErrorRanges`` compilation = compilation |> asFs + |> withLangVersion90 |> withOptions ["--test:ErrorRanges"; "--nowarn:026"] |> typecheck |> shouldFail - |> withDiagnostics - [(Warning 49, Line 3, Col 7, Line 3, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 4, Col 7, Line 4, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 5, Col 7, Line 5, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 9, Col 7, Line 9, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 10, Col 7, Line 10, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 11, Col 7, Line 11, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 15, Col 7, Line 15, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 16, Col 7, Line 16, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 20, Col 7, Line 20, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 24, Col 3, Line 24, Col 6, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 28, Col 3, Line 28, Col 5, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); - (Warning 49, Line 35, Col 14, Line 35, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.")] + |> withDiagnostics [ + (Warning 49, Line 15, Col 7, Line 15, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 16, Col 7, Line 16, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 20, Col 7, Line 20, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 24, Col 3, Line 24, Col 6, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 35, Col 14, Line 35, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 37, Col 17, Line 37, Col 26, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 39, Col 15, Line 39, Col 24, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 45, Col 12, Line 45, Col 15, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 49, Col 20, Line 49, Col 23, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 54, Col 14, Line 54, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 54, Col 21, Line 54, Col 24, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 55, Col 14, Line 55, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 56, Col 14, Line 56, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + ] + + [] + let ``Union - UpperUnionCasePattern01_fs preview - --test:ErrorRanges`` compilation = + compilation + |> asFs + |> withOptions ["--test:ErrorRanges"; "--nowarn:026"] + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Warning 49, Line 3, Col 7, Line 3, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 4, Col 7, Line 4, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 5, Col 7, Line 5, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 9, Col 7, Line 9, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 10, Col 7, Line 10, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 11, Col 7, Line 11, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 15, Col 7, Line 15, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 16, Col 7, Line 16, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 20, Col 7, Line 20, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 24, Col 3, Line 24, Col 6, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 28, Col 3, Line 28, Col 5, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 35, Col 14, Line 35, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 37, Col 17, Line 37, Col 26, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 39, Col 15, Line 39, Col 24, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 54, Col 14, Line 54, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 55, Col 14, Line 55, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name."); + (Warning 49, Line 56, Col 14, Line 56, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + ] - [] - let ``Union - UpperUnionCasePattern_fs preview - --test:ErrorRanges`` compilation = + [] + let ``Union - UpperUnionCasePattern02_fs - --test:ErrorRanges`` compilation = compilation + |> withLangVersion90 |> asFs |> withOptions ["--test:ErrorRanges"; "--nowarn:026"] |> typecheck |> shouldFail |> withDiagnostics [ - (Warning 49, Line 3, Col 7, Line 3, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 4, Col 7, Line 4, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 5, Col 7, Line 5, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 9, Col 7, Line 9, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 10, Col 7, Line 10, Col 9, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 11, Col 7, Line 11, Col 8, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 15, Col 7, Line 15, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 16, Col 7, Line 16, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 20, Col 7, Line 20, Col 10, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 24, Col 3, Line 24, Col 6, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 28, Col 3, Line 28, Col 5, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - (Warning 49, Line 35, Col 14, Line 35, Col 17, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") - ] \ No newline at end of file + (Warning 49, Line 4, Col 7, Line 4, Col 20, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 7, Col 18, Line 7, Col 31, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 9, Col 24, Line 9, Col 37, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 11, Col 22, Line 11, Col 35, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 15, Col 12, Line 15, Col 30, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 19, Col 10, Line 19, Col 28, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 23, Col 10, Line 23, Col 28, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 23, Col 32, Line 23, Col 35, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 27, Col 12, Line 27, Col 30, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 27, Col 34, Line 27, Col 37, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + ] + + [] + let ``Union - UpperUnionCasePattern02_fs preview - --test:ErrorRanges`` compilation = + compilation + |> asFs + |> withOptions ["--test:ErrorRanges"; "--nowarn:026"] + |> typecheck + |> shouldFail + |> withDiagnostics [ + (Warning 49, Line 4, Col 7, Line 4, Col 20, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 7, Col 18, Line 7, Col 31, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 9, Col 24, Line 9, Col 37, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 11, Col 22, Line 11, Col 35, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 15, Col 12, Line 15, Col 30, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 19, Col 10, Line 19, Col 28, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 23, Col 10, Line 23, Col 28, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + (Warning 49, Line 27, Col 12, Line 27, Col 30, "Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name.") + ] diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern01.fs similarity index 93% rename from tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern.fs rename to tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern01.fs index 9d9715f87bf..15c5827a2bf 100644 --- a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern.fs +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern01.fs @@ -34,6 +34,10 @@ let customerId = CustomerId("123") match customerId with | CustomerId BBB -> () +match 42 with | UpperCase -> () + +match 42 with UpperCase -> () + type Record = { Name: string; Age: int } match { Name = "Alice"; Age = 30 } with diff --git a/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern02.fs b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern02.fs new file mode 100644 index 00000000000..c04bae9035f --- /dev/null +++ b/tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/UpperUnionCasePattern02.fs @@ -0,0 +1,27 @@ + +let test1 x = + match x with + | UndefinedCase -> 1 + +let test2 x = + match x with UndefinedCase -> 1 + +let test3 = function | UndefinedCase -> 1 + +let test4 = function UndefinedCase -> 1 + +let test5 () = + try failwith "test" + with | UndefinedException -> 1 + +let test6 () = + try failwith "test" + with UndefinedException -> 1 + +let test7 () = + try failwith "test" + with UndefinedException as Foo -> 1 + +let test8 () = + try failwith "test" + with | UndefinedException as Foo -> 1 \ No newline at end of file diff --git a/tests/fsharp/typecheck/sigs/neg07.bsl b/tests/fsharp/typecheck/sigs/neg07.bsl index 185b6393cfb..fe30f0548eb 100644 --- a/tests/fsharp/typecheck/sigs/neg07.bsl +++ b/tests/fsharp/typecheck/sigs/neg07.bsl @@ -1,3 +1,5 @@ +neg07.fs(7,10,7,29): typecheck error FS0049: Uppercase variable identifiers should not generally be used in patterns, and may indicate a missing open declaration or a misspelt pattern name. + neg07.fs(24,13,24,23): typecheck error FS0039: The value or constructor 'UnionCase1' is not defined. Maybe you want one of the following: X.UnionCase1