Skip to content

Commit 8c13610

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 7a70cb2 commit 8c13610

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2088,7 +2088,7 @@ object Parsers {
20882088
* ClsParam ::= {Annotation} [{ParamModifier} (`val' | `var') | `inline'] Param
20892089
* DefParamClause ::= [nl] `(' [FunArgMods] [DefParams] ')' | InferParamClause
20902090
* InferParamClause ::= ‘given’ (‘(’ DefParams ‘)’ | ContextTypes)
2091-
* ContextTypes ::= RefinedType {`,' RefinedType}
2091+
* ContextTypes ::= AnnotType {`,' AnnotType}
20922092
* DefParams ::= DefParam {`,' DefParam}
20932093
* DefParam ::= {Annotation} [`inline'] Param
20942094
* Param ::= id `:' ParamType [`=' Expr]
@@ -2209,7 +2209,7 @@ object Parsers {
22092209
params :: (if (lastClause) Nil else recur(firstClause = false, nparams + params.length))
22102210
}
22112211
else if (isContextual) {
2212-
val tps = commaSeparated(refinedType)
2212+
val tps = commaSeparated(() => annotType())
22132213
var counter = nparams
22142214
def nextIdx = { counter += 1; counter }
22152215
val params = tps.map(makeSyntheticParameter(nextIdx, _, Given | Implicit))
@@ -2618,8 +2618,8 @@ object Parsers {
26182618

26192619
/** InstanceDef ::= [id] InstanceParams InstanceBody
26202620
* InstanceParams ::= [DefTypeParamClause] {InferParamClause}
2621-
* InstanceBody ::= [‘of’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
2622-
* | ‘of’ Type ‘=’ Expr
2621+
* InstanceBody ::= [‘for’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
2622+
* | ‘for’ Type ‘=’ Expr
26232623
*/
26242624
def instanceDef(start: Offset, mods: Modifiers, instanceMod: Mod) = atSpan(start, nameStart) {
26252625
var mods1 = addMod(mods, instanceMod)

docs/docs/internals/syntax.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ DefParamClause ::= [nl] ‘(’ [DefParams] ‘)’ | InferParamClause
304304
InferParamClause ::= ‘given’ (‘(’ [DefParams] ‘)’ | ContextTypes)
305305
DefParams ::= DefParam {‘,’ DefParam}
306306
DefParam ::= {Annotation} [‘inline’] Param ValDef(mods, id, tpe, expr) -- point of mods at id.
307-
ContextTypes ::= RefinedType {‘,’ RefinedType}
307+
ContextTypes ::= AnnotType {‘,’ AnnotType}
308308
FunArgMods ::= { ‘implicit’ | ‘erased’ }
309309
ClosureMods ::= { ‘implicit’ | ‘erased’ | ‘given’}
310310
```
@@ -357,7 +357,6 @@ Def ::= ‘val’ PatDef
357357
| ‘var’ VarDef
358358
| ‘def’ DefDef
359359
| ‘type’ {nl} TypeDcl
360-
| ‘instance’ {nl} InstanceDef
361360
| TmplDef
362361
| INT
363362
PatDef ::= Pattern2 {‘,’ Pattern2} [‘:’ Type] ‘=’ Expr PatDef(_, pats, tpe?, expr)
@@ -380,8 +379,8 @@ ObjectDef ::= id [Template]
380379
EnumDef ::= id ClassConstr InheritClauses EnumBody EnumDef(mods, name, tparams, template)
381380
InstanceDef ::= [id] InstanceParams InstanceBody
382381
InstanceParams ::= [DefTypeParamClause] {InferParamClause}
383-
InstanceBody ::= [‘of’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
384-
| ‘of’ Type ‘=’ Expr
382+
InstanceBody ::= [‘for’ ConstrApp {‘,’ ConstrApp }] [TemplateBody]
383+
| ‘for’ Type ‘=’ Expr
385384
Template ::= InheritClauses [TemplateBody] Template(constr, parents, self, stats)
386385
InheritClauses ::= [‘extends’ ConstrApps] [‘derives’ QualId {‘,’ QualId}]
387386
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)