Skip to content
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

sourcecode.Text and math #8

Open
stanch opened this issue Mar 7, 2016 · 7 comments
Open

sourcecode.Text and math #8

stanch opened this issue Mar 7, 2016 · 7 comments

Comments

@stanch
Copy link

stanch commented Mar 7, 2016

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

lihaoyi commented Mar 7, 2016

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...

@stanch
Copy link
Author

stanch commented Mar 7, 2016

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

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

@lihaoyi
Copy link
Member

lihaoyi commented Mar 7, 2016

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

@stanch
Copy link
Author

stanch commented Mar 7, 2016

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

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

wtf...

@jducoeur
Copy link

jducoeur commented Mar 7, 2016

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.)

@metasim
Copy link

metasim commented Jul 22, 2016

@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
Copy link

metasim commented Jul 22, 2016

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.)

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

No branches or pull requests

4 participants