From ceda8ae1a75da8f06e45e84034040770543663a2 Mon Sep 17 00:00:00 2001 From: Asumu Takikawa Date: Thu, 1 Aug 2013 14:46:21 -0400 Subject: [PATCH] Error on duplicate type annotations original commit: 98b49deb76dc96af23efb3b0742f1a8bed3e6d36 --- .../typecheck/check-class-unit.rkt | 20 +++++++++++++------ .../typed-racket/unit-tests/class-tests.rkt | 9 ++++++++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt index f8116919..4ce3eee4 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-lib/typed-racket/typecheck/check-class-unit.rkt @@ -911,21 +911,29 @@ [_ '()])) ;; register-internals : Listof -> Dict -;; Find : annotations and register them +;; Find : annotations and register them, error if duplicates are found ;; TODO: support `define-type`? (define (register-internals stxs) - (for/fold ([table '()]) - ([stx stxs]) + (for/fold ([table #hash()]) ([stx stxs]) (syntax-parse stx #:literals (let-values begin quote-syntax :-internal #%plain-app values void) [(let-values ((() (begin - (quote-syntax (:-internal name:id type:expr)) + (quote-syntax (:-internal name-stx:id type-stx:expr)) (#%plain-app values)))) (#%plain-app void)) - (cons (cons (syntax-e #'name) (parse-type #'type)) - table)] + (define name (syntax-e #'name-stx)) + (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]))) ;; infer-self-type : Dict Set Dict diff --git a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/class-tests.rkt b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/class-tests.rkt index e2304f9d..a31370e6 100644 --- a/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/class-tests.rkt +++ b/pkgs/typed-racket-pkgs/typed-racket-test/tests/typed-racket/unit-tests/class-tests.rkt @@ -1028,5 +1028,12 @@ (class object% (super-new) (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])))))