Skip to content

Improve error messages for assert and with-asserts - #1324

Open
NoahStoryM wants to merge 4 commits into
racket:masterfrom
NoahStoryM:assert
Open

NoahStoryM wants to merge 4 commits into
racket:masterfrom
NoahStoryM:assert

Conversation

@NoahStoryM

@NoahStoryM NoahStoryM commented Mar 20, 2023 •

Copy link
Copy Markdown
Contributor

Assertion failures now identify the source assertion and the failing value.
assert also includes the predicate name when available; with-asserts identifies
the first failed clause. Predicates remain ordinary Racket expressions, and this
PR does not introduce a predicate DSL.

The changes preserve evaluation order, single evaluation of predicate expressions,
short-circuiting, multiple body results, and occurrence typing. Assertion failures
use exn:fail:contract, as produced by raise-arguments-error.

Tests cover diagnostics and runtime behavior, plus positive, negative, truthiness,
and sequential refinements over union-typed arguments.

Validation: 10 assertion test cases, 6 existing primitive test cases, and 5 targeted
integration cases pass on both the original PR baseline with Racket 8.8 CS and the
latest upstream with Racket 9.3.0.10 CS. The updated Utilities chapter builds and its
examples run. The full CI suite has not been run locally.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 2 files in this pull request and has added suggestions.

Comment on lines +734 to +739
(define pred-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[(not/p (not/p p)) (pred-parser #'p)]
[_ stx])))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define-lambda-to-define: The define form supports a shorthand for defining functions (including function-returning functions).

Suggested change
(define pred-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[(not/p (not/p p)) (pred-parser #'p)]
[_ stx])))
(define (pred-parser stx)
(syntax-parse stx
#:datum-literals (not/p)
[(not/p (not/p p)) (pred-parser #'p)]
[_ stx]))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("    (define (pred-parser stx)"
       "      (syntax-parse stx"
       "        #:datum-literals (not/p)"
       "        [(not/p (not/p p)) (pred-parser #'p)]"
       "        [_ stx]))")
  #:original-lines
    '#("    (define pred-parser"
       "      (λ (stx)"
       "        (syntax-parse stx"
       "          #:datum-literals (not/p)"
       "          [(not/p (not/p p)) (pred-parser #'p)]"
       "          [_ stx])))")
  #:start-line 734)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:do-make-syntax-introducer>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/snapshot/pkgs/resyntax/default-recommendations/function-definition-shortcuts.rkt:87:3 (define (pred-parser (ORIGINAL-SPLICE stx)) (ORIGINAL-GAP (stx) (syntax-parse stx #:datum-literals (not/p) ((not/p (not/p p)) (pred-parser (syntax p))) (_ stx))) (ORIGINAL-SPLICE (syntax-parse stx #:datum-literals (not/p) ((not/p (not/p p)) (pred-parser...>
  #:original-syntax
    #<syntax:typed-racket-lib/typed-racket/base-env/prims.rkt:734:4 (define pred-parser (λ (stx) (syntax-parse stx #:datum-literals (not/p) ((not/p (not/p p)) (pred-parser (syntax p))) (_ stx))))>)

Comment on lines +740 to +745
(define assert-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[[x (not/p p)] #'(p x)]
[[x p] #'(not (p x))])))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

define-lambda-to-define: The define form supports a shorthand for defining functions (including function-returning functions).

Suggested change
(define assert-parser
(λ (stx)
(syntax-parse stx
#:datum-literals (not/p)
[[x (not/p p)] #'(p x)]
[[x p] #'(not (p x))])))
(define (assert-parser stx)
(syntax-parse stx
#:datum-literals (not/p)
[[x (not/p p)] #'(p x)]
[[x p] #'(not (p x))]))
Debugging details
Textual replacement
(line-replacement
  #:new-lines
    '#("    (define (assert-parser stx)"
       "      (syntax-parse stx"
       "        #:datum-literals (not/p)"
       "        [[x (not/p p)] #'(p x)]"
       "        [[x p] #'(not (p x))]))")
  #:original-lines
    '#("    (define assert-parser"
       "      (λ (stx)"
       "        (syntax-parse stx"
       "          #:datum-literals (not/p)"
       "          [[x (not/p p)] #'(p x)]"
       "          [[x p] #'(not (p x))])))")
  #:start-line 740)
Syntactic replacement
(syntax-replacement
  #:introduction-scope #<procedure:do-make-syntax-introducer>
  #:new-syntax
    #<syntax:/home/runner/.local/share/racket/snapshot/pkgs/resyntax/default-recommendations/function-definition-shortcuts.rkt:87:3 (define (assert-parser (ORIGINAL-SPLICE stx)) (ORIGINAL-GAP (stx) (syntax-parse stx #:datum-literals (not/p) ((x (not/p p)) (syntax (p x))) ((x p) (syntax (not (p x)))))) (ORIGINAL-SPLICE (syntax-parse stx #:datum-literals (not/p) ((x (not/p p)) (syntax...>
  #:original-syntax
    #<syntax:typed-racket-lib/typed-racket/base-env/prims.rkt:740:4 (define assert-parser (λ (stx) (syntax-parse stx #:datum-literals (not/p) ((x (not/p p)) (syntax (p x))) ((x p) (syntax (not (p x)))))))>)

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 2 files in this pull request and found no issues.

@samth

samth commented Sep 30, 2026

Copy link
Copy Markdown
Member

Coming back to this after quite a while, I like the error message improvements but I don't think we should have a DSL for assertions; they should stay regular Racket expressions.

@mfelleisen

Copy link
Copy Markdown

I actually think that such DSLs are a good thing especially in this day and age.

codex added 2 commits October 10, 2026 15:05
…tics

Remove the assertion-specific not/p grammar while retaining source-aware errors. Report the failed value in with-asserts without evaluating predicate expressions again, and document the contract exceptions.
Cover ordinary locally bound predicate constructors, failure messages, evaluation order, short-circuiting, and multiple results. Check positive, negative, truthiness, and sequential occurrence refinements over union-typed arguments.
@NoahStoryM NoahStoryM changed the title Add support for negated predicates in assert and with-asserts. Improve error messages for assert and with-asserts Oct 10, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resyntax analyzed 5 files in this pull request and found no issues.

@NoahStoryM

Copy link
Copy Markdown
Contributor Author

Thanks for revisiting this! I've narrowed this PR to the error-message improvements and removed the assertion-specific not/p syntax. Predicates remain ordinary Racket expressions.

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.

4 participants