Skip to content

Commit 4696911

Browse files
committed
Change scheme again
1 parent 24916b0 commit 4696911

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Names.{Name, TermName}
88
import Constants.Constant
99

1010
import Names.Name
11+
import StdNames.nme
1112
import config.Feature
1213

1314
class TypeUtils:
@@ -189,5 +190,25 @@ class TypeUtils:
189190
def stripRefinement: Type = self match
190191
case self: RefinedOrRecType => self.parent.stripRefinement
191192
case seld => self
193+
194+
def applicableConstructors(argTypes: List[Type], adaptVarargs: Boolean)(using Context): List[Symbol] =
195+
def isApplicable(constr: Symbol): Boolean =
196+
def recur(ctpe: Type): Boolean = ctpe match
197+
case ctpe: PolyType =>
198+
if argTypes.isEmpty then recur(ctpe.resultType) // no need to know instances
199+
else recur(ctpe.instantiate(self.argTypes))
200+
case ctpe: MethodType =>
201+
var paramInfos = ctpe.paramInfos
202+
if adaptVarargs && paramInfos.length == argTypes.length + 1
203+
&& atPhaseNoLater(Phases.elimRepeatedPhase)(constr.info.isVarArgsMethod)
204+
then // accept missing argument for varargs parameter
205+
paramInfos = paramInfos.init
206+
argTypes.corresponds(paramInfos)(_ <:< _)
207+
case _ =>
208+
false
209+
recur(constr.info)
210+
211+
self.decl(nme.CONSTRUCTOR).altsWith(isApplicable).map(_.symbol)
212+
192213
end TypeUtils
193214

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

+5-15
Original file line numberDiff line numberDiff line change
@@ -5954,27 +5954,17 @@ object Types extends TypeUtils {
59545954
// `ContextFunctionN` does not have constructors
59555955
!ctor.exists || zeroParamsOLD(ctor.info)
59565956

5957-
def zeroParams(tp: Type): Boolean = tp.stripPoly match
5958-
case mt: MethodType =>
5959-
val noArgsNeeded = mt.paramInfos match
5960-
case Nil => true
5961-
case info :: Nil => info.isRepeatedParam
5962-
case _ => false
5963-
noArgsNeeded && !mt.resultType.isInstanceOf[MethodType]
5964-
case et: ExprType => true
5965-
case _ => false
59665957
def takesNoArgs(tp: Type) =
5967-
val constrs = tp.decl(nme.CONSTRUCTOR)
5968-
!constrs.exists // `ContextFunctionN` does not have constructors
5969-
|| constrs.hasAltWith(constr => zeroParams(constr.info))
5970-
def firstParentCls = cls.info.parents.head.classSymbol
5958+
!tp.classSymbol.primaryConstructor.exists // `ContextFunctionN` does not have constructors
5959+
|| tp.applicableConstructors(Nil, adaptVarargs = true).nonEmpty
5960+
def firstParentCls = tp.parents.head.classSymbol
59715961
val noArgsNeeded: Boolean =
59725962
takesNoArgs(tp)
5973-
&& (!tp.cls.is(Trait) || takesNoArgs(tp.cls.info.parents.head))
5963+
&& (!tp.cls.is(Trait) || takesNoArgs(tp.parents.head))
59745964

59755965
if noArgsNeeded != validCtorOLD then
59765966
println(
5977-
i"""SAM change for $tp / ${tp.cls.fullName} with parent ${firstParentCls.fullName}, now $noArgsNeeded
5967+
i"""SAM change for $tp with parent ${firstParentCls.fullName}, now $noArgsNeeded
59785968
|takesNoArgs: ${takesNoArgs(tp)}
59795969
|takesNoArgsParent: ${takesNoArgs(tp.cls.info.parents.head)}
59805970
|primary: ${firstParentCls.primaryConstructor.info}""")

0 commit comments

Comments
 (0)