Skip to content

fix(zero-swift): lay out a viewport shorter than its insets as an empty one - #276

Merged
tmm merged 1 commit into
mainfrom
swift-fix-negative-viewport
Nov 22, 2026
Merged

tmm merged 1 commit into
mainfrom
swift-fix-negative-viewport

Conversation

@tmm

@tmm tmm commented Nov 22, 2026

Copy link
Copy Markdown
Member

RowsScrollView passes LayoutHost.layout its height minus its content insets. On iOS, a scroll view shorter than its safe area's insets makes that negative; a 90-pt-tall window on an iPhone is enough. LayoutHost.reach then built (offset - viewport)...(offset + 2 * viewport) with a lower bound above its upper one, which traps with "Range requires lowerBound <= upperBound". macOS's clip view can get there the same way through its content insets.

layout(offset:viewport:) now treats a negative viewport as 0. The host, and the core that reads viewportHeight, see a viewport with no room for rows. Once the view grows, its rows are measured and drawn as before. Viewports of 0 or more take the same path as before.

Tests. A new layout host test starts at a viewport of −40, settles, then grows to 400 and checks that the first row is drawn at the top with no blank space. On main it traps (Swift/ClosedRange.swift:409: Fatal error: Range requires lowerBound <= upperBound); with the fix it passes.

End to end. A scratch probe (not committed) hosted VirtualList in an 800×90 window on the iOS Simulator (iPhone 17 Pro, Xcode 27.3). It trapped in LayoutHost.reach from RowsScrollView.layoutSubviews before the fix, and lays out all 44 of its cases after.

Run locally. Linux: swift test --filter ZeroVirtualTests (133 tests) and swift format lint --strict. iOS Simulator: xcodebuild test -only-testing:ZeroVirtualTests/LayoutHostTests.

Patch changeset.

…ty one

`RowsScrollView` passes `LayoutHost.layout` its height less its content
insets. On iOS a scroll view shorter than its safe area's insets (a
90 pt tall window on an iPhone) makes that negative, and `LayoutHost.reach`
then built `(offset - viewport)...(offset + 2 * viewport)` with its lower
bound above its upper one, which traps: "Range requires lowerBound <=
upperBound". macOS's clip view can do the same with its content insets.

`layout(offset:viewport:)` now takes a negative viewport as 0, so the host,
and the core reading `viewportHeight`, see a viewport with no room for
rows. Once the view grows, its rows are measured and drawn as before.

The new layout host test starts at a viewport of -40 and grows to 400; on
main it traps. A scratch probe hosting `VirtualList` in an 800x90 window on
the iOS Simulator (iPhone 17 Pro) trapped before and lays out every case
after.

Amp-Thread-ID: https://ampcode.com/threads/T-884aab88-be49-4bc7-96a1-044f0e71dabf
Co-authored-by: tmm <tmm@tmm.dev>
@tmm
tmm merged commit 00376cd into main Nov 22, 2026
32 checks passed
@tmm
tmm deleted the swift-fix-negative-viewport branch November 22, 2026 21:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants