Skip to content

Commit aefec6b

Browse files
authored
Reject non-function bindings for single-case and partial active pattern names (dotnet#19763)
1 parent 7e8f7bb commit aefec6b

5 files changed

Lines changed: 25 additions & 14 deletions

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Fixed
22

3+
* Reject non-function bindings for single-case and partial active pattern names with FS1209, matching the existing multi-case behavior. ([PR #19763](https://github.com/dotnet/fsharp/pull/19763))
34
* Fix FS0421 "The address of the variable cannot be used at this point" incorrectly raised for the discard pattern `let _ = &expr` when `let x = &expr` compiles. ([Issue #18841](https://github.com/dotnet/fsharp/issues/18841), [PR #19811](https://github.com/dotnet/fsharp/pull/19811))
45
* Honor `--nowarn` and `--warnaserror` for warnings emitted during command-line option parsing ([Issue #19576](https://github.com/dotnet/fsharp/issues/19576), [PR #19776](https://github.com/dotnet/fsharp/pull/19776))
56
* Fix `[<return: X>]` prefix attributes being silently dropped on class members, and fix false-positive `AllowMultiple=false` errors when `[<X>]` and `[<return: X>]` are applied to the same binding. ([Issue #17904](https://github.com/dotnet/fsharp/issues/17904), [Issue #19020](https://github.com/dotnet/fsharp/issues/19020), [PR #19738](https://github.com/dotnet/fsharp/pull/19738))

src/Compiler/Checking/PostInferenceChecks.fs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2089,10 +2089,12 @@ and CheckBinding cenv env alwaysCheckNoReraise ctxt (TBind(v, bindRhs, _) as bin
20892089

20902090
let env = { env with external = env.external || ValHasWellKnownAttribute g WellKnownValAttributes.DllImportAttribute v }
20912091

2092-
// Check that active patterns don't have free type variables in their result
2092+
// Check active pattern shape/type constraints
20932093
match TryGetActivePatternInfo vref with
2094-
| Some _apinfo when _apinfo.ActiveTags.Length > 1 ->
2095-
if doesActivePatternHaveFreeTypars g vref then
2094+
| Some apinfo ->
2095+
let hasFreeTypars = doesActivePatternHaveFreeTypars g vref
2096+
2097+
if apinfo.ActiveTags.Length > 1 && hasFreeTypars then
20962098
errorR(Error(FSComp.SR.activePatternChoiceHasFreeTypars(v.LogicalName), v.Range))
20972099
| _ -> ()
20982100

tests/FSharp.Compiler.ComponentTests/Conformance/LexicalFiltering/OffsideExceptions/RelaxWhitespace2.fs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ module ActivePatterns =
237237
|) _ = (|
238238
A
239239
|)
240-
let (|C|) =
240+
let (|C|) _ =
241241
if true then ignore (|
242242
A
243243
|)
@@ -255,7 +255,7 @@ module ActivePatterns =
255255
F
256256
|
257257
_
258-
|) as f = Some (|
258+
|) _ = Some (|
259259
C
260260
|)
261261
let (|
@@ -1265,7 +1265,7 @@ type ActivePatterns() =
12651265
|) = (|
12661266
A
12671267
|)
1268-
let (|C|) =
1268+
let (|C|) _ =
12691269
if true then ignore (|
12701270
A
12711271
|)
@@ -1283,7 +1283,7 @@ type ActivePatterns() =
12831283
F
12841284
|
12851285
_
1286-
|) as f = Some (|
1286+
|) _ = Some (|
12871287
C
12881288
|)
12891289
let (|
@@ -1305,7 +1305,7 @@ type ActivePatterns() =
13051305
|) = (|
13061306
A_
13071307
|)
1308-
static let (|C_|) =
1308+
static let (|C_|) _ =
13091309
if true then ignore (|
13101310
A_
13111311
|)
@@ -1323,7 +1323,7 @@ type ActivePatterns() =
13231323
F_
13241324
|
13251325
_
1326-
|) as f_ = Some (|
1326+
|) _ = Some (|
13271327
C_
13281328
|)
13291329
static let (|
@@ -2593,7 +2593,7 @@ let ActivePatterns<'a> =
25932593
|) _ = (|
25942594
A
25952595
|)
2596-
let (|C|) =
2596+
let (|C|) _ =
25972597
if true then ignore (|
25982598
A
25992599
|)
@@ -2611,7 +2611,7 @@ let ActivePatterns<'a> =
26112611
F
26122612
|
26132613
_
2614-
|) as f = Some (|
2614+
|) _ = Some (|
26152615
C
26162616
|)
26172617
let (|
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// #Regression #Conformance #PatternMatching #ActivePatterns
22
// Regression test for FSHARP1.0:5590
3-
//<Expects status="error" span="(5,6-5,11)" id="FS1209">Active pattern '|A|B|' is not a function$</Expects>
3+
//<Expects status="error" span="(7,6-7,11)" id="FS1209">Active pattern '|A|B|' is not a function$</Expects>
4+
//<Expects status="error" span="(8,6-8,9)" id="FS1209">Active pattern '|C|' is not a function$</Expects>
5+
//<Expects status="error" span="(9,6-9,11)" id="FS1209">Active pattern '|D|_|' is not a function$</Expects>
46

57
let (|A|B|) = failwith "" : Choice<int,int>
8+
let (|C|) = 3
9+
let (|D|_|) = None

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ module Named =
184184
|> withOptions ["--test:ErrorRanges"]
185185
|> typecheck
186186
|> shouldFail
187-
|> withSingleDiagnostic (Error 1209, Line 5, Col 6, Line 5, Col 11, "Active pattern '|A|B|' is not a function")
187+
|> withDiagnostics [
188+
(Error 1209, Line 7, Col 6, Line 7, Col 11, "Active pattern '|A|B|' is not a function")
189+
(Error 1209, Line 8, Col 6, Line 8, Col 9, "Active pattern '|C|' is not a function")
190+
(Error 1209, Line 9, Col 6, Line 9, Col 11, "Active pattern '|D|_|' is not a function")
191+
]
188192

189193
// This test was automatically generated (moved from FSharpQA suite - Conformance/PatternMatching/Named)
190194
[<Theory; FileInlineData("E_ActivePatterns01.fs")>]
@@ -621,4 +625,4 @@ but here has type
621625
|> asFs
622626
|> withOptions ["--test:ErrorRanges"]
623627
|> typecheck
624-
|> shouldSucceed
628+
|> shouldSucceed

0 commit comments

Comments
 (0)