sourcecode.Text and math
@ def x[A](text: sourcecode.Text[A]) = text.source
defined function x
@ x(3 + 4)
res10: String = "+ 4"
@ x(3 * 4)
res11: String = "*"
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...
That sounds plausible. What about this one? Is it because of the :-ending operator?
@ x(1 :: List(2, 3))
res4: String = ":: List(2, 3)"
Not sure, but doubtful. That's probably a different matter, will have to dig into it. Keep these coming!
@ x("a" +: Vector("b", "c"))
res6: String = """
+: Vector("b", "c")
"""
@ x("a" +: Vector("b", "c") :+ "q")
res7: String = """
"a" +: Vector("b", "c") :+ "q"
wtf...
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.)
@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).
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.)