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

Exception throw when compiling a project: java.lang.NullPointerException: tried to cast away nullability, but value is null #15845

Closed
OndrejSpanel opened this issue Aug 11, 2022 · 11 comments · Fixed by #15894
Labels
area:typer itype:crash stat:needs minimization Needs a self contained minimization
Milestone

Comments

@OndrejSpanel
Copy link
Member

OndrejSpanel commented Aug 11, 2022

Compiler version

3.1.3

Minimized example

I am afraid I do not have a minimized example yet, I will need some advice on how to extract it.

I am trying to port my complex project to Scala 3. I have fixed most deprecation warnings in 2.13 and upgraded almost all libraries. I am not getting much compiler errors any more, but still use both "-source:3.0-migration" and "-Xignore-scala2-macros", as not all libraries are ported and I still need to compile the project as 2.13 one.

Output

When building my project with Scala 3.1.3, I get following error:

Exception when compiling 382 sources to .......
java.lang.NullPointerException: tried to cast away nullability, but value is null

I am not told which part of the project / what particulat Scala source is the compiler working at, therefore I have no idea how could I pinpoint the problem.

The top of the callstack is:

[error] java.lang.NullPointerException: tried to cast away nullability, but value is null
[error] scala.runtime.Scala3RunTime$.nn(Scala3RunTime.scala:19)
[error] dotty.tools.dotc.core.OrderingConstraint.swapKey$1(OrderingConstraint.scala:495)
[error] dotty.tools.dotc.core.OrderingConstraint.subst(OrderingConstraint.scala:496)
[error] dotty.tools.dotc.core.OrderingConstraint.subst(OrderingConstraint.scala:494)
[error] dotty.tools.dotc.core.TyperState.ensureNotConflicting$$anonfun$1(TyperState.scala:181)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] scala.collection.immutable.List.foreach(List.scala:333)
[error] dotty.tools.dotc.core.TyperState.ensureNotConflicting(TyperState.scala:182)
[error] dotty.tools.dotc.core.TyperState.mergeConstraintWith(TyperState.scala:193)
[error] dotty.tools.dotc.core.TyperState.commit(TyperState.scala:163)
[error] dotty.tools.dotc.typer.Typer.tryEither(Typer.scala:3111)
[error] dotty.tools.dotc.typer.Applications.realApply$1(Applications.scala:1002)
[error] dotty.tools.dotc.typer.Applications.typedApply(Applications.scala:1040)
[error] dotty.tools.dotc.typer.Applications.typedApply$(Applications.scala:327)
[error] dotty.tools.dotc.typer.Typer.typedApply(Typer.scala:117)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2832)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2895)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2960)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2964)
[error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3080)
[error] dotty.tools.dotc.typer.Typer.$anonfun$47(Typer.scala:2278)
[error] dotty.tools.dotc.typer.PrepareInlineable$.dropInlineIfError(PrepareInlineable.scala:248)
[error] dotty.tools.dotc.typer.Typer.typedDefDef(Typer.scala:2278)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2808)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2894)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2960)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2964)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2986)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3036)
[error] dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2476)
[error] dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:2820)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2824)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2894)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2960)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2964)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2986)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3036)
[error] dotty.tools.dotc.typer.Typer.typedClassDef(Typer.scala:2476)
[error] dotty.tools.dotc.typer.Typer.typedTypeOrClassDef$1(Typer.scala:2820)
[error] dotty.tools.dotc.typer.Typer.typedNamed$1(Typer.scala:2824)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2894)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2960)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2964)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:2986)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3036)
[error] dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2603)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2865)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2895)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2960)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2964)
[error] dotty.tools.dotc.typer.Typer.traverse$1(Typer.scala:3013)
[error] dotty.tools.dotc.typer.Typer.typedStats(Typer.scala:3036)
[error] dotty.tools.dotc.typer.Typer.typedPackageDef(Typer.scala:2603)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2865)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2895)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2960)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:2964)
[error] dotty.tools.dotc.typer.Typer.typedExpr(Typer.scala:3080)
[error] dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$1(TyperPhase.scala:43)
[error] dotty.tools.dotc.typer.TyperPhase.typeCheck$$anonfun$adapted$1(TyperPhase.scala:50)
[error] scala.Function0.apply$mcV$sp(Function0.scala:39)
[error] dotty.tools.dotc.core.Phases$Phase.monitor(Phases.scala:414)
[error] dotty.tools.dotc.typer.TyperPhase.typeCheck(TyperPhase.scala:50)
[error] dotty.tools.dotc.typer.TyperPhase.runOn$$anonfun$3(TyperPhase.scala:84)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] scala.collection.immutable.List.foreach(List.scala:333)
[error] dotty.tools.dotc.typer.TyperPhase.runOn(TyperPhase.scala:84)
[error] dotty.tools.dotc.Run.runPhases$1$$anonfun$1(Run.scala:225)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
[error] scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
[error] scala.collection.ArrayOps$.foreach$extension(ArrayOps.scala:1328)
[error] dotty.tools.dotc.Run.runPhases$1(Run.scala:236)```

Expectation

I think compiler can show errors, but should never throw exceptions.

@OndrejSpanel OndrejSpanel added the stat:needs triage Every issue needs to have an "area" and "itype" label label Aug 11, 2022
@KacperFKorban KacperFKorban added the stat:needs minimization Needs a self contained minimization label Aug 11, 2022
@szymon-rd szymon-rd added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 16, 2022
@odersky
Copy link
Contributor

odersky commented Aug 20, 2022

Yes, that's clearly a compiler bug.

@odersky
Copy link
Contributor

odersky commented Aug 20, 2022

@OndrejSpanel if you run this on latest main once #15890 is merged you should hopefully get the compilation unit that caused the crash.

@OndrejSpanel
Copy link
Member Author

if you run this on latest main

To clarify how to proceed: does this mean I should test the issue on 3.1.4 once it is released?

@odersky
Copy link
Contributor

odersky commented Aug 20, 2022 via email

@bishabosha
Copy link
Member

@OndrejSpanel can you try with scalaVersion 3.2.1-RC1-bin-20220820-ead0fbc-NIGHTLY? - it should contain more debug info for this crash

@OndrejSpanel
Copy link
Member Author

OndrejSpanel commented Aug 22, 2022

Thanks for pinging me. When I have tried that, I am told which file is the error in, and that the error is while typechecking:

java.lang.NullPointerException: tried to cast away nullability, but value is null while typechecking <some path>\State.scala

When I remove State.scala completely, I get a different error in a different file:

java.lang.AssertionError: NoDenotation.owner while typechecking <some path>\SomeFile.scala

[error] ## Exception when compiling 387 sources to ccccccc\target\scala-3.2.1-RC1-bin-20220820-ead0fbc-NIGHTLY\classes
[error] java.lang.AssertionError: NoDenotation.owner
[error] dotty.tools.dotc.core.SymDenotations$NoDenotation$.owner(SymDenotations.scala:2501)
[error] dotty.tools.dotc.typer.Typer.canAssign$1(Typer.scala:990)
[error] dotty.tools.dotc.typer.Typer.typedAssign(Typer.scala:1027)
[error] dotty.tools.dotc.typer.Typer.typedUnnamed$1(Typer.scala:2922)
[error] dotty.tools.dotc.typer.Typer.typedUnadapted(Typer.scala:2978)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3045)
[error] dotty.tools.dotc.typer.Typer.typed(Typer.scala:3049)
[error] dotty.tools.dotc.typer.ProtoTypes$FunProto.$anonfun$3$$anonfun$1(ProtoTypes.scala:437)
[error] dotty.tools.dotc.typer.ProtoTypes$FunProto.cacheTypedArg(ProtoTypes.scala:406)
[error] dotty.tools.dotc.typer.ProtoTypes$FunProto.$anonfun$3(ProtoTypes.scala:437)
[error] dotty.tools.dotc.typer.ProtoTypes$FunProto.$anonfun$adapted$1(ProtoTypes.scala:437)
[error] dotty.tools.dotc.core.Decorators$ListDecorator$.loop$3(Decorators.scala:174)
[error] dotty.tools.dotc.core.Decorators$ListDecorator$.mapWithIndexConserve$extension(Decorators.scala:181)
[error] dotty.tools.dotc.typer.ProtoTypes$FunProto.typedArgs(ProtoTypes.scala:437)
[error] dotty.tools.dotc.typer.Applications.resolveOverloaded1(Applications.scala:1975)

I will try removing a much as possible to still get the NullPointerException and not AssertionError from State.scala, perhaps that will tell something useful.

@OndrejSpanel
Copy link
Member Author

OndrejSpanel commented Aug 22, 2022

I was able to reduce the repro into two files, each of them containing only a few lines of code. Repro is available in https://github.com/OndrejSpanel/scala3-error-repro

I was not able to reproduce the issue in a single file, or without a nested state package.

This is how the files currently look like:

Config.scala:

package config
package state

case class Config(simulations: Seq[(String, Any)] = Seq.empty)

State.scala:

package config

import state._

object State {

  object Info {
    def apply(): Info = Info(Config(), Seq.empty)
  }
  case class Info(cfg: AnyRef, allTypes: Seq[AnyRef])
}

@odersky
Copy link
Contributor

odersky commented Aug 22, 2022

Thanks for minimization! I tried to reproduce it, but it works for me. Is there something unexpected on your classpath maybe?

@OndrejSpanel
Copy link
Member Author

Is there something unexpected on your classpath maybe?

How can I check for that?

I get the error by just launching sbt compile in the project directory.

I use Windows 11 x64, my default Java version is 11:

welcome to sbt 1.7.1 (Oracle Corporation Java 11.0.15.1)

@OndrejSpanel
Copy link
Member Author

OndrejSpanel commented Aug 22, 2022

It it not Windows specific, it can be reproduced on a GitHub runner using Ubuntu, see https://github.com/OndrejSpanel/scala3-error-repro/runs/7954324298?check_suite_focus=true

I think the GitHub runner environment should be clean and there should be no class-path issues on it.

@odersky
Copy link
Contributor

odersky commented Aug 22, 2022

I could reproduce it now. The two files have to be compiled as State.scala and Config.scala, in that order on the command line. If the order is swapped, it compiles.

I.e.

~/workspace/dotty/tests/pos/i15845> sc state/Config.scala State.scala
~/workspace/dotty/tests/pos/i15845> sc State.scala state/Config.scala
exception occurred while typechecking State.scala
exception occurred while compiling State.scala, state/Config.scala
java.lang.NullPointerException: tried to cast away nullability, but value is null while compiling State.scala, state/Config.scala
Exception in thread "main" java.lang.NullPointerException: tried to cast away nullability, but value is null
	at scala.runtime.Scala3RunTime$.nnFail(Scala3RunTime.scala:28)
	at dotty.tools.dotc.core.OrderingConstraint.swapKey$1(OrderingConstraint.scala:501)

odersky added a commit to dotty-staging/dotty that referenced this issue Aug 22, 2022
Fixes scala#15845.

Curiously this code was never exercised before, despite the many millions of lines of code we compile. It's even more
curious that the faulty behavior could be triggered by such a simple example.
@odersky odersky mentioned this issue Aug 22, 2022
@Kordyjan Kordyjan added this to the 3.2.1 milestone Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:typer itype:crash stat:needs minimization Needs a self contained minimization
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants