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

Prevent unsafe type ascription #31

Open
carymrobbins opened this issue Jun 21, 2018 · 0 comments
Open

Prevent unsafe type ascription #31

carymrobbins opened this issue Jun 21, 2018 · 0 comments

Comments

@carymrobbins
Copy link
Member

carymrobbins commented Jun 21, 2018

The current encoding allows for some unsafe type ascriptions. While this would basically never happen in a real world scenario, we should still look into improving the encoding.

Here's an example of the problem. I've abridged the debug output for simplicity.

scala> @newtype(debug = true) case class Foo(x: String)
Expanded @newtype Foo:
{
  type Foo = Foo.Type;
  abstract trait Foo$Types extends scala.AnyRef {
    type Repr = String;
    type Base = _root_.scala.Any { type Foo$newtype };
    abstract trait Tag extends _root_.scala.Any;
    type Type <: Base with Tag
  };
  object Foo extends scala.AnyRef with Foo$Types { .. }
}
defined type alias Foo
defined trait Foo$Types
defined object Foo

scala> Foo("a"): Foo.Tag
java.lang.ClassCastException: java.lang.String cannot be cast to Foo$Types$Tag

The main reason for the Tag trait is to anchor the companion so it's in the implicit search path. I've been experimenting with refinements to see if that could help but so far I've been unable to get implicit resolution working without it.

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

No branches or pull requests

1 participant