typed-racket icon indicating copy to clipboard operation
typed-racket copied to clipboard

adjust assertion error message to use ~e

Open jbclements opened this issue 2 years ago • 1 comments

Per discussion on racket discourse, this pull request adjusts the error message associated with an assertion failure to use ~e rather than ~v

jbclements avatar May 05 '22 05:05 jbclements

I was wondering if we could extend assert to verify that the argument doesn't satisfy the constraint?

For example:

Welcome to Racket v8.5 [cs].
> (require (for-syntax racket/base))
> (define-syntax (assert stx)
    (syntax-case stx (not/c)
      [(assert v) #'(assert v (not/c not))]
      [(assert v (not/c (not/c p))) #'(assert v p)]
      [(assert v (not/c p))
       #`(let ([val v]
               [pred p])
           #,(syntax-property
              (syntax/loc stx
                (if (pred val)
                    (error (format "Assertion (not/c ~a) failed on ~e" (or (object-name pred) pred) val))
                    val))
              'feature-profile:TR-dynamic-check #t))]
      [(assert v p)
       #`(let ([val  v]
               [pred p])
           #,(syntax-property
              (quasisyntax/loc stx
                (if (pred val)
                    val
                    (error (format "Assertion ~a failed on ~e" (or (object-name pred) pred) val))))
              'feature-profile:TR-dynamic-check #t))]))
> (assert 123 (not/c (not/c (not/c string?))))
123
> (assert 123 (not/c number?))
Assertion (not/c number?) failed on 123 [,bt for context]
> (assert #f)
Assertion (not/c not) failed on #f [,bt for context]

(maybe off topic) and I wonder if we can use assert to define with-asserts? Then the extension to assert may also apply to with-asserts.

NoahStoryM avatar May 05 '22 07:05 NoahStoryM

Pushed with ~e for the predicate in https://github.com/racket/typed-racket/commit/4165b6baa0c79e7391d990d76d74c483592145c1

bennn avatar Sep 22 '22 21:09 bennn