Skip to content

Fix insufficient number width allocated when using -print-lines #23336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2025

Conversation

natsukagami
Copy link
Contributor

@natsukagami natsukagami commented Jun 9, 2025

Fix #22505

The main problem of the crash was the combination of -print-lines and -explain,
along with the rare case where we need to display the 9-10 range for the line-range 9..10
(which was represented in the codebase as 8..9), that was given insufficient number width to begin with.

Alongside the change, I also added newlines before the TypeMismatch explanation's tree output.
This can be debated, but without it -explain -print-lines looks like this:

-- [E007] Type Mismatch Error: /tmp/tmp.jNWSWbUu4O/src/main/scala/Main.scala:4:21 
 4 |  val tmp: List[B] = for (
   |                     ^
   |                     Found:    IndexedSeq[B]
   |                     Required: List[B]
 5 |    i <- (0 to l.size - 1);
 6 |//       j <- (i + 1 to l.size - 1);
 7 |    a <- (0 to l(i).size - 1);
 8 |//      b   <- (0 to l(j).size - 1);
 9 |    res <- f(l(i)(a))
10 |  ) yield res
   |----------------------------------------------------------------------------
   | Explanation (enabled by `-explain`)
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |
   | Tree:   5|intWrapper(0).to(l.size - 1).flatMap[B]((i: Int) =>
   |     7|  intWrapper(0).to(l.apply(i).size - 1).flatMap[B]((a: Int) =>
   |  9-10|    f.apply(l.apply(i).apply(a)).map[B]((res: B) => res))
   | )
   | I tried to show that
   |   IndexedSeq[B]
   | conforms to
   |   List[B]
   | but none of the attempts shown below succeeded:
   |
   |   ==> IndexedSeq[B]  <:  List[B]  = false
   |
   | The tests were made under the empty constraint
    ----------------------------------------------------------------------------

The error looks much nicer (imo!) with the change:

   |----------------------------------------------------------------------------
   | Explanation (enabled by `-explain`)
   |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
   |
   | Tree:
   |
   |     5|intWrapper(0).to(l.size - 1).flatMap[B]((i: Int) =>
   |     7|  intWrapper(0).to(l.apply(i).size - 1).flatMap[B]((a: Int) =>
   |  9-10|    f.apply(l.apply(i).apply(a)).map[B]((res: B) => res))
   | )
   |
   | I tried to show that
   |   IndexedSeq[B]
   | conforms to
   |   List[B]
   | but none of the attempts shown below succeeded:
   |
   |   ==> IndexedSeq[B]  <:  List[B]  = false
   |
   | The tests were made under the empty constraint
    ----------------------------------------------------------------------------

(I have no idea how to add a line range to the last ), perhaps something can be changed
in the .show implementation, but it seems complicated...)

Changes

  • Change max number width to accomodate the full number range
  • Put the TypeMismatch explanation tree on its own lines

@natsukagami natsukagami merged commit 7d4fb6f into scala:main Jun 12, 2025
29 checks passed
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.

Compiler crash with for-comprehension
2 participants