Skip to content

Commit db229fa

Browse files
committed
Fix racket#1158, racket#848, racket#858: Add null checks in compute-defs
Added null checks in compute-defs to handle cases where identifier-binding returns #f or when the definition lookup fails. This fixes internal errors with recursive MListof types, recursive case-> types, and recursive contract generation. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> Closes racket#1158 Closes racket#848 Closes racket#858
1 parent 388e2fd commit db229fa

File tree

4 files changed

+57
-6
lines changed

4 files changed

+57
-6
lines changed

typed-racket-lib/typed-racket/static-contracts/instantiate.rkt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,15 @@
9696
(set! bound (cons name* bound))
9797
;; traverse what `name` refers to
9898
(define r (ref name*))
99-
;; ref returns a rib, get the one definition we want
100-
(define target (for/first ([k (in-list (car r))]
101-
[v (in-list (cdr r))]
102-
#:when (free-identifier=? name* k))
103-
v))
104-
(loop target #f))]
99+
;; r can be #f if the name is not in all-name-defs
100+
(when r
101+
;; ref returns a rib, get the one definition we want
102+
(define target (for/first ([k (in-list (car r))]
103+
[v (in-list (cdr r))]
104+
#:when (free-identifier=? name* k))
105+
v))
106+
(when target
107+
(loop target #f))))]
105108
[else (sc-traverse sc loop)]))
106109
(for*/hash ([b (in-list bound)]
107110
[v (in-value (ref b))]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#lang typed/racket/base
2+
3+
;; Issue #1158: Regression test - previously caused car: contract violation
4+
;; during contract generation with recursive MListof.
5+
;; Fixed by adding null checks in compute-defs in static-contracts/instantiate.rkt
6+
7+
(provide func-1 func-2)
8+
9+
(define-type MNulls (MListof MNulls))
10+
11+
(: func-1 [-> MNulls Any]) (define (func-1 ns) 1)
12+
(: func-2 [-> MNulls Any]) (define (func-2 ns) 2)
13+
14+
(displayln (func-1 '()))
15+
(displayln (func-2 '()))
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#lang typed/racket/base
2+
3+
;; Issue #848: Regression test - previously caused car: contract violation
4+
;; with recursive case-> types during contract generation.
5+
;; Fixed by adding null checks in compute-defs in static-contracts/instantiate.rkt
6+
7+
(provide
8+
new-T
9+
consume-T)
10+
11+
(define-type T
12+
(case->
13+
['a -> Any]
14+
['b -> (-> T)]))
15+
16+
(: new-T : -> T)
17+
(define (new-T) (new-T))
18+
19+
(: consume-T : T -> Nothing)
20+
(define (consume-T t) (consume-T t))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#lang racket
2+
3+
;; Issue #858: Previously caused "recursive-contract: contract violation"
4+
;; with recursive types. Fixed by null checks in compute-defs.
5+
6+
(module a typed/racket
7+
(define-type T (Rec T (-> (U T String))))
8+
(provide f)
9+
(: f (-> T T))
10+
(define (f x) x))
11+
12+
(require 'a)
13+
(f (lambda () ""))

0 commit comments

Comments
 (0)