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

Fix exception on sequence matching with drop #21281

Merged
merged 4 commits into from
Aug 6, 2024

Conversation

KavinSatheeskumar
Copy link
Contributor

@KavinSatheeskumar KavinSatheeskumar commented Jul 26, 2024

When using scalac with the flag Ysafe-init-global to compile the following example, I get the following exception. This PR fixes it and adds a test case to catch things like this in the future.

Note, I made this pull request in collaboration with Ondrej Lhotak, Enze Xing, Fengyun Liu and David Hua)

The example

object Matcher {
  val vararg_arr = Array(0, 1, 2, 3)
  val vararg_lst = List(vararg_arr*)
  val vararg_splice = vararg_lst match
    case List(0, 1, xs*) => 1  // binds xs to Seq(2, 3)
    case List(1, _*) => 0           // wildcard pattern
    case _ => 2
  println(vararg_splice)
}

The exception

Exception in thread "main" java.lang.AssertionError: NoDenotation.owner
	at dotty.tools.dotc.core.SymDenotations$NoDenotation$.owner(SymDenotations.scala:2623)
	at dotty.tools.dotc.transform.init.Objects.call(Objects.scala:660)
	at dotty.tools.dotc.transform.init.Objects.evalSeqPatterns$1(Objects.scala:1494)
	at dotty.tools.dotc.transform.init.Objects.evalPattern$1(Objects.scala:1406)
	at dotty.tools.dotc.transform.init.Objects.evalCase$1(Objects.scala:1336)
	at dotty.tools.dotc.transform.init.Objects.patternMatch$$anonfun$1(Objects.scala:1505)
	at scala.collection.immutable.List.map(List.scala:247)
	at dotty.tools.dotc.transform.init.Objects.patternMatch(Objects.scala:1505)
	at dotty.tools.dotc.transform.init.Objects.cases(Objects.scala:1255)
	at dotty.tools.dotc.transform.init.Objects.eval$$anonfun$1(Objects.scala:1095)
	at dotty.tools.dotc.transform.init.Objects$Cache$Data.$anonfun$5(Objects.scala:539)
	at dotty.tools.dotc.transform.init.Cache.cachedEval(Cache.scala:112)
	at dotty.tools.dotc.transform.init.Objects$Cache$Data.cachedEval(Objects.scala:538)
	at dotty.tools.dotc.transform.init.Objects.eval(Objects.scala:1095)
	at dotty.tools.dotc.transform.init.Objects.init$$anonfun$4(Objects.scala:1708)
	at scala.collection.immutable.List.foreach(List.scala:334)
	at dotty.tools.dotc.transform.init.Objects.init(Objects.scala:1705)
	at dotty.tools.dotc.transform.init.Objects$State$.iterate$1(Objects.scala:278)
	at dotty.tools.dotc.transform.init.Objects$State$.doCheckObject(Objects.scala:293)
	at dotty.tools.dotc.transform.init.Objects$State$.checkObjectAccess(Objects.scala:320)
	at dotty.tools.dotc.transform.init.Objects.accessObject(Objects.scala:1059)
	at dotty.tools.dotc.transform.init.Objects.checkClasses$$anonfun$2(Objects.scala:1072)
	at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:619)
	at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:617)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:935)
	at scala.collection.IterableOps$WithFilter.foreach(Iterable.scala:905)
	at dotty.tools.dotc.transform.init.Objects.checkClasses(Objects.scala:1070)
	at dotty.tools.dotc.transform.init.Checker.runOn$$anonfun$1(Checker.scala:58)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at dotty.tools.dotc.core.Phases$Phase.cancellable(Phases.scala:521)
	at dotty.tools.dotc.transform.init.Checker.runOn(Checker.scala:59)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:343)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
	at dotty.tools.dotc.Run.runPhases$1(Run.scala:336)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:384)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:396)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:69)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:396)
	at dotty.tools.dotc.Run.compileSources(Run.scala:282)
	at dotty.tools.dotc.Run.compile(Run.scala:267)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
	at dotty.tools.dotc.Driver.process(Driver.scala:201)
	at dotty.tools.dotc.Driver.process(Driver.scala:169)
	at dotty.tools.dotc.Driver.process(Driver.scala:181)
	at dotty.tools.dotc.Driver.main(Driver.scala:211)
	at dotty.tools.dotc.Main.main(Main.scala)

  exception occurred while compiling List(../test-case.scala)

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/scala/scala3/issues/new/choose
  For non-enriched exceptions, compile with -Xno-enrich-error-messages.

     while compiling: <no file>
        during phase: parser
                mode: Mode(ImplicitsEnabled,ReadPositions)
     library version: version 2.13.14
    compiler version: version 3.6.0-RC1-bin-SNAPSHOT-nonbootstrapped-git-3dfd762
            settings: -Ysafe-init-global true -classpath /home/kavin/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.14/scala-library-2.13.14.jar:/home/kavin/Documents/4A/URA2/scala3/library/../out/bootstrap/scala3-library-bootstrapped/scala-3.6.0-RC1-bin-SNAPSHOT-nonbootstrapped/scala3-library_3-3.6.0-RC1-bin-SNAPSHOT.jar -d /home/kavin/Documents/4A/URA2/scala3/compiler/../out/default-last-scalac-out.jar
[error] Nonzero exit code returned from runner: 1
[error] (scala3-compiler / Compile / runMain) Nonzero exit code returned from runner: 1
[error] Total time: 17 s, completed Jul 26, 2024, 6:35:43 PM

Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you @KavinSatheeskumar 👍

PS: please rebase against main to avoid unnecessary merge commits.

@@ -1485,12 +1485,12 @@ class Objects(using Context @constructorOnly):
if isWildcardStarArgList(pats) then
if pats.size == 1 then
// call .toSeq
val toSeqDenot = scrutineeType.member(nme.toSeq).suchThat(_.info.isParameterless)
val toSeqDenot = getMemberMethod(scrutineeType, nme.toSeq, toSeqType(elemType)) //scrutineeType.member(nme.toSeq).suchThat(_.info.isParameterless)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a refactoring or a bug fix? We can remove the commented code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a refactor. I forgot to remove it after I tested the code to see if it still worked.

Side note, I'll make sure rebase next time 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, it's easy to rebase and squash --- you can force push to the PR branch.

@olhotak olhotak merged commit 8064536 into scala:main Aug 6, 2024
28 checks passed
WojciechMazur pushed a commit that referenced this pull request Aug 28, 2024
When using scalac with the flag Ysafe-init-global to compile the
following example, I get the following exception. This PR fixes it and
adds a test case to catch things like this in the future.

Note, I made this pull request in collaboration with Ondrej Lhotak, Enze
Xing, Fengyun Liu and David Hua)

The example
```Scala
object Matcher {
  val vararg_arr = Array(0, 1, 2, 3)
  val vararg_lst = List(vararg_arr*)
  val vararg_splice = vararg_lst match
    case List(0, 1, xs*) => 1  // binds xs to Seq(2, 3)
    case List(1, _*) => 0           // wildcard pattern
    case _ => 2
  println(vararg_splice)
}
```
The exception
```
Exception in thread "main" java.lang.AssertionError: NoDenotation.owner
	at dotty.tools.dotc.core.SymDenotations$NoDenotation$.owner(SymDenotations.scala:2623)
	at dotty.tools.dotc.transform.init.Objects.call(Objects.scala:660)
	at dotty.tools.dotc.transform.init.Objects.evalSeqPatterns$1(Objects.scala:1494)
	at dotty.tools.dotc.transform.init.Objects.evalPattern$1(Objects.scala:1406)
	at dotty.tools.dotc.transform.init.Objects.evalCase$1(Objects.scala:1336)
	at dotty.tools.dotc.transform.init.Objects.patternMatch$$anonfun$1(Objects.scala:1505)
	at scala.collection.immutable.List.map(List.scala:247)
	at dotty.tools.dotc.transform.init.Objects.patternMatch(Objects.scala:1505)
	at dotty.tools.dotc.transform.init.Objects.cases(Objects.scala:1255)
	at dotty.tools.dotc.transform.init.Objects.eval$$anonfun$1(Objects.scala:1095)
	at dotty.tools.dotc.transform.init.Objects$Cache$Data.$anonfun$5(Objects.scala:539)
	at dotty.tools.dotc.transform.init.Cache.cachedEval(Cache.scala:112)
	at dotty.tools.dotc.transform.init.Objects$Cache$Data.cachedEval(Objects.scala:538)
	at dotty.tools.dotc.transform.init.Objects.eval(Objects.scala:1095)
	at dotty.tools.dotc.transform.init.Objects.init$$anonfun$4(Objects.scala:1708)
	at scala.collection.immutable.List.foreach(List.scala:334)
	at dotty.tools.dotc.transform.init.Objects.init(Objects.scala:1705)
	at dotty.tools.dotc.transform.init.Objects$State$.iterate$1(Objects.scala:278)
	at dotty.tools.dotc.transform.init.Objects$State$.doCheckObject(Objects.scala:293)
	at dotty.tools.dotc.transform.init.Objects$State$.checkObjectAccess(Objects.scala:320)
	at dotty.tools.dotc.transform.init.Objects.accessObject(Objects.scala:1059)
	at dotty.tools.dotc.transform.init.Objects.checkClasses$$anonfun$2(Objects.scala:1072)
	at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:619)
	at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:617)
	at scala.collection.AbstractIterable.foreach(Iterable.scala:935)
	at scala.collection.IterableOps$WithFilter.foreach(Iterable.scala:905)
	at dotty.tools.dotc.transform.init.Objects.checkClasses(Objects.scala:1070)
	at dotty.tools.dotc.transform.init.Checker.runOn$$anonfun$1(Checker.scala:58)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at dotty.tools.dotc.core.Phases$Phase.cancellable(Phases.scala:521)
	at dotty.tools.dotc.transform.init.Checker.runOn(Checker.scala:59)
	at dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:343)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1323)
	at dotty.tools.dotc.Run.runPhases$1(Run.scala:336)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:384)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$adapted$1(Run.scala:396)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:69)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:396)
	at dotty.tools.dotc.Run.compileSources(Run.scala:282)
	at dotty.tools.dotc.Run.compile(Run.scala:267)
	at dotty.tools.dotc.Driver.doCompile(Driver.scala:37)
	at dotty.tools.dotc.Driver.process(Driver.scala:201)
	at dotty.tools.dotc.Driver.process(Driver.scala:169)
	at dotty.tools.dotc.Driver.process(Driver.scala:181)
	at dotty.tools.dotc.Driver.main(Driver.scala:211)
	at dotty.tools.dotc.Main.main(Main.scala)

  exception occurred while compiling List(../test-case.scala)

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/scala/scala3/issues/new/choose
  For non-enriched exceptions, compile with -Xno-enrich-error-messages.

     while compiling: <no file>
        during phase: parser
                mode: Mode(ImplicitsEnabled,ReadPositions)
     library version: version 2.13.14
    compiler version: version 3.6.0-RC1-bin-SNAPSHOT-nonbootstrapped-git-3dfd762
            settings: -Ysafe-init-global true -classpath /home/kavin/.cache/coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.14/scala-library-2.13.14.jar:/home/kavin/Documents/4A/URA2/scala3/library/../out/bootstrap/scala3-library-bootstrapped/scala-3.6.0-RC1-bin-SNAPSHOT-nonbootstrapped/scala3-library_3-3.6.0-RC1-bin-SNAPSHOT.jar -d /home/kavin/Documents/4A/URA2/scala3/compiler/../out/default-last-scalac-out.jar
[error] Nonzero exit code returned from runner: 1
[error] (scala3-compiler / Compile / runMain) Nonzero exit code returned from runner: 1
[error] Total time: 17 s, completed Jul 26, 2024, 6:35:43 PM
```
[Cherry-picked 8064536]
WojciechMazur added a commit that referenced this pull request Aug 28, 2024
)

Backports #21281 to the 3.5.2 branch.

PR submitted by the release tooling.
[skip ci]
@WojciechMazur WojciechMazur added this to the 3.5.2 milestone Oct 8, 2024
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

Successfully merging this pull request may close these issues.

4 participants