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

Compiler throws NullPointerException when inferring type #9291

Closed
scabug opened this issue Apr 30, 2015 · 3 comments
Closed

Compiler throws NullPointerException when inferring type #9291

scabug opened this issue Apr 30, 2015 · 3 comments

Comments

@scabug
Copy link

scabug commented Apr 30, 2015

The following test case causes the compiler to throw a NullPointerException:

// more than one field is required to trigger crash
// there must be a default value for one of the parameters
case class OuterObject(field: Int = 1, anotherField: Int = 2)
	
object Test {
	OuterObject().copy(field = OuterObject().field)

	// declaring something without explicit type, with the same name as OuterObject.field
	def field = "anything"
}

Some things to note:

  • the name of the def Test.field is important, if it is not the same as the parameter name in OuterObject, the crash does not occur.
  • if OuterObject.field is the only parameter, the crash does not occur (any number of additional parameter are needed)
  • using the default values is important, i.e. changing the test case to have both parameters specified within the copy call avoids the bug: {code}
    OuterObject().copy(field = OuterObject(1, 2).field) {code}

The exception does not occur on 2.10.4 (we are upgrading from that version). Since this bug seems so bizarrely specific, a workaround should be available by changing pretty much anything about the code, e.g. explicitly defining type information.

Compiling with -Ydebug gives the following additional output:

warning: !!! exception when typing OuterObject().copy(field = OuterObject().field), pt=?
warning: !!! exception when typing <module> object Test extends scala.AnyRef {
  <method> def <init>(): <empty>.this.Test.type = {
    super.<init>();
    ()
  };
  OuterObject().copy(field = OuterObject().field);
  <method> <triedcooking> def field: lang.this.String = "anything"
}, pt=?
warning: !!! exception when typing package <empty> {
  case class OuterObject extends scala.Product with scala.Serializable {
    <caseaccessor> <paramaccessor> <triedcooking> private[this] val field : scala.this.Int = _;
    <caseaccessor> <paramaccessor> <triedcooking> private[this] val anotherField : scala.this.Int = _;
    <method> <triedcooking> def <init>(field: Int = 1, anotherField: Int = 2): <empty>.this.OuterObject = {
      super.<init>();
      ()
    }
  };
  <module> object Test extends scala.AnyRef {
    <method> def <init>(): <empty>.this.Test.type = {
      super.<init>();
      ()
    };
    OuterObject().copy(field = OuterObject().field);
    <method> <triedcooking> def field: lang.this.String = "anything"
  }
}, pt=?

Partial stack trace:

error: java.lang.NullPointerException
	at scala.tools.nsc.typechecker.Typers$Typer.noExpectedType$1(Typers.scala:3378)
	at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$handleMonomorphicCall$1(Typers.scala:3381)
	at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3409)
	at scala.tools.nsc.typechecker.Typers$Typer.tryNamesDefaults$1(Typers.scala:3295)
	at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3367)
	at scala.tools.nsc.typechecker.Typers$Typer.tryNamesDefaults$1(Typers.scala:3353)
	at scala.tools.nsc.typechecker.Typers$Typer.doTypedApply(Typers.scala:3367)
	at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$tryTypedApply$1$1.apply(Typers.scala:4404)
	at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$tryTypedApply$1$1.apply(Typers.scala:4404)
	at scala.tools.nsc.typechecker.Typers$Typer.silent(Typers.scala:676)
	at scala.tools.nsc.typechecker.Typers$Typer.tryTypedApply$1(Typers.scala:4404)
	at scala.tools.nsc.typechecker.Typers$Typer.normalTypedApply$1(Typers.scala:4449)
	at scala.tools.nsc.typechecker.Typers$Typer.typedApply$1(Typers.scala:4484)
	at scala.tools.nsc.typechecker.Typers$Typer.typedInAnyMode$1(Typers.scala:5242)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5259)
	at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5295)
	at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typers.scala:5322)
	at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5269)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5273)
	at scala.tools.nsc.typechecker.Typers$Typer.typedByValueExpr(Typers.scala:5351)
	at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedStat$1(Typers.scala:2977)
	at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$60.apply(Typers.scala:3081)
	at scala.tools.nsc.typechecker.Typers$Typer$$anonfun$60.apply(Typers.scala:3081)
	at scala.collection.immutable.List.loop$1(List.scala:172)
	at scala.collection.immutable.List.mapConserve(List.scala:188)
	at scala.tools.nsc.typechecker.Typers$Typer.typedStats(Typers.scala:3081)
	at scala.tools.nsc.typechecker.Typers$Typer.typedTemplate(Typers.scala:1880)
	at scala.tools.nsc.typechecker.Typers$Typer.typedModuleDef(Typers.scala:1767)
	at scala.tools.nsc.typechecker.Typers$Typer.typedMemberDef$1(Typers.scala:5209)
	at scala.tools.nsc.typechecker.Typers$Typer.typed1(Typers.scala:5258)
	at scala.tools.nsc.typechecker.Typers$Typer.runTyper$1(Typers.scala:5295)
	at scala.tools.nsc.typechecker.Typers$Typer.scala$tools$nsc$typechecker$Typers$Typer$$typedInternal(Typers.scala:5322)
	at scala.tools.nsc.typechecker.Typers$Typer.body$2(Typers.scala:5269)
	at scala.tools.nsc.typechecker.Typers$Typer.typed(Typers.scala:5273)
	at scala.tools.nsc.typechecker.Typers$Typer.typedByValueExpr(Typers.scala:5351)
	at ...
Exception in thread "main" java.lang.NullPointerException
	at scala.tools.nsc.typechecker.Typers$Typer.noExpectedType$1(Typers.scala:3378)
	at ...
@scabug
Copy link
Author

scabug commented Apr 30, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9291?orig=1
Reporter: Frej Bjon (fbjon)
Affected Versions: 2.11.6, 2.11.7
See #5091, #8342

@scabug
Copy link
Author

scabug commented Oct 9, 2015

Frej Bjon (fbjon) said:
Bumped into this again in a different piece of code, but in narrowing down the issue, it changed from a crash to an erroneous "recursive type needs value" exception. It looks like this is related to #5091 and so might be related to #8342.

@hrhino
Copy link
Member

hrhino commented Jan 16, 2018

This is fixed in 2.13.0-M3 by (I presume) scala/scala#6092.

@hrhino hrhino modified the milestones: Backlog, 2.13.0-M3 Jan 16, 2018
hrhino added a commit to hrhino/scala that referenced this issue Jan 16, 2018
- scala/bug#5638 was fixed by something farther back than I can build.
- scala/bug#8348 was fixed by scala#5251.
- scala/bug#9291 was fixed by scala#6092.
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