Skip to content

Commit 1950309

Browse files
Error for primary constructor in delegate definition (#13078)
Co-authored-by: Vlad Zarytovskii <[email protected]>
1 parent 5bf1199 commit 1950309

File tree

8 files changed

+68
-5
lines changed

8 files changed

+68
-5
lines changed

DEVGUIDE.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,11 @@ If your changes involve modifying the list of language keywords in any way, (e.g
128128
```shell
129129
dotnet build src\Compiler /t:UpdateXlf
130130
```
131+
If you are on a Mac, you can run this command from the root of the repository:
131132

132-
This only works on Windows/.NETStandard framework, so changing this from any other platform requires editing and syncing all of the XLF files manually.
133+
```shell
134+
sh build.sh -c Release
135+
```
133136

134137
## Updating baselines in tests
135138

src/Compiler/Checking/CheckDeclarations.fs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3985,7 +3985,13 @@ module EstablishTypeDefinitionCores =
39853985
match fields' with
39863986
| rf :: _ -> errorR (Error(FSComp.SR.tcInterfaceTypesAndDelegatesCannotContainFields(), rf.Range))
39873987
| _ -> ()
3988-
3988+
3989+
let primaryConstructorInDelegateCheck(implicitCtorSynPats : SynSimplePats option) =
3990+
match implicitCtorSynPats with
3991+
| None -> ()
3992+
| Some spats ->
3993+
let ctorArgNames, _ = TcSimplePatsOfUnknownType cenv true CheckCxs envinner tpenv spats
3994+
if not ctorArgNames.IsEmpty then errorR (Error(FSComp.SR.parsOnlyClassCanTakeValueArguments(), m))
39893995

39903996
let envinner = AddDeclaredTypars CheckForDuplicateTypars (tycon.Typars m) envinner
39913997
let envinner = MakeInnerEnvForTyconRef envinner thisTyconRef false
@@ -4182,6 +4188,7 @@ module EstablishTypeDefinitionCores =
41824188
noAllowNullLiteralAttributeCheck()
41834189
noAbstractClassAttributeCheck()
41844190
noFieldsCheck userFields
4191+
primaryConstructorInDelegateCheck(implicitCtorSynPats)
41854192
let tyR, _ = TcTypeAndRecover cenv NoNewTypars CheckCxs ItemOccurence.UseInType envinner tpenv ty
41864193
let _, _, curriedArgInfos, returnTy, _ = GetTopValTypeInCompiledForm g (arity |> TranslateSynValInfo m (TcAttributes cenv envinner) |> TranslatePartialValReprInfo []) 0 tyR m
41874194
if curriedArgInfos.Length < 1 then error(Error(FSComp.SR.tcInvalidDelegateSpecification(), m))
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information.
2+
3+
namespace FSharp.Compiler.ComponentTests.Conformance.DelegateTypes
4+
5+
open Xunit
6+
open FSharp.Test.Compiler
7+
8+
module DelegateDefinition =
9+
10+
[<Fact>]
11+
let ``Delegate definition with primary constructor and argument.`` () =
12+
FSharp
13+
"""
14+
namespace FSharpTest
15+
type T(x: int) =
16+
delegate of int -> int
17+
"""
18+
|> compile
19+
|> shouldFail
20+
|> withErrorCode 552
21+
|> withErrorMessage "Only class types may take value arguments"
22+
23+
[<Fact>]
24+
let ``Delegate definition with primary constructor no argument.`` () =
25+
FSharp
26+
"""
27+
namespace FSharpTest
28+
type T() =
29+
delegate of int -> int
30+
"""
31+
|> compile
32+
|> shouldFail
33+
|> withErrorCode 552
34+
|> withErrorMessage "Only class types may take value arguments"
35+
36+
[<Fact>]
37+
let ``Delegate definition`` () =
38+
FSharp
39+
"""
40+
namespace FSharpTest
41+
type T =
42+
delegate of int -> int
43+
"""
44+
|> compile
45+
|> shouldSucceed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type T(x: int) =
2+
delegate of int -> int
3+
4+
type T() =
5+
delegate of int -> int
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
invalid_delegate_definition.fs (1,6)-(1,15) Only class types may take value arguments
2+
invalid_delegate_definition.fs (4,6)-(1,9) Only class types may take value arguments
3+

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<Compile Include="Conformance\ClassTypes\ExplicitObjectConstructors\ExplicitObjectConstructors.fs" />
3333
<Compile Include="Conformance\ClassTypes\ImplicitObjectConstructors\ImplicitObjectConstructors.fs" />
3434
<Compile Include="Conformance\ClassTypes\ValueRestriction\ValueRestriction.fs" />
35+
<Compile Include="Conformance\DelegateTypes\DelegateDefinition.fs" />
3536
<Compile Include="Conformance\DeclarationElements\AccessibilityAnnotations\Basic\Basic.fs" />
3637
<Compile Include="Conformance\DeclarationElements\AccessibilityAnnotations\OnOverridesAndIFaceImpl\OnOverridesAndIFaceImpl.fs" />
3738
<Compile Include="Conformance\DeclarationElements\AccessibilityAnnotations\OnTypeMembers\OnTypeMembers.fs" />

tests/fsharp/typecheck/sigs/neg06.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type BadSealedInterface =
2424
type BadSealedAbbreviatedType = System.Object
2525

2626
[<Sealed>]
27-
type UnnecessarilySealedDelegate() = delegate of int -> int
27+
type UnnecessarilySealedDelegate = delegate of int -> int
2828

2929
type BadExtensionOfSealedType() =
3030
class
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
SOURCE=ByrefArguments01.fs # ByrefArguments01.fs
22
SOURCE=E_InvalidSignature01.fs # E_InvalidSignature01.fs
33
SOURCE=E_InvalidSignature02.fs # E_InvalidSignature02.fs
4-
54
SOURCE=ValidSignature_MultiArg01.fs # ValidSignature_MultiArg01.fs
65
SOURCE=ValidSignature_ReturningValues01.fs # ValidSignature_ReturningValues01.fs
76

87
# This test has a dependency on NetFx3.5 (i.e. CSC_PIPE must be 3.5 or better)
98
# For this reason, we exclude it from MT
10-
NoMT SOURCE=DelegateBindingInvoke01.fs PRECMD="\$CSC_PIPE /t:library IDelegateBinding.cs" SCFLAGS="-r:IDelegateBinding.dll" # DelegateBindingInvoke01.fs
9+
NoMT SOURCE=DelegateBindingInvoke01.fs PRECMD="\$CSC_PIPE /t:library IDelegateBinding.cs" SCFLAGS="-r:IDelegateBinding.dll" # DelegateBindingInvoke01.fs

0 commit comments

Comments
 (0)