apple / pkl

A configuration as code language with rich validation and tooling.
https://pkl-lang.org
Apache License 2.0
10.11k stars 265 forks source link

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

Open juriad opened 2 months ago

juriad commented 2 months ago

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.

holzensp commented 1 month ago

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).