From bcb56db84e6ddb2625a4be13eb44997cd8ac8821 Mon Sep 17 00:00:00 2001 From: Abel Braaksma Date: Wed, 8 May 2024 22:00:16 +0200 Subject: [PATCH 1/3] Add a test for user-defined types in ranges with steps --- .../FSharp.Core/PrimTypes.fs | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tests/FSharp.Core.UnitTests/FSharp.Core/PrimTypes.fs b/tests/FSharp.Core.UnitTests/FSharp.Core/PrimTypes.fs index e2b6882e146..42cc1ff1b27 100644 --- a/tests/FSharp.Core.UnitTests/FSharp.Core/PrimTypes.fs +++ b/tests/FSharp.Core.UnitTests/FSharp.Core/PrimTypes.fs @@ -1132,6 +1132,33 @@ module RangeTests = if System.UIntPtr.Size >= 8 then RangeTestsHelpers.unsigned 0x0un 0x1un 0x0un 0xffffffffffffffffun + [] + module UserDefinedRange = + type Step(value:int) = + member __.Value = value + + static member Zero = Step 0 + + type Value(value:int) = + member __.Value = value + + static member (+)(a:Value, b:Step) = Value(a.Value + b.Value) + + interface System.IComparable with + member a.CompareTo(b) = + match b with + | :? Value as b' -> compare a.Value b'.Value + | _ -> failwith "unsupported" + + [] + let ``Range.UserDefined``() = + // using a user defined type, as opposed to a primitive, as the base type + let rangeList = [Value 0 .. Step 2 .. Value 2] + let rangeArray = [Value 0 .. Step 2 .. Value 2] + Assert.AreEqual(2, rangeList.Length) + Assert.AreEqual(2, rangeArray.Length) + + /// These tests' arguments are intentionally _not_ inlined /// to force the size of the collection (for lists and arrays) or count (for for-loops) to be computed at runtime. module Runtime = @@ -1216,6 +1243,13 @@ module RangeTests = let zero, one, min0, max0 = System.UIntPtr 0u, System.UIntPtr 1u, System.UIntPtr System.UInt64.MinValue, System.UIntPtr System.UInt64.MaxValue RangeTestsHelpers.unsigned zero one min0 max0 + [] + let ``Range.UserDefined``() = + let rangeList = [Value 0 .. Step 2 .. Value 2] + let rangeArray = [Value 0 .. Step 2 .. Value 2] + Assert.AreEqual(2, rangeList.Length) + () + open NonStructuralComparison From 2de9d1ae543f854d23467e921218b9b1d265f62c Mon Sep 17 00:00:00 2001 From: Abel Braaksma Date: Fri, 10 May 2024 13:06:58 +0200 Subject: [PATCH 2/3] equality & comparison constraints for `^Step` param in `.. ..` operator Fixes #6238. Compiler does not infer this automatically through `and default`. --- src/FSharp.Core/prim-types.fsi | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/FSharp.Core/prim-types.fsi b/src/FSharp.Core/prim-types.fsi index dee677aac31..89b8ec82b38 100644 --- a/src/FSharp.Core/prim-types.fsi +++ b/src/FSharp.Core/prim-types.fsi @@ -3758,6 +3758,8 @@ namespace Microsoft.FSharp.Core and ^Step: (static member Zero: ^Step) and ^T: equality and ^T: comparison + and ^Step: equality + and ^Step: comparison and default ^Step: ^T and default ^T: int From 3ef82550bcf5e9aa09772754a1ce7475660f4731 Mon Sep 17 00:00:00 2001 From: Abel Braaksma Date: Fri, 10 May 2024 13:30:35 +0200 Subject: [PATCH 3/3] Add release notes --- docs/release-notes/.FSharp.Core/8.0.400.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/release-notes/.FSharp.Core/8.0.400.md b/docs/release-notes/.FSharp.Core/8.0.400.md index ee24d8d0a53..56dc33622cc 100644 --- a/docs/release-notes/.FSharp.Core/8.0.400.md +++ b/docs/release-notes/.FSharp.Core/8.0.400.md @@ -6,3 +6,4 @@ * Cache delegate in query extensions. ([PR #17130](https://github.com/dotnet/fsharp/pull/17130)) * Update `AllowNullLiteralAttribute` to also use `AttributeTargets.Interface` ([PR #17173](https://github.com/dotnet/fsharp/pull/17173)) +* Missing constraints added for `^Step` parameter in range expressions for `(.. ..)` operator ([PR #17133](https://github.com/dotnet/fsharp/pull/17133)) \ No newline at end of file