diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index bdc8e0c77970..53a2c40f22f3 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -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 @@ -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 diff --git a/docs/docs/reference/metaprogramming/erased-terms.md b/docs/docs/reference/metaprogramming/erased-terms.md index 2824630b677a..e8aa38a5b508 100644 --- a/docs/docs/reference/metaprogramming/erased-terms.md +++ b/docs/docs/reference/metaprogramming/erased-terms.md @@ -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. @@ -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] diff --git a/tests/pos-custom-args/erased/i10848a.scala b/tests/pos-custom-args/erased/i10848a.scala new file mode 100644 index 000000000000..296937e5e3c2 --- /dev/null +++ b/tests/pos-custom-args/erased/i10848a.scala @@ -0,0 +1,5 @@ +class IsOn[T] +type On +object IsOn { + erased given IsOn[On] = new IsOn[On] +} diff --git a/tests/pos-custom-args/erased/i10848b.scala b/tests/pos-custom-args/erased/i10848b.scala new file mode 100644 index 000000000000..71292b1b859c --- /dev/null +++ b/tests/pos-custom-args/erased/i10848b.scala @@ -0,0 +1,4 @@ +class Foo: + erased given Int = 1 + def foo(using erased x: Int): Unit = () + foo diff --git a/tests/pos-custom-args/phantom-Eq.scala b/tests/pos-custom-args/phantom-Eq.scala index d3c94503bbd1..beb6397d0ad7 100644 --- a/tests/pos-custom-args/phantom-Eq.scala +++ b/tests/pos-custom-args/phantom-Eq.scala @@ -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]] = ??? } diff --git a/tests/pos-custom-args/phantom-Eq2/Phantom-Eq_1.scala b/tests/pos-custom-args/phantom-Eq2/Phantom-Eq_1.scala index 9c12aefebece..cb4f8aa6821f 100644 --- a/tests/pos-custom-args/phantom-Eq2/Phantom-Eq_1.scala +++ b/tests/pos-custom-args/phantom-Eq2/Phantom-Eq_1.scala @@ -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]] } diff --git a/tests/pos-custom-args/phantom-Evidence.scala b/tests/pos-custom-args/phantom-Evidence.scala index 6ad6f927591b..4c416a7aa237 100644 --- a/tests/pos-custom-args/phantom-Evidence.scala +++ b/tests/pos-custom-args/phantom-Evidence.scala @@ -24,5 +24,5 @@ object WithNormalState { object Utils { type =::=[From, To] - implicit erased def tpEquals[A]: A =::= A = ??? + erased given tpEquals[A]: A =::= A = ??? }