@@ -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 =
@@ -986,15 +995,19 @@ class Namer { typer: Typer =>
986995
987996 /** If completion of the owner of the to be completed symbol has not yet started,
988997 * complete the owner first and check again. This prevents cyclic references
989- * where we need to copmplete a type parameter that has an owner that is not
998+ * where we need to complete a type parameter that has an owner that is not
990999 * yet completed. Test case is pos/i10967.scala.
9911000 */
9921001 override def needsCompletion (symd : SymDenotation )(using Context ): Boolean =
9931002 val owner = symd.owner
9941003 ! owner.exists
9951004 || owner.is(Touched )
9961005 || {
997- owner.ensureCompleted()
1006+ // Only complete the owner if it's a type (eg. the class that owns a type parameter)
1007+ // This avoids completing primary constructor methods while completing the type of one of its type parameters
1008+ // See i15177.scala.
1009+ if owner.isType then
1010+ owner.ensureCompleted()
9981011 ! symd.isCompleted
9991012 }
10001013
@@ -1519,12 +1532,9 @@ class Namer { typer: Typer =>
15191532 index(constr)
15201533 index(rest)(using localCtx)
15211534
1522- symbolOfTree(constr).info.stripPoly match // Completes constr symbol as a side effect
1523- case mt : MethodType if cls.is(Case ) && mt.isParamDependent =>
1524- // See issue #8073 for background
1525- report.error(
1526- em """ Implementation restriction: case classes cannot have dependencies between parameters """ ,
1527- cls.srcPos)
1535+ val constrSym = symbolOfTree(constr)
1536+ constrSym.infoOrCompleter match
1537+ case completer : Completer => completer.indexConstructor(constr, constrSym)
15281538 case _ =>
15291539
15301540 tempInfo = denot.asClass.classInfo.integrateOpaqueMembers.asInstanceOf [TempClassInfo ]
@@ -1853,31 +1863,6 @@ class Namer { typer: Typer =>
18531863 // Beware: ddef.name need not match sym.name if sym was freshened!
18541864 val isConstructor = sym.name == nme.CONSTRUCTOR
18551865
1856- // A map from context-bounded type parameters to associated evidence parameter names
1857- val witnessNamesOfParam = mutable.Map [TypeDef , List [TermName ]]()
1858- if ! ddef.name.is(DefaultGetterName ) && ! sym.is(Synthetic ) then
1859- for params <- ddef.paramss; case tdef : TypeDef <- params do
1860- for case WitnessNamesAnnot (ws) <- tdef.mods.annotations do
1861- witnessNamesOfParam(tdef) = ws
1862-
1863- /** Is each name in `wnames` defined somewhere in the longest prefix of all `params`
1864- * that have been typed ahead (i.e. that carry the TypedAhead attachment)?
1865- */
1866- def allParamsSeen (wnames : List [TermName ], params : List [MemberDef ]) =
1867- (wnames.toSet[Name ] -- params.takeWhile(_.hasAttachment(TypedAhead )).map(_.name)).isEmpty
1868-
1869- /** Enter and typecheck parameter list.
1870- * Once all witness parameters for a context bound are seen, create a
1871- * context bound companion for it.
1872- */
1873- def completeParams (params : List [MemberDef ])(using Context ): Unit =
1874- index(params)
1875- for param <- params do
1876- typedAheadExpr(param)
1877- for (tdef, wnames) <- witnessNamesOfParam do
1878- if wnames.contains(param.name) && allParamsSeen(wnames, params) then
1879- addContextBoundCompanionFor(symbolOfTree(tdef), wnames, params.map(symbolOfTree))
1880-
18811866 // The following 3 lines replace what was previously just completeParams(tparams).
18821867 // But that can cause bad bounds being computed, as witnessed by
18831868 // tests/pos/paramcycle.scala. The problematic sequence is this:
@@ -1901,39 +1886,15 @@ class Namer { typer: Typer =>
19011886 // 3. Info of CP is computed (to be copied to DP).
19021887 // 4. CP is completed.
19031888 // 5. Info of CP is copied to DP and DP is completed.
1904- index(ddef.leadingTypeParams)
1905- if (isConstructor) sym.owner.typeParams.foreach(_.ensureCompleted())
1889+ if ! sym.isPrimaryConstructor then index(ddef.leadingTypeParams)
19061890 val completedTypeParams =
19071891 for tparam <- ddef.leadingTypeParams yield typedAheadExpr(tparam).symbol
19081892 if completedTypeParams.forall(_.isType) then
19091893 completer.setCompletedTypeParams(completedTypeParams.asInstanceOf [List [TypeSymbol ]])
1910- ddef.trailingParamss.foreach(completeParams )
1894+ completeTrailingParamss(ddef, sym )
19111895 val paramSymss = normalizeIfConstructor(ddef.paramss.nestedMap(symbolOfTree), isConstructor)
19121896 sym.setParamss(paramSymss)
19131897
1914- /** Under x.modularity, we add `tracked` to context bound witnesses
1915- * that have abstract type members
1916- */
1917- def needsTracked (sym : Symbol , param : ValDef )(using Context ) =
1918- ! sym.is(Tracked )
1919- && param.hasAttachment(ContextBoundParam )
1920- && sym.info.memberNames(abstractTypeNameFilter).nonEmpty
1921-
1922- /** Under x.modularity, set every context bound evidence parameter of a class to be tracked,
1923- * provided it has a type that has an abstract type member. Reset private and local flags
1924- * so that the parameter becomes a `val`.
1925- */
1926- def setTracked (param : ValDef ): Unit =
1927- val sym = symbolOfTree(param)
1928- sym.maybeOwner.maybeOwner.infoOrCompleter match
1929- case info : TempClassInfo if needsTracked(sym, param) =>
1930- typr.println(i " set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}" )
1931- for acc <- info.decls.lookupAll(sym.name) if acc.is(ParamAccessor ) do
1932- acc.resetFlag(PrivateLocal )
1933- acc.setFlag(Tracked )
1934- sym.setFlag(Tracked )
1935- case _ =>
1936-
19371898 def wrapMethType (restpe : Type ): Type =
19381899 instantiateDependent(restpe, paramSymss)
19391900 methodType(paramSymss, restpe, ddef.mods.is(JavaDefined ))
@@ -1942,11 +1903,18 @@ class Namer { typer: Typer =>
19421903 wrapMethType(addParamRefinements(restpe, paramSymss))
19431904
19441905 if isConstructor then
1945- if sym.isPrimaryConstructor && Feature .enabled(modularity) then
1946- ddef.termParamss.foreach(_.foreach(setTracked))
19471906 // set result type tree to unit, but take the current class as result type of the symbol
19481907 typedAheadType(ddef.tpt, defn.UnitType )
1949- wrapMethType(effectiveResultType(sym, paramSymss))
1908+ val mt = wrapMethType(effectiveResultType(sym, paramSymss))
1909+ if sym.isPrimaryConstructor then
1910+ mt.stripPoly match
1911+ case mt : MethodType if sym.owner.is(Case ) && mt.isParamDependent =>
1912+ // See issue #8073 for background
1913+ report.error(
1914+ em """ Implementation restriction: case classes cannot have dependencies between parameters """ ,
1915+ sym.owner.srcPos)
1916+ case _ =>
1917+ mt
19501918 else if sym.isAllOf(Given | Method ) && Feature .enabled(modularity) then
19511919 // set every context bound evidence parameter of a given companion method
19521920 // to be tracked, provided it has a type that has an abstract type member.
@@ -1959,6 +1927,82 @@ class Namer { typer: Typer =>
19591927 valOrDefDefSig(ddef, sym, paramSymss, wrapMethType)
19601928 end defDefSig
19611929
1930+ /** Index the primary constructor of a class, as a part of completing that class.
1931+ * This allows the rest of the constructor completion to be deferred,
1932+ * which avoids non-cyclic classes failing, e.g. pos/i15177.
1933+ */
1934+ def indexConstructor (constr : DefDef , sym : Symbol )(using Context ): Unit =
1935+ index(constr.leadingTypeParams)
1936+ sym.owner.typeParams.foreach(_.ensureCompleted())
1937+ completeTrailingParamss(constr, sym, indexingCtor = true )
1938+ if Feature .enabled(modularity) then
1939+ constr.termParamss.foreach(_.foreach(setTracked))
1940+
1941+ /** Complete the trailing parameters of a DefDef,
1942+ * as a part of indexing the primary constructor or
1943+ * as a part of completing a DefDef, including the primary constructor.
1944+ */
1945+ def completeTrailingParamss (ddef : DefDef , sym : Symbol , indexingCtor : Boolean = false )(using Context ): Unit =
1946+ // A map from context-bounded type parameters to associated evidence parameter names
1947+ val witnessNamesOfParam = mutable.Map [TypeDef , List [TermName ]]()
1948+ if ! ddef.name.is(DefaultGetterName ) && ! sym.is(Synthetic ) && (indexingCtor || ! sym.isPrimaryConstructor) then
1949+ for params <- ddef.paramss; case tdef : TypeDef <- params do
1950+ for case WitnessNamesAnnot (ws) <- tdef.mods.annotations do
1951+ witnessNamesOfParam(tdef) = ws
1952+
1953+ /** Is each name in `wnames` defined somewhere in the previous parameters? */
1954+ def allParamsSeen (wnames : List [TermName ], prevParams1 : List [Name ]) =
1955+ (wnames.toSet[Name ] -- prevParams1).isEmpty
1956+
1957+ /** Enter and typecheck parameter list.
1958+ * Once all witness parameters for a context bound are seen, create a
1959+ * context bound companion for it.
1960+ */
1961+ def completeParams (params : List [MemberDef ])(using Context ): Unit =
1962+ if indexingCtor || ! sym.isPrimaryConstructor then index(params)
1963+ val paramSyms = params.map(symbolOfTree)
1964+
1965+ def loop (nextParams : List [MemberDef ], prevParams : List [Name ]): Unit = nextParams match
1966+ case param :: nextParams1 =>
1967+ if ! indexingCtor then
1968+ typedAheadExpr(param)
1969+
1970+ val prevParams1 = param.name :: prevParams
1971+ for (tdef, wnames) <- witnessNamesOfParam do
1972+ if wnames.contains(param.name) && allParamsSeen(wnames, prevParams1) then
1973+ addContextBoundCompanionFor(symbolOfTree(tdef), wnames, paramSyms)
1974+
1975+ loop(nextParams1, prevParams1)
1976+ case _ =>
1977+ loop(params, Nil )
1978+ end completeParams
1979+
1980+ ddef.trailingParamss.foreach(completeParams)
1981+ end completeTrailingParamss
1982+
1983+ /** Under x.modularity, we add `tracked` to context bound witnesses
1984+ * that have abstract type members
1985+ */
1986+ def needsTracked (sym : Symbol , param : ValDef )(using Context ) =
1987+ ! sym.is(Tracked )
1988+ && param.hasAttachment(ContextBoundParam )
1989+ && sym.info.memberNames(abstractTypeNameFilter).nonEmpty
1990+
1991+ /** Under x.modularity, set every context bound evidence parameter of a class to be tracked,
1992+ * provided it has a type that has an abstract type member. Reset private and local flags
1993+ * so that the parameter becomes a `val`.
1994+ */
1995+ def setTracked (param : ValDef )(using Context ): Unit =
1996+ val sym = symbolOfTree(param)
1997+ sym.maybeOwner.maybeOwner.infoOrCompleter match
1998+ case info : ClassInfo if needsTracked(sym, param) =>
1999+ typr.println(i " set tracked $param, $sym: ${sym.info} containing ${sym.info.memberNames(abstractTypeNameFilter).toList}" )
2000+ for acc <- info.decls.lookupAll(sym.name) if acc.is(ParamAccessor ) do
2001+ acc.resetFlag(PrivateLocal )
2002+ acc.setFlag(Tracked )
2003+ sym.setFlag(Tracked )
2004+ case _ =>
2005+
19622006 def inferredResultType (
19632007 mdef : ValOrDefDef ,
19642008 sym : Symbol ,
0 commit comments