Skip to content

Commit 6a27db2

Browse files
Merge pull request #10856 from dotty-staging/fix-#10848
Fix #10848: Enable `erased given` under `-Yerased-terms`
2 parents 0e4fe3c + b29ca76 commit 6a27db2

File tree

7 files changed

+31
-26
lines changed

7 files changed

+31
-26
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ object Checking {
434434
if sym.isAllOf(flag1 | flag2) then fail(i"illegal combination of modifiers: `${flag1.flagsString}` and `${flag2.flagsString}` for: $sym")
435435
def checkApplicable(flag: FlagSet, ok: Boolean) =
436436
if (!ok && !sym.is(Synthetic))
437-
fail(i"modifier `${flag.flagsString}` is not allowed for this definition")
437+
fail(ModifierNotAllowedForDefinition(Erased))
438438

439439
if (sym.is(Inline) &&
440440
( sym.is(ParamAccessor) && sym.owner.isClass
@@ -500,7 +500,7 @@ object Checking {
500500
sym.setFlag(Private) // break the overriding relationship by making sym Private
501501
}
502502
if (sym.is(Erased))
503-
checkApplicable(Erased, !sym.isOneOf(MutableOrLazy))
503+
checkApplicable(Erased, !sym.isOneOf(MutableOrLazy, butNot = Given))
504504
}
505505

506506
/** Check the type signature of the symbol `M` defined by `tree` does not refer

docs/docs/reference/metaprogramming/erased-terms.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ title: "Erased Terms"
88
Let's describe the motivation behind erased terms with an example. In the
99
following we show a simple state machine which can be in a state `On` or `Off`.
1010
The machine can change state from `Off` to `On` with `turnedOn` only if it is
11-
currently `Off`. This last constraint is captured with the `IsOff[S]` implicit
11+
currently `Off`. This last constraint is captured with the `IsOff[S]` contextual
1212
evidence which only exists for `IsOff[Off]`. For example, not allowing calling
1313
`turnedOn` on in an `On` state as we would require an evidence of type
1414
`IsOff[On]` that will not be found.
@@ -21,11 +21,11 @@ final class Off extends State
2121
@implicitNotFound("State must be Off")
2222
class IsOff[S <: State]
2323
object IsOff {
24-
implicit def isOff: IsOff[Off] = new IsOff[Off]
24+
given isOff: IsOff[Off] = new IsOff[Off]
2525
}
2626

2727
class Machine[S <: State] {
28-
def turnedOn(implicit ev: IsOff[S]): Machine[On] = new Machine[On]
28+
def turnedOn(using IsOff[S]): Machine[On] = new Machine[On]
2929
}
3030

3131
val m = new Machine[Off]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class IsOn[T]
2+
type On
3+
object IsOn {
4+
erased given IsOn[On] = new IsOn[On]
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Foo:
2+
erased given Int = 1
3+
def foo(using erased x: Int): Unit = ()
4+
foo

tests/pos-custom-args/phantom-Eq.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@ object EqUtil {
1717
type PhantomEq[-L, -R]
1818
type PhantomEqEq[T] = PhantomEq[T, T]
1919

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

24-
implicit erased def eqString: PhantomEqEq[String] = ???
25-
implicit erased def eqInt: PhantomEqEq[Int] = ???
26-
implicit erased def eqDouble: PhantomEqEq[Double] = ???
23+
erased given eqString: PhantomEqEq[String] = ???
24+
erased given eqInt: PhantomEqEq[Int] = ???
25+
erased given eqDouble: PhantomEqEq[Double] = ???
2726

28-
implicit erased def eqByteNum: PhantomEq[Byte, Number] = ???
29-
implicit erased def eqNumByte: PhantomEq[Number, Byte] = ???
27+
erased given eqByteNum: PhantomEq[Byte, Number] = ???
28+
erased given eqNumByte: PhantomEq[Number, Byte] = ???
3029

31-
implicit erased def eqSeq[T, U] (using erased eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = ???
30+
erased given eqSeq[T, U](using erased PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = ???
3231
}

tests/pos-custom-args/phantom-Eq2/Phantom-Eq_1.scala

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ object EqUtil {
55
final class PhantomEq[-L, -R] private[EqUtil]()
66
type PhantomEqEq[T] = PhantomEq[T, T]
77

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

12-
implicit erased def eqString: PhantomEqEq[String] = new PhantomEq[String, String]
13-
implicit erased def eqInt: PhantomEqEq[Int] = new PhantomEq[Int, Int]
14-
implicit erased def eqDouble: PhantomEqEq[Double] = new PhantomEq[Double, Double]
15-
16-
implicit erased def eqByteNum: PhantomEq[Byte, Number] = new PhantomEq[Byte, Number]
17-
implicit erased def eqNumByte: PhantomEq[Number, Byte] = new PhantomEq[Number, Byte]
18-
19-
implicit erased def eqSeq[T, U] (using erased eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] =
11+
erased given eqString: PhantomEqEq[String] = new PhantomEq[String, String]
12+
erased given eqInt: PhantomEqEq[Int] = new PhantomEq[Int, Int]
13+
erased given eqDouble: PhantomEqEq[Double] = new PhantomEq[Double, Double]
14+
erased given eqByteNum: PhantomEq[Byte, Number] = new PhantomEq[Byte, Number]
15+
erased given eqNumByte: PhantomEq[Number, Byte] = new PhantomEq[Number, Byte]
16+
erased given eqSeq[T, U] (using erased eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] =
2017
new PhantomEq[Seq[T], Seq[U]]
2118
}

tests/pos-custom-args/phantom-Evidence.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ object WithNormalState {
2424

2525
object Utils {
2626
type =::=[From, To]
27-
implicit erased def tpEquals[A]: A =::= A = ???
27+
erased given tpEquals[A]: A =::= A = ???
2828
}

0 commit comments

Comments
 (0)