From 595b197d3b0b4ca9a538a497e466a771cc4d79cd Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Thu, 16 Nov 2023 15:42:05 -0800 Subject: [PATCH 1/7] Remove Comparable requirement from Range bound Fails due to /Users/dave/src/hylo/Sources/IR/Emitter.swift:2321: Fatal error: lvalue lowering for cast expressions #1049 --- StandardLibrary/Sources/Core/Range.hylo | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/StandardLibrary/Sources/Core/Range.hylo b/StandardLibrary/Sources/Core/Range.hylo index df7e4579b..2eaf441ef 100644 --- a/StandardLibrary/Sources/Core/Range.hylo +++ b/StandardLibrary/Sources/Core/Range.hylo @@ -1,5 +1,5 @@ /// A half-open interval from a lower bound up to, but not including, an uppor bound. -public type Range { +public type Range { // TODO: private(set) /// The minimum value included in `self`. @@ -22,13 +22,13 @@ public type Range { } /// Returns `true` iff `element` is contained in `self`. - public fun contains(_ element: Bound) -> Bool { - (element >= lower_bound) && (element < upper_bound) + public fun contains(_ element: B) -> Bool { + ((element as! Bound) >= lower_bound) && ((element as! Bound) < upper_bound) } /// Returns `true` iff `other` is contained in `self`. - public fun contains(_ other: Self) -> Bool { - (other.lower_bound >= self.lower_bound) && (other.upper_bound <= self.upper_bound) + public fun contains(_ other: Range) -> Bool { + ((other.lower_bound as! Bound) >= self.lower_bound) && ((other.upper_bound as! Bound) <= self.upper_bound) } } From d2038b395551ffa7ca2d6a2257570c9a08d48e06 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Thu, 16 Nov 2023 16:20:47 -0800 Subject: [PATCH 2/7] Use unsafe operations. Tests fail with /Users/dave/src/hylo/Tests/LibraryTests/TestCases/RangeTests.hylo:13: precondition failure --- StandardLibrary/Sources/Core/Range.hylo | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/StandardLibrary/Sources/Core/Range.hylo b/StandardLibrary/Sources/Core/Range.hylo index 2eaf441ef..e3c016f2c 100644 --- a/StandardLibrary/Sources/Core/Range.hylo +++ b/StandardLibrary/Sources/Core/Range.hylo @@ -21,14 +21,16 @@ public type Range { &self.upper_bound = upper_bound } - /// Returns `true` iff `element` is contained in `self`. - public fun contains(_ element: B) -> Bool { - ((element as! Bound) >= lower_bound) && ((element as! Bound) < upper_bound) + /// Returns `true` iff `x` is contained in `self`. + public fun contains(_ x: B) -> Bool { + let e = Pointer(type_punning: pointer[to: x]) + return (e.unsafe[] >= lower_bound) && (e.unsafe[] < upper_bound) } /// Returns `true` iff `other` is contained in `self`. public fun contains(_ other: Range) -> Bool { - ((other.lower_bound as! Bound) >= self.lower_bound) && ((other.upper_bound as! Bound) <= self.upper_bound) + let e = Pointer(type_punning: pointer[to: other]) + return (e.unsafe[].lower_bound >= self.lower_bound) && (e.unsafe[].upper_bound <= self.upper_bound) } } From 45c4bf6ffc4f9b9a629c737e9f74c3be91ce50b3 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 6 Mar 2024 09:57:07 -0800 Subject: [PATCH 3/7] Remove haxx --- StandardLibrary/Sources/Core/Range.hylo | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/StandardLibrary/Sources/Core/Range.hylo b/StandardLibrary/Sources/Core/Range.hylo index b413508de..6015835d7 100644 --- a/StandardLibrary/Sources/Core/Range.hylo +++ b/StandardLibrary/Sources/Core/Range.hylo @@ -21,16 +21,18 @@ public type Range { &self.upper_bound = upper_bound } +} + +public extension Range where Bound: Comparable { + /// Returns `true` iff `x` is contained in `self`. - public fun contains(_ x: B) -> Bool { - let e = Pointer(type_punning: pointer[to: x]) - return (e.unsafe[] >= lower_bound) && (e.unsafe[] < upper_bound) + public fun contains(_ x: Bound) -> Bool { + return (x >= lower_bound) && (x < upper_bound) } /// Returns `true` iff `other` is contained in `self`. - public fun contains(_ other: Range) -> Bool { - let e = Pointer(type_punning: pointer[to: other]) - return (e.unsafe[].lower_bound >= self.lower_bound) && (e.unsafe[].upper_bound <= self.upper_bound) + public fun contains(_ other: Self) -> Bool { + return (other.lower_bound >= self.lower_bound) && (other.upper_bound <= self.upper_bound) } } From 915ef9f556bcfb4d2477c7dcb3196333b6b7a73a Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 6 Mar 2024 09:57:30 -0800 Subject: [PATCH 4/7] Correct spello --- StandardLibrary/Sources/Core/Range.hylo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/StandardLibrary/Sources/Core/Range.hylo b/StandardLibrary/Sources/Core/Range.hylo index 6015835d7..b73e58325 100644 --- a/StandardLibrary/Sources/Core/Range.hylo +++ b/StandardLibrary/Sources/Core/Range.hylo @@ -1,4 +1,4 @@ -/// A half-open interval from a lower bound up to, but not including, an uppor bound. +/// A half-open interval from a lower bound up to, but not including, an upper bound. public type Range { // TODO: private(set) From 489ef5e1c6059bbf976bcf987f959c38de132d66 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 6 Mar 2024 10:08:33 -0800 Subject: [PATCH 5/7] Flesh out tests. --- StandardLibrary/Sources/Core/Range.hylo | 8 ++++++-- Tests/LibraryTests/TestCases/RangeTests.hylo | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/StandardLibrary/Sources/Core/Range.hylo b/StandardLibrary/Sources/Core/Range.hylo index b73e58325..9e039b404 100644 --- a/StandardLibrary/Sources/Core/Range.hylo +++ b/StandardLibrary/Sources/Core/Range.hylo @@ -14,13 +14,17 @@ public type Range { /// Creates a half-open interval [`lower_bound`, `upper_bound`). /// - /// - Requires: `lower_bound <= upper_bound`. + /// - Requires: `lower_bound <= upper_bound` if `Bound` models `Comparable`. public init(from lower_bound: sink Bound, up_to upper_bound: sink Bound) { - // precondition(lower_bound <= upper_bound) &self.lower_bound = lower_bound &self.upper_bound = upper_bound } + /// Returns true iff `lower_bound == upper_bound`. + public fun is_empty() -> Bool { + return lower_bound == upper_bound + } + } public extension Range where Bound: Comparable { diff --git a/Tests/LibraryTests/TestCases/RangeTests.hylo b/Tests/LibraryTests/TestCases/RangeTests.hylo index 86ea05660..1d2e2d884 100644 --- a/Tests/LibraryTests/TestCases/RangeTests.hylo +++ b/Tests/LibraryTests/TestCases/RangeTests.hylo @@ -1,6 +1,6 @@ //- compileAndRun expecting: success -public fun test_conformance_to_iterator() { +private fun test_conformance_to_iterator() { var r = 1 ..< 3 var x: Int @@ -14,6 +14,18 @@ public fun test_conformance_to_iterator() { precondition(r.lower_bound == r.upper_bound) } +private fun test_non_comparable() { + struct NonComparable: SemiRegular { + public memberwise init + let x: Int + } + + var r0 = NonComparable(0) ..< NonComparable(0) + precondition(r0.is_empty()) + var r1 = NonComparable(0) ..< NonComparable(1) + precondition(!r1.is_empty()) +} + public fun main() { let r0 = Range(from: 0, up_to: 10) @@ -27,4 +39,5 @@ public fun main() { precondition(!r0.contains(5 ..< 15)) test_conformance_to_iterator() + test_non_comparable() } From 1b69469c84e90348c36accfa45192518027745cd Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Wed, 6 Mar 2024 10:42:33 -0800 Subject: [PATCH 6/7] Really try to fix the windows build. --- .github/workflows/build-and-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index e67cdad7b..b6d22607a 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -210,7 +210,7 @@ jobs: name: Build the dependencies of build tools run: | echo 'SPM_BUILD_TOOL_SUPPORT_NO_REENTRANT_BUILD=1' >> $env:GITHUB_ENV - swift ${{ env.build-options }} --target BuildToolDependencies + swift build ${{ env.build-options }} --target BuildToolDependencies working-directory: hylo - name: Build From ba807a5a39679342b2342c3330f43ea651d34824 Mon Sep 17 00:00:00 2001 From: Dave Abrahams Date: Thu, 7 Mar 2024 08:02:07 -0800 Subject: [PATCH 7/7] Apply changes from code review Co-authored-by: Dimi Racordon --- StandardLibrary/Sources/Core/Range.hylo | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/StandardLibrary/Sources/Core/Range.hylo b/StandardLibrary/Sources/Core/Range.hylo index 9e039b404..4ab0a0a4f 100644 --- a/StandardLibrary/Sources/Core/Range.hylo +++ b/StandardLibrary/Sources/Core/Range.hylo @@ -13,7 +13,6 @@ public type Range { memberwise init /// Creates a half-open interval [`lower_bound`, `upper_bound`). - /// /// - Requires: `lower_bound <= upper_bound` if `Bound` models `Comparable`. public init(from lower_bound: sink Bound, up_to upper_bound: sink Bound) { &self.lower_bound = lower_bound @@ -22,7 +21,7 @@ public type Range { /// Returns true iff `lower_bound == upper_bound`. public fun is_empty() -> Bool { - return lower_bound == upper_bound + lower_bound == upper_bound } } @@ -31,12 +30,12 @@ public extension Range where Bound: Comparable { /// Returns `true` iff `x` is contained in `self`. public fun contains(_ x: Bound) -> Bool { - return (x >= lower_bound) && (x < upper_bound) + (x >= lower_bound) && (x < upper_bound) } /// Returns `true` iff `other` is contained in `self`. public fun contains(_ other: Self) -> Bool { - return (other.lower_bound >= self.lower_bound) && (other.upper_bound <= self.upper_bound) + (other.lower_bound >= self.lower_bound) && (other.upper_bound <= self.upper_bound) } }