Error on duplicate type annotations

This commit is contained in:
Asumu Takikawa 2013-08-01 14:46:21 -04:00
parent 0ef3f09768
commit 98b49deb76
2 changed files with 22 additions and 7 deletions

View File

@ -911,21 +911,29 @@
[_ '()])) [_ '()]))
;; register-internals : Listof<Syntax> -> Dict<Symbol, Type> ;; register-internals : Listof<Syntax> -> Dict<Symbol, Type>
;; Find : annotations and register them ;; Find : annotations and register them, error if duplicates are found
;; TODO: support `define-type`? ;; TODO: support `define-type`?
(define (register-internals stxs) (define (register-internals stxs)
(for/fold ([table '()]) (for/fold ([table #hash()]) ([stx stxs])
([stx stxs])
(syntax-parse stx (syntax-parse stx
#:literals (let-values begin quote-syntax :-internal #:literals (let-values begin quote-syntax :-internal
#%plain-app values void) #%plain-app values void)
[(let-values ((() [(let-values ((()
(begin (begin
(quote-syntax (:-internal name:id type:expr)) (quote-syntax (:-internal name-stx:id type-stx:expr))
(#%plain-app values)))) (#%plain-app values))))
(#%plain-app void)) (#%plain-app void))
(cons (cons (syntax-e #'name) (parse-type #'type)) (define name (syntax-e #'name-stx))
table)] (define type (parse-type #'type-stx))
(cond [(and (hash-has-key? table name)
(not (equal? (hash-ref table name)
type)))
(tc-error/expr
#:stx #'name
"Duplicate type annotation of ~a for ~a, previous was ~a"
type name (hash-ref table name))
table]
[else (hash-set table name type)])]
[_ table]))) [_ table])))
;; infer-self-type : Dict<Symbol, Type> Set<Symbol> Dict<Symbol, Symbol> ;; infer-self-type : Dict<Symbol, Type> Set<Symbol> Dict<Symbol, Symbol>

View File

@ -1028,5 +1028,12 @@
(class object% (class object%
(super-new) (super-new)
(field [(x y) : Integer 0]))) (field [(x y) : Integer 0])))
(+ 1 (get-field y (new c%)))))) (+ 1 (get-field y (new c%))))
;; fails, duplicate type annotation
(check-err #:exn #rx"Duplicate type annotation of Real"
(class object%
(super-new)
(: x Real)
(field [x : Integer 0])))))