Skip to content

Commit dcf0254

Browse files
authored
Merge pull request #7976 from dotty-staging/fix-#7972
Fix #7972: Disallow signatures with `_:*` return types
2 parents d16d89b + e9f4189 commit dcf0254

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,11 @@ trait Checking {
937937
errorTree(tpt, MissingTypeParameterFor(tpt.tpe))
938938
else tpt
939939

940+
/** Check that the signature of the class mamber does not return a repeated parameter type */
941+
def checkSignatureRepeatedParam(sym: Symbol)(implicit ctx: Context): Unit =
942+
if (!sym.isOneOf(Synthetic | InlineProxy | Param) && sym.info.finalResultType.isRepeatedParam)
943+
ctx.error(em"Cannot return repeated parameter type ${sym.info.finalResultType}", sym.sourcePos)
944+
940945
/** Verify classes extending AnyVal meet the requirements */
941946
def checkDerivedValueClass(clazz: Symbol, stats: List[Tree])(implicit ctx: Context): Unit =
942947
Checking.checkDerivedValueClass(clazz, stats)

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,7 @@ class Typer extends Namer
16091609
case rhs => typedExpr(rhs, tpt1.tpe.widenExpr)
16101610
}
16111611
val vdef1 = assignType(cpy.ValDef(vdef)(name, tpt1, rhs1), sym)
1612+
checkSignatureRepeatedParam(sym)
16121613
if (sym.is(Inline, butNot = DeferredOrTermParamOrAccessor))
16131614
checkInlineConformant(rhs1, isFinal = sym.is(Final), em"right-hand side of inline $sym")
16141615
patchFinalVals(vdef1)
@@ -1694,7 +1695,9 @@ class Typer extends Namer
16941695
checkThisConstrCall(rhs1)
16951696
}
16961697

1697-
assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym).setDefTree
1698+
val ddef2 = assignType(cpy.DefDef(ddef)(name, tparams1, vparamss1, tpt1, rhs1), sym)
1699+
checkSignatureRepeatedParam(sym)
1700+
ddef2.setDefTree
16981701
//todo: make sure dependent method types do not depend on implicits or by-name params
16991702
}
17001703

tests/neg/i7972.scala

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object O {
2+
def m1(a: Int*) = (a: _*) // error: Cannot return repeated parameter type Int*
3+
def m2(a: Int*) = { // error: Cannot return repeated parameter type Int*
4+
val b = (a: _*) // error: Cannot return repeated parameter type Int*
5+
b
6+
}
7+
def m3(a: Int*): Any = {
8+
val b = (a: _*) // error: Cannot return repeated parameter type Int*
9+
b
10+
}
11+
def m4(a: 2*) = (a: _*) // error: Cannot return repeated parameter type Int*
12+
13+
}
14+
15+
class O(a: Int*) {
16+
val m = (a: _*) // error: Cannot return repeated parameter type Int*
17+
}
18+

0 commit comments

Comments
 (0)