-
Notifications
You must be signed in to change notification settings - Fork 874
Fix enum constraints in recursive groups and improve constraint diagnostics (#14580) #20454
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
edgarfgp
wants to merge
12
commits into
dotnet:main
Choose a base branch
from
edgarfgp:fix/14580-enum-constraint-diagnostics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
62abe6a
Report a proper error for a bare 'enum' constraint (#14580)
edgarfgp 18ec629
Add PR number to release notes
edgarfgp 4ab88c7
Merge branch 'main' into fix/14580-enum-constraint-diagnostics
edgarfgp 5539ed4
Solve enum constraints once the representations of a recursive group …
edgarfgp e18218f
Merge branch 'fix/14580-enum-constraint-diagnostics' of github.com:ed…
edgarfgp 3ee74dd
Trim comments
edgarfgp 1f47b0c
Keep the xml docs in the signature file only
edgarfgp b9a12e5
Merge branch 'main' into fix/14580-enum-constraint-diagnostics
edgarfgp fd47a71
Merge branch 'main' into fix/14580-enum-constraint-diagnostics
edgarfgp b0bde80
Merge branch 'main' into fix/14580-enum-constraint-diagnostics
edgarfgp 4247fb7
Merge branch 'main' into fix/14580-enum-constraint-diagnostics
edgarfgp 9daa923
Merge branch 'main' into fix/14580-enum-constraint-diagnostics
edgarfgp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
tests/FSharp.Compiler.ComponentTests/Conformance/Constraints/ConstraintSyntax.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| namespace Conformance.Constraints | ||
|
|
||
| open Xunit | ||
| open FSharp.Test.Compiler | ||
|
|
||
| module ConstraintSyntax = | ||
|
|
||
| // https://github.com/dotnet/fsharp/issues/14580 | ||
| [<Fact>] | ||
| let ``Bare 'enum' constraint reports the enum constraint form error`` () = | ||
| Fsx """ | ||
| type I<'T when 'T : enum> = interface end | ||
| """ | ||
| |> withOptions ["--test:ErrorRanges"] | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Error 699, Line 2, Col 21, Line 2, Col 25, "An 'enum' constraint must be of the form 'enum<type>'") | ||
|
|
||
| [<Fact>] | ||
| let ``Unknown identifier constraint reports the identifier without internal markers`` () = | ||
| Fsx """ | ||
| type I<'T when 'T : notAConstraint> = interface end | ||
| """ | ||
| |> withOptions ["--test:ErrorRanges"] | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Error 571, Line 2, Col 21, Line 2, Col 35, "Unexpected identifier: 'notAConstraint'") | ||
|
|
||
| [<Fact>] | ||
| let ``Unknown identifier constraint with type arguments reports the identifier without internal markers`` () = | ||
| Fsx """ | ||
| type I<'T when 'T : notAConstraint<int>> = interface end | ||
| """ | ||
| |> withOptions ["--test:ErrorRanges"] | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Error 571, Line 2, Col 21, Line 2, Col 35, "Unexpected identifier: 'notAConstraint'") | ||
|
|
||
| [<Fact>] | ||
| let ``Unknown identifier before 'null' constraint reports the identifier without internal markers`` () = | ||
| Fsx """ | ||
| type I<'T when 'T : maybe null> = interface end | ||
| """ | ||
| |> withOptions ["--test:ErrorRanges"] | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Error 571, Line 2, Col 21, Line 2, Col 26, "Unexpected identifier: 'maybe'") | ||
|
|
||
| [<Fact>] | ||
| let ``'enum' constraint with an underlying type is accepted`` () = | ||
| Fsx """ | ||
| type I<'T when 'T : enum<int>> = interface end | ||
| type E = A = 1 | ||
| type Ok = I<E> | ||
| """ | ||
| |> typecheck | ||
| |> shouldSucceed | ||
|
|
||
| // https://github.com/dotnet/fsharp/issues/14580 | ||
| [<Fact>] | ||
| let ``'enum' constraint on a type of the same recursive group is accepted`` () = | ||
| FSharp """ | ||
| module rec MyModule | ||
|
|
||
| type MyEnum = | ||
| | Alpha = 1 | ||
| | Beta = 2 | ||
|
|
||
| type MyInter<'TEnum when 'TEnum : enum<int>> = interface end | ||
|
|
||
| type MyAlias = MyInter<MyEnum> | ||
| """ | ||
| |> asLibrary | ||
| |> compile | ||
| |> shouldSucceed | ||
|
|
||
| [<Fact>] | ||
| let ``'enum' constraint on a type of the same 'and' group is accepted`` () = | ||
| FSharp """ | ||
| module MyModule | ||
|
|
||
| type MyEnum = | ||
| | Alpha = 1 | ||
| | Beta = 2 | ||
|
|
||
| and MyInter<'TEnum when 'TEnum : enum<int>> = interface end | ||
|
|
||
| and MyAlias = MyInter<MyEnum> | ||
| """ | ||
| |> asLibrary | ||
| |> compile | ||
| |> shouldSucceed | ||
|
|
||
| [<Fact>] | ||
| let ``'enum' constraint on an inherited interface of the same recursive group is accepted`` () = | ||
| FSharp """ | ||
| module rec MyModule | ||
|
|
||
| type MyEnum = | ||
| | Alpha = 1 | ||
|
|
||
| type MyInter<'TEnum when 'TEnum : enum<int>> = interface end | ||
|
|
||
| type C() = | ||
| interface MyInter<MyEnum> | ||
| """ | ||
| |> asLibrary | ||
| |> compile | ||
| |> shouldSucceed | ||
|
|
||
| [<Fact>] | ||
| let ``Mismatched 'enum' constraint on a type of the same recursive group is reported`` () = | ||
| FSharp """ | ||
| module rec MyModule | ||
|
|
||
| type MyEnum = | ||
| | Alpha = 1 | ||
|
|
||
| type MyInter<'TEnum when 'TEnum : enum<int64>> = interface end | ||
|
|
||
| type MyAlias = MyInter<MyEnum> | ||
| """ | ||
| |> asLibrary | ||
| |> withOptions ["--test:ErrorRanges"] | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withDiagnosticMessageMatches "The type 'int64' does not match the type 'int'" | ||
|
|
||
| [<Fact>] | ||
| let ``Mismatched 'enum' constraint outside a recursive group is reported`` () = | ||
| FSharp """ | ||
| module MyModule | ||
|
|
||
| type MyEnum = | ||
| | Alpha = 1 | ||
|
|
||
| type MyInter<'TEnum when 'TEnum : enum<int64>> = interface end | ||
|
|
||
| type MyAlias = MyInter<MyEnum> | ||
| """ | ||
| |> asLibrary | ||
| |> withOptions ["--test:ErrorRanges"] | ||
| |> typecheck | ||
| |> shouldFail | ||
| |> withSingleDiagnostic (Error 1, Line 9, Col 16, Line 9, Col 31, "The type 'int64' does not match the type 'int'") | ||
|
|
||
| [<Fact>] | ||
| let ``Generic code over an 'enum' constraint in a recursive module runs`` () = | ||
| FSharp """ | ||
| module rec MyModule | ||
|
|
||
| type Color = | ||
| | Red = 1 | ||
| | Blue = 4 | ||
|
|
||
| let combine<'T when 'T : enum<int>> (a: 'T) (b: 'T) : 'T = | ||
| LanguagePrimitives.EnumOfValue (LanguagePrimitives.EnumToValue a ||| LanguagePrimitives.EnumToValue b) | ||
|
|
||
| [<EntryPoint>] | ||
| let main _ = | ||
| if LanguagePrimitives.EnumToValue (combine Color.Red Color.Blue) <> 5 then | ||
| failwith "expected Red ||| Blue to be 5" | ||
| 0 | ||
| """ | ||
| |> asExe | ||
| |> compileExeAndRun | ||
| |> shouldSucceed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
tests/service/data/SyntaxTree/SynTyparDecl/Constraint - Enum 01.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module Module | ||
|
|
||
| type I<'T when 'T : enum> = interface end |
23 changes: 23 additions & 0 deletions
23
tests/service/data/SyntaxTree/SynTyparDecl/Constraint - Enum 01.fs.bsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| ImplFile | ||
| (ParsedImplFileInput | ||
| ("/root/SynTyparDecl/Constraint - Enum 01.fs", false, | ||
| QualifiedNameOfFile Module, [], | ||
| [SynModuleOrNamespace | ||
| ([Module], false, NamedModule, | ||
| [Types | ||
| ([SynTypeDefn | ||
| (SynComponentInfo | ||
| ([], None, [], None, | ||
| PreXmlDoc ((3,0), FSharp.Compiler.Xml.XmlDocCollector), | ||
| false, None, (3,5--3,25)), | ||
| ObjectModel (Interface, [], (3,28--3,41)), [], None, | ||
| (3,5--3,41), { LeadingKeyword = Type (3,0--3,4) | ||
| EqualsRange = Some (3,26--3,27) | ||
| WithKeyword = None })], (3,0--3,41))], | ||
| PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None, | ||
| (1,0--3,41), { LeadingKeyword = Module (1,0--1,6) })], (true, true), | ||
| { ConditionalDirectives = [] | ||
| WarnDirectives = [] | ||
| CodeComments = [] }, set [])) | ||
|
|
||
| (3,20)-(3,24) parse error An 'enum' constraint must be of the form 'enum<type>' |
3 changes: 3 additions & 0 deletions
3
tests/service/data/SyntaxTree/SynTyparDecl/Constraint - Unknown identifier 01.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module Module | ||
|
|
||
| type I<'T when 'T : notAConstraint> = interface end |
23 changes: 23 additions & 0 deletions
23
tests/service/data/SyntaxTree/SynTyparDecl/Constraint - Unknown identifier 01.fs.bsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| ImplFile | ||
| (ParsedImplFileInput | ||
| ("/root/SynTyparDecl/Constraint - Unknown identifier 01.fs", false, | ||
| QualifiedNameOfFile Module, [], | ||
| [SynModuleOrNamespace | ||
| ([Module], false, NamedModule, | ||
| [Types | ||
| ([SynTypeDefn | ||
| (SynComponentInfo | ||
| ([], None, [], None, | ||
| PreXmlDoc ((3,0), FSharp.Compiler.Xml.XmlDocCollector), | ||
| false, None, (3,5--3,35)), | ||
| ObjectModel (Interface, [], (3,38--3,51)), [], None, | ||
| (3,5--3,51), { LeadingKeyword = Type (3,0--3,4) | ||
| EqualsRange = Some (3,36--3,37) | ||
| WithKeyword = None })], (3,0--3,51))], | ||
| PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None, | ||
| (1,0--3,51), { LeadingKeyword = Module (1,0--1,6) })], (true, true), | ||
| { ConditionalDirectives = [] | ||
| WarnDirectives = [] | ||
| CodeComments = [] }, set [])) | ||
|
|
||
| (3,20)-(3,34) parse error Unexpected identifier: 'notAConstraint' |
3 changes: 3 additions & 0 deletions
3
tests/service/data/SyntaxTree/SynTyparDecl/Constraint - Unknown identifier 02.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module Module | ||
|
|
||
| type I<'T when 'T : notAConstraint<int>> = interface end |
23 changes: 23 additions & 0 deletions
23
tests/service/data/SyntaxTree/SynTyparDecl/Constraint - Unknown identifier 02.fs.bsl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| ImplFile | ||
| (ParsedImplFileInput | ||
| ("/root/SynTyparDecl/Constraint - Unknown identifier 02.fs", false, | ||
| QualifiedNameOfFile Module, [], | ||
| [SynModuleOrNamespace | ||
| ([Module], false, NamedModule, | ||
| [Types | ||
| ([SynTypeDefn | ||
| (SynComponentInfo | ||
| ([], None, [], None, | ||
| PreXmlDoc ((3,0), FSharp.Compiler.Xml.XmlDocCollector), | ||
| false, None, (3,5--3,39)), | ||
| ObjectModel (Interface, [], (3,43--3,56)), [], None, | ||
| (3,5--3,56), { LeadingKeyword = Type (3,0--3,4) | ||
| EqualsRange = Some (3,41--3,42) | ||
| WithKeyword = None })], (3,0--3,56))], | ||
| PreXmlDoc ((1,0), FSharp.Compiler.Xml.XmlDocCollector), [], None, | ||
| (1,0--3,56), { LeadingKeyword = Module (1,0--1,6) })], (true, true), | ||
| { ConditionalDirectives = [] | ||
| WarnDirectives = [] | ||
| CodeComments = [] }, set [])) | ||
|
|
||
| (3,20)-(3,34) parse error Unexpected identifier: 'notAConstraint' |
3 changes: 3 additions & 0 deletions
3
tests/service/data/SyntaxTree/SynTyparDecl/Constraint - Unknown identifier 03.fs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module Module | ||
|
|
||
| type I<'T when 'T : maybe null> = interface end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 🕵️ Invalid enum constraints in recursive
.fsifiles go undiagnosed in FCS:CheckOneSigFilenever runs this deferred check.