From d23e05f2c314074c0757dbfa88a55c495c5f6d05 Mon Sep 17 00:00:00 2001 From: Asumu Takikawa Date: Wed, 2 Mar 2016 04:18:06 -0500 Subject: [PATCH] Escape "~" in tc-error/fields arguments Fixes issue #314 --- typed-racket-lib/typed-racket/utils/tc-utils.rkt | 14 ++++++++++---- typed-racket-test/unit-tests/parse-type-tests.rkt | 3 +++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/typed-racket-lib/typed-racket/utils/tc-utils.rkt b/typed-racket-lib/typed-racket/utils/tc-utils.rkt index 4d6bb0b6..9a91c70d 100644 --- a/typed-racket-lib/typed-racket/utils/tc-utils.rkt +++ b/typed-racket-lib/typed-racket/utils/tc-utils.rkt @@ -202,12 +202,14 @@ don't depend on any other portion of the system ;; Produce a type error using modern Racket error syntax. ;; Avoid using format directives in the `msg`, `more`, and `field` -;; strings in the rest argument (may cause unexpected errors) -(define (tc-error/fields msg - #:more [more #f] +;; strings in the rest argument because they will be escaped. +(define (tc-error/fields *msg + #:more [*more #f] #:stx [stx (current-orig-stx)] #:delayed? [delayed? #f] . rst) + (define msg (escape-~ *msg)) + (define more (and *more (escape-~ *more))) (unless (even? (length rst)) (raise-argument-error 'tc-error/fields @@ -216,7 +218,7 @@ don't depend on any other portion of the system (define-values (field-strs vals) (for/fold ([field-strs null] [vals null]) ([field+value (in-slice 2 rst)]) - (define field (car field+value)) + (define field (escape-~ (car field+value))) (define value (cadr field+value)) (define field-strs* (cons (format " ~a: ~~a" field) field-strs)) @@ -229,6 +231,10 @@ don't depend on any other portion of the system (apply tc-error/delayed #:stx stx final-msg (reverse vals)) (apply tc-error/stx stx final-msg (reverse vals)))) +;; escape "~" to avoid breaking `format` down the line +(define (escape-~ str) + (regexp-replace #rx"~" str "~~")) + ;; produce a type error, using the current syntax (define (tc-error msg . rest) (let* ([ostx (current-orig-stx)] diff --git a/typed-racket-test/unit-tests/parse-type-tests.rkt b/typed-racket-test/unit-tests/parse-type-tests.rkt index 7c2ec486..a16e6937 100644 --- a/typed-racket-test/unit-tests/parse-type-tests.rkt +++ b/typed-racket-test/unit-tests/parse-type-tests.rkt @@ -403,6 +403,9 @@ [FAIL (Unit (import bad) (export) String)] [FAIL (Unit (import) (export bad) String)] [(Sequenceof Any Any) (-seq Univ Univ)] + + ;; GH issue #314 + [FAIL ~> #:msg "unbound"] )) ;; FIXME - add tests for parse-values-type, parse-tc-results