Skip to content

Commit e434841

Browse files
authored
Fix: warn FS0049 on upper union case labels (#19003)
1 parent 0677d60 commit e434841

File tree

8 files changed

+128
-38
lines changed

8 files changed

+128
-38
lines changed

docs/release-notes/.FSharp.Compiler.Service/11.0.0.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* Fix excessive StackGuard thread jumping ([PR #18971](https://github.com/dotnet/fsharp/pull/18971))
55
* Checking: Fix checking nested fields for records and anonymous ([PR #18964](https://github.com/dotnet/fsharp/pull/18964))
66
* Fix name is bound multiple times is not reported in 'as' pattern ([PR #18984](https://github.com/dotnet/fsharp/pull/18984))
7-
* Type relations cache: handle potentially "infinite" types ([PR #19010](https://github.com/dotnet/fsharp/pull/19010))
7+
* Fix: warn FS0049 on upper union case label. ([PR #19003](https://github.com/dotnet/fsharp/pull/19003))
8+
* Type relations cache: handle potentially "infinite" types ([PR #19010](https://github.com/dotnet/fsharp/pull/19010))
89

910
### Added
1011

src/Compiler/Checking/CheckPatterns.fs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,18 @@ and TcPatNamedAs warnOnUpper cenv env valReprInfo vFlags patEnv ty synInnerPat i
401401
phase2, acc
402402

403403
and TcPatUnnamedAs warnOnUpper cenv env vFlags patEnv ty pat1 pat2 m =
404-
let pats = [pat1; pat2]
405-
let warnOnUpper =
404+
// Type-check pat1 with the original warnOnUpper flag (to warn on uppercase identifiers)
405+
let pat1R, patEnv1 = TcPat warnOnUpper cenv env None vFlags patEnv ty pat1
406+
407+
// For pat2 (the binding variable like UppercaseIdentifier as Foo), suppress uppercase warnings if the feature is enabled
408+
let warnOnUpperForPat2 =
406409
if cenv.g.langVersion.SupportsFeature(LanguageFeature.DontWarnOnUppercaseIdentifiersInBindingPatterns) then
407410
AllIdsOK
408411
else
409412
warnOnUpper
410-
411-
let patsR, patEnvR = TcPatterns warnOnUpper cenv env vFlags patEnv (List.map (fun _ -> ty) pats) pats
412-
let phase2 values = TPat_conjs(List.map (fun f -> f values) patsR, m)
413+
414+
let pat2R, patEnvR = TcPat warnOnUpperForPat2 cenv env None vFlags patEnv1 ty pat2
415+
let phase2 values = TPat_conjs([pat1R values; pat2R values], m)
413416
phase2, patEnvR
414417

415418
and TcPatNamed warnOnUpper cenv env vFlags patEnv id ty isMemberThis vis valReprInfo m =

src/Compiler/SyntaxTree/SyntaxTree.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ type SynMatchClause =
10571057

10581058
member this.IsTrueMatchClause =
10591059
let (SynMatchClause(trivia = trivia)) = this
1060-
trivia.BarRange.IsSome && trivia.ArrowRange.IsSome
1060+
trivia.ArrowRange.IsSome
10611061

10621062
member this.Range =
10631063
match this with

tests/FSharp.Compiler.ComponentTests/Conformance/Expressions/BindingExpressions/BindingExpressions.fs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,4 +198,10 @@ module BindingExpressions =
198198
|> asExe
199199
|> withOptions ["--test:ErrorRanges"]
200200
|> typecheck
201-
|> shouldSucceed
201+
|> shouldFail
202+
|> withDiagnostics [
203+
(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.");
204+
(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.");
205+
(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.");
206+
(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.")
207+
]

tests/FSharp.Compiler.ComponentTests/Conformance/PatternMatching/Union/Union.fs

Lines changed: 77 additions & 30 deletions
Large diffs are not rendered by default.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ let customerId = CustomerId("123")
3434
match customerId with
3535
| CustomerId BBB -> ()
3636

37+
match 42 with | UpperCase -> ()
38+
39+
match 42 with UpperCase -> ()
40+
3741
type Record = { Name: string; Age: int }
3842

3943
match { Name = "Alice"; Age = 30 } with
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
let test1 x =
3+
match x with
4+
| UndefinedCase -> 1
5+
6+
let test2 x =
7+
match x with UndefinedCase -> 1
8+
9+
let test3 = function | UndefinedCase -> 1
10+
11+
let test4 = function UndefinedCase -> 1
12+
13+
let test5 () =
14+
try failwith "test"
15+
with | UndefinedException -> 1
16+
17+
let test6 () =
18+
try failwith "test"
19+
with UndefinedException -> 1
20+
21+
let test7 () =
22+
try failwith "test"
23+
with UndefinedException as Foo -> 1
24+
25+
let test8 () =
26+
try failwith "test"
27+
with | UndefinedException as Foo -> 1

tests/fsharp/typecheck/sigs/neg07.bsl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
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.
2+
13
neg07.fs(24,13,24,23): typecheck error FS0039: The value or constructor 'UnionCase1' is not defined. Maybe you want one of the following:
24
X.UnionCase1
35

0 commit comments

Comments
 (0)