Skip to content

Fails to parse XML literal as NodeBuffer #8214

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

Closed
ashawley opened this issue Feb 5, 2020 · 4 comments
Closed

Fails to parse XML literal as NodeBuffer #8214

ashawley opened this issue Feb 5, 2020 · 4 comments

Comments

@ashawley
Copy link
Member

ashawley commented Feb 5, 2020

In 2.13.1, it was possible to specify multiple XML literal elements and have them parsed as NodeBuffer (usually parsing one would be an Elem). I don't think this capability is used very often in Scala code bases, but given how much Scala code is out there, it is worth surfacing this regression.

import scala.xml.NodeBuffer
val nodeBuffer: NodeBuffer = <hello/><world/>
println()

It works fine in a REPL (dotty doesn't have :paste?), but not in a file:

[error] -- [E008] Member Not Found Error: src/main/scala/XMLHelloWorld.scala:9:4 
[error] 8 |    val nodeBuffer: NodeBuffer = <hello/><world/>
[error] 9 |    println()
[error]   |                                  ^
[error]   |                     value println is not a member of scala.xml.NodeBuffer
[error] one error found
[error] (Compile / compileIncremental) Compilation failed

Here's an example repo with a branch with this defect:

https://github.com/ashawley/dotty-scala-xml-example/tree/nodebuffer

@smarter
Copy link
Member

smarter commented Feb 5, 2020

Somehow, the println on the last line is seen by the parser as part of the expression started on the previous line:

 ./bin/dotc -Xprint:parser -classpath "$(coursier fetch -p org.scala-lang.modules:scala-xml_2.13:2.0.0-M1)" try/i8214.scala
parsed:
package <empty> {
  import scala.xml.NodeBuffer
  module object Test {
    val nodeBuffer: NodeBuffer =
      {
        val $buf = new _root_.scala.xml.NodeBuffer()
        $buf.&+(
          {
            {
              new _root_.scala.xml.Elem(null, "hello", _root_.scala.xml.Null,
                $scope
              , true)
            }
          }
        )
        $buf.&+(
          {
            {
              new _root_.scala.xml.Elem(null, "world", _root_.scala.xml.Null,
                $scope
              , true)
            }
          }
        )
        $buf
      } println ()
  }
}
-- [E008] Member Not Found Error: try/i8214.scala:4:2 --------------------------
3 |  val nodeBuffer: NodeBuffer = <hello/><world/>
4 |  println()
  |                                ^
  |                     value println is not a member of scala.xml.NodeBuffer
1 error found

@odersky
Copy link
Contributor

odersky commented Feb 6, 2020

Since I am strongly in favor of abandoning XML literals for Scala 3 I won't put effort in fixing this. It seems like a non-obvious interaction between the XML parser (which I don't know) and the normal parser. Somebody else should take this on if there's interest in fixing this.

@odersky odersky removed their assignment Feb 6, 2020
@smarter
Copy link
Member

smarter commented Feb 6, 2020

As a workaround, one can add braces around the xml expression:

import scala.xml.NodeBuffer
object Test {
  val nodeBuffer: NodeBuffer = {
    <hello/><world/>
  }

  println()
}

@som-snytt
Copy link
Contributor

som-snytt commented Feb 6, 2020

This is a duplicate of scala/bug#9027. I'll attempt a PR that reapplies the patch.

That old PR was on 2.11, had a backport to 2.10, and now will have a forward port to 3.

som-snytt added a commit to som-snytt/dotty that referenced this issue Feb 6, 2020
@odersky odersky closed this as completed in 624a6e0 Feb 7, 2020
odersky added a commit that referenced this issue Feb 7, 2020
Fix #8214 Don't consume whitespace after XML
gabro pushed a commit to gabro/dotty that referenced this issue Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants