From 7eb1ef925035296b397ada1435a46453310943a2 Mon Sep 17 00:00:00 2001 From: Matthew Flatt Date: Sat, 23 Nov 2019 15:25:34 -0500 Subject: [PATCH] cs: fix printing on struct type with custom write Don't try to use a struct type's custom-write procedure on the struct type itself. Relevant to #2926 --- pkgs/racket-test-core/tests/racket/struct.rktl | 2 ++ racket/src/io/print/graph.rkt | 6 ++++-- racket/src/io/print/main.rkt | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkgs/racket-test-core/tests/racket/struct.rktl b/pkgs/racket-test-core/tests/racket/struct.rktl index fe45e24beb..0b08a6002c 100644 --- a/pkgs/racket-test-core/tests/racket/struct.rktl +++ b/pkgs/racket-test-core/tests/racket/struct.rktl @@ -867,6 +867,8 @@ (define-values (s:tuple make-tuple tuple? tuple-ref tuple-set!) (make-struct-type 'tuple #f 1 0 #f (list (cons prop:custom-write tuple-print)))) + + (test "#" values (format "~s" s:tuple)) ; shouldn't trigger custom write (define (with-output-string thunk) (let ([p (open-output-string)]) diff --git a/racket/src/io/print/graph.rkt b/racket/src/io/print/graph.rkt index 534d1cb079..6b251da3fe 100644 --- a/racket/src/io/print/graph.rkt +++ b/racket/src/io/print/graph.rkt @@ -53,7 +53,8 @@ (and (not print-graph?) (not (eq? mode PRINT-MODE/UNQUOTED)) (quick-no-graph? (mcdr v) (quick-no-graph? (mcar v) (sub1 fuel))))] - [(custom-write? v) + [(and (custom-write? v) + (not (struct-type? v))) #f] [(and (struct? v) (config-get config print-struct)) @@ -163,7 +164,8 @@ (build-graph (mcar v) mode) (build-graph (mcdr v) mode) (done! v (eq? mode PRINT-MODE/UNQUOTED))] - [(custom-write? v) + [(and (custom-write? v) + (not (struct-type? v))) (define print-quotable (if (eq? mode PRINT-MODE/UNQUOTED) (custom-print-quotable-accessor v 'self) 'self)) diff --git a/racket/src/io/print/main.rkt b/racket/src/io/print/main.rkt index 29f3cffebe..7a33fc9679 100644 --- a/racket/src/io/print/main.rkt +++ b/racket/src/io/print/main.rkt @@ -200,6 +200,7 @@ (hash? v) (prefab-struct-key v) (and (custom-write? v) + (not (struct-type? v)) (not (printable-regexp? v)) (not (eq? 'self (custom-print-quotable-accessor v 'self)))))) ;; Since this value is not marked for constructor mode, @@ -292,7 +293,8 @@ (fail-unreadable who v)] [(mpair? v) (print-mlist p who v mode o max-length graph config)] - [(custom-write? v) + [(and (not (struct-type? v)) + (custom-write? v)) (let ([o/m (make-output-port/max o max-length)]) (set-port-handlers-to-recur! o/m