From b4b1a3ad95cdfdce2958274fd16f6d1f80a89f73 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 01/20] Fix 10 occurrences of `let-to-define` Internal definitions are recommended instead of `let` expressions, to reduce nesting. --- .../typed-racket/private/parse-type.rkt | 23 ++--- .../typed-racket/private/shallow-rewrite.rkt | 22 +++-- .../typed-racket/private/type-annotation.rkt | 83 ++++++++++--------- .../typed-racket/private/type-contract.rkt | 12 ++- .../typed-racket/typecheck/tc-app-helper.rkt | 45 ++++++---- .../typed-racket/typecheck/tc-literal.rkt | 6 +- .../typed-racket/typecheck/tc-structs.rkt | 6 +- 7 files changed, 103 insertions(+), 94 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/parse-type.rkt b/typed-racket-lib/typed-racket/private/parse-type.rkt index 81497c8d1..f454805eb 100644 --- a/typed-racket-lib/typed-racket/private/parse-type.rkt +++ b/typed-racket-lib/typed-racket/private/parse-type.rkt @@ -183,8 +183,8 @@ ;; (Syntax -> Type) -> Syntax Any -> Syntax ;; See `parse-type/id`. This is a curried generalization. (define ((parse/id p) loc datum) - (let* ([stx* (datum->syntax loc datum loc loc)]) - (p stx*))) + (define stx* (datum->syntax loc datum loc loc)) + (p stx*)) (define (parse-literal-alls stx) (syntax-parse stx @@ -902,15 +902,16 @@ (k Err) (remove-duplicates res))) ([ty (in-syntax #'(tys ...))]) - (let ([t (do-parse ty)]) - (match (resolve t) - [(Fun: arrows) (values (append res arrows) err?)] - [_ (if (side-effect-mode? mode) - (values res #t) - (parse-error - #:stx ty - "expected a function type for component of case-> type" - "given" t))])))) + (define t (do-parse ty)) + (match (resolve t) + [(Fun: arrows) (values (append res arrows) err?)] + [_ + (if (side-effect-mode? mode) + (values res #t) + (parse-error #:stx ty + "expected a function type for component of case-> type" + "given" + t))]))) (make-Fun arrows))] [(:Rec^ x:id t) (let* ([var (syntax-e #'x)]) diff --git a/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt b/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt index 91dad491f..4f3dc3aa6 100644 --- a/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt +++ b/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt @@ -415,9 +415,9 @@ stx) (define (maybe-add-typeof-expr new-stx old-stx) - (let ((old-type (maybe-type-of old-stx))) - (when old-type - (add-typeof-expr new-stx old-type)))) + (define old-type (maybe-type-of old-stx)) + (when old-type + (add-typeof-expr new-stx old-type))) (define (maybe-add-test-position new-stx old-stx) (maybe-add-test-true new-stx old-stx) @@ -425,9 +425,9 @@ (void)) (define (maybe-add-scoped-tvar new-stx old-stx) - (let ([old-layer (lookup-scoped-tvar-layer old-stx)]) - (when old-layer - (add-scoped-tvars new-stx old-layer)))) + (define old-layer (lookup-scoped-tvar-layer old-stx)) + (when old-layer + (add-scoped-tvars new-stx old-layer))) (define (maybe-add-test-true new-stx old-stx) (when (test-position-takes-true-branch old-stx) @@ -629,12 +629,10 @@ (λ (mpi) (hash-ref! cache mpi (λ () ;; Typed Racket always installs a `#%type-decl` submodule - (let* ([mpi+ (module-path-index-join '(submod "." #%type-decl) mpi)]) - (parameterize ([current-namespace (make-base-namespace)]) - (with-handlers ([exn:fail:contract? (lambda (exn) #f)]) - (and mpi+ - (dynamic-require mpi+ #f) - #t))))))))) + (define mpi+ (module-path-index-join '(submod "." #%type-decl) mpi)) + (parameterize ([current-namespace (make-base-namespace)]) + (with-handlers ([exn:fail:contract? (lambda (exn) #f)]) + (and mpi+ (dynamic-require mpi+ #f) #t)))))))) (define (protect-domain dom-type dom-stx ctx ctc-cache) (define-values [extra-def* ctc-stx] diff --git a/typed-racket-lib/typed-racket/private/type-annotation.rkt b/typed-racket-lib/typed-racket/private/type-annotation.rkt index c714b5805..a826de157 100644 --- a/typed-racket-lib/typed-racket/private/type-annotation.rkt +++ b/typed-racket-lib/typed-racket/private/type-annotation.rkt @@ -89,46 +89,49 @@ (listof tc-result?)) (match stxs [(list stx ...) - (let ([anns (for/list ([s (in-list stxs)]) - (cond - ;; if the lhs identifier is the rest parameter, its type is - ;; (Listof ty), where ty is the annotated type - [(rst-arg-property s) - (make-Listof (type-annotation s #:infer #t))] - [else (type-annotation s #:infer #t)]))]) - (if (for/and ([a (in-list anns)]) a) - (match (tc-expr/check expr (ret anns)) - [(tc-results: tcrs _) tcrs]) - (match (tc-expr expr) - [(tc-any-results: _) - (tc-error/expr - #:return (map (λ _ (-tc-result -Bottom)) stxs) - "Expression should produce ~a values, but produces an unknown number of values" - (length stxs))] - [(tc-result1: (== -Bottom)) - (for/list ([_ (in-range (length stxs))]) - (-tc-result -Bottom))] - [(tc-results: tcrs _) - (cond - [(not (= (length stxs) (length tcrs))) - (tc-error/expr #:return (map (λ _ (-tc-result -Bottom)) stxs) - "Expression should produce ~a values, but produces ~a values of types ~a" - (length stxs) - (length tcrs) - (stringify (map tc-result-t tcrs)))] - [else - (for/list ([stx (in-list stxs)] - [tcr (in-list tcrs)] - [a (in-list anns)]) - (match tcr - [(tc-result: ty ps o) - (cond [a (check-type stx ty a) - (-tc-result a ps o)] - ;; mutated variables get generalized, so that we don't - ;; infer too small a type - [(is-var-mutated? stx) - (-tc-result (generalize ty) ps o)] - [else (-tc-result ty ps o)])]))])])))])) + (define anns + (for/list ([s (in-list stxs)]) + (cond + ;; if the lhs identifier is the rest parameter, its type is + ;; (Listof ty), where ty is the annotated type + [(rst-arg-property s) (make-Listof (type-annotation s #:infer #t))] + [else (type-annotation s #:infer #t)]))) + (if (for/and ([a (in-list anns)]) + a) + (match (tc-expr/check expr (ret anns)) + [(tc-results: tcrs _) tcrs]) + (match (tc-expr expr) + [(tc-any-results: _) + (tc-error/expr + #:return (map (λ _ (-tc-result -Bottom)) stxs) + "Expression should produce ~a values, but produces an unknown number of values" + (length stxs))] + [(tc-result1: (== -Bottom)) + (for/list ([_ (in-range (length stxs))]) + (-tc-result -Bottom))] + [(tc-results: tcrs _) + (cond + [(not (= (length stxs) (length tcrs))) + (tc-error/expr + #:return (map (λ _ (-tc-result -Bottom)) stxs) + "Expression should produce ~a values, but produces ~a values of types ~a" + (length stxs) + (length tcrs) + (stringify (map tc-result-t tcrs)))] + [else + (for/list ([stx (in-list stxs)] + [tcr (in-list tcrs)] + [a (in-list anns)]) + (match tcr + [(tc-result: ty ps o) + (cond + [a + (check-type stx ty a) + (-tc-result a ps o)] + ;; mutated variables get generalized, so that we don't + ;; infer too small a type + [(is-var-mutated? stx) (-tc-result (generalize ty) ps o)] + [else (-tc-result ty ps o)])]))])]))])) ;; check that e-type is compatible with ty in context of stx ;; otherwise, error diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index 988018b7b..7af3da31f 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -554,13 +554,11 @@ ;; Avoid putting (-> any T) contracts on struct predicates (where Boolean <: T) ;; Optimization: if the value is typed, we can assume it's not wrapped ;; in a type-unsafe chaperone/impersonator and use the unsafe contract - (let* ([unsafe-spp/sc (flat/sc #'struct-predicate-procedure?)] - [safe-spp/sc (flat/sc #'struct-predicate-procedure?/c)] - [optimized/sc (if (from-typed? typed-side) - unsafe-spp/sc - safe-spp/sc)] - [spt-pred-procedure?/sc (flat/sc #'struct-type-property-predicate-procedure?)]) - (or/sc optimized/sc spt-pred-procedure?/sc (t->sc/fun t)))] + (define unsafe-spp/sc (flat/sc #'struct-predicate-procedure?)) + (define safe-spp/sc (flat/sc #'struct-predicate-procedure?/c)) + (define optimized/sc (if (from-typed? typed-side) unsafe-spp/sc safe-spp/sc)) + (define spt-pred-procedure?/sc (flat/sc #'struct-type-property-predicate-procedure?)) + (or/sc optimized/sc spt-pred-procedure?/sc (t->sc/fun t))] [(? Fun? t) (t->sc/fun t)] [(? DepFun? t) (t->sc/fun t)] [(Set: t) (set/sc (t->sc t))] diff --git a/typed-racket-lib/typed-racket/typecheck/tc-app-helper.rkt b/typed-racket-lib/typed-racket/typecheck/tc-app-helper.rkt index 4dd83fe35..77e6ea206 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-app-helper.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-app-helper.rkt @@ -379,25 +379,34 @@ msg-vars (Fun: (list (Arrow: msg-doms msg-rests kws msg-rngs) ...)) _)) - (let ([fcn-string (if name - (format "function with keywords ~a" (syntax->datum name)) - "function with keywords")]) - (if (and (andmap null? msg-doms) - (null? argtypes)) - (tc-error/expr (string-append - "Could not infer types for applying polymorphic " + (define fcn-string + (if name + (format "function with keywords ~a" (syntax->datum name)) + "function with keywords")) + (if (and (andmap null? msg-doms) (null? argtypes)) + (tc-error/expr + (string-append "Could not infer types for applying polymorphic " fcn-string "\n")) + (domain-mismatches + f-stx + args-stx + t + msg-doms + msg-rests + msg-rngs + argtypes + #f + #f + #:expected expected + #:msg-thunk + (lambda (dom) + (string-append "Polymorphic " fcn-string - "\n")) - (domain-mismatches f-stx args-stx t msg-doms msg-rests - msg-rngs argtypes #f #f #:expected expected - #:msg-thunk (lambda (dom) - (string-append - "Polymorphic " fcn-string " could not be applied to arguments:\n" - dom - (if (not (subset? (apply set-union (seteq) (map fv/list msg-doms)) - (list->seteq msg-vars))) - (string-append "Type Variables: " (stringify msg-vars) "\n") - ""))))))])) + " could not be applied to arguments:\n" + dom + (if (not (subset? (apply set-union (seteq) (map fv/list msg-doms)) + (list->seteq msg-vars))) + (string-append "Type Variables: " (stringify msg-vars) "\n") + "")))))])) ;; name->function-str : (Option Identifier) -> String ;; Produce a function name string for error messages diff --git a/typed-racket-lib/typed-racket/typecheck/tc-literal.rkt b/typed-racket-lib/typed-racket/typecheck/tc-literal.rkt index 182f32e8f..728a718dc 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-literal.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-literal.rkt @@ -190,9 +190,9 @@ [vt (apply Un vts)]) (tycon (check-below kt expected-kt) (check-below vt expected-vt)))] [(check-element h tycon) - (let ([kt (generalize (apply Un (map check-element (hash-keys h))))] - [vt (generalize (apply Un (map check-element (hash-values h))))]) - (tycon kt vt))])) + (define kt (generalize (apply Un (map check-element (hash-keys h))))) + (define vt (generalize (apply Un (map check-element (hash-values h))))) + (tycon kt vt)])) ;; Typecheck a prefab struct literal (or result of syntax-e) ;; `check-field` allows prefabs in syntax to be checked by passing diff --git a/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt b/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt index 66496b11b..54ff1efaf 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt @@ -413,9 +413,9 @@ (for/list ([opname (in-list operators)] [self-fld (in-list self-fields)] [idx-parent-cnt (in-naturals parent-count)]) - (let-values ([(fn-args poly-ty) (mk-vals opname self-fld idx-parent-cnt st-type-alias)]) - (apply add-struct-operator-fn! opname fn-args) - (make-def-binding opname poly-ty)))) + (define-values (fn-args poly-ty) (mk-vals opname self-fld idx-parent-cnt st-type-alias)) + (apply add-struct-operator-fn! opname fn-args) + (make-def-binding opname poly-ty))) (define bindings (list* (make-def-binding struct-type (make-StructType sty)) From 478293eecb079898faa4d1f76882e15cd1292c6b Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 02/20] Fix 8 occurrences of `single-clause-match-to-match-define` This `match` expression can be simplified using `match-define`. --- .../typed-racket/private/type-contract.rkt | 42 +++++++----------- .../private/user-defined-type-constr.rkt | 9 ++-- .../typecheck/check-class-unit.rkt | 8 ++-- .../typed-racket/typecheck/tc-apply.rkt | 27 ++++++++---- .../typed-racket/typecheck/tc-if.rkt | 44 +++++++++---------- .../typed-racket/typecheck/tc-structs.rkt | 13 +++--- 6 files changed, 69 insertions(+), 74 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index 7af3da31f..cec7c85b9 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -1163,10 +1163,8 @@ ;; Match the range of an arr and determine if a contract can be generated ;; and call the given thunk or raise an error (define (handle-arrow-range arrow proceed) - (match arrow - [(or (Arrow: _ _ _ rng) - (DepFun: _ _ rng)) - (handle-range rng proceed)])) + (match-define (or (Arrow: _ _ _ rng) (DepFun: _ _ rng)) arrow) + (handle-range rng proceed)) (define (handle-range rng proceed) (match rng [(Values: (list (Result: _ @@ -1291,28 +1289,20 @@ arrows)))])] [(DepFun/ids: ids dom pre rng) (define (continue) - (match rng - [(Values: (list (Result: rngs _ _) ...)) - (define (dom-id? id) (member id ids free-identifier=?)) - (define-values (dom* dom-deps) - (for/lists (_1 _2) ([d (in-list dom)]) - (values (t->sc/neg d) - (filter dom-id? (free-ids d))))) - (define pre* (if (TrueProp? pre) #f (t->sc/neg pre))) - (define pre-deps (filter dom-id? (free-ids pre))) - (define rng* (map t->sc rngs)) - (define rng-deps (filter dom-id? - (remove-duplicates - (apply append (map free-ids rngs)) - free-identifier=?))) - (->i/sc (from-typed? typed-side) - ids - dom* - dom-deps - pre* - pre-deps - rng* - rng-deps)])) + (match-define (Values: (list (Result: rngs _ _) ...)) rng) + (define (dom-id? id) + (member id ids free-identifier=?)) + (define-values (dom* dom-deps) + (for/lists (_1 _2) ([d (in-list dom)]) (values (t->sc/neg d) (filter dom-id? (free-ids d))))) + (define pre* + (if (TrueProp? pre) + #f + (t->sc/neg pre))) + (define pre-deps (filter dom-id? (free-ids pre))) + (define rng* (map t->sc rngs)) + (define rng-deps + (filter dom-id? (remove-duplicates (apply append (map free-ids rngs)) free-identifier=?))) + (->i/sc (from-typed? typed-side) ids dom* dom-deps pre* pre-deps rng* rng-deps)) (handle-range rng continue)])) ;; Generate a contract for a object/class method clause diff --git a/typed-racket-lib/typed-racket/private/user-defined-type-constr.rkt b/typed-racket-lib/typed-racket/private/user-defined-type-constr.rkt index 0f9f51ee3..d0512c0c3 100644 --- a/typed-racket-lib/typed-racket/private/user-defined-type-constr.rkt +++ b/typed-racket-lib/typed-racket/private/user-defined-type-constr.rkt @@ -30,7 +30,8 @@ [_ #f])) (define (recursive-type-constr? constr) - (match constr - [(struct* TypeConstructor - ([real-trep-constr (struct* user-defined-type-op ([recursive? recursive?]))])) - recursive?])) + (match-define (struct* TypeConstructor + ([real-trep-constr + (struct* user-defined-type-op ([recursive? recursive?]))])) + constr) + recursive?) diff --git a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt index b1d016b23..5427b64d5 100644 --- a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt +++ b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt @@ -290,12 +290,10 @@ [(tc-result1: type) (resolve type)] [_ #f])) (match expected-type - [(? Class? class-type) - (ret (parse-and-check form class-type))] + [(? Class? class-type) (ret (parse-and-check form class-type))] [(Poly-names: ns body-type) - (match (check-class form (ret body-type)) - [(tc-result1: t f o) - (ret (make-Poly ns t) f o)])] + (match-define (tc-result1: t f o) (check-class form (ret body-type))) + (ret (make-Poly ns t) f o)] [_ (ret (parse-and-check form #f))])) ;; Syntax Option -> Type diff --git a/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt b/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt index 26d84d985..3aeb3055e 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt @@ -50,15 +50,24 @@ ;; Raises an error message for the case that the arguments do not match any of the domains (define (failure) - (match f-ty - [(tc-result1: - (and t (AnyPoly-names: _ _ - (Fun: (list (Arrow: doms rests (list (Keyword: _ _ #f) ...) rngs) ..1))))) - (domain-mismatches f args t doms rests rngs arg-tres full-tail-ty #f - #:msg-thunk (lambda (dom) - (string-append - "Bad arguments to function in `apply':\n" - dom)))])) + (match-define (tc-result1: (and t + (AnyPoly-names: + _ + _ + (Fun: (list (Arrow: doms rests (list (Keyword: _ _ #f) ...) rngs) + ..1))))) + f-ty) + (domain-mismatches f + args + t + doms + rests + rngs + arg-tres + full-tail-ty + #f + #:msg-thunk + (lambda (dom) (string-append "Bad arguments to function in `apply':\n" dom)))) (match f-ty ;; apply of a simple function or polymorphic function diff --git a/typed-racket-lib/typed-racket/typecheck/tc-if.rkt b/typed-racket-lib/typed-racket/typecheck/tc-if.rkt index 4cea80d0c..7e1e97a8a 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-if.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-if.rkt @@ -15,27 +15,25 @@ (export tc-if^) (define (tc/if-twoarm tst thn els [expected #f]) - (match (single-value tst) - [(tc-result1: _ (PropSet: p+ p-) _) - (define thn-res - (with-lexical-env+props (list p+) - #:expected expected - #:unreachable (warn-unreachable thn) - (test-position-add-true tst) - (tc-expr/check thn expected))) - (define els-res - (with-lexical-env+props (list p-) - #:expected expected - #:unreachable (warn-unreachable els) - (test-position-add-false tst) - (tc-expr/check els expected))) + (match-define (tc-result1: _ (PropSet: p+ p-) _) (single-value tst)) + (define thn-res + (with-lexical-env+props (list p+) + #:expected expected + #:unreachable (warn-unreachable thn) + (test-position-add-true tst) + (tc-expr/check thn expected))) + (define els-res + (with-lexical-env+props (list p-) + #:expected expected + #:unreachable (warn-unreachable els) + (test-position-add-false tst) + (tc-expr/check els expected))) - (match expected - ;; if there was not any expected results, then merge the 'then' - ;; and 'else' results so we propogate the correct info upwards - [(or #f (tc-any-results: #f)) - (merge-tc-results (list thn-res els-res))] - ;; otherwise, the subcomponents have already been checked and - ;; we just return the expected result 'fixed' to replace any - ;; missing fields (i.e. #f props or objects) - [_ (fix-results expected)])])) + (match expected + ;; if there was not any expected results, then merge the 'then' + ;; and 'else' results so we propogate the correct info upwards + [(or #f (tc-any-results: #f)) (merge-tc-results (list thn-res els-res))] + ;; otherwise, the subcomponents have already been checked and + ;; we just return the expected result 'fixed' to replace any + ;; missing fields (i.e. #f props or objects) + [_ (fix-results expected)])) diff --git a/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt b/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt index 54ff1efaf..ad7b816a8 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-structs.rkt @@ -193,10 +193,10 @@ (if (null? l) (values (reverse getters) (reverse setters)) (loop (cddr l) (cons (car l) getters) (cons (cadr l) setters))))) - (match (build-struct-names nm flds #f #f nm #:constructor-name maker*) - [(list sty maker pred getters/setters ...) - (let-values ([(getters setters) (split getters/setters)]) - (struct-names nm type-name sty maker extra-maker pred getters setters))])) + (match-define (list sty maker pred getters/setters ...) + (build-struct-names nm flds #f #f nm #:constructor-name maker*)) + (let-values ([(getters setters) (split getters/setters)]) + (struct-names nm type-name sty maker extra-maker pred getters setters))) ;; gets the fields of the parent type, if they exist ;; Option[Struct-Ty] -> Listof[Type] @@ -458,9 +458,8 @@ def-bindings)))) (define (register-parsed-struct-sty! ps) - (match ps - ((parsed-struct sty names desc si) - (register-sty! sty names desc)))) + (match-define (parsed-struct sty names desc si) ps) + (register-sty! sty names desc)) (define (register-parsed-struct-bindings! ps) (match ps From 569e2260f7dc85d7f96f0b19522e74fb135da90a Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 03/20] Fix 1 occurrence of `define-begin0-extraction` The `begin0` in this definition can be extracted into the surrounding definition context. --- typed-racket-lib/typed-racket/typecheck/tc-apply.rkt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt b/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt index 3aeb3055e..101173790 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-apply.rkt @@ -83,9 +83,8 @@ ;; Takes a possible substitution and computes ;; the substituted range type if it is not #f (define (finish substitution) - (begin0 - (and substitution (do-ret (subst-all substitution rng))) - (add-typeof-expr f (ret (make-Fun (list arrow)))))) + (and substitution (do-ret (subst-all substitution rng)))) + (add-typeof-expr f (ret (make-Fun (list arrow)))) (finish (infer vars dotted-vars (list (-Tuple* arg-tys full-tail-ty)) From 715a8d1ccb0c1619f31380e580f4a6f7c9370ac2 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 04/20] Fix 1 occurrence of `and-match-to-match` This `and` expression can be turned into a clause of the inner `match` expression, reducing nesting. --- .../typed-racket/typecheck/possible-domains.rkt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/typed-racket-lib/typed-racket/typecheck/possible-domains.rkt b/typed-racket-lib/typed-racket/typecheck/possible-domains.rkt index 6e0c8b46a..9be78c7ae 100644 --- a/typed-racket-lib/typed-racket/typecheck/possible-domains.rkt +++ b/typed-racket-lib/typed-racket/typecheck/possible-domains.rkt @@ -55,11 +55,11 @@ ;; currently does not take advantage of multi-valued or arbitrary-valued expected types, (define expected-ty - (and expected - (match expected - [(tc-result1: t) t] - [(tc-any-results: (or #f (TrueProp:))) #t] ; anything is a subtype of expected - [_ #f]))) ; don't know what it is, don't do any pruning + (match expected + [#f #f] + [(tc-result1: t) t] + [(tc-any-results: (or #f (TrueProp:))) #t] ; anything is a subtype of expected + [_ #f])) ; don't know what it is, don't do any pruning (define (returns-subtype-of-expected? fun-ty) (or (not expected) ; no expected type, anything is fine (eq? expected-ty #t) ; expected is tc-anyresults, anything is fine From 76206de35827646c27dbb3161f48e6fc522adf3a Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 05/20] Fix 3 occurrences of `zero-comparison-to-positive?` This expression is equivalent to calling the `positive?` predicate. --- .../typed-racket/typecheck/check-class-unit.rkt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt index 5427b64d5..eb350dd66 100644 --- a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt +++ b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt @@ -213,7 +213,7 @@ opt:opt-lambda^) ;; it's only an interesting opt-lambda expansion if the number ;; of optional arguments is greater than zero - #:when (> (cadr (attribute opt.value)) 0) + #:when (positive? (cadr (attribute opt.value))) #:do [(register/method #'meth-name)] #:with props-core (let* ([prop-val (attribute opt.value)] @@ -1330,10 +1330,10 @@ (match-define (super-init-stxs provided-pos-args provided-inits) super-new) (define pos-init-diff (- (length provided-pos-args) (length super-inits))) - (cond [(and (> pos-init-diff 0) (not init-rest)) + (cond [(and (positive? pos-init-diff) (not init-rest)) ;; errror case that's caught above, do nothing (void)] - [(> pos-init-diff 0) + [(positive? pos-init-diff) (define-values (pos-args for-init-rest) (split-at provided-pos-args (length super-inits))) (for ([pos-arg pos-args] From 9b0f222e111cf1df67fa7a00711301a2264223be Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 06/20] Fix 3 occurrences of `define-values-values-to-define` This use of `define-values` is unnecessary. --- .../typecheck/check-class-unit.rkt | 31 +++++++------------ .../typed-racket/typecheck/tc-let-unit.rkt | 4 +-- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt index eb350dd66..3a109fcac 100644 --- a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt +++ b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt @@ -712,18 +712,12 @@ (localize local-augment-table 'augment-internals) (localize local-inner-table '(pubment-internals augment-internals)) (localize local-init-table 'only-init-internals))) - (define-values (localized-field-get-names - localized-field-set-names - localized-private-field-get-names - localized-private-field-set-names - localized-inherit-field-get-names - localized-inherit-field-set-names) - (values (map car localized-field-pairs) - (map cadr localized-field-pairs) - (map car localized-private-field-pairs) - (map cadr localized-private-field-pairs) - (map car localized-inherit-field-pairs) - (map cadr localized-inherit-field-pairs))) + (define localized-field-get-names (map car localized-field-pairs)) + (define localized-field-set-names (map cadr localized-field-pairs)) + (define localized-private-field-get-names (map car localized-private-field-pairs)) + (define localized-private-field-set-names (map cadr localized-private-field-pairs)) + (define localized-inherit-field-get-names (map car localized-inherit-field-pairs)) + (define localized-inherit-field-set-names (map cadr localized-inherit-field-pairs)) ;; construct the types for method accessors (define (make-method-types method-names type-map @@ -1428,13 +1422,12 @@ [(Class: _ inits fields publics augments init-rest) (values inits fields publics augments init-rest)] [_ (values #f #f #f #f #f)])) - (define-values (inits fields publics pubments overrides init-rest-name) - (values (hash-ref parse-info 'init-internals) - (hash-ref parse-info 'field-internals) - (hash-ref parse-info 'public-internals) - (hash-ref parse-info 'pubment-internals) - (hash-ref parse-info 'override-internals) - (hash-ref parse-info 'init-rest-name))) + (define inits (hash-ref parse-info 'init-internals)) + (define fields (hash-ref parse-info 'field-internals)) + (define publics (hash-ref parse-info 'public-internals)) + (define pubments (hash-ref parse-info 'pubment-internals)) + (define overrides (hash-ref parse-info 'override-internals)) + (define init-rest-name (hash-ref parse-info 'init-rest-name)) (define init-types (make-inits inits super-inits expected-inits)) (define field-types (make-type-dict fields super-fields expected-fields Univ)) diff --git a/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt b/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt index 89ef796d4..9d1090a79 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt @@ -284,8 +284,8 @@ (if (null? names) (values (cons clause non-binding) other-clauses) (values non-binding (cons clause other-clauses))))) - (define-values (non-binding other-clauses) - (values (reverse *non-binding) (reverse *other-clauses))) + (define non-binding (reverse *non-binding)) + (define other-clauses (reverse *other-clauses)) ;; Set up vertices for Tarjan's algorithm, where each letrec-values ;; clause is a vertex but mapped in the table for each of the clause names From 9931b8133dda3cf7bf284dab247358ec6d13beb4 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 07/20] Fix 1 occurrence of `unless-expression-in-for-loop-to-unless-keyword` Use the `#:unless` keyword instead of `unless` to reduce loop body indentation. --- .../typed-racket/typecheck/check-class-unit.rkt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt index 3a109fcac..3e01248bd 100644 --- a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt +++ b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt @@ -1309,14 +1309,14 @@ ;; Check that by-name inits are valid for the superclass (define (check-by-name init-stxs super-inits) (match-define (super-init-stxs _ by-name) init-stxs) - (for ([(name _) (in-dict by-name)]) - (unless (dict-ref super-inits name #f) - (tc-error/fields - "invalid `super-new' or `super-instantiate'" - #:more "init argument not accepted by superclass" - "init name" name - #:stx #`#,name - #:delayed? #t)))) + (for ([(name _) (in-dict by-name)] + #:unless (dict-ref super-inits name #f)) + (tc-error/fields "invalid `super-new' or `super-instantiate'" + #:more "init argument not accepted by superclass" + "init name" + name + #:stx #`#,name + #:delayed? #t))) ;; check-super-new : super-init-stxs Dict Type -> Void ;; Check if the super-new call is well-typed From f006b00488af624add09cadbf5fcca710c3f68b8 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 08/20] Fix 1 occurrence of `when-expression-in-for-loop-to-when-keyword` Use the `#:when` keyword instead of `when` to reduce loop body indentation. --- .../typed-racket/typecheck/check-class-unit.rkt | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt index 3e01248bd..8b81d8e48 100644 --- a/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt +++ b/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt @@ -1353,12 +1353,9 @@ ;; the pubment types as default augment types if an augment type ;; was not already provided (define (setup-pubment-defaults pubment-names annotations augment-annotations) - (for ([name pubment-names]) - (when (and (not (hash-has-key? augment-annotations name)) - (hash-has-key? annotations name)) - (hash-set! augment-annotations - name - (dict-ref annotations name))))) + (for ([name pubment-names] + #:when (and (not (hash-has-key? augment-annotations name)) (hash-has-key? annotations name))) + (hash-set! augment-annotations name (dict-ref annotations name)))) ;; infer-self-type : Dict RowVar Class Dict Dict ;; Set Dict From 74f311cff420621366474b808d289b0d86dcdcfe Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 09/20] Fix 4 occurrences of `if-begin-to-cond` Using `cond` instead of `if` here makes `begin` unnecessary --- .../typed-racket/private/parse-type.rkt | 11 +++++------ .../typed-racket/private/type-contract.rkt | 9 +++++---- .../typecheck/toplevel-trampoline.rkt | 16 ++++++++-------- typed-racket-test/external/tr-random-testing.rkt | 10 +++++----- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/parse-type.rkt b/typed-racket-lib/typed-racket/private/parse-type.rkt index f454805eb..846ed3d87 100644 --- a/typed-racket-lib/typed-racket/private/parse-type.rkt +++ b/typed-racket-lib/typed-racket/private/parse-type.rkt @@ -831,12 +831,11 @@ refinement-type] [(:Instance^ t) (let ([v (do-parse #'t)]) - (if (not (or (F? v) (Mu? v) (Name? v) (Class? v) (Error? v))) - (begin (parse-error #:delayed? #t - "expected a class type for argument to Instance" - "given" v) - (make-Instance (Un))) - (make-Instance v)))] + (cond + [(not (or (F? v) (Mu? v) (Name? v) (Class? v) (Error? v))) + (parse-error #:delayed? #t "expected a class type for argument to Instance" "given" v) + (make-Instance (Un))] + [else (make-Instance v)]))] [(:Unit^ (:import^ import:id ...) (:export^ export:id ...) (~optional (:init-depend^ init-depend:id ...) diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index cec7c85b9..d47e9c1fd 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -239,10 +239,11 @@ (define (change-contract-fixups forms [ctc-cache (make-hash)]) (with-new-name-tables (for/list ((e (in-list forms))) - (if (not (has-contract-def-property? e)) - e - (begin (set-box! include-extra-requires? #t) - (generate-contract-def e ctc-cache)))))) + (cond + [(not (has-contract-def-property? e)) e] + [else + (set-box! include-extra-requires? #t) + (generate-contract-def e ctc-cache)])))) ;; TODO: These are probably all in a specific place, which could avoid ;; the big traversal diff --git a/typed-racket-lib/typed-racket/typecheck/toplevel-trampoline.rkt b/typed-racket-lib/typed-racket/typecheck/toplevel-trampoline.rkt index 29aacdf6c..23c258b33 100644 --- a/typed-racket-lib/typed-racket/typecheck/toplevel-trampoline.rkt +++ b/typed-racket-lib/typed-racket/typecheck/toplevel-trampoline.rkt @@ -61,14 +61,14 @@ (define-for-syntax (maybe-optimize body) ;; do we optimize? - (if (and (optimize?) - (memq (current-type-enforcement-mode) (list deep shallow)) - (not (getenv "PLT_TR_NO_OPTIMIZE"))) - (begin - (do-time "Starting optimizer") - (begin0 (stx-map optimize-top body) - (do-time "Optimized"))) - body)) + (cond + [(and (optimize?) + (memq (current-type-enforcement-mode) (list deep shallow)) + (not (getenv "PLT_TR_NO_OPTIMIZE"))) + (do-time "Starting optimizer") + (begin0 (stx-map optimize-top body) + (do-time "Optimized"))] + [else body])) (define-for-syntax (maybe-shallow-rewrite body-stx ctc-cache) (case (current-type-enforcement-mode) diff --git a/typed-racket-test/external/tr-random-testing.rkt b/typed-racket-test/external/tr-random-testing.rkt index cdb13b790..453bf089a 100644 --- a/typed-racket-test/external/tr-random-testing.rkt +++ b/typed-racket-test/external/tr-random-testing.rkt @@ -334,11 +334,11 @@ ))) (or both-failed? (and (not racket-failed?) - (if (same-result? racket-result racketbc-result) - #t - (begin (printf "not same as bc: racketcs: ~s racketbc: ~s\n" - racket-result racketbc-result) - #f))))) + (cond + [(same-result? racket-result racketbc-result) #t] + [else + (printf "not same as bc: racketcs: ~s racketbc: ~s\n" racket-result racketbc-result) + #f])))) (define num-exceptions 0) From 1c29459f1068e3f03755d69529d2a63a901f773b Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 10/20] Fix 3 occurrences of `if-let-to-cond` `cond` with internal definitions is preferred over `if` with `let`, to reduce nesting --- .../typed-racket/private/shallow-rewrite.rkt | 171 ++++++++++-------- 1 file changed, 95 insertions(+), 76 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt b/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt index 4f3dc3aa6..2406fb7e3 100644 --- a/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt +++ b/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt @@ -222,36 +222,48 @@ [check-formal* (let protect-loop ([args #'formals] [dom* dom*]) - (if (or (identifier? args) - (null? args) - (and (syntax? args) (null? (syntax-e args)))) - '() - (let*-values ([(fst rst) - (cond - [(pair? args) - (values (car args) (cdr args))] - [(syntax? args) - (let ((e (syntax-e args))) - (values (car e) (cdr e)))] - [else - (raise-syntax-error 'shallow-rewrite-top "#%plain-lambda formals" #'formals args)])] - [(check*) - (let ((dom+ - (for/fold ((acc '())) - ((dom (in-list dom*))) - (if (pair? dom) (cons (cdr dom) acc) acc)))) - (protect-loop rst dom+))] - [(fst-ty) - (let ((ann-ty (and (type-annotation fst #:infer #f) (get-type fst #:infer #t #:default Univ)))) - (if (and ann-ty (not (Error? ann-ty))) - ann-ty - (apply Un (for/list ((dom (in-list dom*)) #:when (pair? dom)) (car dom)))))] - [(ex* fst+) - (if skip-dom? - (values '() #f) - (protect-domain fst-ty fst (build-source-location-list fst) ctc-cache))]) - (void (register-extra-defs! ex*)) - (if fst+ (cons fst+ check*) check*))))]) + (cond + [(or (identifier? args) + (null? args) + (and (syntax? args) (null? (syntax-e args)))) + '()] + [else + (define-values (fst rst) + (cond + [(pair? args) (values (car args) (cdr args))] + [(syntax? args) + (let ([e (syntax-e args)]) (values (car e) (cdr e)))] + [else + (raise-syntax-error 'shallow-rewrite-top + "#%plain-lambda formals" + #'formals + args)])) + (define check* + (let ([dom+ (for/fold ([acc '()]) ([dom (in-list dom*)]) + (if (pair? dom) + (cons (cdr dom) acc) + acc))]) + (protect-loop rst dom+))) + (define fst-ty + (let ([ann-ty (and (type-annotation fst #:infer #f) + (get-type fst #:infer #t #:default Univ))]) + (if (and ann-ty (not (Error? ann-ty))) + ann-ty + (apply Un + (for/list ([dom (in-list dom*)] + #:when (pair? dom)) + (car dom)))))) + (define-values (ex* fst+) + (if skip-dom? + (values '() #f) + (protect-domain fst-ty + fst + (build-source-location-list fst) + ctc-cache))) + (void (register-extra-defs! ex*)) + (if fst+ + (cons fst+ check*) + check*)]))]) (if (null? check-formal*) body+ (cons @@ -284,38 +296,50 @@ [check-formal* (let protect-loop ([args formals] [dom* matching-dom*]) - (if (or (identifier? args) - (null? args) - (and (syntax? args) (null? (syntax-e args)))) - '() - (let*-values ([(fst rst) - (cond - [(pair? args) - (values (car args) (cdr args))] - [(syntax? args) - (let ((e (syntax-e args))) - (values (car e) (cdr e)))] - [else - (raise-syntax-error 'shallow-rewrite-top "#%plain-lambda formals" formals args)])] - [(check*) - (let ((dom+ - (for/fold ((acc '())) - ((dom (in-list dom*))) - (if (pair? dom) (cons (cdr dom) acc) acc)))) - (protect-loop rst dom+))] - [(fst-ty) - (if (type-annotation fst #:infer #f) - (get-type fst #:infer #t #:default Univ) - (apply Un - (for/fold ((acc '())) - ((dom (in-list dom*))) - (if (pair? dom) (cons (car dom) acc) acc))))] - [(ex* fst+) - (if skip-dom? - (values '() #f) - (protect-domain fst-ty fst (build-source-location-list fst) ctc-cache))]) - (void (register-extra-defs! ex*)) - (if fst+ (cons fst+ check*) check*))))]) + (cond + [(or (identifier? args) + (null? args) + (and (syntax? args) (null? (syntax-e args)))) + '()] + [else + (define-values (fst rst) + (cond + [(pair? args) (values (car args) (cdr args))] + [(syntax? args) + (let ([e (syntax-e args)]) + (values (car e) (cdr e)))] + [else + (raise-syntax-error 'shallow-rewrite-top + "#%plain-lambda formals" + formals + args)])) + (define check* + (let ([dom+ (for/fold ([acc '()]) + ([dom (in-list dom*)]) + (if (pair? dom) + (cons (cdr dom) acc) + acc))]) + (protect-loop rst dom+))) + (define fst-ty + (if (type-annotation fst #:infer #f) + (get-type fst #:infer #t #:default Univ) + (apply Un + (for/fold ([acc '()]) + ([dom (in-list dom*)]) + (if (pair? dom) + (cons (car dom) acc) + acc))))) + (define-values (ex* fst+) + (if skip-dom? + (values '() #f) + (protect-domain fst-ty + fst + (build-source-location-list fst) + ctc-cache))) + (void (register-extra-defs! ex*)) + (if fst+ + (cons fst+ check*) + check*)]))]) (if (null? check-formal*) body+ (cons @@ -449,20 +473,15 @@ (define (formals-fold init f stx) (let loop ((v stx)) - (if (or (identifier? v) - (null? v) - (and (syntax? v) (null? (syntax-e v)))) - init - (let*-values (((fst rst) - (cond - [(pair? v) - (values (car v) (cdr v))] - [(syntax? v) - (let ((e (syntax-e v))) - (values (car e) (cdr e)))] - [else - (raise-syntax-error 'formals-fold "lambda formals" stx)]))) - (f (loop rst) fst))))) + (cond + [(or (identifier? v) (null? v) (and (syntax? v) (null? (syntax-e v)))) init] + [else + (define-values (fst rst) + (cond + [(pair? v) (values (car v) (cdr v))] + [(syntax? v) (let ([e (syntax-e v)]) (values (car e) (cdr e)))] + [else (raise-syntax-error 'formals-fold "lambda formals" stx)])) + (f (loop rst) fst)]))) ;; is-application? : Syntax -> Boolean ;; Returns #true if `stx` is a function application (an app that may need dynamic checking) From cc314a9e6c0b1535c76ac625d25396b4a76ada6e Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 11/20] Fix 1 occurrence of `define-let-to-double-define` This `let` expression can be pulled up into a `define` expression. --- typed-racket-lib/typed-racket/private/shallow-rewrite.rkt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt b/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt index 2406fb7e3..bf1d1a124 100644 --- a/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt +++ b/typed-racket-lib/typed-racket/private/shallow-rewrite.rkt @@ -284,11 +284,11 @@ ;; no type (quasisyntax/loc formals [#,formals . #,body])] [else + (define len (formals-length formals)) (define matching-dom* - (let ([len (formals-length formals)]) - (for/list ((dom (in-list all-dom*)) - #:when (= len (length dom))) - dom))) + (for/list ([dom (in-list all-dom*)] + #:when (= len (length dom))) + dom)) (quasisyntax/loc stx [#,formals . #,(let* ([body+ From 73b873bbdb211aded0ec77a4456071a6e1249867 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 12/20] Fix 1 occurrence of `always-throwing-cond-to-when` Using `when` and `unless` is simpler than a conditional with an always-throwing branch. --- typed-racket-lib/typed-racket/private/type-contract.rkt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index d47e9c1fd..6e46baefa 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -834,10 +834,10 @@ [else (is-flat-type/sc (obj->sc o) sc)])] [(NotTypeProp: o t) (define sc (t->sc t bound-all-vars)) - (cond - [(not (equal? flat-sym (get-max-contract-kind sc))) - (raise-user-error 'type->static-contract/shallow "proposition contract generation not supported for non-flat types")] - [else (not-flat-type/sc (obj->sc o) sc)])] + (unless (equal? flat-sym (get-max-contract-kind sc)) + (raise-user-error 'type->static-contract/shallow + "proposition contract generation not supported for non-flat types")) + (not-flat-type/sc (obj->sc o) sc)] [(LeqProp: (app obj->sc lhs) (app obj->sc rhs)) (leq/sc lhs rhs)] [(AndProp: ps) From 59f77fd8c76b2a72a173c152616cea67057d6a76 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 13/20] Fix 1 occurrence of `zero-comparison-lambda-to-positive?` This lambda function is equivalent to the built-in `positive?` predicate. --- typed-racket-lib/typed-racket/private/type-contract.rkt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index 6e46baefa..e082ae544 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -1264,8 +1264,7 @@ (when (and (not (empty? kws))) (fail #:reason (~a "cannot generate contract for case function type" " with optional keyword arguments"))) - (when (ormap (lambda (n-exi) - (> n-exi 0)) + (when (ormap positive? n-exis) (fail #:reason (~a "cannot generate contract for case function type with existentials"))) From faa6a617b15d55c42ffa62ad0603e2f81c89b410 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 14/20] Fix 1 occurrence of `map-to-for` This `map` operation can be replaced with a `for/list` loop. --- typed-racket-lib/typed-racket/private/type-contract.rkt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index e082ae544..2a84840dd 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -1283,10 +1283,8 @@ (handle-arrow-range (first arrows) (lambda () (convert-single-arrow (first arrows)))) - (case->/sc (map (lambda (arr) - (handle-arrow-range arr (lambda () - (convert-one-arrow-in-many arr)))) - arrows)))])] + (case->/sc (for/list ([arr (in-list arrows)]) + (handle-arrow-range arr (lambda () (convert-one-arrow-in-many arr))))))])] [(DepFun/ids: ids dom pre rng) (define (continue) (match-define (Values: (list (Result: rngs _ _) ...)) rng) From 998b89ba00c87d2f69cfafc993e4cf3d258ad6ae Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 15/20] Fix 5 occurrences of `define-lambda-to-define` The `define` form supports a shorthand for defining functions. --- .../typed-racket/private/type-contract.rkt | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/type-contract.rkt b/typed-racket-lib/typed-racket/private/type-contract.rkt index 2a84840dd..da5eaf1a6 100644 --- a/typed-racket-lib/typed-racket/private/type-contract.rkt +++ b/typed-racket-lib/typed-racket/private/type-contract.rkt @@ -1532,11 +1532,16 @@ (require racket/extflonum) (provide nonnegative? nonpositive? extflonum? extflzero? extflnonnegative? extflnonpositive?) - (define nonnegative? (lambda (x) (>= x 0))) - (define nonpositive? (lambda (x) (<= x 0))) - (define extflzero? (lambda (x) (extfl= x 0.0t0))) - (define extflnonnegative? (lambda (x) (extfl>= x 0.0t0))) - (define extflnonpositive? (lambda (x) (extfl<= x 0.0t0)))) + (define (nonnegative? x) + (>= x 0)) + (define (nonpositive? x) + (<= x 0)) + (define (extflzero? x) + (extfl= x 0.0t0)) + (define (extflnonnegative? x) + (extfl>= x 0.0t0)) + (define (extflnonpositive? x) + (extfl<= x 0.0t0))) (module numeric-contracts racket/base (require From 506cef5f305385ea1d5a0b508d387fabc1e50de3 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 16/20] Fix 2 occurrences of `inline-unnecessary-define` This variable is returned immediately and can be inlined. --- .../typed-racket/private/parse-type.rkt | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/parse-type.rkt b/typed-racket-lib/typed-racket/private/parse-type.rkt index 846ed3d87..ccb06e6a2 100644 --- a/typed-racket-lib/typed-racket/private/parse-type.rkt +++ b/typed-racket-lib/typed-racket/private/parse-type.rkt @@ -1401,8 +1401,7 @@ [_ (apply rator args^)])]))] [(? Name?) (resolve-app-check-error rator args^ stx) - (define app (make-App rator args^)) - app] + (make-App rator args^)] [(Error:) Err] [_ (parse-error "bad syntax in type application: expected a type constructor" "given a type" @@ -1655,12 +1654,12 @@ ;; of init arguments. (define parent-inits (get-parent-inits parent/init-type)) - (define class-type - (make-Class row-var - (append given-inits parent-inits) - fields methods augments given-init-rest)) - - class-type] + (make-Class row-var + (append given-inits parent-inits) + fields + methods + augments + given-init-rest)] [else ;; Conservatively assume that if there *are* #:implements ;; clauses, then the current type alias will be recursive From 16d444513f013bb5a9810fe2ab4569795bf52025 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 17/20] Fix 1 occurrence of `for/fold-with-conditional-body-to-unless-keyword` This `for/fold` loop can be simplified by using the `#:unless` keyword. --- typed-racket-lib/typed-racket/private/parse-type.rkt | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/typed-racket-lib/typed-racket/private/parse-type.rkt b/typed-racket-lib/typed-racket/private/parse-type.rkt index ccb06e6a2..5d8c85581 100644 --- a/typed-racket-lib/typed-racket/private/parse-type.rkt +++ b/typed-racket-lib/typed-racket/private/parse-type.rkt @@ -1506,10 +1506,9 @@ ;; Merge all the non-duplicate entries from the parent types (define (merge-clause parent-clause clause) (for/fold ([clause clause]) - ([(k v) (in-dict parent-clause)]) - (if (dict-has-key? clause k) - clause - (dict-set clause k v)))) + ([(k v) (in-dict parent-clause)] + #:unless (dict-has-key? clause k)) + (dict-set clause k v))) (define (match-parent-type parent-type) (define resolved (resolve parent-type)) From 7f45b207d51a19b7a61421191c8b3655875dcf90 Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 18/20] Fix 1 occurrence of `inline-unnecessary-begin` This `begin` form can be flattened into the surrounding definition context. --- .../typed-racket/typecheck/tc-let-unit.rkt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt b/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt index 9d1090a79..6720c4d93 100644 --- a/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt +++ b/typed-racket-lib/typed-racket/typecheck/tc-let-unit.rkt @@ -256,12 +256,12 @@ non-bindings expected #:before-check-body - (λ () (begin (for ([expr (in-list remaining-exprs)] - [results (in-list given-rhs-types)]) - (match results - [(list (tc-result: ts fs os) ...) - (tc-expr/check expr (ret ts fs os))])) - (check-thunk))))]))))) + (λ () + (for ([expr (in-list remaining-exprs)] + [results (in-list given-rhs-types)]) + (match results + [(list (tc-result: ts fs os) ...) (tc-expr/check expr (ret ts fs os))])) + (check-thunk)))]))))) ;; An lr-clause is a ;; (lr-clause (Listof Identifier) Syntax) From 32d2a82f18fb5f1e51488afc2a9bceda83ee24bc Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:06 +0000 Subject: [PATCH 19/20] Fix 1 occurrence of `inverted-when` This negated `when` expression can be replaced by an `unless` expression. --- typed-racket-lib/typed-racket/typecheck/error-message.rkt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typed-racket-lib/typed-racket/typecheck/error-message.rkt b/typed-racket-lib/typed-racket/typecheck/error-message.rkt index 6a04a7bce..fd909f39f 100644 --- a/typed-racket-lib/typed-racket/typecheck/error-message.rkt +++ b/typed-racket-lib/typed-racket/typecheck/error-message.rkt @@ -77,7 +77,7 @@ (define class/object (if object? "object" "class")) (match-define (Class: row inits fields methods augments init-rest) c1) (match-define (Class: row* inits* fields* methods* augments* init-rest*) c2) - (when (not object?) + (unless object? (when (and (F? row) (not (F? row*))) (type-mismatch (format "Class with row variable `~a'" row) (format "Class with no row variable"))) From eb5463da42bbc9ce03e6aaba9f00b239511ce9bd Mon Sep 17 00:00:00 2001 From: "resyntax-ci[bot]" <181813515+resyntax-ci[bot]@users.noreply.github.com> Date: Fri, 7 Mar 2025 00:40:07 +0000 Subject: [PATCH 20/20] Fix 1 occurrence of `inverted-unless` This negated `unless` expression can be replaced by a `when` expression. --- typed-racket-lib/typed-racket/private/with-types.rkt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typed-racket-lib/typed-racket/private/with-types.rkt b/typed-racket-lib/typed-racket/private/with-types.rkt index 316273273..bc1e66529 100644 --- a/typed-racket-lib/typed-racket/private/with-types.rkt +++ b/typed-racket-lib/typed-racket/private/with-types.rkt @@ -50,7 +50,7 @@ (define (with-type-helper stx body fvids fvtys exids extys resty expr? ctx te-mode) (define old-context (unbox typed-context?)) (define old-te-mode (unbox type-enforcement-mode)) - (unless (not old-context) + (when old-context (tc-error/stx stx (format "with-type cannot be used in a typed module. ~a " old-context))) (set-box! typed-context? #t) (set-box! type-enforcement-mode te-mode)