typed-racket icon indicating copy to clipboard operation
typed-racket copied to clipboard

`begin-for-syntax` run twice after type check in typed/racket

Open NoahStoryM opened this issue 4 years ago • 1 comments

I'm not sure if it's normal behavior. I just want to know why begin-for-syntax needs to be calculated twice after the type check.

What version of Racket are you using?

v8.2

What program did you run?

Program 1:

#lang typed/racket

(begin-for-syntax

  (define times 0)
  (set! times (add1 times))
  (displayln times))

(display "Hello, world!\n")

Program 2:

#lang typed/racket

(begin-for-syntax

  (define times 0)
  (set! times (add1 times))
  (displayln times))

(+ 'a 'b)

If you got an error message, please include it here.

Program 1: print

1
1
1
Hello, world!

Program 2: print

1
draft.rkt:9:3: Type Checker: type mismatch
  expected: Number
  given: 'a
  in: (quote a)
  location...:
   draft.rkt:9:3
  context...:
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:391:0: type-check
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:635:0: tc-module
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:101:12
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:22:4
draft.rkt:9:6: Type Checker: type mismatch
  expected: Number
  given: 'b
  in: (quote b)
  location...:
   draft.rkt:9:6
  context...:
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/utils/tc-utils.rkt:123:0: report-all-errors
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:391:0: type-check
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:635:0: tc-module
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:101:12
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:22:4
Type Checker: Summary: 2 errors encountered
  location...:
   draft.rkt:9:3
   draft.rkt:9:6
  context...:
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:391:0: type-check
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typecheck/tc-toplevel.rkt:635:0: tc-module
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/tc-setup.rkt:101:12
   /usr/share/racket/pkgs/typed-racket-lib/typed-racket/typed-racket.rkt:22:4

NoahStoryM avatar Oct 25 '21 02:10 NoahStoryM

I guess this example may provide more information:

#lang typed/racket

(module before typed/racket
  (begin-for-syntax
    (define times 0)
    (set! times (add1 times))
    (displayln times))
  (displayln "hello world!"))

(require 'before)

(begin-for-syntax
  (displayln 2))

(displayln 'hi)

print:

1
1
1
1
2
1
1
2
1
2
hello world!
hi

And if I remove (require 'before), or use racket instead of typed/racket, I can get more information about the sequence and times, but I feel a little more confused.

NoahStoryM avatar Oct 25 '21 03:10 NoahStoryM