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

inline secondary constructor is allowed and crashes when calling it #12986

Closed
prolativ opened this issue Jun 30, 2021 · 1 comment · Fixed by #13370
Closed

inline secondary constructor is allowed and crashes when calling it #12986

prolativ opened this issue Jun 30, 2021 · 1 comment · Fixed by #13370
Assignees
Labels
Milestone

Comments

@prolativ
Copy link
Contributor

Compiler version

3.0.1-RC2

Minimized code 1

class Foo inline (s: String)

Output 1

1 |class Foo inline (s: String)
  |          ^^^^^^
  |          end of statement expected but identifier found

Minimized code 2

class Bar(i: Int):
  inline def this() = this(0)

val bar = Bar()

Output 2 (click arrow to expand)

Error while emitting rs$line$1
Exception in thread "main" java.lang.RuntimeException: Unexpected New(...Bar/TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class repl$)),object rs$line$1),class rs$line$1$Bar)]) reached GenBCode.
  Call was genLoad(New(TypeTree[TypeRef(TermRef(ThisType(TypeRef(NoPrefix,module class repl$)),object rs$line$1),class rs$line$1$Bar)]),Lrepl$/rs$line$1$Bar;)
	at dotty.tools.backend.jvm.BCodeHelpers.abort(BCodeHelpers.scala:937)
	at dotty.tools.backend.jvm.BCodeHelpers.abort$(BCodeHelpers.scala:41)
	at dotty.tools.backend.jvm.GenBCodePipeline.abort(GenBCode.scala:81)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:319)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:290)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genStat(BCodeBodyBuilder.scala:96)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genBlock$$anonfun$1(BCodeBodyBuilder.scala:895)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genBlock(BCodeBodyBuilder.scala:895)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:417)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genStat(BCodeBodyBuilder.scala:82)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genBlock$$anonfun$1(BCodeBodyBuilder.scala:895)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genBlock(BCodeBodyBuilder.scala:895)
	at dotty.tools.backend.jvm.BCodeBodyBuilder$PlainBodyBuilder.genLoad(BCodeBodyBuilder.scala:417)
	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.emitNormalMethodBody$1(BCodeSkelBuilder.scala:765)
	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genDefDef(BCodeSkelBuilder.scala:800)
	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:608)
	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen$$anonfun$1(BCodeSkelBuilder.scala:614)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
	at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
	at scala.collection.immutable.List.foreach(List.scala:333)
	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.gen(BCodeSkelBuilder.scala:614)
	at dotty.tools.backend.jvm.BCodeSkelBuilder$PlainSkelBuilder.genPlainClass(BCodeSkelBuilder.scala:229)
	at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.visit(GenBCode.scala:229)
	at dotty.tools.backend.jvm.GenBCodePipeline$Worker1.run(GenBCode.scala:194)
	at dotty.tools.backend.jvm.GenBCodePipeline.buildAndSendToDisk(GenBCode.scala:529)
	at dotty.tools.backend.jvm.GenBCodePipeline.run(GenBCode.scala:495)
	at dotty.tools.backend.jvm.GenBCode.run(GenBCode.scala:60)
	at dotty.tools.dotc.core.Phases$Phase.runOn$$anonfun$1(Phases.scala:303)
	at scala.collection.immutable.List.map(List.scala:246)
	at dotty.tools.dotc.core.Phases$Phase.runOn(Phases.scala:304)
	at dotty.tools.backend.jvm.GenBCode.runOn(GenBCode.scala:64)
	at dotty.tools.dotc.Run.runPhases$4$$anonfun$4(Run.scala:205)
	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$5(Run.scala:216)
	at dotty.tools.dotc.Run.compileUnits$$anonfun$1(Run.scala:224)
	at scala.runtime.java8.JFunction0$mcV$sp.apply(JFunction0$mcV$sp.scala:18)
	at dotty.tools.dotc.util.Stats$.maybeMonitored(Stats.scala:67)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:231)
	at dotty.tools.dotc.Run.compileUnits(Run.scala:172)
	at dotty.tools.repl.ReplCompiler.runCompilationUnit(ReplCompiler.scala:152)
	at dotty.tools.repl.ReplCompiler.compile(ReplCompiler.scala:162)
	at dotty.tools.repl.ReplDriver.compile(ReplDriver.scala:249)
	at dotty.tools.repl.ReplDriver.interpret(ReplDriver.scala:212)
	at dotty.tools.repl.ReplDriver.loop$1(ReplDriver.scala:146)
	at dotty.tools.repl.ReplDriver.runUntilQuit$$anonfun$1(ReplDriver.scala:149)
	at dotty.tools.repl.ReplDriver.withRedirectedOutput(ReplDriver.scala:168)
	at dotty.tools.repl.ReplDriver.runUntilQuit(ReplDriver.scala:149)
	at dotty.tools.repl.ReplDriver.tryRunning(ReplDriver.scala:115)
	at dotty.tools.repl.Main$.main(Main.scala:6)
	at dotty.tools.repl.Main.main(Main.scala)

Expectations

Not sure if this should be reported as 2 separate issues because I don't know if there are any implementational limitations here by design or not.
First of all it seems syntactically impossible to define an inline primary constructor for a class (if it was I would assume this syntax to work per analogy to private constructors). It is however possible to define an inline secondary constructor but an attempt to invoke it crashes the compiler.
So basically I can see two options here:

  1. If inline constructors are for some reason problematic (and we don't want to support them yet or ever) then disallow defining secondary inline constructors and improve the error message for primary ones
  2. Otherwise make it possible to define inline primary constructors and prevent inline constructors in general from crashing the compiler

@odersky @nicolasstucki

@bishabosha bishabosha changed the title Inline constructors not working inline secondary constructor is allowed and crashes when calling it Jun 30, 2021
@odersky
Copy link
Contributor

odersky commented Jun 30, 2021

Definitely disallow inline secondary constructors.

@nicolasstucki nicolasstucki added the Spree Suitable for a future Spree label Jul 27, 2021
@Kordyjan Kordyjan added this to the 3.1.0 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants