pkl icon indicating copy to clipboard operation
pkl copied to clipboard

Type constraint violation error messages include formatting/comments from original definition

Open HT154 opened this issue 9 months ago • 0 comments

Noticed this while testing out https://github.com/apple/pkl-pantry/pull/105

The Recur module includes some fairly complex type constraints with comments inline for clarity, eg.

until: (
  * DateTime(
      // if dtstart is defined and a datetime, until must also be a datetime if defined
      dtstart?.ifNonNull((it) -> it is DateTime) ?? true,
      dtstart?.ifNonNull((it) -> utc == (it as DateTime).utc) ?? true,
      utc.implies(tzid == null)
    )
  |Date(
    // if dtstart is defined and a date, until must also be a date if defined
    dtstart?.ifNonNull((it) -> it is Date) ?? true,
    tzid == null
  )
  )?

If non of these constraints can be satisfied, eg. by input such as

import "@icalendar/Recur.pkl"
result = new Recur {
  tzid = "America/Los_Angeles"
  freq = "YEARLY"
  until = DateTime.DateTime("20250101T000000Z")
}.toString()

Then the resulting error message will include the full original code with only newlines stripped:

Expected value of type `* DateTime(       // if dtstart is defined and a datetime, until must also be a datetime if defined       dtstart?.ifNonNull((it) -> it is DateTime) ?? true,       dtstart?.ifNonNull((it) -> utc == (it as DateTime).utc) ?? true,       utc.implies(tzid == null)     )   |Date(     // if dtstart is defined and a date, until must also be a date if defined     dtstart?.ifNonNull((it) -> it is Date) ?? true,     tzid == null   )`, but got a different `icalendar.DateTime`. Value: new ModuleClass { date = ?; time { h = ?; m = ?; s = ?; utc = true }; utc = t...

Ideally this should produce output with the type/constraints canonically formatted, eg. (without addition of newlines for line length)

Expected value of type `*DateTime(dtstart?.ifNonNull((it) -> it is DateTime) ?? true, dtstart?.ifNonNull((it) -> utc == (it as DateTime).utc) ?? true, utc.implies(tzid == null))|Date(dtstart?.ifNonNull((it) -> it is Date) ?? true, tzid == null)`, but got a different `icalendar.DateTime`. Value: new ModuleClass { date = ?; time { h = ?; m = ?; s = ?; utc = true }; utc = t...

HT154 avatar Mar 21 '25 00:03 HT154