Subject: Misleading error for optargs

From: Mike Gran <[email-masked]>
Date: Wed, 22 Jun 2016 05:03:59 +0000 (UTC)

The following program returns a confusing error with guile 2.0.11

  (use-modules (ice-9 optargs)) 
  (define* (func A #:key B) 
     #t) 
  (func 1 2)

When run with auto-compilation on, it gives a valid warning during the compilation
step but returns a confusing warning at runtime.

;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0 
;;;       or pass the --no-auto-compile argument to disable. 
;;; compiling /home/mike/Projects/pip-tui/pip-tui/bug.scm 
;;; /home/mike/Projects/pip-tui/pip-tui/bug.scm:6:0: warning: wrong number of arguments to `func' 
;;; compiled /home/mike/.cache/guile/ccache/2.0-LE-8-2.0/home/mike/Projects/pip-tui/pip-tui/bug.scm.go 
Backtrace: 
In ice-9/boot-9.scm: 
157: 7 [catch #t #<catch-closure 56422c2dade0> ...] 
In unknown file: 
?: 6 [apply-smob/1 #<catch-closure 56422c2dade0>] 
In ice-9/boot-9.scm: 
63: 5 [call-with-prompt prompt0 ...] 
In ice-9/eval.scm: 
432: 4 [eval # #] 
In ice-9/boot-9.scm: 
2401: 3 [save-module-excursion #<procedure 56422c2f6a00 at ice-9/boot-9.scm:4045:3 ()>] 
4052: 2 [#<procedure 56422c2f6a00 at ice-9/boot-9.scm:4045:3 ()>] 
In unknown file: 
?: 1 [load-compiled/vm "/home/mike/.cache/guile/ccache/2.0-LE-8-2.0/home/mike/Projects/pip-tui/pip-tui/bug.scm.go"] 
In /home/mike/Projects/pip-tui/pip-tui/bug.scm: 
3: 0 [func 1 #<undefined> 2] 

/home/mike/Projects/pip-tui/pip-tui/bug.scm:3:0: In procedure func: 
/home/mike/Projects/pip-tui/pip-tui/bug.scm:3:0: Error while printing exception

When run with GUILE_AUTO_COMPILE=0 it returns an intelligible warning:

Backtrace: 
In ice-9/boot-9.scm: 
157: 9 [catch #t #<catch-closure 560f68600020> ...] 
In unknown file: 
?: 8 [apply-smob/1 #<catch-closure 560f68600020>] 
In ice-9/boot-9.scm: 
63: 7 [call-with-prompt prompt0 ...] 
In ice-9/eval.scm: 
432: 6 [eval # #] 
In ice-9/boot-9.scm: 
2401: 5 [save-module-excursion #<procedure 560f68620a00 at ice-9/boot-9.scm:4045:3 ()>] 
4052: 4 [#<procedure 560f68620a00 at ice-9/boot-9.scm:4045:3 ()>] 
1724: 3 [%start-stack load-stack ...] 
1729: 2 [#<procedure 560f68638fc0 ()>] 
In unknown file: 
?: 1 [primitive-load "/home/mike/Projects/pip-tui/pip-tui/bug.scm"] 
?: 0 [scm-error keyword-argument-error "eval" "Invalid keyword" () (2)] 

ERROR: In procedure scm-error: 
ERROR: Invalid keyword: 2

I can create some improvement by replacing the 'keyword-error-printer'
procedure in ice-9/boot-9.scm with the following
but I don't think that is the correct answer.

--- ice-9/boot-9.scm.0  2016-06-22 00:37:38.929112125 -0400 
+++ ice-9/boot-9.scm    2016-06-22 01:02:11.032206795 -0400 
@@ -1020,8 +1020,10 @@ 

(define (keyword-error-printer port key args default-printer) 
(let ((message (cadr args)) 
-          (faulty  (car (cadddr args)))) ; I won't do it again, I promise. 
-      (format port "~a: ~s" message faulty))) 
+          (faulty (cadddr args))) 
+      (if (pair? faulty) 
+          (format port "~a: ~s" message (car faulty)) 
+          (format port "~a" message))))