Skip to content

Fix incorrect spans assigned in some cases#469

Merged
sjrd merged 1 commit into
scalacenter:mainfrom
lucidsoftware:jpeterson-fix-incorrect-spans
May 7, 2026
Merged

Fix incorrect spans assigned in some cases#469
sjrd merged 1 commit into
scalacenter:mainfrom
lucidsoftware:jpeterson-fix-incorrect-spans

Conversation

@jadenPete
Copy link
Copy Markdown

I've observed tasty-query occasionally reading the wrong position for certain trees.

Consider this example:

class InferredTypeArgs {
  def id[T](x: T): T = x

  val a = id(42)
}

Here, the compiler inserts an inferred type annotation, so in TASTy, the ValDef statement looks like this:

val a: scala.Int = id[scala.Int](42)

The scala.Int type annotation should have a zero-width span because it doesn't exist in the source text, but tasty-query incorrectly reads its span as the same as that of id(42). Also, the scala.Int type argument should have the same span as the function it's applied to (id), but it's instead the same as that of the entire function call (id(42)).

This PR fixes both of these issues. I've verified that the tests I added fail before the fix, and succeed after the fix.

Copy link
Copy Markdown
Contributor

@sjrd sjrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice. Thank you. I have some comments.

* positions that goes wild.
*/
"""type AbstractType
| type AbstractWithBounds >: Null <: Product""".stripMargin,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it suspicious that this item disappears, instead of being fixed. Same thing below. Could you explain why that happens?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's because of the !pos.isZeroExtent check here:
https://github.com/scalacenter/tasty-query/blob/main/tasty-query/shared/src/test/scala/tastyquery/PositionSuite.scala#L27

The type has no bounds, so the returned string is empty. If I remove that check, I get this error:

==> X tastyquery.PositionSuite.type-definition-tree-1  0.011s munit.ComparisonFailException: /home/jpeterson/lucid/tasty-query/tasty-query/shared/src/test/scala/tastyquery/PositionSuite.scala:404
403:      )
404:    )
405:  }
values are not the same
=> Obtained
List(
  "Int",
  "",
  ">: Null <: Product",
  "Int",
  "Null <: Product = Null",
  "Null <: Product"
)
=> Diff (- obtained, + expected)
   "Int",
-  "",
   ">: Null <: Product",
    at munit.Assertions.failComparison(Assertions.scala:278)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that makes sense.

Comment thread tasty-query/shared/src/test/scala/tastyquery/PositionSuite.scala Outdated
@jadenPete jadenPete force-pushed the jpeterson-fix-incorrect-spans branch from 24761ee to 01eeb11 Compare May 7, 2026 16:44
@sjrd sjrd merged commit 3b60b17 into scalacenter:main May 7, 2026
2 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.

2 participants