Fail early on a non-pair input to `list-ref'.

There's no index that works with non-pairs, and the generic error
message in such cases is confusing.

Closes PR12740.
This commit is contained in:
Eli Barzilay 2012-05-03 03:29:42 -04:00
parent 1eed0c3d92
commit ed2b579e3c
3 changed files with 14 additions and 10 deletions

View File

@ -206,7 +206,7 @@ Returns the number of elements in @racket[lst].
]} ]}
@defproc[(list-ref [lst any/c] [pos exact-nonnegative-integer?]) @defproc[(list-ref [lst pair?] [pos exact-nonnegative-integer?])
any/c]{ any/c]{
Returns the element of @racket[lst] at position @racket[pos], where Returns the element of @racket[lst] at position @racket[pos], where

View File

@ -192,7 +192,7 @@ Unsafe variants of @racket[car], @racket[cdr], @racket[mcar],
@deftogether[( @deftogether[(
@defproc[(unsafe-list-ref [lst any/c] [pos (and/c exact-nonnegative-integer? fixnum?)]) any/c] @defproc[(unsafe-list-ref [lst pair?] [pos (and/c exact-nonnegative-integer? fixnum?)]) any/c]
@defproc[(unsafe-list-tail [lst any/c] [pos (and/c exact-nonnegative-integer? fixnum?)]) any/c] @defproc[(unsafe-list-tail [lst any/c] [pos (and/c exact-nonnegative-integer? fixnum?)]) any/c]
)]{ )]{

View File

@ -743,7 +743,7 @@ scheme_init_unsafe_list (Scheme_Env *env)
| SCHEME_PRIM_IS_UNSAFE_FUNCTIONAL); | SCHEME_PRIM_IS_UNSAFE_FUNCTIONAL);
scheme_add_global_constant ("unsafe-cdr", p, env); scheme_add_global_constant ("unsafe-cdr", p, env);
p = scheme_make_folding_prim(unsafe_list_ref, "unsafe-list-tail", 2, 2, 1); p = scheme_make_folding_prim(unsafe_list_ref, "unsafe-list-ref", 2, 2, 1);
SCHEME_PRIM_PROC_FLAGS(p) |= (SCHEME_PRIM_IS_UNARY_INLINED SCHEME_PRIM_PROC_FLAGS(p) |= (SCHEME_PRIM_IS_UNARY_INLINED
| SCHEME_PRIM_IS_UNSAFE_FUNCTIONAL); | SCHEME_PRIM_IS_UNSAFE_FUNCTIONAL);
scheme_add_global_constant ("unsafe-list-ref", p, env); scheme_add_global_constant ("unsafe-list-ref", p, env);
@ -1291,20 +1291,24 @@ do_list_ref(char *name, int takecar, int argc, Scheme_Object *argv[])
intptr_t i, k; intptr_t i, k;
Scheme_Object *lst, *index, *bnindex; Scheme_Object *lst, *index, *bnindex;
if (SCHEME_BIGNUMP(argv[1])) { lst = argv[0];
bnindex = argv[1]; index = argv[1];
if (!SCHEME_PAIRP(lst) && takecar) {
scheme_wrong_type(name, "pair", 0, argc, argv);;
}
if (SCHEME_BIGNUMP(index)) {
bnindex = index;
k = 0; k = 0;
} else if (!SCHEME_INTP(argv[1])) { } else if (!SCHEME_INTP(index)) {
scheme_wrong_type(name, "non-negative exact integer", 1, argc, argv); scheme_wrong_type(name, "non-negative exact integer", 1, argc, argv);
return NULL; return NULL;
} else { } else {
bnindex = NULL; bnindex = NULL;
k = SCHEME_INT_VAL(argv[1]); k = SCHEME_INT_VAL(index);
} }
lst = argv[0];
index = argv[1];
if ((bnindex && !SCHEME_BIGPOS(bnindex)) if ((bnindex && !SCHEME_BIGPOS(bnindex))
|| (!bnindex && (k < 0))) { || (!bnindex && (k < 0))) {
scheme_wrong_type(name, "non-negative exact integer", 1, argc, argv); scheme_wrong_type(name, "non-negative exact integer", 1, argc, argv);