-
Notifications
You must be signed in to change notification settings - Fork 7
Support rep.int() in subset indexing #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| test_that("rep.int works in subset indexing", { | ||
| f <- function(x) { | ||
| declare(type(x = double(10))) | ||
| x[rep.int(1L, 5L)] | ||
| } | ||
|
|
||
| expect_quick_identical(f, list(as.double(1:10))) | ||
| }) | ||
|
|
||
| test_that("rep.int is not silently supported outside subset indexing", { | ||
| # Regression: a rep.int() handler intended for subscripts must not change | ||
| # program semantics when called as a general value constructor. | ||
| f <- function(n) { | ||
| declare(type(n = integer(1))) | ||
| rep.int(1.5, n) | ||
| } | ||
|
|
||
| expect_error(quick(f), "rep.int", fixed = TRUE) | ||
| }) | ||
|
|
||
| test_that("rep.int fails cleanly (no internal slot errors) for NULL args in indexing", { | ||
| bad_x <- function(x) { | ||
| declare(type(x = double(10))) | ||
| x[rep.int(NULL, 2L)] | ||
| } | ||
| bad_times <- function(x) { | ||
| declare(type(x = double(10))) | ||
| x[rep.int(1L, NULL)] | ||
| } | ||
|
|
||
| # Prior bug: would error with "no applicable method for '@' applied to an | ||
| # object of class \"NULL\"" rather than a controlled message. | ||
| expect_error(quick(bad_x), "rep.int\\(\\)", fixed = FALSE) | ||
| expect_error(quick(bad_times), "rep.int\\(\\)", fixed = FALSE) | ||
| }) | ||
|
|
||
| test_that("rep.int supports named times= in subset indexing", { | ||
| f <- function(x) { | ||
| declare(type(x = double(10))) | ||
| x[rep.int(3L, times = 2L)] | ||
| } | ||
|
|
||
| expect_quick_identical(f, list(as.double(1:10))) | ||
| }) | ||
|
|
||
| test_that("rep.int supports non-literal times in subset indexing", { | ||
| f <- function(x, n) { | ||
| declare(type(x = double(10))) | ||
| declare(type(n = integer(1))) | ||
| x[rep.int(2L, n)] | ||
| } | ||
|
|
||
| expect_quick_identical(f, list(as.double(1:10), 4L)) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The length you derive for the replicated index uses
r2size(times_arg), which returns the size of thetimesexpression (usually 1 for a scalar), not its runtime value. Inx[rep.int(2L, n)]wherenis a scalar integer (e.g., 4), this sets the index vector dims to length 1, so the subscript is treated as scalar during drop/shape inference in[and the output shape becomes incorrect. The generated Fortran array constructor will still produce lengthn, so the wrapper’s metadata can disagree with the actual result. Consider propagating the actualtimesexpression (or an unknown length) instead ofr2size(times_arg).Useful? React with 👍 / 👎.