sourcecode icon indicating copy to clipboard operation
sourcecode copied to clipboard

sourcecode.Text and math

Open stanch opened this issue 9 years ago • 7 comments

@ def x[A](text: sourcecode.Text[A]) = text.source
defined function x
@ x(3 + 4)
res10: String = "+ 4"
@ x(3 * 4)
res11: String = "*"

stanch avatar Mar 07 '16 21:03 stanch

I believe this is due to constant folding within the Scala compiler. I think it disappears the 3 + 4 tree and leaves a 7 tree in its place, positioned at the +. Not sure if it's possible to work around this but worth tracking...

lihaoyi avatar Mar 07 '16 22:03 lihaoyi

That sounds plausible. What about this one? Is it because of the :-ending operator?

@ x(1 :: List(2, 3))
res4: String = ":: List(2, 3)"

stanch avatar Mar 07 '16 22:03 stanch

Not sure, but doubtful. That's probably a different matter, will have to dig into it. Keep these coming!

lihaoyi avatar Mar 07 '16 22:03 lihaoyi

@ x("a" +: Vector("b", "c"))
res6: String = """
+: Vector("b", "c")
"""

@ x("a" +: Vector("b", "c") :+ "q")
res7: String = """
"a" +: Vector("b", "c") :+ "q"

wtf...

stanch avatar Mar 07 '16 22:03 stanch

Hmm. Put 'em all together, and they do rather sound like :-ending operators are causing confusion. I don't recall the precedence rules off the top of my head, but if :+ is higher precedence than +:, then the latter three examples all seem to suggest that, if the highest-precedence operator is :-ending, then that's considered the location of the expression.

(In other words, my suspicion is that the :+ in the last example overrides the problem introduced by the +: in the previous one, changing the inferred location.)

jducoeur avatar Mar 07 '16 22:03 jducoeur

@lihaoyi Any tips on how we might help debug this? How might we add code or compiler flags to spit out intermediate representations with positioning information (sorry, I've not programmed macros beyond 1-liners, but am willing to help).

metasim avatar Jul 22 '16 13:07 metasim

Desugaring tests:

These work:

scala> x(List(2,3).::(1))
res1: String = List(2,3).::(1)

scala> x(List(2,3).$colon$colon(1))
res2: String = List(2,3).$colon$colon(1)

scala> x(Vector("b", "c").+:("a"))
res3: String = Vector("b", "c").+:("a")

This does not:

scala> x(3.$times(4))
res4: String = (4)

This is with Scala 2.11.7 (wondering if behavior might be slightly different in 2.10 since the library is using calls to deprecated positioning APIs.)

metasim avatar Jul 22 '16 14:07 metasim