Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions R/community.R
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,11 @@ make_clusters <- function(
) {
# BEGIN GENERATED ARG_HANDLE: make_clusters, do not edit, see tools/generate-migrations.R
if (...length() > 0L) {
migrate_check_call_tags(
sys.call(),
c("m", "me"),
"make_clusters"
)
.arg_handle <- migrate_recover_args(
list(...),
current = list(
Expand Down Expand Up @@ -1550,6 +1555,11 @@ cluster_spinglass <- function(
) {
# BEGIN GENERATED ARG_HANDLE: cluster_spinglass, do not edit, see tools/generate-migrations.R
if (...length() > 0L) {
migrate_check_call_tags(
sys.call(),
c("g"),
"cluster_spinglass"
)
.arg_handle <- migrate_recover_args(
list(...),
current = list(
Expand Down
34 changes: 34 additions & 0 deletions R/migrate-args.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,37 @@ migrate_recover_args <- function(
)
)
}

# Runtime guard for head/recoverable prefix overlaps. A supplied tag that is
# a strict prefix of a head arg *and* of a recoverable name was ambiguous --
# an error -- under the old signature; base R partial matching now binds it
# to the head arg before the recovery layer can see it. On its own that is
# accepted: previously broken code that now works in a well-defined, silent
# way is not a problem. What must not happen is that same tag combined with
# legacy arguments in `...`: the tag steals the head slot, positionals shift
# into the wrong formals, and the recovery layer would rescue a never-valid
# call behind a soft-deprecation warning. The generator enumerates those
# tags per function (`forbidden_tags`) and the ARG_HANDLE block calls this
# as the first statement inside its recovery gate -- i.e. only ever when
# `...` is non-empty and recovery is about to engage -- so exactly that
# combination is rejected.

#' @noRd
migrate_check_call_tags <- function(
call,
forbidden,
fn_name,
env = rlang::caller_env()
) {
bad <- intersect(names(call), forbidden)
if (length(bad) == 0L) {
return(invisible(NULL))
}
cli::cli_abort(
c(
"Argument {.arg {bad}} matches multiple formal arguments of {.fn {fn_name}}.",
i = "Spell out the full argument name."
),
call = env
)
}
49 changes: 49 additions & 0 deletions R/migration-fixture.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,52 @@ migration_fixture <- function(
directed = directed
)
}

# Second fixture: head/recoverable prefix overlaps. `dim` (recoverable) is a
# strict prefix of the head arg `dimvector`, and the head arg `p` is a strict
# prefix of the recoverable `permutation` -- the two shapes that
# make_lattice()- and sample_correlated_gnp_pair()-style signatures hit. The
# generated block gains a migrate_check_call_tags() guard that rejects the
# forbidden prefixes (`d`, `di`) when legacy arguments in `...` engage
# recovery; with empty dots they bind the head arg via plain partial matching.
migration_fixture_prefix <- function(
dimvector,
p,
...,
dim = NULL,
permutation = NULL
) {
# BEGIN GENERATED ARG_HANDLE: migration_fixture_prefix, do not edit, see tools/generate-migrations.R
if (...length() > 0L) {
migrate_check_call_tags(
sys.call(),
c("d", "di"),
"migration_fixture_prefix"
)
.arg_handle <- migrate_recover_args(
list(...),
current = list(dim = dim, permutation = permutation),
recover_new = c("dim", "permutation"),
recover_old = c("dim", "permutation"),
match_names = c("dim", "permutation"),
match_to = c("dim", "permutation"),
defaults = list(dim = NULL, permutation = NULL),
head_args = c("dimvector", "p"),
fn_name = "migration_fixture_prefix"
)
list2env(.arg_handle$values, environment())
lifecycle::deprecate_soft(
"3.0.0",
what = I(.arg_handle$what),
details = .arg_handle$details
)
}
# END GENERATED ARG_HANDLE

list(
dimvector = dimvector,
p = p,
dim = dim,
permutation = permutation
)
}
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/migration-fixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
[1] FALSE


# forbidden prefixes error only when legacy arguments engage recovery

Code
migration_fixture_prefix(c(2, 2), 0.5, di = 2)
Condition
Error in `migration_fixture_prefix()`:
! Argument `di` matches multiple formal arguments of `migration_fixture_prefix()`.
i Spell out the full argument name.

# recovery deprecation messages

Code
Expand Down
185 changes: 153 additions & 32 deletions tests/testthat/test-migration-fixture.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,86 @@ test_that("recovery emits a single deprecation warning, not one per slot", {
expect_length(warnings, 1L)
})

# ---- prefix-overlap fixture -------------------------------------------------

# migration_fixture_prefix(dimvector, p, ..., dim = NULL, permutation = NULL):
# `dim` is a strict prefix of the head arg `dimvector`, `p` is a strict prefix
# of the recoverable `permutation`. Previously the generator rejected such
# signatures outright; now it allows them and enumerates the forbidden
# prefixes (`d`, `di`), which error only when legacy arguments in `...`
# engage the recovery layer.

test_that("full tail names bind exactly despite the head prefix overlap", {
expect_no_condition(
res <- migration_fixture_prefix(c(2, 2), 0.5, dim = 2)
)
expect_equal(
res,
list(dimvector = c(2, 2), p = 0.5, dim = 2, permutation = NULL)
)
})

test_that("legacy positional calls are recovered across the overlap", {
rlang::local_options(lifecycle_verbosity = "warning")
lifecycle::expect_deprecated(
res <- migration_fixture_prefix(c(2, 2), 0.5, 2, "perm")
)
expect_equal(
res,
migration_fixture_prefix(c(2, 2), 0.5, dim = 2, permutation = "perm")
)
})

test_that("abbreviations longer than the head arg are recovered", {
rlang::local_options(lifecycle_verbosity = "warning")
# `perm` is no prefix of the head `p`, so it reaches `...` and recovery.
lifecycle::expect_deprecated(
res <- migration_fixture_prefix(c(2, 2), 0.5, perm = "x")
)
expect_identical(res$permutation, "x")
})

test_that("forbidden prefixes error only when legacy arguments engage recovery", {
# `di =` steals the head slot `dimvector`, `c(2, 2)` shifts into `p`,
# and `0.5` lands in `...`:
# recovery would rescue this never-valid call behind a deprecation
# warning, so the guard errors instead.
expect_snapshot(
migration_fixture_prefix(c(2, 2), 0.5, di = 2),
error = TRUE
)
# A named legacy argument in `...` engages recovery just the same.
expect_error(
migration_fixture_prefix(c(2, 2), 0.5, d = 2, perm = "x"),
"matches multiple formal arguments"
)
})

test_that("forbidden prefixes with empty dots bind the head arg silently", {
# Previously broken calls that now work in a well-defined, silent way
# are accepted:
# `d =` was an ambiguity error under the old signature and now binds
# `dimvector` via ordinary base R partial matching.
# Only the warning-rescued combination -- a forbidden prefix plus legacy
# arguments in `...` -- stays an error.
expect_no_condition(res <- migration_fixture_prefix(d = c(2, 2), p = 0.5))
expect_equal(
res,
list(dimvector = c(2, 2), p = 0.5, dim = NULL, permutation = NULL)
)
})

test_that("the guard ignores calls without hazardous tags", {
# Unnamed / fully named calls never trip the guard, including do.call().
expect_no_condition(
res <- do.call(migration_fixture_prefix, list(c(2, 2), 0.5))
)
expect_null(res$dim)
expect_no_condition(
migration_fixture_prefix(dimvector = c(2, 2), p = 0.5, dim = 1)
)
})

# ---- deprecation message snapshots -----------------------------------------

test_that("recovery deprecation messages", {
Expand Down Expand Up @@ -175,51 +255,92 @@ test_that("the generated block is in sync with the registry", {
expect_identical(spliced$lines, lines)
})

test_that("normalise_migration() rejects head/recoverable prefix clashes", {
# Head args are matched by base R (with partial matching) before `...`, so a
# head arg in a prefix relationship with a recoverable name would silently
# capture it before the recovery layer runs. The generator must reject this.
test_that("normalise_migration() handles head/recoverable prefix overlaps", {
# Head args are matched by base R (with partial matching) before `...`.
# Exact matching protects full names, so prefix overlaps are allowed; the
# generator enumerates the forbidden prefixes (strict prefixes of a head
# arg that also prefix a recoverable name) for the runtime guard, which
# rejects them only when legacy arguments in `...` engage recovery.
generator <- testthat::test_path("..", "..", "tools", "generate-migrations.R")
skip_if_not(file.exists(generator))
gen_env <- new.env()
sys.source(generator, envir = gen_env)

# Head arg `type` is a prefix of the recoverable `typeof`.
expect_error(
gen_env$normalise_migration(
"bad_head_prefix",
list(
old = function(graph, typeof) {},
new = function(graph, type = "x", ..., typeof = NULL) {},
when = "3.0.0"
)
),
"prefix relationship"
# Head arg `type` is a prefix of the recoverable `typeof`: allowed. The
# strict prefixes of `type` all prefix `typeof` too, so they are forbidden
# in combination with legacy arguments in `...`.
norm <- gen_env$normalise_migration(
"head_prefix",
list(
old = function(graph, type, typeof) {},
new = function(graph, type = "x", ..., typeof = NULL) {},
when = "3.0.0"
)
)
expect_identical(norm$forbidden_tags, c("t", "ty", "typ"))

# Recoverable `type` is a prefix of the head arg `typeof`.
expect_error(
gen_env$normalise_migration(
"bad_recover_prefix",
list(
old = function(graph, type) {},
new = function(graph, typeof = "x", ..., type = NULL) {},
when = "3.0.0"
)
),
"prefix relationship"
# Recoverable `type` is a prefix of the head arg `typeof`: allowed. A
# supplied `type =` binds the post-`...` formal exactly; only the shorter
# tags (which were ambiguous before) are enumerated.
norm <- gen_env$normalise_migration(
"recover_prefix",
list(
old = function(graph, typeof, type) {},
new = function(graph, typeof = "x", ..., type = NULL) {},
when = "3.0.0"
)
)
expect_identical(norm$forbidden_tags, c("t", "ty", "typ"))

# Sharing a leading letter (`graph` vs `groups`) enumerates the shared
# prefixes too -- `f(g = )` binding `graph` is fine on its own, but not
# when the recovery layer would rescue the rest of the call.
norm <- gen_env$normalise_migration(
"ok_shared_letter",
list(
old = function(graph, groups) {},
new = function(graph, ..., groups = NULL) {},
when = "3.0.0"
)
)
expect_identical(norm$forbidden_tags, c("g", "gr"))

# Merely sharing a leading letter (`graph` vs `groups`) is allowed.
expect_no_error(
# A tag that abbreviates two head args stays out of the forbidden list:
# base R errors on it by itself before the body ever runs.
norm <- gen_env$normalise_migration(
"two_heads",
list(
old = function(g1, g2, graph.attr.comb) {},
new = function(g1, g2, ..., graph.attr.comb = NULL) {},
when = "3.0.0"
)
)
expect_identical(norm$forbidden_tags, character(0))

# No overlap at all: no tags, no guard.
norm <- gen_env$normalise_migration(
"no_overlap",
list(
old = function(graph, n, weights) {},
new = function(graph, n, ..., weights = NULL) {},
when = "3.0.0"
)
)
expect_identical(norm$forbidden_tags, character(0))

# A renamed-away old name that is a prefix of a head arg stays fatal: the
# old name is no longer a formal, so a valid legacy `f(weight = )` would
# silently bind the head arg -- no runtime guard can restore it.
expect_error(
gen_env$normalise_migration(
"ok_shared_letter",
"renamed_prefix",
list(
old = function(graph, groups) {},
new = function(graph, ..., groups = NULL) {},
old = function(graph, weightx, weight = weights) {},
new = function(graph, weightx = 1, ..., weights = NULL) {},
when = "3.0.0"
)
)
),
"renamed-away"
)
})

Expand Down
Loading
Loading