Skip to content

Commit 18fc8e1

Browse files
committed
Simplify non-consecutive errors
1 parent c780043 commit 18fc8e1

File tree

2 files changed

+32
-47
lines changed

2 files changed

+32
-47
lines changed

R/subscript-loc.R

+8-31
Original file line numberDiff line numberDiff line change
@@ -489,15 +489,10 @@ stop_subscript_oob <- function(i,
489489

490490
#' @export
491491
cnd_body.vctrs_error_subscript_oob <- function(cnd, ...) {
492-
switch(cnd_subscript_type(cnd),
493-
numeric =
494-
if (cnd_subscript_oob_non_consecutive(cnd)) {
495-
cnd_body_vctrs_error_subscript_oob_non_consecutive(cnd, ...)
496-
} else {
497-
cnd_body_vctrs_error_subscript_oob_location(cnd, ...)
498-
},
499-
character =
500-
cnd_body_vctrs_error_subscript_oob_name(cnd, ...),
492+
switch(
493+
cnd_subscript_type(cnd),
494+
numeric = cnd_body_vctrs_error_subscript_oob_location(cnd, ...),
495+
character = cnd_body_vctrs_error_subscript_oob_name(cnd, ...),
501496
abort("Internal error: subscript type can't be `logical` for OOB errors.")
502497
)
503498
}
@@ -532,10 +527,13 @@ cnd_body_vctrs_error_subscript_oob_location <- function(cnd, ...) {
532527
not <- ""
533528
}
534529

530+
allow_extension <- cnd_subscript_oob_non_consecutive(cnd)
531+
535532
# TODO: Switch to `format_inline()` and format bullets lazily through rlang
536533
cli::format_error(c(
537534
"x" = "{cli::qty(n_loc)} Location{?s} must be less than or equal to {n}{not}.",
538-
"i" = "There {cli::qty(n)} {?is/are} only {elt}."
535+
"i" = "There {cli::qty(n)} {?is/are} only {elt}.",
536+
"i" = if (allow_extension) "Extension with consecutive locations is allowed."
539537
))
540538
}
541539
cnd_body_vctrs_error_subscript_oob_name <- function(cnd, ...) {
@@ -570,27 +568,6 @@ stop_location_oob_non_consecutive <- function(i,
570568
)
571569
}
572570

573-
cnd_body_vctrs_error_subscript_oob_non_consecutive <- function(cnd, ...) {
574-
i <- sort(cnd$i)
575-
i <- i[i > cnd$size]
576-
577-
non_consecutive <- i[c(TRUE, diff(i) != 1L)]
578-
579-
arg <- append_arg("Subscript", cnd$subscript_arg)
580-
if (length(non_consecutive) == 1) {
581-
x_line <- glue::glue("{arg} contains non-consecutive location {non_consecutive}.")
582-
} else {
583-
non_consecutive <- ensure_full_stop(enumerate(non_consecutive))
584-
x_line <- glue::glue("{arg} contains non-consecutive locations {non_consecutive}")
585-
}
586-
587-
glue_data_bullets(
588-
cnd,
589-
i = "Input has size {size}.",
590-
x = x_line
591-
)
592-
}
593-
594571
cnd_subscript_oob_non_consecutive <- function(cnd) {
595572
out <- cnd$subscript_oob_non_consecutive %||% FALSE
596573
check_bool(out)

tests/testthat/_snaps/subscript-loc.md

+24-16
Original file line numberDiff line numberDiff line change
@@ -416,44 +416,49 @@
416416
<error/vctrs_error_subscript_oob>
417417
Error:
418418
! Can't subset elements with `3`.
419-
i Input has size 1.
420-
x Subscript `3` contains non-consecutive location 3.
419+
x Location must be less than or equal to 1, not 3.
420+
i There is only 1 element.
421+
i Extension with consecutive locations is allowed.
421422
Code
422423
(expect_error(num_as_location(c(1, 3), 1, oob = "extend"), class = "vctrs_error_subscript_oob")
423424
)
424425
Output
425426
<error/vctrs_error_subscript_oob>
426427
Error:
427428
! Can't subset elements with `c(1, 3)`.
428-
i Input has size 1.
429-
x Subscript `c(1, 3)` contains non-consecutive location 3.
429+
x Location must be less than or equal to 1.
430+
i There is only 1 element.
431+
i Extension with consecutive locations is allowed.
430432
Code
431433
(expect_error(num_as_location(c(1:5, 7), 3, oob = "extend"), class = "vctrs_error_subscript_oob")
432434
)
433435
Output
434436
<error/vctrs_error_subscript_oob>
435437
Error:
436438
! Can't subset elements with `c(1:5, 7)`.
437-
i Input has size 3.
438-
x Subscript `c(1:5, 7)` contains non-consecutive locations 4 and 7.
439+
x Locations must be less than or equal to 3.
440+
i There are only 3 elements.
441+
i Extension with consecutive locations is allowed.
439442
Code
440443
(expect_error(num_as_location(c(1:5, 7, 1), 3, oob = "extend"), class = "vctrs_error_subscript_oob")
441444
)
442445
Output
443446
<error/vctrs_error_subscript_oob>
444447
Error:
445448
! Can't subset elements with `c(1:5, 7, 1)`.
446-
i Input has size 3.
447-
x Subscript `c(1:5, 7, 1)` contains non-consecutive locations 4 and 7.
449+
x Locations must be less than or equal to 3.
450+
i There are only 3 elements.
451+
i Extension with consecutive locations is allowed.
448452
Code
449453
(expect_error(class = "vctrs_error_subscript_oob", num_as_location(c(1:5, 7, 1,
450454
10), 3, oob = "extend")))
451455
Output
452456
<error/vctrs_error_subscript_oob>
453457
Error:
454458
! Can't subset elements with `c(1:5, 7, 1, 10)`.
455-
i Input has size 3.
456-
x Subscript `c(1:5, 7, 1, 10)` contains non-consecutive locations 4, 7, and 10.
459+
x Locations must be less than or equal to 3.
460+
i There are only 3 elements.
461+
i Extension with consecutive locations is allowed.
457462

458463
# num_as_location() errors when inverting oob negatives unless `oob = 'remove'` (#1630)
459464

@@ -553,8 +558,9 @@
553558
<error/vctrs_error_subscript_oob>
554559
Error:
555560
! Can't subset elements with `c(1, NA, 3)`.
556-
i Input has size 1.
557-
x Subscript `c(1, NA, 3)` contains non-consecutive location 3.
561+
x Location must be less than or equal to 1.
562+
i There is only 1 element.
563+
i Extension with consecutive locations is allowed.
558564

559565
# can disallow missing values
560566

@@ -778,8 +784,9 @@
778784
<error/vctrs_error_subscript_oob>
779785
Error in `my_function()`:
780786
! Can't subset elements with `foo`.
781-
i Input has size 2.
782-
x Subscript `foo` contains non-consecutive location 4.
787+
x Location must be less than or equal to 2.
788+
i There are only 2 elements.
789+
i Extension with consecutive locations is allowed.
783790
Code
784791
(expect_error(num_as_location(0, 1, zero = "error", arg = "foo", call = call(
785792
"my_function")), class = "vctrs_error_subscript_type"))
@@ -863,8 +870,9 @@
863870
<error/vctrs_error_subscript_oob>
864871
Error:
865872
! Can't rename columns with `foo(bar)`.
866-
i Input has size 2.
867-
x Subscript `foo(bar)` contains non-consecutive location 4.
873+
x Location must be less than or equal to 2.
874+
i There are only 2 columns.
875+
i Extension with consecutive locations is allowed.
868876
Code
869877
(expect_error(with_tibble_cols(num_as_location(0, 1, zero = "error")), class = "vctrs_error_subscript_type")
870878
)

0 commit comments

Comments
 (0)