@@ -4,7 +4,7 @@ package typer
4
4
5
5
import transform ._
6
6
import core ._
7
- import Symbols ._ , Types ._ , Contexts ._ , Flags ._ , Names ._ , NameOps ._
7
+ import Symbols ._ , Types ._ , Contexts ._ , Flags ._ , Names ._ , NameOps ._ , NameKinds . _
8
8
import StdNames ._ , Denotations ._ , SymUtils ._ , Phases ._ , SymDenotations ._
9
9
import NameKinds .DefaultGetterName
10
10
import Annotations ._
@@ -1047,6 +1047,47 @@ object RefChecks {
1047
1047
report.error(i " private $sym cannot override ${other.showLocated}" , sym.srcPos)
1048
1048
end checkNoPrivateOverrides
1049
1049
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
+
1050
1091
type LevelAndIndex = immutable.Map [Symbol , (LevelInfo , Int )]
1051
1092
1052
1093
class OptLevelInfo {
@@ -1254,6 +1295,7 @@ class RefChecks extends MiniPhase { thisPhase =>
1254
1295
checkExperimentalAnnots(tree.symbol)
1255
1296
checkExperimentalSignature(tree.symbol, tree)
1256
1297
checkImplicitNotFoundAnnotation.defDef(tree.symbol.denot)
1298
+ checkUnaryMethods(tree.symbol)
1257
1299
tree
1258
1300
}
1259
1301
0 commit comments