Skip to content

Commit

Permalink
Fix crash when inspecting a CHRSXP (#701)
Browse files Browse the repository at this point in the history
* Fix crash when inspecting a `CHRSXP`

* Skip if rlang is not installed.
  • Loading branch information
dfalbel authored Feb 10, 2025
1 parent e83f36c commit b6eafe4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
32 changes: 32 additions & 0 deletions crates/ark/src/variables/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl WorkspaceVariableDisplayValue {
CLOSXP => Self::from_closure(value),
ENVSXP => Self::from_env(value),
LANGSXP => Self::from_language(value),
CHARSXP => Self::from_charsxp(value),
_ if r_is_matrix(value) => Self::from_matrix(value)?,
RAWSXP | LGLSXP | INTSXP | REALSXP | STRSXP | CPLXSXP => Self::from_default(value)?,
_ if r_is_s4(value) => Self::from_s4(value)?,
Expand Down Expand Up @@ -356,6 +357,10 @@ impl WorkspaceVariableDisplayValue {
Ok(Self::new(display_value, false))
}

fn from_charsxp(_: SEXP) -> Self {
Self::new(String::from("<CHARSXP>"), false)
}

fn from_default(value: SEXP) -> anyhow::Result<Self> {
let formatted = FormattedVector::new(RObject::from(value))?;

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

// We can't check attributes of CHARSXP, so we just short-circuit here
if r_typeof(value) == CHARSXP {
return Self::simple(String::from("CHARSXP"));
}

if r_is_simple_vector(value) {
let display_type = match include_length {
true => match r_vec_is_single_dimension_with_single_value(value) {
Expand Down Expand Up @@ -2078,4 +2088,26 @@ mod tests {
assert!(vars[0].display_value.starts_with("<S4 class"),);
})
}

#[test]
fn test_charsxp() {
r_task(|| {
// Skip test if rlang is not installed
if let Ok(false) = harp::parse_eval_global(r#".ps.is_installed("rlang")"#)
.unwrap()
.try_into()
{
return;
}

let env = Environment::new_empty().unwrap();
let value = harp::parse_eval_base(r#"rlang:::chr_get("foo", 0L)"#).unwrap();
env.bind("x".into(), &value);

let path = vec![];
let vars = PositronVariable::inspect(env.into(), &path).unwrap();
assert_eq!(vars.len(), 1);
assert_eq!(vars[0].display_value, "<CHARSXP>");
})
}
}
5 changes: 5 additions & 0 deletions crates/harp/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,11 @@ pub fn r_is_simple_vector(value: SEXP) -> bool {
///
/// Notably returns `false` for 1D arrays and >=3D arrays.
pub fn r_is_matrix(object: SEXP) -> bool {
// We can't check the `dim` attribute for CHARSXP's
if r_typeof(object) == CHARSXP {
return false;
}

let dim = r_dim(object);

if dim == r_null() {
Expand Down

0 comments on commit b6eafe4

Please sign in to comment.