Skip to content

Commit c2bab93

Browse files
committed
Disallow refinements in GivenTypes
Test case is i2567.scala. We would like to write ``` class Foo given T { ... } ``` But previously this mistook the class body as a refinement for `T`.
1 parent b40b519 commit c2bab93

File tree

4 files changed

+32
-12
lines changed

4 files changed

+32
-12
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,12 +2082,12 @@ object Parsers {
20822082
if (in.token == LBRACKET) typeParamClause(ownerKind) else Nil
20832083

20842084
/** ClsParamClause ::= [nl] [‘erased’] ‘(’ [ClsParams] ‘)’
2085-
* | ‘given’ [‘erased’] (‘(’ ClsParams ‘)’ | ContextTypes)
2085+
* | ‘given’ [‘erased’] (‘(’ ClsParams ‘)’ | GivenTypes)
20862086
* ClsParams ::= ClsParam {`' ClsParam}
20872087
* ClsParam ::= {Annotation} [{ParamModifier} (`val' | `var') | `inline'] Param
20882088
* DefParamClause ::= [nl] [‘erased’] ‘(’ [DefParams] ‘)’ | GivenParamClause
2089-
* GivenParamClause ::= ‘given’ [‘erased’] (‘(’ DefParams ‘)’ | ContextTypes)
2090-
* ContextTypes ::= RefinedType {`,' RefinedType}
2089+
* GivenParamClause ::= ‘given’ [‘erased’] (‘(’ DefParams ‘)’ | GivenTypes)
2090+
* GivenTypes ::= RefinedType {`,' RefinedType}
20912091
* DefParams ::= DefParam {`,' DefParam}
20922092
* DefParam ::= {Annotation} [`inline'] Param
20932093
* Param ::= id `:' ParamType [`=' Expr]
@@ -2203,7 +2203,7 @@ object Parsers {
22032203
params :: (if (lastClause) Nil else recur(firstClause = false, nparams + params.length))
22042204
}
22052205
else if (isContextual) {
2206-
val tps = commaSeparated(refinedType)
2206+
val tps = commaSeparated(() => annotType())
22072207
var counter = nparams
22082208
def nextIdx = { counter += 1; counter }
22092209
val params = tps.map(makeSyntheticParameter(nextIdx, _, Given | Implicit))
@@ -2612,8 +2612,8 @@ object Parsers {
26122612

26132613
/** InstanceDef ::= [id] InstanceParams InstanceBody
26142614
* InstanceParams ::= [DefTypeParamClause] {GivenParamClause}
2615-
* InstanceBody ::= [‘of’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
2616-
* | ‘of’ Type ‘=’ Expr
2615+
* InstanceBody ::= [‘for’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
2616+
* | ‘for’ Type ‘=’ Expr
26172617
*/
26182618
def instanceDef(start: Offset, mods: Modifiers, instanceMod: Mod) = atSpan(start, nameStart) {
26192619
var mods1 = addMod(mods, instanceMod)

docs/docs/internals/syntax.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ HkTypeParam ::= {Annotation} [‘+’ | ‘-’] (Id[HkTypeParamClause] |
292292
293293
ClsParamClauses ::= {ClsParamClause} [[nl] ‘(’ [‘implicit’] ClsParams ‘)’]
294294
ClsParamClause ::= [nl] [‘erased’] ‘(’ [ClsParams] ‘)’
295-
| ‘given’ [‘erased’] (‘(’ ClsParams ‘)’ | ContextTypes)
295+
| ‘given’ [‘erased’] (‘(’ ClsParams ‘)’ | GivenTypes)
296296
ClsParams ::= ClsParam {‘,’ ClsParam}
297297
ClsParam ::= {Annotation} ValDef(mods, id, tpe, expr) -- point of mods on val/var
298298
[{Modifier} (‘val’ | ‘var’) | ‘inline’] Param
@@ -301,10 +301,10 @@ Param ::= id ‘:’ ParamType [‘=’ Expr]
301301
302302
DefParamClauses ::= {DefParamClause} [[nl] ‘(’ [‘implicit’] DefParams ‘)’]
303303
DefParamClause ::= [nl] [‘erased’] ‘(’ [DefParams] ‘)’ | GivenParamClause
304-
GivenParamClause ::= ‘given’ [‘erased’] (‘(’ DefParams ‘)’ | ContextTypes)
304+
GivenParamClause ::= ‘given’ [‘erased’] (‘(’ DefParams ‘)’ | GivenTypes)
305305
DefParams ::= DefParam {‘,’ DefParam}
306306
DefParam ::= {Annotation} [‘inline’] Param ValDef(mods, id, tpe, expr) -- point of mods at id.
307-
ContextTypes ::= RefinedType {‘,’ RefinedType}
307+
GivenTypes ::= AnnotType {‘,’ AnnotType}
308308
ClosureMods ::= { ‘implicit’ | ‘erased’ | ‘given’}
309309
```
310310

@@ -356,7 +356,6 @@ Def ::= ‘val’ PatDef
356356
| ‘var’ VarDef
357357
| ‘def’ DefDef
358358
| ‘type’ {nl} TypeDcl
359-
| ‘instance’ {nl} InstanceDef
360359
| TmplDef
361360
| INT
362361
PatDef ::= Pattern2 {‘,’ Pattern2} [‘:’ Type] ‘=’ Expr PatDef(_, pats, tpe?, expr)
@@ -379,8 +378,8 @@ ObjectDef ::= id [Template]
379378
EnumDef ::= id ClassConstr InheritClauses EnumBody EnumDef(mods, name, tparams, template)
380379
InstanceDef ::= [id] InstanceParams InstanceBody
381380
InstanceParams ::= [DefTypeParamClause] {GivenParamClause}
382-
InstanceBody ::= [‘of’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
383-
| ‘of’ Type ‘=’ Expr
381+
InstanceBody ::= [‘for’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
382+
| ‘for’ Type ‘=’ Expr
384383
Template ::= InheritClauses [TemplateBody] Template(constr, parents, self, stats)
385384
InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
386385
ConstrApps ::= ConstrApp {‘with’ ConstrApp}

tests/run/i2567.check

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
hi
2+
hi
3+
hi
4+
hi
5+
hi
6+
hi

tests/run/i2567.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class TC
2+
implied tc for TC
3+
4+
class Foo given TC {
5+
println("hi")
6+
}
7+
8+
object Test extends App {
9+
new Foo
10+
new Foo given tc
11+
new Foo()
12+
new Foo() given tc
13+
Foo()
14+
Foo() given tc
15+
}

0 commit comments

Comments
 (0)