-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #7788: Add new syntax for conditional given instances #7794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d1579e6
Fix #7788: Add new syntax for conditional given instances
odersky 631b5fe
Tweak syntax of conditions in given instances
odersky 0e5d3bf
Convert more conditional given instances to new syntax
odersky a4b0c70
Fix curried conditional given instances
odersky dfb6996
Drop outdated notice in docs
odersky 4de44e3
Update tests/run/i7788.scala
odersky 93258da
Turn i7788.scala into a proper test
odersky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -901,6 +901,7 @@ object Parsers { | |
|
||
/** Are the next tokens a prefix of a formal parameter or given type? | ||
* @pre: current token is LPAREN | ||
* TODO: Drop once syntax has stabilized | ||
*/ | ||
def followingIsParamOrGivenType() = | ||
val lookahead = in.LookaheadScanner() | ||
|
@@ -915,6 +916,22 @@ object Parsers { | |
else false | ||
else false | ||
|
||
/** Are the next tokens a prefix of a formal parameter? | ||
* @pre: current token is LPAREN | ||
*/ | ||
def followingIsParam() = | ||
val lookahead = in.LookaheadScanner() | ||
lookahead.nextToken() | ||
if startParamTokens.contains(lookahead.token) then true | ||
else if lookahead.token == IDENTIFIER then | ||
if lookahead.name == nme.inline then | ||
lookahead.nextToken() | ||
if lookahead.token == IDENTIFIER then | ||
lookahead.nextToken() | ||
lookahead.token == COLON | ||
else false | ||
else false | ||
|
||
/** Are the next token the "GivenSig" part of a given definition, | ||
* i.e. an identifier followed by type and value parameters, followed by `:`? | ||
* @pre The current token is an identifier | ||
|
@@ -2766,15 +2783,20 @@ object Parsers { | |
def typeParamClauseOpt(ownerKind: ParamOwner.Value): List[TypeDef] = | ||
if (in.token == LBRACKET) typeParamClause(ownerKind) else Nil | ||
|
||
/** OLD: GivenTypes ::= AnnotType {‘,’ AnnotType} | ||
* NEW: GivenTypes ::= Type {‘,’ Type} | ||
*/ | ||
def givenTypes(nparams: Int, ofClass: Boolean): List[ValDef] = | ||
val tps = commaSeparated(typ) | ||
def typesToGivenParams(tps: List[Tree], ofClass: Boolean, nparams: Int): List[ValDef] = | ||
var counter = nparams | ||
def nextIdx = { counter += 1; counter } | ||
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param | ||
tps.map(makeSyntheticParameter(nextIdx, _, paramFlags | Synthetic | Given)) | ||
val tps1 = tps match | ||
case Tuple(tps1) :: Nil => tps1 | ||
case _ => tps | ||
tps1.map(makeSyntheticParameter(nextIdx, _, paramFlags | Synthetic | Given)) | ||
|
||
/** OLD: GivenTypes ::= AnnotType {‘,’ AnnotType} | ||
* NEW: GivenTypes ::= Type {‘,’ Type} | ||
*/ | ||
def givenTypes(ofClass: Boolean, nparams: Int): List[ValDef] = | ||
typesToGivenParams(commaSeparated(typ), ofClass, nparams) | ||
|
||
/** ClsParamClause ::= ‘(’ [‘erased’] ClsParams ‘)’ | ||
* GivenClsParamClause::= ‘(’ ‘given’ [‘erased’] (ClsParams | GivenTypes) ‘)’ | ||
|
@@ -2873,7 +2895,7 @@ object Parsers { | |
|| startParamTokens.contains(in.token) | ||
|| isIdent && (in.name == nme.inline || in.lookaheadIn(BitSet(COLON))) | ||
if isParams then commaSeparated(() => param()) | ||
else givenTypes(nparams, ofClass) | ||
else givenTypes(ofClass, nparams) | ||
checkVarArgsRules(clause) | ||
clause | ||
} | ||
|
@@ -3384,12 +3406,13 @@ object Parsers { | |
syntaxError(i"extension clause can only define methods", stat.span) | ||
} | ||
|
||
/** GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr | ||
* | [GivenSig ‘:’] ConstrApps [[‘with’] TemplateBody] | ||
/** GivenDef ::= [GivenSig (‘:’ | <:)] {FunArgTypes ‘=>’} AnnotType ‘=’ Expr | ||
* | [GivenSig ‘:’] {FunArgTypes ‘=>’} ConstrApps [[‘with’] TemplateBody] | ||
* | [id ‘:’] ExtParamClause {GivenParamClause} ‘extended’ ‘with’ ExtMethods | ||
* GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause} | ||
* ExtParamClause ::= [DefTypeParamClause] DefParamClause | ||
* ExtMethods ::= [nl] ‘{’ ‘def’ DefDef {semi ‘def’ DefDef} ‘}’ | ||
* TODO: cleanup once syntax has stabilized | ||
*/ | ||
def givenDef(start: Offset, mods: Modifiers, instanceMod: Mod) = atSpan(start, nameStart) { | ||
var mods1 = addMod(mods, instanceMod) | ||
|
@@ -3414,13 +3437,18 @@ object Parsers { | |
templ.body.foreach(checkExtensionMethod(tparams, _)) | ||
ModuleDef(name, templ) | ||
else | ||
val hasLabel = !name.isEmpty && in.token == COLON | ||
if hasLabel then in.nextToken() | ||
var hasLabel = false | ||
def skipColon() = | ||
if !hasLabel && in.token == COLON then | ||
hasLabel = true | ||
in.nextToken() | ||
if !name.isEmpty then skipColon() | ||
val tparams = typeParamClauseOpt(ParamOwner.Def) | ||
if !tparams.isEmpty then skipColon() | ||
val paramsStart = in.offset | ||
val vparamss = | ||
var vparamss = | ||
if in.token == LPAREN && followingIsParamOrGivenType() | ||
then paramClauses() | ||
then paramClauses() // todo: ONLY admit a single paramClause | ||
else Nil | ||
val isExtension = isIdent(nme.extended) | ||
def checkAllGivens(vparamss: List[List[ValDef]], what: String) = | ||
|
@@ -3440,19 +3468,45 @@ object Parsers { | |
stats.foreach(checkExtensionMethod(tparams, _)) | ||
ModuleDef(name, Template(makeConstructor(tparams, vparamss), Nil, Nil, self, stats)) | ||
else | ||
checkAllGivens(vparamss, "parameter of given instance") | ||
def makeGiven(params: List[ValDef]): List[ValDef] = | ||
params.map(param => param.withMods(param.mods | Given)) | ||
def conditionalParents(): List[Tree] = | ||
accept(ARROW) | ||
if in.token == LPAREN && followingIsParam() then | ||
vparamss = vparamss :+ makeGiven(paramClause(vparamss.flatten.length)) | ||
conditionalParents() | ||
else | ||
val constrs = constrApps(commaOK = true, templateCanFollow = true) | ||
if in.token == ARROW && constrs.forall(_.isType) then | ||
vparamss = vparamss | ||
:+ typesToGivenParams(constrs, ofClass = false, vparamss.flatten.length) | ||
conditionalParents() | ||
else constrs | ||
|
||
val isConditional = | ||
in.token == ARROW | ||
&& vparamss.length == 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there should probably be a neg test for curried param lists: trait Show[-A] with
def show(a:A): String
given Show[String] = x => x
given Show[Int] = _.toString
given [A,B]: (sA: Show[A])(sB: Show[B]) => Show[(A,B)] = (a,b) => s"(${sA.show(a)}, ${sB.show(b)})"
given [A,B,C]: (Show[A])(Show[B])(Show[C]) => Show[(A,B,C)] = (a,b,c) => s"(${summon[Show[A]].show(a)}, ${summon[Show[B]].show(b)}, ${summon[Show[C]].show(c)})"
given showEither[A,B]: (sA: Show[A])(sb: Show[B]) => Show[Either[A,B]] = _.fold(a => s"Left(${summon[Show[A]].show(a)})", b => s"Right(${summon[Show[B]].show(b)})")
@main def ShowDemo =
println(summon[Show[(Int, String)]].show(0 -> "hello")) |
||
&& (hasLabel || name.isEmpty && tparams.isEmpty) | ||
if !isConditional then checkAllGivens(vparamss, "parameter of given instance") | ||
val parents = | ||
if hasLabel then | ||
constrApps(commaOK = true, templateCanFollow = true) | ||
else if in.token == SUBTYPE then | ||
if in.token == SUBTYPE && !hasLabel then | ||
if !mods.is(Inline) then | ||
syntaxError("`<:` is only allowed for given with `inline` modifier") | ||
in.nextToken() | ||
TypeBoundsTree(EmptyTree, toplevelTyp()) :: Nil | ||
TypeBoundsTree(EmptyTree, annotType()) :: Nil | ||
else if isConditional then | ||
vparamss = vparamss.map(makeGiven) | ||
conditionalParents() | ||
else | ||
if !(name.isEmpty && tparams.isEmpty && vparamss.isEmpty) then | ||
if !hasLabel && !(name.isEmpty && tparams.isEmpty && vparamss.isEmpty) then | ||
accept(COLON) | ||
constrApps(commaOK = true, templateCanFollow = true) | ||
val constrs = constrApps(commaOK = true, templateCanFollow = true) | ||
if in.token == ARROW && vparamss.isEmpty && constrs.forall(_.isType) then | ||
vparamss = typesToGivenParams(constrs, ofClass = false, 0) :: Nil | ||
conditionalParents() | ||
else | ||
constrs | ||
|
||
if in.token == EQUALS && parents.length == 1 && parents.head.isType then | ||
in.nextToken() | ||
mods1 |= Final | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
object Test extends App { | ||
given f[H](given h: H): H = h | ||
given f[H]: (h: H) => H = h | ||
summon[Int] // error | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class A | ||
class B | ||
class C | ||
class D | ||
class E | ||
trait F | ||
|
||
given A() | ||
given B() | ||
given (A, B) => C() // this one is equivalent to ... | ||
given A, B => D(), F // ... the one without the parens | ||
given ((A, B)) => E() // to demand a tuple, add an extra pair of parens | ||
|
||
@main def Test = | ||
summon[C] | ||
summon[D] | ||
summon[F] | ||
given (A, B) = (summon[A], summon[B]) | ||
summon[E] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rules still accept
given [A]: (given showA: Show[A]) => Show[List[A]]
e.g. :There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this syntax is still ok to be alongside the other?