@@ -4,7 +4,7 @@ package typer
44
55import transform ._
66import core ._
7- import Symbols ._ , Types ._ , Contexts ._ , Flags ._ , Names ._ , NameOps ._
7+ import Symbols ._ , Types ._ , Contexts ._ , Flags ._ , Names ._ , NameOps ._ , NameKinds . _
88import StdNames ._ , Denotations ._ , SymUtils ._ , Phases ._ , SymDenotations ._
99import NameKinds .DefaultGetterName
1010import Annotations ._
@@ -1047,6 +1047,47 @@ object RefChecks {
10471047 report.error(i " private $sym cannot override ${other.showLocated}" , sym.srcPos)
10481048 end checkNoPrivateOverrides
10491049
1050+ /** Check that unary method definition do not receive parameters.
1051+ * They can only receive inferred parameters such as type parameters and implicit parameters.
1052+ */
1053+ def checkUnaryMethods (sym : Symbol )(using Context ): Unit =
1054+ /** Check that the only term parameters are contextual or implicit */
1055+ def checkParameters (tpe : Type ): Unit =
1056+ tpe match
1057+ case tpe : MethodType =>
1058+ if tpe.isImplicitMethod || tpe.isContextualMethod then
1059+ checkParameters(tpe.resType)
1060+ else
1061+ val what =
1062+ if tpe.paramNames.isEmpty then " empty parameter list.\n\n Possible fix: remove the `()` arguments."
1063+ else " parameters"
1064+ report.warning(s " Unary method cannot take $what" , sym.sourcePos)
1065+ case tpe : PolyType =>
1066+ checkParameters(tpe.resType)
1067+ case _ =>
1068+ // ok
1069+
1070+ /** Skip leading type and contextual parameters, then skip the
1071+ * self parameter, and finally check the parameter
1072+ */
1073+ def checkExtensionParameters (tpe : Type ): Unit =
1074+ tpe match
1075+ case tpe : MethodType =>
1076+ assert(tpe.paramNames.length == 1 )
1077+ if tpe.isContextualMethod then checkExtensionParameters(tpe.resType)
1078+ else checkParameters(tpe.resType)
1079+ case tpe : PolyType =>
1080+ checkExtensionParameters(tpe.resType)
1081+
1082+ if sym.name.startsWith(nme.UNARY_PREFIX .toString) then
1083+ if sym.is(Extension ) || sym.name.is(ExtMethName ) then
1084+ // if is method from `extension` or value class
1085+ checkExtensionParameters(sym.info)
1086+ else
1087+ checkParameters(sym.info)
1088+
1089+ end checkUnaryMethods
1090+
10501091 type LevelAndIndex = immutable.Map [Symbol , (LevelInfo , Int )]
10511092
10521093 class OptLevelInfo {
@@ -1254,6 +1295,7 @@ class RefChecks extends MiniPhase { thisPhase =>
12541295 checkExperimentalAnnots(tree.symbol)
12551296 checkExperimentalSignature(tree.symbol, tree)
12561297 checkImplicitNotFoundAnnotation.defDef(tree.symbol.denot)
1298+ checkUnaryMethods(tree.symbol)
12571299 tree
12581300 }
12591301
0 commit comments