-
Notifications
You must be signed in to change notification settings - Fork 831
Port Member constraints and PrimitiveConstraints #7210
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
45 changes: 45 additions & 0 deletions
45
tests/fsharp/Compiler/ConstraintSolver/MemberConstraints.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,45 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| namespace FSharp.Compiler.UnitTests | ||
|
|
||
| open NUnit.Framework | ||
| open FSharp.Compiler.SourceCodeServices | ||
|
|
||
| [<TestFixture>] | ||
| module MemberConstraints = | ||
|
|
||
| [<Test>] | ||
| let ``we can overload operators on a type and not add all the extra jazz such as inlining and the ^ operator.``() = | ||
| CompilerAssert.CompileExeAndRun | ||
| """ | ||
| type Foo(x : int) = | ||
| member this.Val = x | ||
|
|
||
| static member (-->) ((src : Foo), (target : Foo)) = new Foo(src.Val + target.Val) | ||
| static member (-->) ((src : Foo), (target : int)) = new Foo(src.Val + target) | ||
|
|
||
| static member (+) ((src : Foo), (target : Foo)) = new Foo(src.Val + target.Val) | ||
| static member (+) ((src : Foo), (target : int)) = new Foo(src.Val + target) | ||
|
|
||
| let x = Foo(3) --> 4 | ||
| let y = Foo(3) --> Foo(4) | ||
| let x2 = Foo(3) + 4 | ||
| let y2 = Foo(3) + Foo(4) | ||
|
|
||
| if x.Val <> 7 then exit 1 | ||
| if y.Val <> 7 then exit 1 | ||
| if x2.Val <> 7 then exit 1 | ||
| if y2.Val <> 7 then exit 1 | ||
| """ | ||
|
|
||
| [<Test>] | ||
| let ``Invalid member constraint with ErrorRanges``() = // Regression test for FSharp1.0:2262 | ||
| CompilerAssert.TypeCheckSingleErrorWithOptions | ||
| [| "--test:ErrorRanges" |] | ||
| """ | ||
| let inline length (x: ^a) : int = (^a : (member Length : int with get, set) (x, ())) | ||
| """ | ||
| FSharpErrorSeverity.Error | ||
| 697 | ||
| (2, 42, 2, 75) | ||
| "Invalid constraint" |
109 changes: 109 additions & 0 deletions
109
tests/fsharp/Compiler/ConstraintSolver/PrimitiveConstraints.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,109 @@ | ||
| // Copyright (c) Microsoft Corporation. All Rights Reserved. See License.txt in the project root for license information. | ||
|
|
||
| namespace FSharp.Compiler.UnitTests | ||
|
|
||
| open NUnit.Framework | ||
| open FSharp.Compiler.SourceCodeServices | ||
|
|
||
| [<TestFixture>] | ||
| module PrimitiveConstraints = | ||
|
|
||
| [<Test>] | ||
| let ``Test primitive : constraints``() = | ||
| CompilerAssert.CompileExeAndRun | ||
| """ | ||
| #light | ||
|
|
||
| type Foo(x : int) = | ||
| member this.Value = x | ||
| override this.ToString() = "Foo" | ||
|
|
||
| type Bar(x : int) = | ||
| inherit Foo(-1) | ||
| member this.Value2 = x | ||
| override this.ToString() = "Bar" | ||
|
|
||
| let test1 (x : Foo) = x.Value | ||
| let test2 (x : Bar) = (x.Value, x.Value2) | ||
|
|
||
| let f = new Foo(128) | ||
| let b = new Bar(256) | ||
|
|
||
| if test1 f <> 128 then exit 1 | ||
| if test2 b <> (-1, 256) then exit 1 | ||
| """ | ||
|
|
||
| [<Test>] | ||
| let ``Test primitive :> constraints``() = | ||
| CompilerAssert.CompileExeAndRun | ||
| """ | ||
| #light | ||
| type Foo(x : int) = | ||
| member this.Value = x | ||
| override this.ToString() = "Foo" | ||
|
|
||
| type Bar(x : int) = | ||
| inherit Foo(-1) | ||
| member this.Value2 = x | ||
| override this.ToString() = "Bar" | ||
|
|
||
| type Ram(x : int) = | ||
| inherit Foo(10) | ||
| member this.ValueA = x | ||
| override this.ToString() = "Ram" | ||
|
|
||
| let test (x : Foo) = (x.Value, x.ToString()) | ||
|
|
||
| let f = new Foo(128) | ||
| let b = new Bar(256) | ||
| let r = new Ram(314) | ||
|
|
||
| if test f <> (128, "Foo") then exit 1 | ||
| if test b <> (-1, "Bar") then exit 1 | ||
| if test r <> (10, "Ram") then exit 1 | ||
| """ | ||
|
|
||
| [<Test>] | ||
| let ``Test primitive : null constraint``() = | ||
| CompilerAssert.CompileExeAndRun | ||
| """ | ||
| let inline isNull<'a when 'a : null> (x : 'a) = | ||
| match x with | ||
| | null -> "is null" | ||
| | _ -> (x :> obj).ToString() | ||
|
|
||
| let runTest = | ||
| // Wrapping in try block to work around FSB 1989 | ||
| try | ||
| if isNull null <> "is null" then exit 1 | ||
| if isNull "F#" <> "F#" then exit 1 | ||
| true | ||
| with _ -> exit 1 | ||
|
|
||
| if runTest <> true then exit 1 | ||
|
|
||
| exit 0 | ||
| """ | ||
|
|
||
| [<Test>] | ||
| /// Title: Type checking oddity | ||
| /// | ||
| /// This suggestion was resolved as by design, | ||
| /// so the test makes sure, we're emitting error message about 'not being a valid object construction expression' | ||
| let ``Invalid object constructor``() = // Regression test for FSharp1.0:4189 | ||
| CompilerAssert.TypeCheckWithErrorsAndOptions | ||
| [| "--test:ErrorRanges" |] | ||
| """ | ||
| type ImmutableStack<'a> private(items: 'a list) = | ||
|
|
||
| member this.Push item = ImmutableStack(item::items) | ||
| member this.Pop = match items with | [] -> failwith "No elements in stack" | x::xs -> x,ImmutableStack(xs) | ||
|
|
||
| // Notice type annotation is commented out, which results in an error | ||
| new(col (*: seq<'a>*)) = ImmutableStack(List.ofSeq col) | ||
|
|
||
| """ | ||
| [| FSharpErrorSeverity.Error, 41, (4, 29, 4, 56), "A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: new : col:'b -> ImmutableStack<'a>, private new : items:'a list -> ImmutableStack<'a>" | ||
| FSharpErrorSeverity.Error, 41, (5, 93, 5, 111), "A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: new : col:'b -> ImmutableStack<'a>, private new : items:'a list -> ImmutableStack<'a>" | ||
| FSharpErrorSeverity.Error, 41, (8, 30, 8, 60), "A unique overload for method 'ImmutableStack`1' could not be determined based on type information prior to this program point. A type annotation may be needed. Candidates: new : col:'b -> ImmutableStack<'a> when 'b :> seq<'c>, private new : items:'a list -> ImmutableStack<'a>" | ||
| FSharpErrorSeverity.Error, 696, (8, 30, 8, 60), "This is not a valid object construction expression. Explicit object constructors must either call an alternate constructor or initialize all fields of the object and specify a call to a super class constructor." |] | ||
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
6 changes: 0 additions & 6 deletions
6
...sharpqa/Source/Conformance/InferenceProcedures/ConstraintSolving/E_MemberConstraints01.fs
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
...s/fsharpqa/Source/Conformance/InferenceProcedures/ConstraintSolving/E_PrimConstraint04.fs
This file was deleted.
Oops, something went wrong.
24 changes: 0 additions & 24 deletions
24
.../fsharpqa/Source/Conformance/InferenceProcedures/ConstraintSolving/MemberConstraints01.fs
This file was deleted.
Oops, something went wrong.
26 changes: 0 additions & 26 deletions
26
tests/fsharpqa/Source/Conformance/InferenceProcedures/ConstraintSolving/PrimConstraint01.fs
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
tests/fsharpqa/Source/Conformance/InferenceProcedures/ConstraintSolving/PrimConstraint02.fs
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
tests/fsharpqa/Source/Conformance/InferenceProcedures/ConstraintSolving/PrimConstraint03.fs
This file was deleted.
Oops, something went wrong.
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
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.
I think we could modify this test. Since we're not testing that the code runs as expected, we're really just interested in verifying that the
nullconstraint applies. So we'd like to verify thatisNull xwill compile ifxhasnullas a proper value, and that it does not compile ofxdoes not havenullas a proper value.This will incidentally also remove the need for
exit.