Koltin generator fails with "'copy' overrides nothing." for subclasses of abstract class #178

Closed
opened 2025-12-30 01:21:48 +01:00 by adam · 2 comments
Owner

Originally created by @juriad on GitHub (Jul 6, 2024).

Try generating Kotlin classes for this template:

abstract class Day {
  day: Int
  month: Int
  year: Int
}

class Weekend extends Day {}
class Workday extends Day {}

days: Listing<Day>

The classes Weekend and Workday contain copy methods:

    override fun copy(
      day: Long,
      month: Long,
      year: Long
    ): Workday = Workday(day, month, year)

but the class Day which they extend:

class Workday(
    day: Long,
    month: Long,
    year: Long
  ) : Day(day, month, year) {

does not define copy method, which leads to a compilation error:

e: file:///home/whoever/whatever/app/build/generated/pkl/template/kotlin/Calendar.kt:52:5 'copy' overrides nothing.

It looks like the logic to skip generation of copy methods for abstract classes is flawed.

When making the hierarchy deeper:

open class TimeInterval {}

abstract  class Day extends TimeInterval {
...

it even fails to build with "Cannot create an instance of an abstract class." (apart from 'copy' overrides nothing.). This case should also be considered when fixing the implementation of copy.

Marking Day as open instead of abstract can be used to work around this issue.

Originally created by @juriad on GitHub (Jul 6, 2024). Try generating Kotlin classes for this template: ``` abstract class Day { day: Int month: Int year: Int } class Weekend extends Day {} class Workday extends Day {} days: Listing<Day> ``` The classes `Weekend` and `Workday` contain copy methods: ``` override fun copy( day: Long, month: Long, year: Long ): Workday = Workday(day, month, year) ``` but the class `Day` which they extend: ``` class Workday( day: Long, month: Long, year: Long ) : Day(day, month, year) { ``` does not define `copy` method, which leads to a compilation error: ``` e: file:///home/whoever/whatever/app/build/generated/pkl/template/kotlin/Calendar.kt:52:5 'copy' overrides nothing. ``` It looks like the logic to [skip generation](https://github.com/apple/pkl/blob/5cc2ea2d003a1e2c6e856363684e2aa085ed38a9/pkl-codegen-kotlin/src/main/kotlin/org/pkl/codegen/kotlin/KotlinCodeGenerator.kt#L282) of `copy` methods for abstract classes is flawed. When making the hierarchy deeper: ``` open class TimeInterval {} abstract class Day extends TimeInterval { ... ``` it even fails to build with "Cannot create an instance of an abstract class." (apart from `'copy' overrides nothing.`). This case should also be considered when fixing the implementation of `copy`. Marking `Day` as `open` instead of `abstract` can be used to work around this issue.
adam closed this issue 2025-12-30 01:21:48 +01:00
Author
Owner

@holzensp commented on GitHub (Jul 15, 2024):

Doesn't a copy method require that the class is instantiable? I would not expect a copy method on an abstract class.

The spurious override does seem to be a bug (but I'd say a separate one).

@holzensp commented on GitHub (Jul 15, 2024): Doesn't a `copy` method require that the class is instantiable? I would not expect a `copy` method on an `abstract` class. The spurious `override` does seem to be a bug (but I'd say a separate one).
Author
Owner

@odenix commented on GitHub (Oct 17, 2024):

I’ll send a PR that fixes both of the bugs reported here.

@odenix commented on GitHub (Oct 17, 2024): I’ll send a PR that fixes both of the bugs reported here.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#178