Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@


* Allow `stat` in `geom_hline`, `geom_vline`, and `geom_abline`. (@sierrajohnson, #6559)
* Circularly defined `ggproto()` classes will throw more informative warning
(@teunbrand, #6583).

# ggplot2 4.0.0

Expand Down
9 changes: 8 additions & 1 deletion R/ggproto.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,14 @@ fetch_ggproto <- function(x, name) {
if (is.null(super)) {
# no super class
} else if (is.function(super)) {
res <- fetch_ggproto(super(), name)
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."
)
} else {
cli::cli_abort(c(
"{class(x)[[1]]} was built with an incompatible version of ggproto.",
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/_snaps/ggproto.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@

`_inherit` must be a <ggproto> object, not a <data.frame> object.

# circular definitions are protested

<Circular> cannot have a circular definition.

6 changes: 6 additions & 0 deletions tests/testthat/test-ggproto.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ test_that("construction checks input", {
expect_snapshot_error(ggproto("Test", mtcars, a = function(self, a) a))
})

test_that("circular definitions are protested", {
circular <- ggproto("Circular")
circular <- ggproto(NULL, circular)
expect_snapshot_error(print(circular))
})

test_that("all ggproto methods start with `{` (#6459)", {

ggprotos <- Filter(
Expand Down
Loading