Skip to content

Commit b6eafe4

Browse files
authored
Fix crash when inspecting a CHRSXP (#701)
* Fix crash when inspecting a `CHRSXP` * Skip if rlang is not installed.
1 parent e83f36c commit b6eafe4

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

crates/ark/src/variables/variable.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ impl WorkspaceVariableDisplayValue {
9999
CLOSXP => Self::from_closure(value),
100100
ENVSXP => Self::from_env(value),
101101
LANGSXP => Self::from_language(value),
102+
CHARSXP => Self::from_charsxp(value),
102103
_ if r_is_matrix(value) => Self::from_matrix(value)?,
103104
RAWSXP | LGLSXP | INTSXP | REALSXP | STRSXP | CPLXSXP => Self::from_default(value)?,
104105
_ if r_is_s4(value) => Self::from_s4(value)?,
@@ -356,6 +357,10 @@ impl WorkspaceVariableDisplayValue {
356357
Ok(Self::new(display_value, false))
357358
}
358359

360+
fn from_charsxp(_: SEXP) -> Self {
361+
Self::new(String::from("<CHARSXP>"), false)
362+
}
363+
359364
fn from_default(value: SEXP) -> anyhow::Result<Self> {
360365
let formatted = FormattedVector::new(RObject::from(value))?;
361366

@@ -445,6 +450,11 @@ impl WorkspaceVariableDisplayType {
445450
return Self::from_class(value, String::from("S4"));
446451
}
447452

453+
// We can't check attributes of CHARSXP, so we just short-circuit here
454+
if r_typeof(value) == CHARSXP {
455+
return Self::simple(String::from("CHARSXP"));
456+
}
457+
448458
if r_is_simple_vector(value) {
449459
let display_type = match include_length {
450460
true => match r_vec_is_single_dimension_with_single_value(value) {
@@ -2078,4 +2088,26 @@ mod tests {
20782088
assert!(vars[0].display_value.starts_with("<S4 class"),);
20792089
})
20802090
}
2091+
2092+
#[test]
2093+
fn test_charsxp() {
2094+
r_task(|| {
2095+
// Skip test if rlang is not installed
2096+
if let Ok(false) = harp::parse_eval_global(r#".ps.is_installed("rlang")"#)
2097+
.unwrap()
2098+
.try_into()
2099+
{
2100+
return;
2101+
}
2102+
2103+
let env = Environment::new_empty().unwrap();
2104+
let value = harp::parse_eval_base(r#"rlang:::chr_get("foo", 0L)"#).unwrap();
2105+
env.bind("x".into(), &value);
2106+
2107+
let path = vec![];
2108+
let vars = PositronVariable::inspect(env.into(), &path).unwrap();
2109+
assert_eq!(vars.len(), 1);
2110+
assert_eq!(vars[0].display_value, "<CHARSXP>");
2111+
})
2112+
}
20812113
}

crates/harp/src/utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,11 @@ pub fn r_is_simple_vector(value: SEXP) -> bool {
158158
///
159159
/// Notably returns `false` for 1D arrays and >=3D arrays.
160160
pub fn r_is_matrix(object: SEXP) -> bool {
161+
// We can't check the `dim` attribute for CHARSXP's
162+
if r_typeof(object) == CHARSXP {
163+
return false;
164+
}
165+
161166
let dim = r_dim(object);
162167

163168
if dim == r_null() {

0 commit comments

Comments
 (0)