Skip to content

Commit e888601

Browse files
Backport "Fix(#16459) xml parse regression" to LTS (#20877)
Backports #19531 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents c4b5824 + a406088 commit e888601

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+2
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,7 @@ object Parsers {
889889
var braces = 0
890890
while (true) {
891891
val token = lookahead.token
892+
if (query != LARROW && token == XMLSTART) return false
892893
if (braces == 0) {
893894
if (token == query) return true
894895
if (stopScanTokens.contains(token) || lookahead.isNestedEnd) return false
@@ -910,6 +911,7 @@ object Parsers {
910911
lookahead.nextToken()
911912
while (parens != 0 && lookahead.token != EOF) {
912913
val token = lookahead.token
914+
if (token == XMLSTART) return true
913915
if (token == LPAREN) parens += 1
914916
else if (token == RPAREN) parens -= 1
915917
lookahead.nextToken()

tests/run/i16459.scala

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
object Test {
2+
import scala.xml.*
3+
def main(args: Array[String]): Unit = {
4+
5+
val xml = if(true) {
6+
<script type="text/javascript">
7+
'location.reload()'
8+
'foo bar'
9+
</script>
10+
} else <div>empty</div>
11+
12+
assert(
13+
xml match
14+
case elm: Elem if
15+
elm.label == "script"
16+
&& elm.child.length == 1
17+
&& elm.child(0) == Atom(Text("\n 'location.reload()'\n 'foo bar'\n "))
18+
=> true
19+
case _ => false
20+
,
21+
xml
22+
)
23+
// Scala 3 syntax
24+
val auxiliary0 = if true then {
25+
<script type="text/javascript">
26+
'location.reload()'
27+
'foo bar'
28+
</script>
29+
} else <div>empty</div>
30+
31+
val auxiliary1 = if true then
32+
<script type="text/javascript">
33+
'location.reload()'
34+
'foo bar'
35+
</script>
36+
else <div>empty</div>
37+
38+
val auxiliary2 = if true then <div>A</div>else <div>B</div>
39+
40+
// Note:
41+
// This does not pass in Scala 2.12.18 and 2.13.12
42+
// due to "Sequence argument type annotation `: _*` cannot be used here:"
43+
val auxiliary3 = if(true) <div>A</div>else <div>B</div>
44+
45+
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
46+
val auxiliary4 = if(true) <div attr="...">A</div>else <div attr="...">B</div>
47+
48+
// Pattern match without guard.
49+
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
50+
val auxiliary5 = for (case _ @ <div>empty</div> <- Seq(xml)) yield ()
51+
// Note: These pass in Scala 2.12.18 and 2.13.12.
52+
val auxiliary6 = for (case _ @ <div>empty</div><- Seq(xml)) yield ()
53+
val auxiliary7 = for (case _ @ <div>empty</div><-Seq(xml)) yield ()
54+
// Pattern match with if guard.
55+
// Note: This passes in Scala 2.12.18 and 2.13.12 too.
56+
val auxiliary8 = for (case _ @ <foo>FooBar</foo> <- Seq(xml) if true)
57+
yield ()
58+
// Note: These pass in Scala 2.12.18 and 2.13.12.
59+
val auxiliary9 = for (case _ @ <foo>FooBar</foo><- Seq(xml) if true)
60+
yield ()
61+
val auxiliary10 = for (case _ @ <foo>FooBar</foo><-Seq(xml) if true)
62+
yield ()
63+
64+
}
65+
66+
}
67+
68+
package scala.xml {
69+
type MetaData = AnyRef
70+
71+
class UnprefixedAttribute(
72+
val key: String,
73+
val value: Text,
74+
next1: MetaData
75+
) extends MetaData
76+
77+
trait NamespaceBinding
78+
object TopScope extends NamespaceBinding
79+
object Null
80+
abstract class Node {
81+
def label: String
82+
def child: Seq[Node]
83+
override def toString = label + child.mkString
84+
}
85+
86+
class Elem(prefix: String, val label: String, attributes1: MetaData, scope: NamespaceBinding, minimizeEmpty: Boolean, val child: Node*) extends Node
87+
object Elem {
88+
def unapply(e:Elem):Option[(String,String,Any,Text,Any)] = Some(("dummy","dummy",null,null,null))
89+
}
90+
class NodeBuffer extends Seq[Node] {
91+
val nodes = scala.collection.mutable.ArrayBuffer.empty[Node]
92+
def &+(o: Any): NodeBuffer = o match {
93+
case n: Node => nodes.addOne(n) ; this
94+
case t: Text => nodes.addOne(Atom(t)) ; this
95+
}
96+
// Members declared in scala.collection.IterableOnce
97+
def iterator: Iterator[scala.xml.Node] = nodes.iterator
98+
// Members declared in scala.collection.SeqOps
99+
def apply(i: Int): scala.xml.Node = nodes(i)
100+
def length: Int = nodes.length
101+
}
102+
case class Text(text: String)
103+
case class Atom(t: Text) extends Node {
104+
def label = t.text
105+
def child = Nil
106+
}
107+
}

0 commit comments

Comments
 (0)