Skip to content

Commit d31eba1

Browse files
committed
Restrict given instance syntax
If a type is implemented, it must be an `AnnotType`, which is the only kind of type allowed in a ConstrApp or extends clause.
1 parent b0933d3 commit d31eba1

File tree

3 files changed

+7
-5
lines changed

3 files changed

+7
-5
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3549,7 +3549,7 @@ object Parsers {
35493549
syntaxError(i"extension clause can only define methods", stat.span)
35503550
}
35513551

3552-
/** GivenDef ::= [GivenSig] (Type [‘=’ Expr] | ConstrApps TemplateBody)
3552+
/** GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | ConstrApps TemplateBody)
35533553
* GivenSig ::= [id] [DefTypeParamClause] {UsingParamClauses} ‘:’
35543554
*/
35553555
def givenDef(start: Offset, mods: Modifiers, givenMod: Mod) = atSpan(start, nameStart) {
@@ -3567,7 +3567,9 @@ object Parsers {
35673567
newLinesOpt()
35683568
val noParams = tparams.isEmpty && vparamss.isEmpty
35693569
if !(name.isEmpty && noParams) then accept(COLON)
3570-
val parents = if isSimpleLiteral then toplevelTyp() :: Nil else constrApps()
3570+
val parents =
3571+
if isSimpleLiteral then rejectWildcardType(annotType()) :: Nil
3572+
else constrApps()
35713573
val parentsIsType = parents.length == 1 && parents.head.isType
35723574
newLineOptWhenFollowedBy(LBRACE)
35733575
if in.token == EQUALS && parentsIsType then

docs/docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ ClassConstr ::= [ClsTypeParamClause] [ConstrMods] ClsParamClauses
385385
ConstrMods ::= {Annotation} [AccessModifier]
386386
ObjectDef ::= id [Template] ModuleDef(mods, name, template) // no constructor
387387
EnumDef ::= id ClassConstr InheritClauses EnumBody
388-
GivenDef ::= [GivenSig] (Type [‘=’ Expr] | ConstrApps TemplateBody)
388+
GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | ConstrApps TemplateBody)
389389
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
390390
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
391391
{UsingParamClause}] ExtMethods

docs/docs/reference/contextual/givens.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ Here is the syntax for given instances:
154154
TmplDef ::= ...
155155
| ‘given’ GivenDef
156156
GivenDef ::= [GivenSig] ConstrApps TemplateBody
157-
| [GivenSig] Type ‘=’ Expr
158-
| [GivenSig] Type
157+
| [GivenSig] AnnotType ‘=’ Expr
158+
| [GivenSig] AnnotType
159159
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’
160160
```
161161

0 commit comments

Comments
 (0)