Skip to content

Commit

Permalink
Allow intrinsic extensions for static members in interfaces (dotnet#1…
Browse files Browse the repository at this point in the history
…6157)

Allow intrinsic extensions for static members in interfaces
  • Loading branch information
gusty authored and auduchinok committed Oct 23, 2023
1 parent 1378088 commit 69cb2fb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Compiler/Checking/CheckDeclarations.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4069,6 +4069,10 @@ module TcDeclarations =
tcref.Deref.IsFSharpDelegateTycon ||
tcref.Deref.IsFSharpEnumTycon

let isDelegateOrEnum =
tcref.Deref.IsFSharpDelegateTycon ||
tcref.Deref.IsFSharpEnumTycon

let reqTypars = tcref.Typars m

// Member definitions are intrinsic (added directly to the type) if:
Expand Down Expand Up @@ -4104,7 +4108,7 @@ module TcDeclarations =
// Note we return 'reqTypars' for intrinsic extensions since we may only have given warnings
IntrinsicExtensionBinding, reqTypars
else
if isInSameModuleOrNamespace && isInterfaceOrDelegateOrEnum then
if isInSameModuleOrNamespace && isDelegateOrEnum then
errorR(Error(FSComp.SR.tcMembersThatExtendInterfaceMustBePlacedInSeparateModule(), tcref.Range))
if nReqTypars <> synTypars.Length then
error(Error(FSComp.SR.tcDeclaredTypeParametersForExtensionDoNotMatchOriginal(tcref.DisplayNameWithStaticParametersAndUnderscoreTypars), m))
Expand Down
18 changes: 18 additions & 0 deletions tests/FSharp.Compiler.ComponentTests/Language/InterfaceTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ type I<'T> =
(Error 3350, Line 4, Col 19, Line 4, Col 23, "Feature 'Static members in interfaces' is not available in F# 7.0. Please use language version 8.0 or greater.")
]

[<Fact>]
let ``Concrete static members are allowed in interfaces as intrinsics in lang preview``() =
FSharp $"""
[<Interface>]
type I<'T> =
static member Prop = Unchecked.defaultof<'T>
type I<'T> with
static member Echo (x: 'T) = x
if I<int>.Echo 42 <> 42 || I<int>.Prop <> 0 || not (isNull I<string>.Prop) then
failwith "failed"
"""
|> withLangVersion80
|> asExe
|> compileAndRun
|> shouldSucceed


[<Fact>]
let ``Interface with concrete static members can be implemented in lang preview``() =
FSharp $"""
Expand Down

0 comments on commit 69cb2fb

Please sign in to comment.