@@ -822,8 +822,11 @@ class Namer { typer: Typer =>
822
822
if (sym.is(Module )) moduleValSig(sym)
823
823
else valOrDefDefSig(original, sym, Nil , identity)(using localContext(sym).setNewScope)
824
824
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 ))
827
830
typer1.defDefSig(original, sym, this )(using localContext(sym).setTyper(typer1))
828
831
case imp : Import =>
829
832
try
@@ -833,6 +836,12 @@ class Namer { typer: Typer =>
833
836
typr.println(s " error while completing ${imp.expr}" )
834
837
throw ex
835
838
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
+
836
845
final override def complete (denot : SymDenotation )(using Context ): Unit = {
837
846
if (Config .showCompletions && ctx.typerState != creationContext.typerState) {
838
847
def levels (c : Context ): Int =
@@ -993,15 +1002,19 @@ class Namer { typer: Typer =>
993
1002
994
1003
/** If completion of the owner of the to be completed symbol has not yet started,
995
1004
* 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
997
1006
* yet completed. Test case is pos/i10967.scala.
998
1007
*/
999
1008
override def needsCompletion (symd : SymDenotation )(using Context ): Boolean =
1000
1009
val owner = symd.owner
1001
1010
! owner.exists
1002
1011
|| owner.is(Touched )
1003
1012
|| {
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()
1005
1018
! symd.isCompleted
1006
1019
}
1007
1020
@@ -1526,7 +1539,10 @@ class Namer { typer: Typer =>
1526
1539
index(constr)
1527
1540
index(rest)(using localCtx)
1528
1541
1529
- checkCaseClassParamDependencies(symbolOfTree(constr).info, cls) // Completes constr symbol as a side effect
1542
+ val constrSym = symbolOfTree(constr)
1543
+ constrSym.infoOrCompleter match
1544
+ case completer : Completer => completer.indexConstructor(constr, constrSym)
1545
+ case _ =>
1530
1546
1531
1547
tempInfo = denot.asClass.classInfo.integrateOpaqueMembers.asInstanceOf [TempClassInfo ]
1532
1548
denot.info = savedInfo
@@ -1756,6 +1772,17 @@ class Namer { typer: Typer =>
1756
1772
val sym = tree.symbol
1757
1773
if sym.isConstructor then sym.owner else sym
1758
1774
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
+
1759
1786
/** The signature of a module valdef.
1760
1787
* This will compute the corresponding module class TypeRef immediately
1761
1788
* without going through the defined type of the ValDef. This is necessary
@@ -1877,13 +1904,13 @@ class Namer { typer: Typer =>
1877
1904
// 3. Info of CP is computed (to be copied to DP).
1878
1905
// 4. CP is completed.
1879
1906
// 5. Info of CP is copied to DP and DP is completed.
1880
- index(ddef.leadingTypeParams)
1881
- if (isConstructor) sym.owner.typeParams.foreach(_.ensureCompleted() )
1907
+ if ! sym.isPrimaryConstructor then
1908
+ index(ddef.leadingTypeParams )
1882
1909
val completedTypeParams =
1883
1910
for tparam <- ddef.leadingTypeParams yield typedAheadExpr(tparam).symbol
1884
1911
if completedTypeParams.forall(_.isType) then
1885
1912
completer.setCompletedTypeParams(completedTypeParams.asInstanceOf [List [TypeSymbol ]])
1886
- completeTrailingParamss(ddef, sym)
1913
+ completeTrailingParamss(ddef, sym, indexingCtor = false )
1887
1914
val paramSymss = normalizeIfConstructor(ddef.paramss.nestedMap(symbolOfTree), isConstructor)
1888
1915
sym.setParamss(paramSymss)
1889
1916
@@ -1895,11 +1922,11 @@ class Namer { typer: Typer =>
1895
1922
wrapMethType(addParamRefinements(restpe, paramSymss))
1896
1923
1897
1924
if isConstructor then
1898
- if sym.isPrimaryConstructor && Feature .enabled(modularity) then
1899
- ddef.termParamss.foreach(_.foreach(setTracked))
1900
1925
// set result type tree to unit, but take the current class as result type of the symbol
1901
1926
typedAheadType(ddef.tpt, defn.UnitType )
1902
- wrapMethType(effectiveResultType(sym, paramSymss))
1927
+ val mt = wrapMethType(effectiveResultType(sym, paramSymss))
1928
+ if sym.isPrimaryConstructor then checkCaseClassParamDependencies(mt, sym.owner)
1929
+ mt
1903
1930
else if sym.isAllOf(Given | Method ) && Feature .enabled(modularity) then
1904
1931
// set every context bound evidence parameter of a given companion method
1905
1932
// to be tracked, provided it has a type that has an abstract type member.
@@ -1912,30 +1939,37 @@ class Namer { typer: Typer =>
1912
1939
valOrDefDefSig(ddef, sym, paramSymss, wrapMethType)
1913
1940
end defDefSig
1914
1941
1915
- def completeTrailingParamss (ddef : DefDef , sym : Symbol )(using Context ): Unit =
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 =
1916
1947
// A map from context-bounded type parameters to associated evidence parameter names
1917
1948
val witnessNamesOfParam = mutable.Map [TypeDef , List [TermName ]]()
1918
- if ! ddef.name.is(DefaultGetterName ) && ! sym.is(Synthetic ) then
1949
+ if ! ddef.name.is(DefaultGetterName ) && ! sym.is(Synthetic ) && (indexingCtor || ! sym.isPrimaryConstructor) then
1919
1950
for params <- ddef.paramss; case tdef : TypeDef <- params do
1920
1951
for case WitnessNamesAnnot (ws) <- tdef.mods.annotations do
1921
1952
witnessNamesOfParam(tdef) = ws
1922
1953
1923
- /** Is each name in `wnames` defined somewhere in the longest prefix of all `params`
1924
- * that have been typed ahead (i.e. that carry the TypedAhead attachment)?
1925
- */
1926
- def allParamsSeen (wnames : List [TermName ], params : List [MemberDef ]) =
1927
- (wnames.toSet[Name ] -- params.takeWhile(_.hasAttachment(TypedAhead )).map(_.name)).isEmpty
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
1928
1957
1929
1958
/** Enter and typecheck parameter list.
1930
1959
* Once all witness parameters for a context bound are seen, create a
1931
1960
* context bound companion for it.
1932
1961
*/
1933
1962
def completeParams (params : List [MemberDef ])(using Context ): Unit =
1934
- index(params)
1963
+ if indexingCtor || ! sym.isPrimaryConstructor then
1964
+ index(params)
1965
+ var prevParams = Set .empty[Name ]
1935
1966
for param <- params do
1936
- typedAheadExpr(param)
1967
+ if ! indexingCtor then
1968
+ typedAheadExpr(param)
1969
+
1970
+ prevParams += param.name
1937
1971
for (tdef, wnames) <- witnessNamesOfParam do
1938
- if wnames.contains(param.name) && allParamsSeen(wnames, params ) then
1972
+ if wnames.contains(param.name) && allParamsSeen(wnames, prevParams ) then
1939
1973
addContextBoundCompanionFor(symbolOfTree(tdef), wnames, params.map(symbolOfTree))
1940
1974
1941
1975
ddef.trailingParamss.foreach(completeParams)
@@ -1966,7 +2000,7 @@ class Namer { typer: Typer =>
1966
2000
def setTracked (param : ValDef )(using Context ): Unit =
1967
2001
val sym = symbolOfTree(param)
1968
2002
sym.maybeOwner.maybeOwner.infoOrCompleter match
1969
- case info : TempClassInfo if needsTracked(sym, param) =>
2003
+ case info : ClassInfo if needsTracked(sym, param) =>
1970
2004
typr.println(i " set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}" )
1971
2005
for acc <- info.decls.lookupAll(sym.name) if acc.is(ParamAccessor ) do
1972
2006
acc.resetFlag(PrivateLocal )
0 commit comments