From f35a6fd5cbf5f4dc5151cff7e3b162d1738778bf Mon Sep 17 00:00:00 2001 From: Sam Tobin-Hochstadt Date: Wed, 8 Nov 2017 09:49:22 -0500 Subject: [PATCH] Avoid wrapping 5 when contracted as (Sequenceof Integer) Closes racket/math#13 --- .../typed-racket/private/type-contract.rkt | 7 ++++++- .../succeed/sequenceof-integer.rkt | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 typed-racket-test/succeed/sequenceof-integer.rkt diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index 58242cd6..435b6931 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -10,7 +10,7 @@ (env type-name-env row-constraint-env) (rep core-rep rep-utils free-ids type-mask values-rep base-types numeric-base-types) - (types resolve utils printer match-expanders union) + (types resolve utils printer match-expanders union subtype) (prefix-in t: (types abbrev numeric-tower subtype)) (private parse-type syntax-properties) racket/match racket/syntax racket/list @@ -525,6 +525,11 @@ [(? Fun? t) (t->sc/fun t)] [(? DepFun? t) (t->sc/fun t)] [(Set: t) (set/sc (t->sc t))] + [(Sequence: (list t)) + #:when (subtype t:-Nat t) + ;; sequence/c is always a wrapper, so avoid it when we just have a number + (or/sc (flat/sc #'exact-nonnegative-integer?) + (sequence/sc (t->sc t)))] [(Sequence: ts) (apply sequence/sc (map t->sc ts))] [(Vector: t) (vectorof/sc (t->sc/both t))] [(HeterogeneousVector: ts) (apply vector/sc (map t->sc/both ts))] diff --git a/typed-racket-test/succeed/sequenceof-integer.rkt b/typed-racket-test/succeed/sequenceof-integer.rkt new file mode 100644 index 00000000..44997768 --- /dev/null +++ b/typed-racket-test/succeed/sequenceof-integer.rkt @@ -0,0 +1,19 @@ +#lang racket/base + +(module typed typed/racket/base + (provide foo) + (: foo (-> (U Integer (Sequenceof Integer)) String)) + (define (foo x) + (if (integer? x) + (format "I got an integer: ~a" x) + (error "I did not get an integer: ~a" x)))) + +(module other-typed typed/racket/base + (provide bar) + (require (submod ".." typed)) + (define (bar) (foo 0))) + +(require 'typed + 'other-typed) +(foo 0) +(bar)