Skip to content

Commit 81950ef

Browse files
committed
Coomputed type defs have a TypeBounds as declared result type.
1 parent 06dbeae commit 81950ef

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

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

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,11 +1062,11 @@ object Parsers {
10621062
def typeRHS(): Tree =
10631063
if (in.token == IF)
10641064
atPos(in.skipToken()) {
1065-
val cond = typeRHS()
1065+
val cond = expr()
10661066
accept(THEN)
10671067
val thenp = typeRHS()
10681068
accept(ELSE)
1069-
val elsep = expr()
1069+
val elsep = typeRHS()
10701070
If(cond, thenp, elsep)
10711071
}
10721072
else toplevelTyp()
@@ -2200,7 +2200,7 @@ object Parsers {
22002200
Block(stats, Literal(Constant(())))
22012201
}
22022202

2203-
/** TypeDcl ::= id [TypTypeParamClause] {DefParamClause} [‘:’ Type] ‘=’ TypeRHS
2203+
/** TypeDcl ::= id [TypTypeParamClause] {DefParamClause} TypeBounds ‘=’ TypeRHS
22042204
* | id [HkTypeParamClause] TypeBounds
22052205
*/
22062206
def typeDefOrDcl(start: Offset, mods: Modifiers): Tree = {
@@ -2209,23 +2209,22 @@ object Parsers {
22092209
val name = ident().toTypeName
22102210
val tparams = typeParamClauseOpt(ParamOwner.Type)
22112211
val vparamss = paramClauses(ParamOwner.Type)
2212-
val tpt = typedOpt()
2213-
val isDef = !vparamss.isEmpty || !tpt.isEmpty
2214-
in.token match {
2215-
case EQUALS =>
2212+
val isBounded = in.token == SUPERTYPE || in.token == SUBTYPE
2213+
val bounds = typeBounds()
2214+
val res =
2215+
if (in.token == EQUALS) {
22162216
in.nextToken()
22172217
val rhs = typeRHS()
2218-
val res =
2219-
if (isTypeDefRHS(rhs) || isDef) DefDef(name, tparams, vparamss, tpt, rhs)
2220-
else TypeDef(name, lambdaAbstract(tparams, rhs))
2221-
res.withMods(mods).setComment(in.getDocComment(start))
2222-
case SUPERTYPE | SUBTYPE | SEMI | NEWLINE | NEWLINES | COMMA | RBRACE | EOF =>
2223-
if (isDef) syntaxError("`=' expected")
2224-
TypeDef(name, lambdaAbstract(tparams, typeBounds())).withMods(mods).setComment(in.getDocComment(start))
2225-
case _ =>
2226-
syntaxErrorOrIncomplete(ExpectedTypeBoundOrEquals(in.token))
2227-
EmptyTree
2228-
}
2218+
if (isTypeDefRHS(rhs) || isBounded || vparamss.nonEmpty)
2219+
DefDef(name, tparams, vparamss, bounds, rhs)
2220+
else
2221+
TypeDef(name, lambdaAbstract(tparams, rhs))
2222+
}
2223+
else {
2224+
if (vparamss.nonEmpty) syntaxError("`=' expected")
2225+
TypeDef(name, lambdaAbstract(tparams, bounds))
2226+
}
2227+
res.withMods(mods).setComment(in.getDocComment(start))
22292228
}
22302229
}
22312230

docs/docs/internals/syntax.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,9 @@ ValDcl ::= ids ‘:’ Type
313313
VarDcl ::= ids ‘:’ Type PatDef(_, ids, tpe, EmptyTree)
314314
DefDcl ::= DefSig [‘:’ Type] DefDef(_, name, tparams, vparamss, tpe, EmptyTree)
315315
DefSig ::= id [DefTypeParamClause] DefParamClauses
316-
TypeDcl ::= id [TypTypeParamClause] {DefParamClause} [‘:’ Type] TypeDefTree(_, name, tparams, tpt)
316+
TypeDcl ::= id [TypTypeParamClause] {DefParamClause} TypeBounds DefDef(name, tparams, vparamss, bounds, rhs)
317317
‘=’ TypeRHS
318-
| id [HkTypeParamClause] TypeBounds TypeDefTree(_, name, tparams, bounds)
318+
| id [HkTypeParamClause] TypeBounds TypeDef(name, tparams, bounds)
319319
320320
Def ::= ‘val’ PatDef
321321
| ‘var’ VarDef

tests/run/typelevel1.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ object Test extends App {
4848
val zs1 = zs(1)
4949
val zs2 = zs(2)
5050
val zs3 = zs(3)
51-
def zs4 = zs(4)
51+
val zs4 = zs(4)
52+
def zs5 = zs(5)
5253
}

0 commit comments

Comments
 (0)