Skip to content

Commit c04e247

Browse files
authored
Avoid forcing ctors & parents which caused cycles (#17086)
2 parents ba2c731 + 987235e commit c04e247

21 files changed

+248
-74
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+18-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,24 @@ object desugar {
9393
override def ensureCompletions(using Context): Unit = {
9494
def completeConstructor(sym: Symbol) =
9595
sym.infoOrCompleter match {
96-
case completer: Namer#ClassCompleter =>
96+
case completer: Namer#ClassCompleter if !sym.isCompleting =>
97+
// An example, derived from tests/run/t6385.scala
98+
//
99+
// class Test():
100+
// def t1: Foo = Foo(1)
101+
// final case class Foo(value: Int)
102+
//
103+
// Here's the sequence of events:
104+
// * The symbol for Foo.apply is forced to complete
105+
// * The symbol for the `value` parameter of the apply method is forced to complete
106+
// * Completing that value parameter requires typing its type, which is a DerivedTypeTrees,
107+
// which only types if it has an OriginalSymbol.
108+
// * So if the case class hasn't been completed, we need (at least) its constructor to be completed
109+
//
110+
// Test tests/neg/i9294.scala is an example of why isCompleting is necessary.
111+
// Annotations are added while completing the constructor,
112+
// so the back reference to foo reaches here which re-initiates the constructor completion.
113+
// So we just skip, as completion is already being triggered.
97114
completer.completeConstructor(sym)
98115
case _ =>
99116
}

compiler/src/dotty/tools/dotc/core/NamerOps.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ object NamerOps:
272272
* where
273273
*
274274
* <context-bound-companion> is the CBCompanion type created in Definitions
275-
* withnessRefK is a refence to the K'th witness.
275+
* withnessRefK is a reference to the K'th witness.
276276
*
277277
* The companion has the same access flags as the original type.
278278
*/

compiler/src/dotty/tools/dotc/core/TypeApplications.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ class TypeApplications(val self: Type) extends AnyVal {
267267
*/
268268
def hkResult(using Context): Type = self.dealias match {
269269
case self: TypeRef =>
270-
if (self.symbol == defn.AnyKindClass) self else self.info.hkResult
270+
if self.symbol == defn.AnyKindClass then self
271+
else if self.symbol.isClass then NoType // avoid forcing symbol if it's a class, not an alias to a HK type lambda
272+
else self.info.hkResult
271273
case self: AppliedType =>
272274
if (self.tycon.typeSymbol.isClass) NoType else self.superType.hkResult
273275
case self: HKTypeLambda => self.resultType

compiler/src/dotty/tools/dotc/core/Types.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,9 @@ object Types extends TypeUtils {
199199
*/
200200
def isRef(sym: Symbol, skipRefined: Boolean = true)(using Context): Boolean = this match {
201201
case this1: TypeRef =>
202-
this1.info match { // see comment in Namer#TypeDefCompleter#typeSig
202+
// avoid forcing symbol if it's a class, not a type alias (see i15177.FakeEnum.scala)
203+
if this1.symbol.isClass then this1.symbol eq sym
204+
else this1.info match { // see comment in Namer#TypeDefCompleter#typeSig
203205
case TypeAlias(tp) => tp.isRef(sym, skipRefined)
204206
case _ => this1.symbol eq sym
205207
}

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

+106-64
Original file line numberDiff line numberDiff line change
@@ -822,8 +822,11 @@ class Namer { typer: Typer =>
822822
if (sym.is(Module)) moduleValSig(sym)
823823
else valOrDefDefSig(original, sym, Nil, identity)(using localContext(sym).setNewScope)
824824
case original: DefDef =>
825-
val typer1 = ctx.typer.newLikeThis(ctx.nestingLevel + 1)
826-
nestedTyper(sym) = typer1
825+
// For the primary constructor DefDef, it is:
826+
// * indexed as a part of completing the class, with indexConstructor; and
827+
// * typed ahead when completing the constructor
828+
// So we need to make sure to reuse the same local/nested typer.
829+
val typer1 = nestedTyper.getOrElseUpdate(sym, ctx.typer.newLikeThis(ctx.nestingLevel + 1))
827830
typer1.defDefSig(original, sym, this)(using localContext(sym).setTyper(typer1))
828831
case imp: Import =>
829832
try
@@ -833,6 +836,12 @@ class Namer { typer: Typer =>
833836
typr.println(s"error while completing ${imp.expr}")
834837
throw ex
835838

839+
/** Context setup for indexing the constructor. */
840+
def indexConstructor(constr: DefDef, sym: Symbol): Unit =
841+
val typer1 = ctx.typer.newLikeThis(ctx.nestingLevel + 1)
842+
nestedTyper(sym) = typer1
843+
typer1.indexConstructor(constr, sym)(using localContext(sym).setTyper(typer1))
844+
836845
final override def complete(denot: SymDenotation)(using Context): Unit = {
837846
if (Config.showCompletions && ctx.typerState != creationContext.typerState) {
838847
def levels(c: Context): Int =
@@ -993,15 +1002,19 @@ class Namer { typer: Typer =>
9931002

9941003
/** If completion of the owner of the to be completed symbol has not yet started,
9951004
* complete the owner first and check again. This prevents cyclic references
996-
* where we need to copmplete a type parameter that has an owner that is not
1005+
* where we need to complete a type parameter that has an owner that is not
9971006
* yet completed. Test case is pos/i10967.scala.
9981007
*/
9991008
override def needsCompletion(symd: SymDenotation)(using Context): Boolean =
10001009
val owner = symd.owner
10011010
!owner.exists
10021011
|| owner.is(Touched)
10031012
|| {
1004-
owner.ensureCompleted()
1013+
// Only complete the owner if it's a type (eg. the class that owns a type parameter)
1014+
// This avoids completing primary constructor methods while completing the type of one of its type parameters
1015+
// See i15177.scala.
1016+
if owner.isType then
1017+
owner.ensureCompleted()
10051018
!symd.isCompleted
10061019
}
10071020

@@ -1526,12 +1539,9 @@ class Namer { typer: Typer =>
15261539
index(constr)
15271540
index(rest)(using localCtx)
15281541

1529-
symbolOfTree(constr).info.stripPoly match // Completes constr symbol as a side effect
1530-
case mt: MethodType if cls.is(Case) && mt.isParamDependent =>
1531-
// See issue #8073 for background
1532-
report.error(
1533-
em"""Implementation restriction: case classes cannot have dependencies between parameters""",
1534-
cls.srcPos)
1542+
val constrSym = symbolOfTree(constr)
1543+
constrSym.infoOrCompleter match
1544+
case completer: Completer => completer.indexConstructor(constr, constrSym)
15351545
case _ =>
15361546

15371547
tempInfo = denot.asClass.classInfo.integrateOpaqueMembers.asInstanceOf[TempClassInfo]
@@ -1762,6 +1772,17 @@ class Namer { typer: Typer =>
17621772
val sym = tree.symbol
17631773
if sym.isConstructor then sym.owner else sym
17641774

1775+
/** Index the primary constructor of a class, as a part of completing that class.
1776+
* This allows the rest of the constructor completion to be deferred,
1777+
* which avoids non-cyclic classes failing, e.g. pos/i15177.
1778+
*/
1779+
def indexConstructor(constr: DefDef, sym: Symbol)(using Context): Unit =
1780+
index(constr.leadingTypeParams)
1781+
sym.owner.typeParams.foreach(_.ensureCompleted())
1782+
completeTrailingParamss(constr, sym, indexingCtor = true)
1783+
if Feature.enabled(modularity) then
1784+
constr.termParamss.foreach(_.foreach(setTracked))
1785+
17651786
/** The signature of a module valdef.
17661787
* This will compute the corresponding module class TypeRef immediately
17671788
* without going through the defined type of the ValDef. This is necessary
@@ -1860,31 +1881,6 @@ class Namer { typer: Typer =>
18601881
// Beware: ddef.name need not match sym.name if sym was freshened!
18611882
val isConstructor = sym.name == nme.CONSTRUCTOR
18621883

1863-
// A map from context-bounded type parameters to associated evidence parameter names
1864-
val witnessNamesOfParam = mutable.Map[TypeDef, List[TermName]]()
1865-
if !ddef.name.is(DefaultGetterName) && !sym.is(Synthetic) then
1866-
for params <- ddef.paramss; case tdef: TypeDef <- params do
1867-
for case WitnessNamesAnnot(ws) <- tdef.mods.annotations do
1868-
witnessNamesOfParam(tdef) = ws
1869-
1870-
/** Is each name in `wnames` defined somewhere in the longest prefix of all `params`
1871-
* that have been typed ahead (i.e. that carry the TypedAhead attachment)?
1872-
*/
1873-
def allParamsSeen(wnames: List[TermName], params: List[MemberDef]) =
1874-
(wnames.toSet[Name] -- params.takeWhile(_.hasAttachment(TypedAhead)).map(_.name)).isEmpty
1875-
1876-
/** Enter and typecheck parameter list.
1877-
* Once all witness parameters for a context bound are seen, create a
1878-
* context bound companion for it.
1879-
*/
1880-
def completeParams(params: List[MemberDef])(using Context): Unit =
1881-
index(params)
1882-
for param <- params do
1883-
typedAheadExpr(param)
1884-
for (tdef, wnames) <- witnessNamesOfParam do
1885-
if wnames.contains(param.name) && allParamsSeen(wnames, params) then
1886-
addContextBoundCompanionFor(symbolOfTree(tdef), wnames, params.map(symbolOfTree))
1887-
18881884
// The following 3 lines replace what was previously just completeParams(tparams).
18891885
// But that can cause bad bounds being computed, as witnessed by
18901886
// tests/pos/paramcycle.scala. The problematic sequence is this:
@@ -1908,39 +1904,16 @@ class Namer { typer: Typer =>
19081904
// 3. Info of CP is computed (to be copied to DP).
19091905
// 4. CP is completed.
19101906
// 5. Info of CP is copied to DP and DP is completed.
1911-
index(ddef.leadingTypeParams)
1912-
if (isConstructor) sym.owner.typeParams.foreach(_.ensureCompleted())
1907+
if !sym.isPrimaryConstructor then
1908+
index(ddef.leadingTypeParams)
19131909
val completedTypeParams =
19141910
for tparam <- ddef.leadingTypeParams yield typedAheadExpr(tparam).symbol
19151911
if completedTypeParams.forall(_.isType) then
19161912
completer.setCompletedTypeParams(completedTypeParams.asInstanceOf[List[TypeSymbol]])
1917-
ddef.trailingParamss.foreach(completeParams)
1913+
completeTrailingParamss(ddef, sym, indexingCtor = false)
19181914
val paramSymss = normalizeIfConstructor(ddef.paramss.nestedMap(symbolOfTree), isConstructor)
19191915
sym.setParamss(paramSymss)
19201916

1921-
/** Under x.modularity, we add `tracked` to context bound witnesses
1922-
* that have abstract type members
1923-
*/
1924-
def needsTracked(sym: Symbol, param: ValDef)(using Context) =
1925-
!sym.is(Tracked)
1926-
&& param.hasAttachment(ContextBoundParam)
1927-
&& sym.info.memberNames(abstractTypeNameFilter).nonEmpty
1928-
1929-
/** Under x.modularity, set every context bound evidence parameter of a class to be tracked,
1930-
* provided it has a type that has an abstract type member. Reset private and local flags
1931-
* so that the parameter becomes a `val`.
1932-
*/
1933-
def setTracked(param: ValDef): Unit =
1934-
val sym = symbolOfTree(param)
1935-
sym.maybeOwner.maybeOwner.infoOrCompleter match
1936-
case info: TempClassInfo if needsTracked(sym, param) =>
1937-
typr.println(i"set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}")
1938-
for acc <- info.decls.lookupAll(sym.name) if acc.is(ParamAccessor) do
1939-
acc.resetFlag(PrivateLocal)
1940-
acc.setFlag(Tracked)
1941-
sym.setFlag(Tracked)
1942-
case _ =>
1943-
19441917
def wrapMethType(restpe: Type): Type =
19451918
instantiateDependent(restpe, paramSymss)
19461919
methodType(paramSymss, restpe, ddef.mods.is(JavaDefined))
@@ -1949,11 +1922,11 @@ class Namer { typer: Typer =>
19491922
wrapMethType(addParamRefinements(restpe, paramSymss))
19501923

19511924
if isConstructor then
1952-
if sym.isPrimaryConstructor && Feature.enabled(modularity) then
1953-
ddef.termParamss.foreach(_.foreach(setTracked))
19541925
// set result type tree to unit, but take the current class as result type of the symbol
19551926
typedAheadType(ddef.tpt, defn.UnitType)
1956-
wrapMethType(effectiveResultType(sym, paramSymss))
1927+
val mt = wrapMethType(effectiveResultType(sym, paramSymss))
1928+
if sym.isPrimaryConstructor then checkCaseClassParamDependencies(mt, sym.owner)
1929+
mt
19571930
else if sym.isAllOf(Given | Method) && Feature.enabled(modularity) then
19581931
// set every context bound evidence parameter of a given companion method
19591932
// to be tracked, provided it has a type that has an abstract type member.
@@ -1966,6 +1939,75 @@ class Namer { typer: Typer =>
19661939
valOrDefDefSig(ddef, sym, paramSymss, wrapMethType)
19671940
end defDefSig
19681941

1942+
/** Complete the trailing parameters of a DefDef,
1943+
* as a part of indexing the primary constructor or
1944+
* as a part of completing a DefDef, including the primary constructor.
1945+
*/
1946+
def completeTrailingParamss(ddef: DefDef, sym: Symbol, indexingCtor: Boolean)(using Context): Unit =
1947+
// A map from context-bounded type parameters to associated evidence parameter names
1948+
val witnessNamesOfParam = mutable.Map[TypeDef, List[TermName]]()
1949+
if !ddef.name.is(DefaultGetterName) && !sym.is(Synthetic) && (indexingCtor || !sym.isPrimaryConstructor) then
1950+
for params <- ddef.paramss; case tdef: TypeDef <- params do
1951+
for case WitnessNamesAnnot(ws) <- tdef.mods.annotations do
1952+
witnessNamesOfParam(tdef) = ws
1953+
1954+
/** Is each name in `wnames` defined somewhere in the previous parameters? */
1955+
def allParamsSeen(wnames: List[TermName], prevParams: Set[Name]) =
1956+
(wnames.toSet[Name] -- prevParams).isEmpty
1957+
1958+
/** Enter and typecheck parameter list.
1959+
* Once all witness parameters for a context bound are seen, create a
1960+
* context bound companion for it.
1961+
*/
1962+
def completeParams(params: List[MemberDef])(using Context): Unit =
1963+
if indexingCtor || !sym.isPrimaryConstructor then
1964+
index(params)
1965+
var prevParams = Set.empty[Name]
1966+
for param <- params do
1967+
if !indexingCtor then
1968+
typedAheadExpr(param)
1969+
1970+
prevParams += param.name
1971+
for (tdef, wnames) <- witnessNamesOfParam do
1972+
if wnames.contains(param.name) && allParamsSeen(wnames, prevParams) then
1973+
addContextBoundCompanionFor(symbolOfTree(tdef), wnames, params.map(symbolOfTree))
1974+
1975+
ddef.trailingParamss.foreach(completeParams)
1976+
end completeTrailingParamss
1977+
1978+
/** Checks an implementation restriction on case classes. */
1979+
def checkCaseClassParamDependencies(mt: Type, cls: Symbol)(using Context): Unit =
1980+
mt.stripPoly match
1981+
case mt: MethodType if cls.is(Case) && mt.isParamDependent =>
1982+
// See issue #8073 for background
1983+
report.error(
1984+
em"""Implementation restriction: case classes cannot have dependencies between parameters""",
1985+
cls.srcPos)
1986+
case _ =>
1987+
1988+
/** Under x.modularity, we add `tracked` to context bound witnesses
1989+
* that have abstract type members
1990+
*/
1991+
def needsTracked(sym: Symbol, param: ValDef)(using Context) =
1992+
!sym.is(Tracked)
1993+
&& param.hasAttachment(ContextBoundParam)
1994+
&& sym.info.memberNames(abstractTypeNameFilter).nonEmpty
1995+
1996+
/** Under x.modularity, set every context bound evidence parameter of a class to be tracked,
1997+
* provided it has a type that has an abstract type member. Reset private and local flags
1998+
* so that the parameter becomes a `val`.
1999+
*/
2000+
def setTracked(param: ValDef)(using Context): Unit =
2001+
val sym = symbolOfTree(param)
2002+
sym.maybeOwner.maybeOwner.infoOrCompleter match
2003+
case info: ClassInfo if needsTracked(sym, param) =>
2004+
typr.println(i"set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}")
2005+
for acc <- info.decls.lookupAll(sym.name) if acc.is(ParamAccessor) do
2006+
acc.resetFlag(PrivateLocal)
2007+
acc.setFlag(Tracked)
2008+
sym.setFlag(Tracked)
2009+
case _ =>
2010+
19692011
def inferredResultType(
19702012
mdef: ValOrDefDef,
19712013
sym: Symbol,

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

-6
Original file line numberDiff line numberDiff line change
@@ -2516,12 +2516,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
25162516
(arg, tparamBounds)
25172517
else
25182518
(arg, WildcardType)
2519-
if (tpt1.symbol.isClass)
2520-
tparam match {
2521-
case tparam: Symbol =>
2522-
tparam.ensureCompleted() // This is needed to get the test `compileParSetSubset` to work
2523-
case _ =>
2524-
}
25252519
if (desugaredArg.isType)
25262520
arg match {
25272521
case untpd.WildcardTypeBoundsTree()
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Like tests/neg/i15177.FakeEnum.min.scala
2+
// But with an actual upper-bound requirement
3+
// Which shouldn't be ignored as a part of overcoming the the cycle
4+
trait Foo
5+
trait X[T <: Foo] { trait Id }
6+
object A extends X[B] // error: Type argument B does not conform to upper bound Foo
7+
class B extends A.Id

tests/neg/i15177.constr-dep.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// An example of how constructor _type_ parameters
2+
// Which can _not_ be passed to the extends part
3+
// That makes it part of the parent type,
4+
// which has been found to be unsound.
5+
class Foo[A]
6+
class Foo1(val x: Int)
7+
extends Foo[ // error: The type of a class parent cannot refer to constructor parameters, but Foo[(Foo1.this.x : Int)] refers to x
8+
x.type
9+
]

tests/neg/i15177.ub.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// like tests/pos/i15177.scala
2+
// but with T having an upper bound
3+
// that B doesn't conform to
4+
// just to be sure that not forcing B
5+
// doesn't backdoor an illegal X[B]
6+
class X[T <: C] {
7+
type Id
8+
}
9+
object A
10+
extends X[ // error
11+
B] // error
12+
class B(id: A.Id)
13+
class C
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Like tests/neg/i15177.FakeEnum.min.scala
2+
// With an actual upper-bound requirement
3+
// But that is satisfied on class B
4+
trait Foo
5+
trait X[T <: Foo] { trait Id }
6+
object A extends X[B]
7+
class B extends A.Id with Foo
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Like tests/neg/i15177.FakeEnum.min.scala
2+
// With an actual upper-bound requirement
3+
// But that is satisfied on trait Id
4+
trait Foo
5+
trait X[T <: Foo] { trait Id extends Foo }
6+
object A extends X[B]
7+
class B extends A.Id

tests/pos/i15177.FakeEnum.min.scala

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Minimisation of tests/neg/i15177.FakeEnum.scala
2+
trait X[T] { trait Id }
3+
object A extends X[B]
4+
class B extends A.Id

tests/pos/i15177.FakeEnum.scala

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// From https://github.com/scala/scala3/issues/15177#issuecomment-1463088400
2+
trait FakeEnum[A, @specialized(Byte, Short, Int, Long) B]
3+
{
4+
trait Value {
5+
self: A =>
6+
def name: String
7+
def id: B
8+
}
9+
}
10+
11+
object FakeEnumType
12+
extends FakeEnum[FakeEnumType, Short]
13+
{
14+
val MEMBER1 = new FakeEnumType((0: Short), "MEMBER1") {}
15+
val MEMBER2 = new FakeEnumType((1: Short), "MEMBER2") {}
16+
}
17+
18+
sealed abstract
19+
class FakeEnumType(val id: Short, val name: String)
20+
extends FakeEnumType.Value
21+
{}

0 commit comments

Comments
 (0)