Skip to content

Prevent unsafe type ascription #31

Open
@carymrobbins

Description

@carymrobbins

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions