Wrap higher-order predicate errors with source text#18
Merged
Conversation
What:
- Predicate errors raised inside map/filter/any/all/find/count are
wrapped with the form name, the predicate's source text printed
back from the AST, and the failing element's index. Example:
map predicate `it.Nmae` failed on element 0: ...
- Nested forms each add their own layer, top-down through the
iteration tree. The internal `map` keyword rewrite is reversed
before printing so users see the source as they typed it.
- Errors that do not wrap ErrEvaluate (context.Canceled,
context.DeadlineExceeded) pass through unchanged so cancellation
stays observable.
- Predicates whose source text contains a backtick fall back to a
position-only message to avoid clashing with the surrounding
delimiters.
- Same wrapping is applied in tryFilterIndex (the filter(xs, p)[N]
fast path) so the fast path and slow path stay consistent.
Why:
- "at index N" alone left users guessing what N referred to. Showing
the predicate text plus an "element N" wording connects the failure
to a specific iteration step inside a specific form, which is the
whole win this change is supposed to deliver. See
docs/design/language-evolution.md (step 3).
Spec and llms.txt updated.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Predicate errors raised inside
map/filter/any/all/find/countare now wrapped with the form name, the predicate's source text (printed back from the AST), and the failing element's index.Nested forms add their own layer, top-down through the iteration tree. The internal
__expr_map__rewrite is reversed before printing, so users see the source they typed:Behavior details
ErrEvaluate. Wrapping preserves the chain, soerrors.Is(err, ErrEvaluate)still matches.context.Canceled/context.DeadlineExceededso cancellation stays observable.map predicate failed on element 0: ...), since backticks are the surrounding delimiters.tryFilterIndexinprogram.go(thefilter(xs, p)[N]fast path) uses the same helper, so the fast path is not a debuggability regression.Implementation
wrapPredicateErr(name, predicate, index, err)inhigher_order.go.formatPredicate(node)usesgo/printer.Fprintwith a freshFileSetand post-processes themapsentinel back tomap. No new external deps;go/printeris stdlib.forEachnow takes a form name and the predicate AST. Each of the six forms passes its own user-visible name.Tests
higher_order_test.gogained six new tests:TestHigherOrder_PredicateError(updated): pins the new format on amap(...)typo.TestHigherOrder_PredicateErrorReportsIndex: failure on the 3rd element reportselement 2.TestHigherOrder_PredicateErrorNestedForms: both layers appear; sentinel does not leak.TestHigherOrder_PredicateErrorFormNames: spot-check filter / find / count / any / all.TestHigherOrder_PredicateErrorFilterIndexFastPath: fast path uses the same wrapping.TestHigherOrder_PredicateErrorBacktickFallback: predicate with a backtick falls back cleanly.TestHigherOrder_PredicateErrorIgnoresCancellation: cancellation is not wrapped.Test plan
go test ./...go test -race ./...FuzzCompile20sFuzzEval20s