Skip to content

Commit 9584970

Browse files
committed
Revert "Adapt given instance definitions"
This reverts commit b0933d3. # Conflicts: # compiler/src/dotty/tools/dotc/parsing/Parsers.scala # docs/docs/internals/syntax.md # docs/docs/reference/contextual/givens.md
1 parent b57b985 commit 9584970

File tree

4 files changed

+22
-14
lines changed

4 files changed

+22
-14
lines changed

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

Lines changed: 16 additions & 9 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] (AnnotType [‘=’ Expr] | ConstrApps TemplateBody)
3552+
/** GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | StructuralInstance)
35533553
* GivenSig ::= [id] [DefTypeParamClause] {UsingParamClauses} ‘:’
35543554
*/
35553555
def givenDef(start: Offset, mods: Modifiers, givenMod: Mod) = atSpan(start, nameStart) {
@@ -3569,9 +3569,8 @@ object Parsers {
35693569
if !(name.isEmpty && noParams) then accept(COLON)
35703570
val parents =
35713571
if isSimpleLiteral then rejectWildcardType(annotType()) :: Nil
3572-
else constrApps()
3572+
else constrApp() :: withConstrApps()
35733573
val parentsIsType = parents.length == 1 && parents.head.isType
3574-
newLineOptWhenFollowedBy(LBRACE)
35753574
if in.token == EQUALS && parentsIsType then
35763575
accept(EQUALS)
35773576
mods1 |= Final
@@ -3580,17 +3579,17 @@ object Parsers {
35803579
ValDef(name, parents.head, subExpr())
35813580
else
35823581
DefDef(name, joinParams(tparams, vparamss), parents.head, subExpr())
3583-
else if isTemplateBodyStart then
3582+
else if in.token != WITH && parentsIsType then
3583+
if name.isEmpty then
3584+
syntaxError(em"anonymous given cannot be abstract")
3585+
DefDef(name, joinParams(tparams, vparamss), parents.head, EmptyTree)
3586+
else
35843587
val tparams1 = tparams.map(tparam => tparam.withMods(tparam.mods | PrivateLocal))
35853588
val vparamss1 = vparamss.map(_.map(vparam =>
35863589
vparam.withMods(vparam.mods &~ Param | ParamAccessor | Protected)))
3587-
val templ = templateBodyOpt(makeConstructor(tparams1, vparamss1), parents, Nil)
3590+
val templ = withTemplate(makeConstructor(tparams1, vparamss1), parents)
35883591
if noParams then ModuleDef(name, templ)
35893592
else TypeDef(name.toTypeName, templ)
3590-
else
3591-
if name.isEmpty then
3592-
syntaxError(em"anonymous given cannot be abstract")
3593-
DefDef(name, joinParams(tparams, vparamss), parents.head, EmptyTree)
35943593
end gdef
35953594
finalizeDef(gdef, mods1, start)
35963595
}
@@ -3780,6 +3779,14 @@ object Parsers {
37803779
vd
37813780
}
37823781

3782+
/** with Template, with EOL <indent> interpreted */
3783+
def withTemplate(constr: DefDef, parents: List[Tree]): Template =
3784+
if in.token != WITH then syntaxError(em"`with` expected")
3785+
possibleTemplateStart() // consumes a WITH token
3786+
val (self, stats) = templateBody()
3787+
Template(constr, parents, Nil, self, stats)
3788+
.withSpan(Span(constr.span.orElse(parents.head.span).start, in.lastOffset))
3789+
37833790
/* -------- STATSEQS ------------------------------------------- */
37843791

37853792
/** Create a tree representing a packaging */

docs/docs/internals/syntax.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,9 @@ 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] (AnnotType [‘=’ Expr] | ConstrApps TemplateBody)
388+
GivenDef ::= [GivenSig] (AnnotType [‘=’ Expr] | StructuralInstance)
389389
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’ -- one of `id`, `DefParamClause`, `UsingParamClause` must be present
390+
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} TemplateBody
390391
Extension ::= ‘extension’ [DefTypeParamClause] ‘(’ DefParam ‘)’
391392
{UsingParamClause}] ExtMethods
392393
ExtMethods ::= ExtMethod | [nl] ‘{’ ExtMethod {semi ExtMethod ‘}’

docs/docs/reference/contextual/givens.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,11 @@ Here is the syntax for given instances:
153153
```
154154
TmplDef ::= ...
155155
| ‘given’ GivenDef
156-
GivenDef ::= [GivenSig] ConstrApps TemplateBody
156+
GivenDef ::= [GivenSig] StructuralInstance
157157
| [GivenSig] AnnotType ‘=’ Expr
158158
| [GivenSig] AnnotType
159159
GivenSig ::= [id] [DefTypeParamClause] {UsingParamClause} ‘:’
160+
StructuralInstance ::= ConstrApp {‘with’ ConstrApp} ‘with’ TemplateBody
160161
```
161162

162163
A given instance starts with the reserved word `given` and an optional _signature_. The signature

tests/pos-custom-args/erased/i7868.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ object Coproduct {
1212

1313
object At {
1414

15-
given atHead[Head, Tail]: At[Head +: Tail, Head, 0] {
15+
given atHead[Head, Tail]: At[Head +: Tail, Head, 0] with {
1616
def cast: Head <:< Head +: Tail = summon[Head <:< Head +: Tail]
1717
}
1818

1919
given atTail[Head, Tail, Value, NextIndex <: Int]
2020
(using atNext: At[Tail, Value, NextIndex])
21-
: At[Head +: Tail, Value, S[NextIndex]]
22-
with
21+
: At[Head +: Tail, Value, S[NextIndex]] with
2322
val cast: Value <:< Head +: Tail = atNext.cast
2423

2524
given [A](using A): (() => A) = { () => summon[A]}

0 commit comments

Comments
 (0)