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

LinearSeqOps.lengthCompare calls isEmpty, which calls lengthCompare, hence StackOverflowException #12805

Closed
noresttherein opened this issue Jun 12, 2023 · 2 comments

Comments

@noresttherein
Copy link

Reproduction steps

Scala version: 2.13.11

	class ConstSeq[X](override val head :X, override val knownSize :Int) extends AbstractSeq[X] with LinearSeq[X] {
		override def tail :LinearSeq[X] = {
			if (knownSize == 0)
				throw new UnsupportedOperationException("Seq.empty.tail")
			new ConstSeq(head, knownSize - 1)
		}
	}
	new ConstSeq(42, 2).isEmpty

Problem

Exception in thread "main" java.lang.StackOverflowError
	at scala.collection.LinearSeqOps.lengthCompare(LinearSeq.scala:95)
	at scala.collection.LinearSeqOps.lengthCompare$(LinearSeq.scala:86)
	at Playground$ConstSeq.lengthCompare(Playground.scala:12)
	at scala.collection.SeqOps.isEmpty(Seq.scala:843)
	at scala.collection.SeqOps.isEmpty$(Seq.scala:843)
	at scala.collection.AbstractSeq.isEmpty(Seq.scala:1189)
	at scala.collection.LinearSeqOps.loop$1(LinearSeq.scala:89)
	at scala.collection.LinearSeqOps.lengthCompare(LinearSeq.scala:96)
	at scala.collection.LinearSeqOps.lengthCompare$(LinearSeq.scala:86)
	at Playground$ConstSeq.lengthCompare(Playground.scala:12)
	at scala.collection.SeqOps.isEmpty(Seq.scala:843)
	at scala.collection.SeqOps.isEmpty$(Seq.scala:843)
	at scala.collection.AbstractSeq.isEmpty(Seq.scala:1189)
	at scala.collection.LinearSeqOps.loop$1(LinearSeq.scala:89)
	at scala.collection.LinearSeqOps.lengthCompare(LinearSeq.scala:96)
	at scala.collection.LinearSeqOps.lengthCompare$(LinearSeq.scala:86)
	at Playground$ConstSeq.lengthCompare(Playground.scala:12)
	at scala.collection.SeqOps.isEmpty(Seq.scala:843)
	at scala.collection.SeqOps.isEmpty$(Seq.scala:843)
	at scala.collection.AbstractSeq.isEmpty(Seq.scala:1189)
	at scala.collection.LinearSeqOps.loop$1(LinearSeq.scala:89)
	at scala.collection.LinearSeqOps.lengthCompare(LinearSeq.scala:96)
	at scala.collection.LinearSeqOps.lengthCompare$(LinearSeq.scala:86)
	at Playground$ConstSeq.lengthCompare(Playground.scala:12)

Now, a 'fix' for this could be just as well mentioning in the docs that isEmpty must be overridden, or similar, but the current situation is not ideal. Also, perhaps both should try knownSize first? The current implementation is a bit too specific to our immutable.List, while an immutable list implementation of a known size (nomen omen) is not unheard of.

@SethTisue
Copy link
Member

@scala/collections

@som-snytt
Copy link

Duplicates #11697 and scala/scala-library-next#128

scala/scala#10106

Possibly this is the Scaladoc bug where it drops text in the presence of macros.

It's visible in Scala 3:

image

@som-snytt som-snytt closed this as not planned Won't fix, can't repro, duplicate, stale Jun 12, 2023
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

3 participants