Skip to content

Commit 0cf25db

Browse files
authored
add child to parent in completion context (#21214)
This ensures that the positions of forced Child annotations always happens in the source file of the parent class, and not in another file that forced the completion to happen fixes #21154
2 parents 46ff151 + a2bec5e commit 0cf25db

File tree

4 files changed

+1361
-4
lines changed

4 files changed

+1361
-4
lines changed

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

+6-4
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ class Namer { typer: Typer =>
848848
else
849849
try
850850
completeInCreationContext(denot)
851-
if (denot.isCompleted) registerIfChild(denot)
851+
if (denot.isCompleted) registerIfChildInCreationContext(denot)
852852
catch
853853
case ex: CompilationUnit.SuspendException =>
854854
val completer = SuspendCompleter()
@@ -937,10 +937,12 @@ class Namer { typer: Typer =>
937937
denot.markAbsent()
938938
end invalidateIfClashingSynthetic
939939

940-
/** If completed symbol is an enum value or a named class, register it as a child
940+
/** Intentionally left without `using Context` parameter.
941+
* This action should be performed in the context of where the completer was created.
942+
* If completed symbol is an enum value or a named class, register it as a child
941943
* in all direct parent classes which are sealed.
942944
*/
943-
def registerIfChild(denot: SymDenotation)(using Context): Unit = {
945+
def registerIfChildInCreationContext(denot: SymDenotation): Unit = {
944946
val sym = denot.symbol
945947

946948
def register(child: Symbol, parentCls: ClassSymbol) = {
@@ -964,7 +966,7 @@ class Namer { typer: Typer =>
964966
end if
965967
}
966968

967-
/** Intentionally left without `implicit ctx` parameter. We need
969+
/** Intentionally left without `using Context` parameter. We need
968970
* to pick up the context at the point where the completer was created.
969971
*/
970972
def completeInCreationContext(denot: SymDenotation): Unit = {

tests/pos/i21154/A.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Z.*
2+
3+
object A:
4+
val a: Option[AOptions] = ???
5+
val b: Option[BOptions] = ???
6+
val c: Option[COptions] = ???

tests/pos/i21154/Z.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//> using options -Ytest-pickler-check
2+
3+
// in the original issue https://github.com/scala/scala3/issues/21154, the non-deterministic tasty
4+
// depends on the order of compilation of files, the use-site (A.scala) has to come first,
5+
// and the file defining the enum has to come second (Z.scala), A.scala in namer will force Z to complete.
6+
enum Z:
7+
case AOptions()
8+
case BOptions()
9+
case COptions()

0 commit comments

Comments
 (0)