Skip to content

Fix #10848: Enable erased given under -Yerased-terms #10856

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

Merged
merged 1 commit into from
Dec 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ object Checking {
if sym.isAllOf(flag1 | flag2) then fail(i"illegal combination of modifiers: `${flag1.flagsString}` and `${flag2.flagsString}` for: $sym")
def checkApplicable(flag: FlagSet, ok: Boolean) =
if (!ok && !sym.is(Synthetic))
fail(i"modifier `${flag.flagsString}` is not allowed for this definition")
fail(ModifierNotAllowedForDefinition(Erased))

if (sym.is(Inline) &&
( sym.is(ParamAccessor) && sym.owner.isClass
Expand Down Expand Up @@ -500,7 +500,7 @@ object Checking {
sym.setFlag(Private) // break the overriding relationship by making sym Private
}
if (sym.is(Erased))
checkApplicable(Erased, !sym.isOneOf(MutableOrLazy))
checkApplicable(Erased, !sym.isOneOf(MutableOrLazy, butNot = Given))
}

/** Check the type signature of the symbol `M` defined by `tree` does not refer
Expand Down
6 changes: 3 additions & 3 deletions docs/docs/reference/metaprogramming/erased-terms.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ title: "Erased Terms"
Let's describe the motivation behind erased terms with an example. In the
following we show a simple state machine which can be in a state `On` or `Off`.
The machine can change state from `Off` to `On` with `turnedOn` only if it is
currently `Off`. This last constraint is captured with the `IsOff[S]` implicit
currently `Off`. This last constraint is captured with the `IsOff[S]` contextual
evidence which only exists for `IsOff[Off]`. For example, not allowing calling
`turnedOn` on in an `On` state as we would require an evidence of type
`IsOff[On]` that will not be found.
Expand All @@ -21,11 +21,11 @@ final class Off extends State
@implicitNotFound("State must be Off")
class IsOff[S <: State]
object IsOff {
implicit def isOff: IsOff[Off] = new IsOff[Off]
given isOff: IsOff[Off] = new IsOff[Off]
}

class Machine[S <: State] {
def turnedOn(implicit ev: IsOff[S]): Machine[On] = new Machine[On]
def turnedOn(using IsOff[S]): Machine[On] = new Machine[On]
}

val m = new Machine[Off]
Expand Down
5 changes: 5 additions & 0 deletions tests/pos-custom-args/erased/i10848a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class IsOn[T]
type On
object IsOn {
erased given IsOn[On] = new IsOn[On]
}
4 changes: 4 additions & 0 deletions tests/pos-custom-args/erased/i10848b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Foo:
erased given Int = 1
def foo(using erased x: Int): Unit = ()
foo
17 changes: 8 additions & 9 deletions tests/pos-custom-args/phantom-Eq.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ object EqUtil {
type PhantomEq[-L, -R]
type PhantomEqEq[T] = PhantomEq[T, T]

implicit class EqualsDeco[T](val x: T) extends AnyVal {
def ===[U] (y: U) (using erased ce: PhantomEq[T, U]) = x.equals(y)
}
extension [T](x: T)
def ===[U](y: U)(using erased PhantomEq[T, U]) = x.equals(y)

implicit erased def eqString: PhantomEqEq[String] = ???
implicit erased def eqInt: PhantomEqEq[Int] = ???
implicit erased def eqDouble: PhantomEqEq[Double] = ???
erased given eqString: PhantomEqEq[String] = ???
erased given eqInt: PhantomEqEq[Int] = ???
erased given eqDouble: PhantomEqEq[Double] = ???

implicit erased def eqByteNum: PhantomEq[Byte, Number] = ???
implicit erased def eqNumByte: PhantomEq[Number, Byte] = ???
erased given eqByteNum: PhantomEq[Byte, Number] = ???
erased given eqNumByte: PhantomEq[Number, Byte] = ???

implicit erased def eqSeq[T, U] (using erased eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = ???
erased given eqSeq[T, U](using erased PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = ???
}
19 changes: 8 additions & 11 deletions tests/pos-custom-args/phantom-Eq2/Phantom-Eq_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ object EqUtil {
final class PhantomEq[-L, -R] private[EqUtil]()
type PhantomEqEq[T] = PhantomEq[T, T]

implicit class EqualsDeco[T](val x: T) extends AnyVal {
def ===[U] (y: U) (using erased ce: PhantomEq[T, U]) = x.equals(y)
}
extension [T](x: T)
def ===[U] (y: U) (using erased PhantomEq[T, U]) = x.equals(y)

implicit erased def eqString: PhantomEqEq[String] = new PhantomEq[String, String]
implicit erased def eqInt: PhantomEqEq[Int] = new PhantomEq[Int, Int]
implicit erased def eqDouble: PhantomEqEq[Double] = new PhantomEq[Double, Double]

implicit erased def eqByteNum: PhantomEq[Byte, Number] = new PhantomEq[Byte, Number]
implicit erased def eqNumByte: PhantomEq[Number, Byte] = new PhantomEq[Number, Byte]

implicit erased def eqSeq[T, U] (using erased eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] =
erased given eqString: PhantomEqEq[String] = new PhantomEq[String, String]
erased given eqInt: PhantomEqEq[Int] = new PhantomEq[Int, Int]
erased given eqDouble: PhantomEqEq[Double] = new PhantomEq[Double, Double]
erased given eqByteNum: PhantomEq[Byte, Number] = new PhantomEq[Byte, Number]
erased given eqNumByte: PhantomEq[Number, Byte] = new PhantomEq[Number, Byte]
erased given eqSeq[T, U] (using erased eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] =
new PhantomEq[Seq[T], Seq[U]]
}
2 changes: 1 addition & 1 deletion tests/pos-custom-args/phantom-Evidence.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@ object WithNormalState {

object Utils {
type =::=[From, To]
implicit erased def tpEquals[A]: A =::= A = ???
erased given tpEquals[A]: A =::= A = ???
}