Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Compiler/Checking/NameResolution.fs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ let ActivePatternElemsOfValRef g (vref: ValRef) =
false
else
let _, apReturnTy = stripFunTy g vref.TauType
isStructTy g apReturnTy
let hasStructAttribute() =
vref.Attribs
|> List.exists (function
| Attrib(targetsOpt = Some(System.AttributeTargets.ReturnValue)) as a -> IsMatchingFSharpAttribute g g.attrib_StructAttribute a
| _ -> false)
isStructTy g apReturnTy || hasStructAttribute()
apinfo.ActiveTags |> List.mapi (fun i _ -> APElemRef(apinfo, vref, i, isStructRetTy))
| None -> []

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.

module FSharp.Compiler.ComponentTests.Conformance.StructActivePatterns

open Xunit
open FSharp.Test
open FSharp.Test.Compiler


[<Fact>]
let ``Struct active pattern is possible`` () =
Fs """
[<return: Struct>]
let rec (|IsOne|_|) someNumber =
match someNumber with
| 1 -> ValueSome 1
| _ -> ValueNone
"""
|> withOptions ["--warnaserror+"]
|> typecheck
|> shouldSucceed

[<Fact>]
let ``Struct active pattern must not lie about its return value when using Struct attribute`` () =
Fs """
[<return: Struct>]
let rec (|IsOne|_|) someNumber =
match someNumber with
| 1 -> Some 1
| _ -> None
"""
|> withOptions ["--warnaserror+"]
|> typecheck
|> shouldFail
|> withSingleDiagnostic (Error 1,Line 2, Col 1 , Line 3, Col 31, """This expression was expected to have type
''a voption'
but here has type
'int option' """)

[<Fact>]
let ``Voption active pattern fails if not using return:Struct attribute`` () =
Fs """
let rec (|IsOne|_|) someNumber =
match someNumber with
| 1 -> ValueSome 1
| _ -> ValueNone
"""
|> withOptions ["--warnaserror+"]
|> typecheck
|> shouldFail
|> withSingleDiagnostic (Error 1,Line 2, Col 9 , Line 2, Col 31, """This expression was expected to have type
''a option'
but here has type
'int voption' """)

[<Fact>]
let ``Rec struct active pattern is possible`` () =
Fs """
[<return: Struct>]
let rec (|HasOne|_|) xs =
match xs with
| [] -> ValueNone
| h::_ when h = 1 -> ValueSome true
| _::tail ->
match tail with
| HasOne x -> ValueSome x
| _ -> ValueNone
"""
|> withOptions ["--warnaserror+"]
|> typecheck
|> shouldSucceed
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
<Compile Include="Conformance\RecordTypes\AnonymousRecords.fs" />
<Compile Include="Conformance\RecordTypes\RecordTypes.fs" />
<Compile Include="Conformance\StructTypes\StructTypes.fs" />
<Compile Include="Conformance\StructTypes\StructActivePatterns.fs" />
<Compile Include="Conformance\TypesAndTypeConstraints\CheckingSyntacticTypes\CheckingSyntacticTypes.fs" />
<Compile Include="Conformance\TypesAndTypeConstraints\LogicalPropertiesOfTypes\LogicalPropertiesOfTypes.fs" />
<Compile Include="Conformance\TypesAndTypeConstraints\IWSAMsAndSRTPs\IWSAMsAndSRTPsTests.fs" />
Expand Down