From 9971858fc2fd82d72f731f4cfae6ff7df1088ecd Mon Sep 17 00:00:00 2001 From: Robby Findler Date: Mon, 19 Jan 2015 16:37:15 -0600 Subject: [PATCH] small clean up to list contract error messages (grammar and abstraction) --- .../collects/racket/contract/private/misc.rkt | 52 ++++++++----------- 1 file changed, 21 insertions(+), 31 deletions(-) diff --git a/racket/collects/racket/contract/private/misc.rkt b/racket/collects/racket/contract/private/misc.rkt index 5400d6a333..f1c5fcebb3 100644 --- a/racket/collects/racket/contract/private/misc.rkt +++ b/racket/collects/racket/contract/private/misc.rkt @@ -1258,14 +1258,8 @@ val)] [else (λ (neg-party) - (raise-blame-error - blame #:missing-party neg-party - val - '(expected: "a list of ~a elements" given: "~a element~a\n complete list: ~e") - expected-length - actual-length - (if (= actual-length 1) "" "s") - val))])] + (expected-a-list-of-len val actual-length expected-length blame + #:missing-party neg-party))])] [else (λ (neg-party) (raise-blame-error blame #:missing-party neg-party @@ -1281,14 +1275,7 @@ (let* ([args (generic-list/c-args c)] [expected (length args)] [actual (length x)]) - (unless (= actual expected) - (raise-blame-error - blame x - '(expected: "a list of ~a elements" given: "~a element~a\n complete list: ~e") - expected - actual - (if (= actual 1) "" "s") - x)) + (expected-a-list-of-len x actual expected blame) (for ([arg/c (in-list args)] [v (in-list x)] [i (in-naturals 1)]) (((contract-projection arg/c) (add-list-context blame i)) @@ -1306,14 +1293,7 @@ (λ (x) (unless (list? x) (expected-a-list x blame)) (define actual (length x)) - (unless (= actual expected) - (raise-blame-error - blame x - '(expected: "a list of ~a elements" given: "~a element~a\n complete list: ~e") - expected - actual - (if (= actual 1) "" "s") - x)) + (expected-a-list-of-len x actual expected blame #:missing-party #f) (for/list ([item (in-list x)] [proj (in-list projs)]) (proj item))))) @@ -1324,13 +1304,23 @@ (define (expected-a-list-of-len x actual expected blame #:missing-party [missing-party #f]) (unless (= actual expected) - (raise-blame-error - blame #:missing-party missing-party x - '(expected: "a list of ~a elements" given: "~a element~a\n complete list: ~e") - expected - actual - (if (= actual 1) "" "s") - x))) + (cond + [(null? x) + (raise-blame-error + blame #:missing-party missing-party x + '(expected: "a list of ~a element~a" given: "~e") + expected + (if (= expected 1) "" "s") + x)] + [else + (raise-blame-error + blame #:missing-party missing-party x + '(expected: "a list of ~a element~a" given: "~a element~a\n complete list: ~e") + expected + (if (= expected 1) "" "s") + actual + (if (= actual 1) "" "s") + x)]))) (define (list/c-chaperone/other-val-first-projection c) (define projs (map get/build-val-first-projection (generic-list/c-args c)))