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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion crates/ry-checker/src/infer/construct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Comment thread
sims1253 marked this conversation as resolved.
Resolved
Comment thread
sims1253 marked this conversation as resolved.
Resolved
let mut result = RType::new(mode, length);
if c.class.is_empty() && mode.is_unknown() {
// An opaque return without a class entry knows nothing
Expand Down
7 changes: 6 additions & 1 deletion crates/ry-checker/src/infer/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,12 @@ pub(crate) fn json_length_to_length(spec: Option<JsonLength>) -> 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();
Expand Down
22 changes: 22 additions & 0 deletions crates/ry-checker/testdata/cases/type_inference.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
11 changes: 11 additions & 0 deletions crates/ry-checker/testdata/oracle/union_return_arg_length.R
Original file line number Diff line number Diff line change
@@ -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"))
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
Loading