[racket-dev] Preserving source location in macro-generating macros, in collects

From: Neil Toronto (neil.toronto at gmail.com)
Date: Fri Oct 7 11:35:33 EDT 2011

Why I care: I wrote a macro that both defines a contracted function and 
defines a macro that expands to a Scribble 'defproc' with the same 
contract. With the source locations gone (recursively), Scribble can't 
format the contracts, and I end up with things like (real-in01) instead 
of (real-in 0 1).

(I know about scribble/srcdoc, but dependencies keep me from using it.)

Suppose I have a "location reporter" macro, which is generated by a macro:

#lang racket

(require (for-syntax syntax/srcloc))

(provide location-reporter)

(define-syntax (make-location-reporter stx)
   (syntax-case stx ()
     [(_ name)  #'(define-syntax (name inner-stx)
                    (syntax-case inner-stx ()
                      [(_)  #`#,(source-location->string #'name)]))]))

(make-location-reporter location-reporter)

If I compile this in DrRacket, require it from another module, and do


it expands to


as I expect. But if I compile it as part of "raco setup", it expands to


I understand that stripping source locations is an optimization. But is 
there a way to turn it off, or preserve them, recursively, another way?

Also, exactly how is the location information disappearing? Does it 
happen when 'name' above is double-syntax-quoted?

Neil T

Posted on the dev mailing list.