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

Cannot create an instance of the case class with some encoded field names #10825

Closed
plokhotnyuk opened this issue Apr 12, 2018 · 10 comments
Closed

Comments

@plokhotnyuk
Copy link

plokhotnyuk commented Apr 12, 2018

It looks like a bug in scalac and affects case classes which have 2 fields where name of one is a prefix for the another name that contains a character that should be encoded immediately after the prefix (like o and o-o).

For 2.11.12 I got a compilation error:

[error] scala.reflect.internal.Types$TypeError: type mismatch;
[error]  found   : Int
[error]  required: Seq
[error] 	at scala.tools.nsc.typechecker.Contexts$ThrowingReporter.handleError(Contexts.scala:1402)
[error] 	at scala.tools.nsc.typechecker.Contexts$ContextReporter.issue(Contexts.scala:1254)
[error] 	at scala.tools.nsc.typechecker.Contexts$Context.issue(Contexts.scala:573)
[error] 	at scala.tools.nsc.typechecker.ContextErrors$ErrorUtils$.issueTypeError(ContextErrors.scala:106)

While in 2.12 it fails in the runtime during a call of the constructor:

java.lang.Integer cannot be cast to scala.collection.Seq
java.lang.ClassCastException: java.lang.Integer cannot be cast to scala.collection.Seq
	at Publication.<init>(KebabFieldNameSpec.scala:3)
	at KebabFieldNameSpec.$anonfun$new$2(KebabFieldNameSpec.scala:8)
	at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
	at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
	at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
	at org.scalatest.Transformer.apply(Transformer.scala:22)

Code to reproduce:

import org.scalatest.{Matchers, WordSpec}

case class Publication(title: String, author: String, `reference-count`: Int, reference: Seq[String])

class KebabFieldNameSpec extends WordSpec with Matchers {
  "Publication" should {
    "be created by the constructor" in {
      new Publication("", "", 0, Nil)
    }
  }
}
@plokhotnyuk plokhotnyuk changed the title Cannot create instance of the case class with some encoded field names Cannot create an instance of the case class with some encoded field names Apr 12, 2018
@plokhotnyuk
Copy link
Author

plokhotnyuk commented Apr 12, 2018

W/a for this scalac bug is to move a definition of the field with encoded chars (`reference-count` in our case) to be after the field that is affected by the exception (after the reference field):

case class Publication(title: String, author: String, reference: Seq[String], `reference-count`: Int)

@hrhino
Copy link

hrhino commented Apr 12, 2018

    // The constructor parameter with given name. This means the parameter
    // has given name, or starts with given name, and continues with a `$` afterwards.
    def parameterNamed(name: Name): Symbol = {
      def matchesName(param: Symbol) = param.name == name || param.name.startsWith(name + nme.NAME_JOIN_STRING) // nme.NAME_JOIN_STRING = "$"

      primaryConstrParams filter matchesName match {
        case Nil    => abort(name + " not in " + primaryConstrParams)
        case p :: _ => p
      }

More fallout from using $ for everything name-y. (#2806) This may hail from '06: scala/scala@cb7711d.

@som-snytt
Copy link

Probably the same problem as #10625 where Constructors looks at prefixes.

@som-snytt
Copy link

Another symptom #8831

@som-snytt
Copy link

@hrhino should this and 10625 also be closed in favor of 8831 as an umbrella issue? Asking in case you understand any nuances. I haven't re-read the issues.

@hrhino
Copy link

hrhino commented Oct 18, 2018

I'm not a very nuanced person. Re-rereading the issues, this is almost certainly a duplicate of #8831, and has the same root cause as #10625 (IIRC, if I remember constructors) but I don't feel confident saying, externally, that it's a duplicate. Not sure what the criteria for that are.

@hrhino
Copy link

hrhino commented Oct 18, 2018

Also, what happened to the touted GitHub "duplicate of" feature from a few months ago? Did it get rebased away?

@som-snytt
Copy link

I'll take a @hrhino "almost certainly" and "don't feel confident" any day. I think the issue that says "bizarre" always takes priority.

On github, it's easy to navigate between linked issues, so I won't try to copy/paste to #8831.

@dwijnand
Copy link
Member

Duplicate of #8831

@dwijnand dwijnand marked this as a duplicate of #8831 May 13, 2019
@SethTisue SethTisue removed this from the Backlog milestone May 13, 2019
@hrhino
Copy link

hrhino commented Oct 26, 2019

Woah, I just noticed that when @dwijnand says "duplicate of #...", github listens!

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

5 participants