Skip to content

Conversation

@sbearrows
Copy link
Contributor

First pass for adding this error message. Error message could prolly still be more informative but let me know what you think!

@sbearrows sbearrows changed the title Feature add merror message for incorrect decorators Feature add error message for incorrect decorators Jun 9, 2021
R/register.R Outdated

out <- decorations[decorations$decoration == tag, ]

if(any(!decorations$decoration %in% c("cpp11::register", "cpp11::init"))) {
Copy link
Member

Choose a reason for hiding this comment

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

maybe store the result in a variable to make this easier to read?

e.g.

bad_decorations <- !decorations$decoration %in% c("cpp11::register", "cpp11::init")

if (any(bad_decorations)) { # etc..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure!

R/register.R Outdated
out <- decorations[decorations$decoration == tag, ]

if(any(!decorations$decoration %in% c("cpp11::register", "cpp11::init"))) {
lines <- decorations$line[which(!decorations$decoration %in% c("cpp11::register", "cpp11::init"))]
Copy link
Member

Choose a reason for hiding this comment

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

The which() is unnecessary here, you can just index with the logical vector directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

R/register.R Outdated
if(any(!decorations$decoration %in% c("cpp11::register", "cpp11::init"))) {
lines <- decorations$line[which(!decorations$decoration %in% c("cpp11::register", "cpp11::init"))]
if(length(lines) > 1) {
stop(call. = FALSE, paste0("Can't capture cpp11 decorators on lines ",
Copy link
Member

Choose a reason for hiding this comment

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

Maybe the error should be something like

cpp11 attributes must be either `cpp11::register` or `cpp11::init`:
- Invalid attribute [[cpp11::foobar]] on line 123 in file xyz.
- Invalid attribute [[cpp11::baz]] on line 456 in file abc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh that looks great!
Is the best way to make multi-line error messages by using a combination of paste() statements with sep = "\n" ? Or would this be a message() following by an empty stop() ?

Copy link
Member

@jimhester jimhester Jun 9, 2021

Choose a reason for hiding this comment

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

You can actually use glue to do this a little more nicely, something like this (untested, might need tweaks)

bad_lines <- glue::glue_collapse(
  glue::glue("- Invalid attribute `{names}` on line {line} in file '{file}'."),
  "\n"
)

msg <- glue::glue("
  cpp11 attributes must be either `cpp11::register` or `cpp11::init`:
  {bad_lines}
  ")
stop(msg, call. = FALSE)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh nice!

@jimhester
Copy link
Member

I think maybe we should pull this check out into a separate check_valid_attributes() function, otherwise we would run in twice.

@sbearrows
Copy link
Contributor Author

fixes #127

@jimhester jimhester merged commit 7a7da57 into r-lib:master Jun 16, 2021
@sbearrows sbearrows deleted the feature-error_mesg_register branch June 16, 2021 21:31
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.

2 participants