Skip to content

Commit 7651df4

Browse files
committed
Disallow empty implicit parameter sections
Previously, `given ()` was legal, but it serves no purpose and only complicates things.
1 parent c34cf2b commit 7651df4

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,7 +2038,7 @@ object Parsers {
20382038
* ClsParams ::= ClsParam {`' ClsParam}
20392039
* ClsParam ::= {Annotation} [{Modifier} (`val' | `var') | `inline'] Param
20402040
* DefParamClause ::= [nl] `(' [FunArgMods] [DefParams] ')' | InstParamClause
2041-
* InstParamClause ::= ‘with’ (‘(’ [DefParams] ‘)’ | ContextTypes)
2041+
* InstParamClause ::= ‘given’ (‘(’ DefParams ‘)’ | ContextTypes)
20422042
* ContextTypes ::= RefinedType {`,' RefinedType}
20432043
* DefParams ::= DefParam {`,' DefParam}
20442044
* DefParam ::= {Annotation} [`inline'] Param
@@ -2109,10 +2109,11 @@ object Parsers {
21092109

21102110
// begin paramClause
21112111
inParens {
2112-
if (in.token == RPAREN && !prefix) Nil
2112+
val isContextual = impliedMods.is(Contextual)
2113+
if (in.token == RPAREN && !prefix && !isContextual) Nil
21132114
else {
21142115
def funArgMods(mods: Modifiers): Modifiers =
2115-
if (in.token == IMPLICIT && !mods.is(Contextual))
2116+
if (in.token == IMPLICIT && !isContextual)
21162117
funArgMods(addMod(mods, atSpan(accept(IMPLICIT)) { Mod.Implicit() }))
21172118
else if (in.token == ERASED)
21182119
funArgMods(addMod(mods, atSpan(accept(ERASED)) { Mod.Erased() }))
@@ -2148,7 +2149,7 @@ object Parsers {
21482149
newLineOptWhenFollowedBy(LPAREN)
21492150
if (in.token == LPAREN) {
21502151
if (ofInstance && !isContextual)
2151-
syntaxError(em"parameters of instance definitions must come after `with'")
2152+
syntaxError(em"parameters of instance definitions must come after `given'")
21522153
val params = paramClause(
21532154
ofClass = ofClass,
21542155
ofCaseClass = ofCaseClass,

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ trait NamerContextOps { this: Context =>
131131
termParamss
132132

133133
/** The method type corresponding to given parameters and result type */
134-
def methodType(typeParams: List[Symbol], valueParamss: List[List[Symbol]], resultType: Type,
135-
isJava: Boolean = false, isInstance: Boolean = false)(implicit ctx: Context): Type = {
134+
def methodType(typeParams: List[Symbol], valueParamss: List[List[Symbol]], resultType: Type, isJava: Boolean = false)(implicit ctx: Context): Type = {
136135
val monotpe =
137136
(valueParamss :\ resultType) { (params, resultType) =>
138137
val (isImplicit, isErased, isContextual) =
139-
if (params.isEmpty) (isInstance, false, false)
138+
if (params.isEmpty) (false, false, false)
140139
else (params.head is Implicit, params.head is Erased, params.head.is(Contextual))
141140
val make = MethodType.maker(isJava = isJava, isImplicit = isImplicit, isErased = isErased, isContextual = isContextual)
142141
if (isJava)
@@ -1301,8 +1300,7 @@ class Namer { typer: Typer =>
13011300
val termParamss = ctx.normalizeIfConstructor(vparamss.nestedMap(symbolOfTree), isConstructor)
13021301
def wrapMethType(restpe: Type): Type = {
13031302
instantiateDependent(restpe, typeParams, termParamss)
1304-
ctx.methodType(tparams map symbolOfTree, termParamss, restpe,
1305-
isJava = ddef.mods is JavaDefined, isInstance = ddef.mods.hasMod(classOf[Mod.Instance]))
1303+
ctx.methodType(tparams map symbolOfTree, termParamss, restpe, isJava = ddef.mods is JavaDefined)
13061304
}
13071305
if (isConstructor) {
13081306
// set result type tree to unit, but take the current class as result type of the symbol

tests/neg/contextual-params.scala renamed to tests/neg/implicit-params.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ object Test {
66

77
def g(x: Int) given (c: C) (y: Int) = x + c.x + y
88

9+
def h(x: Int) given () = x // error
10+
911
implicit object C extends C(11)
1012

1113
f(1)

0 commit comments

Comments
 (0)