From d1d4ff187aae42142eb85b236fcbee1c78fa4a07 Mon Sep 17 00:00:00 2001 From: Maximilian Scholz Date: Fri, 13 Nov 2026 00:47:58 +0100 Subject: [PATCH] fix(checker): keep arg-derived lengths on union stub returns A typeshed return with mode "union" returned early, before the call's length spec was resolved, so round(x) and cumprod(x) lost length(x). Build the union after the length, like the double_or_int path. Closes #899 --- CHANGELOG.md | 3 +++ crates/ry-checker/src/infer/construct.rs | 8 ++++++- crates/ry-checker/src/infer/types.rs | 7 +++++- .../testdata/cases/type_inference.R | 22 +++++++++++++++++++ .../testdata/oracle/union_return_arg_length.R | 11 ++++++++++ .../corpus__cases_type_inference.snap | 14 ++++++++++++ 6 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 crates/ry-checker/testdata/oracle/union_return_arg_length.R diff --git a/CHANGELOG.md b/CHANGELOG.md index 888c8ec0..d9119a52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,9 @@ All notable changes to ry are documented in this file. ### Fixed +- `round(x)`, `cumprod(x)`, `sqrt(x)` and the other base stubs that return + double or complex keep the length of `x` again, so `if (round(x) > 1)` + on a two-element `x` reports RY002 (#899). - Stop reporting RY000 for calls and subsets nested 43 to 49 deep, which GNU R parses. Argument and parameter lists no longer count toward ry's syntax nesting limit. Past the limit, a file within R's 49-bracket limit diff --git a/crates/ry-checker/src/infer/construct.rs b/crates/ry-checker/src/infer/construct.rs index 2ed48d44..aaf18330 100644 --- a/crates/ry-checker/src/infer/construct.rs +++ b/crates/ry-checker/src/infer/construct.rs @@ -988,12 +988,15 @@ impl Checker { } ReturnSpec::Concrete(c) => { let mut integer_or_double = false; + let mut union_return = false; let mode = if let Some(mode) = concrete_json_mode(&c.mode) { mode } else { match JsonMode::parse(&c.mode) { + // A union is built below, once the length is known. Some(JsonMode::Union) => { - return json_rtype_to_rtype(c); + union_return = true; + Mode::Opaque } // "double_or_int" follows the numeric data arguments. // An `integer | double` result is built below, once @@ -1084,6 +1087,9 @@ impl Checker { if integer_or_double { return integer_or_double_type(length); } + if union_return { + return json_rtype_with_length(c, length); + } let mut result = RType::new(mode, length); if c.class.is_empty() && mode.is_unknown() { // An opaque return without a class entry knows nothing diff --git a/crates/ry-checker/src/infer/types.rs b/crates/ry-checker/src/infer/types.rs index a5c87e02..01aed30d 100644 --- a/crates/ry-checker/src/infer/types.rs +++ b/crates/ry-checker/src/infer/types.rs @@ -452,7 +452,12 @@ pub(crate) fn json_length_to_length(spec: Option) -> Length { } pub(crate) fn json_rtype_scalar(jt: &JsonRType) -> RType { - let length = json_length_to_length(JsonLength::parse(&jt.length)); + json_rtype_with_length(jt, json_length_to_length(JsonLength::parse(&jt.length))) +} + +/// `json_rtype_scalar` with a length the caller already resolved, such as +/// a call's `arg0` length; a union gives it to every member. +pub(crate) fn json_rtype_with_length(jt: &JsonRType, length: Length) -> RType { let scalar = |mode: Mode| { let class = if !jt.class.is_empty() { let refs: Vec<_> = jt.class.iter().map(String::as_str).collect(); diff --git a/crates/ry-checker/testdata/cases/type_inference.R b/crates/ry-checker/testdata/cases/type_inference.R index 49c7d7e4..c352a62f 100644 --- a/crates/ry-checker/testdata/cases/type_inference.R +++ b/crates/ry-checker/testdata/cases/type_inference.R @@ -1495,3 +1495,25 @@ last_match <- function(x) { for (i in seq_along(x)) if (x[[i]] > 0) break if (i == length(x)) "last" else switch(i, "first", "other") } + +## case: a union stub return keeps its argument's length (#899) +# expect: RY002 +f <- function() { + x <- c(1.4, 2.6) + r <- round(x) + if (r > 1) "big" +} + +## case: cumprod keeps its argument's length through a union return +# expect: RY002 +f <- function() { + x <- c(1.4, 2.6) + if (cumprod(x) > 1) "big" +} + +## case: a union stub return of a scalar argument stays quiet +# no-diag +f <- function() { + r <- round(1.4) + if (r > 1) "big" +} diff --git a/crates/ry-checker/testdata/oracle/union_return_arg_length.R b/crates/ry-checker/testdata/oracle/union_return_arg_length.R new file mode 100644 index 00000000..0327c269 --- /dev/null +++ b/crates/ry-checker/testdata/oracle/union_return_arg_length.R @@ -0,0 +1,11 @@ +# oracle: must-warn RY002 +# round() and cumprod() return as many elements as their argument, though +# their stubs return a double/complex union (#899). A two-element result +# used as an `if` condition is an error in R, so ry must report it. +x <- c(1.4, 2.6) +stopifnot(length(round(x)) == 2L, length(cumprod(x)) == 2L) +error <- tryCatch({ + if (round(x) > 1) 1L + NULL +}, error = identity) +stopifnot(inherits(error, "error")) diff --git a/crates/ry-checker/tests/snapshots/corpus__cases_type_inference.snap b/crates/ry-checker/tests/snapshots/corpus__cases_type_inference.snap index 69b2f62f..8e07ca1e 100644 --- a/crates/ry-checker/tests/snapshots/corpus__cases_type_inference.snap +++ b/crates/ry-checker/tests/snapshots/corpus__cases_type_inference.snap @@ -930,3 +930,17 @@ expression: rows severity: error - case: the loop variable after a loop that may not run is quiet diagnostics: [] +- case: "a union stub return keeps its argument's length (#899)" + diagnostics: + - code: RY002 + line: 1504 + message: "`if` condition has length 2; R requires a length-1 condition" + severity: warning +- case: "cumprod keeps its argument's length through a union return" + diagnostics: + - code: RY002 + line: 1511 + message: "`if` condition has length 2; R requires a length-1 condition" + severity: warning +- case: a union stub return of a scalar argument stays quiet + diagnostics: []