@@ -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,7 +1539,10 @@ class Namer { typer: Typer =>
15261539 index(constr)
15271540 index(rest)(using localCtx)
15281541
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 _ =>
15301546
15311547 tempInfo = denot.asClass.classInfo.integrateOpaqueMembers.asInstanceOf [TempClassInfo ]
15321548 denot.info = savedInfo
@@ -1756,6 +1772,17 @@ class Namer { typer: Typer =>
17561772 val sym = tree.symbol
17571773 if sym.isConstructor then sym.owner else sym
17581774
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+
17591786 /** The signature of a module valdef.
17601787 * This will compute the corresponding module class TypeRef immediately
17611788 * without going through the defined type of the ValDef. This is necessary
@@ -1877,13 +1904,13 @@ class Namer { typer: Typer =>
18771904 // 3. Info of CP is computed (to be copied to DP).
18781905 // 4. CP is completed.
18791906 // 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 )
18821909 val completedTypeParams =
18831910 for tparam <- ddef.leadingTypeParams yield typedAheadExpr(tparam).symbol
18841911 if completedTypeParams.forall(_.isType) then
18851912 completer.setCompletedTypeParams(completedTypeParams.asInstanceOf [List [TypeSymbol ]])
1886- completeTrailingParamss(ddef, sym)
1913+ completeTrailingParamss(ddef, sym, indexingCtor = false )
18871914 val paramSymss = normalizeIfConstructor(ddef.paramss.nestedMap(symbolOfTree), isConstructor)
18881915 sym.setParamss(paramSymss)
18891916
@@ -1895,11 +1922,11 @@ class Namer { typer: Typer =>
18951922 wrapMethType(addParamRefinements(restpe, paramSymss))
18961923
18971924 if isConstructor then
1898- if sym.isPrimaryConstructor && Feature .enabled(modularity) then
1899- ddef.termParamss.foreach(_.foreach(setTracked))
19001925 // set result type tree to unit, but take the current class as result type of the symbol
19011926 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
19031930 else if sym.isAllOf(Given | Method ) && Feature .enabled(modularity) then
19041931 // set every context bound evidence parameter of a given companion method
19051932 // to be tracked, provided it has a type that has an abstract type member.
@@ -1912,30 +1939,37 @@ class Namer { typer: Typer =>
19121939 valOrDefDefSig(ddef, sym, paramSymss, wrapMethType)
19131940 end defDefSig
19141941
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 =
19161947 // A map from context-bounded type parameters to associated evidence parameter names
19171948 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
19191950 for params <- ddef.paramss; case tdef : TypeDef <- params do
19201951 for case WitnessNamesAnnot (ws) <- tdef.mods.annotations do
19211952 witnessNamesOfParam(tdef) = ws
19221953
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
19281957
19291958 /** Enter and typecheck parameter list.
19301959 * Once all witness parameters for a context bound are seen, create a
19311960 * context bound companion for it.
19321961 */
19331962 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 ]
19351966 for param <- params do
1936- typedAheadExpr(param)
1967+ if ! indexingCtor then
1968+ typedAheadExpr(param)
1969+
1970+ prevParams += param.name
19371971 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
19391973 addContextBoundCompanionFor(symbolOfTree(tdef), wnames, params.map(symbolOfTree))
19401974
19411975 ddef.trailingParamss.foreach(completeParams)
@@ -1966,7 +2000,7 @@ class Namer { typer: Typer =>
19662000 def setTracked (param : ValDef )(using Context ): Unit =
19672001 val sym = symbolOfTree(param)
19682002 sym.maybeOwner.maybeOwner.infoOrCompleter match
1969- case info : TempClassInfo if needsTracked(sym, param) =>
2003+ case info : ClassInfo if needsTracked(sym, param) =>
19702004 typr.println(i " set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}" )
19712005 for acc <- info.decls.lookupAll(sym.name) if acc.is(ParamAccessor ) do
19722006 acc.resetFlag(PrivateLocal )
0 commit comments