Skip to content

Abort when ggproto definition is circular#6648

Closed
teunbrand wants to merge 3 commits intotidyverse:mainfrom
teunbrand:ggproto_circularity
Closed

Abort when ggproto definition is circular#6648
teunbrand wants to merge 3 commits intotidyverse:mainfrom
teunbrand:ggproto_circularity

Conversation

@teunbrand
Copy link
Copy Markdown
Collaborator

This PR aims to fix #6583.

AFAICT, we cannot detect this during object construction.
The second best way to detect this is in fetch_ggproto(), as it is such a widely used accessor.
Reprex from issue:

devtools::load_all("~/packages/ggplot2/")
#> ℹ Loading ggplot2

A <- ggproto("A")
A <- ggproto(NULL, A)
A
#> Error in `fetch_ggproto()` at ggplot2/R/ggproto.R:172:3:
#> ! <A> cannot have a circular definition.

Created on 2025-09-25 with reprex v2.1.1

Comment thread R/ggproto.R
Comment on lines +134 to +141
parent <- super()
if (!identical(parent, x)) {
# happy path
return(fetch_ggproto(parent, name))
}
cli::cli_abort(
"{.cls {class(x)[1]}} cannot have a circular definition."
)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this catch circular inheritance that has a larger radius, e.g. A->B->C->A?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good question. No it does not. I imagine this is a less common occurrance of an already obscure problem. I don't have instant inspiration on how this would be solved without laboriously tracking all parents, which may not be worth it for throwing a rare error message.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think I've ever stumbled upon this issue in any form so I can't be the judge of that. If you feel this PR solves a real issue and what I brought up is contrived, then I'm fine accepting as-is

@teunbrand
Copy link
Copy Markdown
Collaborator Author

Ok, if Thomas has never encountered this issue, I'm going to argue that this too obscure of an error to be worth fixing. I'll close this PR andn the related issue.

@teunbrand teunbrand closed this Dec 4, 2025
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.

Confusing error with circular ggproto definitions

2 participants