Can this contract error be easily improved?

Here's a program:

#lang racket

(define foo-contract
  (-> integer? day))

(struct day (a b c))

Note that I've omitted the intended ? for the day? predicate in the contract, writing instead day. Oops.

The error it signals is

day1: undefined;
 cannot reference an identifier before its definition

... with no error location.

In a program of this size, I can probably figure out what's going on here. In a larger program, it's hard. I got an error message about an undefined reference to weekless-day2, and I spent several minutes searching for instances of weekless-day2 in my code before a bell went off in my head and I took a look at the contract part.

How hard would it be to get a source code location for this error message?

DrRacket, at least, highlights the day and shows the line number in the error backtrace.

Racket Mode shows the error locations:

————— run contract-error.rkt —————
day1: undefined;
 cannot reference an identifier before its definition
  in module: "/home/greg/src/racket/examples/contract-error.rkt"
  internal name: day1.1
Context (errortrace):
 /home/greg/src/racket/examples/contract-error.rkt:4:15 day
 /home/greg/src/racket/examples/contract-error.rkt:4:2 (-> integer? day)

Although not apparent from this text copy/paste, those are links and work with M-x next-error.

Caveat: That only happens when the (Emacs) variable racket-error-context is set to 'high -- causing the program to be run using errortrace. (That errortrace-annotated version of your program will be slower, which isn't great for timing, but enables better error messages.)

Probably similar caveat for the "debugging" option in DrRacket (which IIRC is on by default).

You can also do this with racket command line, see docs.

Ah... okay, interesting. I see that I've been running with "No Debugging or Profiling". So I guess that's a big fat mea culpa, except for the fact that this program:

#lang racket
(+ 3 a)
(define a 9)

... does give me at least an approximate error location when running in "No Debugging or Profiling", and has an error backtrace as well.

Okay, I guess I learned something today...

Apologies for the noise.

1 Like