Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug with raw identifiers in function arguments (#528) #531

Merged
merged 10 commits into from
May 5, 2023
Merged

Fix bug with raw identifiers in function arguments (#528) #531

merged 10 commits into from
May 5, 2023

Conversation

bicarlsen
Copy link
Contributor

Added functionality to remove the raw identifier prefix (r#) from function arguments in the R bindings.

@Ilia-Kosenkov
Copy link
Member

Hey, thanks for the contribution. There a couple of edge cases we need to cover, but right now I do not have time to comment on this. Can you hold on to this PR for a little bit longer?

@bicarlsen
Copy link
Contributor Author

Of course, just let me know.

@Ilia-Kosenkov
Copy link
Member

Ok so I was thinking about touching this thing:

fn sanitize_identifier(name: &str) -> String {
if name.starts_with('_') {
format!("`{}`", name)
} else {
name.to_string()
}
}

We have a method that sanitizes rust identifiers on the R side. E.g., if you have a fn my_fn(_x :i32) {}, R cannot handle the _x argument, but it instead can handle `_x`, which we effectively pass around when we detect an incompatible name. I propose to add another check, like .contains('#'), instead of changing generated code. We just sanitize wrappers properly.

There is another thing, we should also consider a case of functions that have r# in the name, I suggest we also add a r#true() function to the test package. Now if sanitize_warppers() is updated, r#true() will be wrapped accordingly, but not the argument passed to the .Call() (here it is something like wrap__mypkg_r#true, which is invalid). This part I have not yet solved, but we need to also sanitize it the same way we sanitize arguments -- `wrap__mapkg_r#true` is a valid R identifier.

This way we preserve exact 1-to-1 mapping between rust names and R names.

Added functionality and tests for raw identifiers in function names.
@bicarlsen
Copy link
Contributor Author

I updated the implementation to incorporate the sanitize_identifiers method mentioned by @Ilia-Kosenkov above. This did not address the wrapped function names in the extendr-wrappers.R file though, so still needed sanitization in the extendr_macros/wrappers.rs module.

@Ilia-Kosenkov
Copy link
Member

Oh shoot, completely forgot about this, my apologies. I will check it today.

@Ilia-Kosenkov
Copy link
Member

I think you need to merge master in your branch to update CI

@bicarlsen
Copy link
Contributor Author

I don't understand why the Markdown links are causing errors in the tests. This issue was discussed in #534 . I fixed the typos with the parentheses, so not sure what is causing the issue now.

Copy link
Member

@CGMossa CGMossa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are more places where the markdown-links are done incorrectly. But here you go!
Just accept these suggestions.

tests/extendrtests/R/extendr-wrappers.R Outdated Show resolved Hide resolved
tests/extendrtests/R/extendr-wrappers.R Outdated Show resolved Hide resolved
tests/extendrtests/R/extendr-wrappers.R Outdated Show resolved Hide resolved
bicarlsen and others added 3 commits April 27, 2023 12:46
Co-authored-by: CGMossa <cgmossa@gmail.com>
Co-authored-by: CGMossa <cgmossa@gmail.com>
Co-authored-by: CGMossa <cgmossa@gmail.com>
@bicarlsen
Copy link
Contributor Author

Geeze! Thanks @CGMossa.

@CGMossa
Copy link
Member

CGMossa commented Apr 27, 2023

@bicarlsen Right, you need to run the document() function again, as to regenerate Rd-files with the right markdown link notation in them.

@bicarlsen
Copy link
Contributor Author

Okay, I was having trouble running the tests on my local machine, but noticed that I had to change the tests/extendrtests/src/rust/Cargo.toml > extendr-api to have an absolute path. I am still receiving an error

── Error ('test-graphic-device.R:2:5'): `my_device()` works as expected ────────
Error in `my_device("foo")`: Graphics API version mismatch
Backtrace:
     ▆
  1. ├─testthat::expect_output(...) at test-graphic-device.R:2:4
  2. │ └─testthat:::quasi_capture(...)
  3. │   ├─testthat (local) .capture(...)
  4. │   │ └─testthat::capture_output_lines(code, print, width = width)
  5. │   │   └─testthat:::eval_with_output(code, print = print, width = width)
  6. │   │     ├─withr::with_output_sink(path, withVisible(code))
  7. │   │     │ └─base::force(code)
  8. │   │     └─base::withVisible(code)
  9. │   └─rlang::eval_bare(quo_get_expr(.quo), quo_get_env(.quo))
 10. └─extendrtests::my_device("foo")

but am not sure why as I didn't touch that code and it seems unrelated to this PR.

@CGMossa
Copy link
Member

CGMossa commented Apr 27, 2023 via email

Copy link
Member

@CGMossa CGMossa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, that part is resolved. Did you have success with running tests after running rextendr::clean().

@Ilia-Kosenkov
Copy link
Member

I would say looks good. We have tests to cover these edge cases, everything seems to be in place. Please finalize the change request discussion with @CGMossa and we can proceed.

Oh and BTW, please write a note in the changelog.

@bicarlsen
Copy link
Contributor Author

After a rextendr::clean() I still receive the error

Error (test-graphic-device.R:2:5): `my_device()` works as expected
Error in `my_device("foo")`: Graphics API version mismatch
Backtrace:
  1. testthat::expect_output(...)
       at test-graphic-device.R:2:4
 10. extendrtests::my_device("foo")

@bicarlsen
Copy link
Contributor Author

bicarlsen commented Apr 28, 2023

@Ilia-Kosenkov I browsed the CHANGELOG but had a difficult time understanding how exactly to add an entry in the correct format. Could you give some insight?

@CGMossa
Copy link
Member

CGMossa commented Apr 28, 2023

After a rextendr::clean() I still receive the error

Error (test-graphic-device.R:2:5): `my_device()` works as expected
Error in `my_device("foo")`: Graphics API version mismatch
Backtrace:
  1. testthat::expect_output(...)
       at test-graphic-device.R:2:4
 10. extendrtests::my_device("foo")

When I had this error, I couldn't even do plot.new() in R. Are you able to do that? Please try to restart your session as well.

@bicarlsen
Copy link
Contributor Author

Restarted the session a few times, cleaned and redocumented a few times, still the same error. Plotting seems to work for me though.

@Ilia-Kosenkov
Copy link
Member

Summoning @yutannihilation . You have experience with graphic devices, why are we getting version mismatch here (when executed locally)? Can it be that our bindings are for R 4.2.3 and are labeled 4.2, so if you now use R 4.2.x, the bindings are resolved to 4.2, but there still is a mismatch (i.e. if R people bumped graphics version in patch)/

@Ilia-Kosenkov
Copy link
Member

We (are trying to) maintain CHANGELOG according to these rules. In your case, you need to go to the top of the file and add under unreleased/fixed section something that describes your changes. Make it human readable, so it goes something like:

[Fixed]
- <existing entry>
- A bug preventing using raw rust literals in exported extendr functions [PR link]

Use present tense and simple wording. Give an example of a raw literal (like r#type) in and how it is converted to type now. I would say 1-2 sentences are enough.

@yutannihilation
Copy link
Contributor

R people bumped graphics version in patch

It can happen.

https://github.com/wch/r-source/blob/ed2b4252a103c2f8494b906c404fbc9a8cd2d5ef/src/include/R_ext/GraphicsEngine.h#L81

But, I guess it's not the case (unless you are using R 4.1). Let me try reproducing it on my local.

@bicarlsen
Copy link
Contributor Author

Shall I push wish the new CHANGELOG or wait to resolve the graphics issue?

@CGMossa
Copy link
Member

CGMossa commented Apr 28, 2023

Push the changes. Gives us time to review that.

@yutannihilation
Copy link
Contributor

Okay, I reproduced it. I think you can just remove your tests/extendrtests/src/rust/Cargo.lock to update the dependency crates.

@bicarlsen
Copy link
Contributor Author

Okay, I reproduced it. I think you can just remove your tests/extendrtests/src/rust/Cargo.lock to update the dependency crates.

Yup, that did the trick :)

@bicarlsen
Copy link
Contributor Author

Is there anything else to be done here before we can merge?

@Ilia-Kosenkov Ilia-Kosenkov requested a review from CGMossa May 5, 2023 11:55
Copy link
Member

@CGMossa CGMossa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@CGMossa
Copy link
Member

CGMossa commented May 5, 2023

I had already seen this and thought @Ilia-Kosenkov wanted to give it a last look and then submission. Looks great from my end.

@Ilia-Kosenkov Ilia-Kosenkov merged commit ca00103 into extendr:master May 5, 2023
14 checks passed
@bicarlsen bicarlsen deleted the bug-raw_identifiers branch May 5, 2023 15:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants