Skip to content

Fix #7078: Allow <: T in given alias definitions #7334

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 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -907,7 +907,7 @@ object Parsers {
lookahead.nextToken()
while lookahead.token == LPAREN || lookahead.token == LBRACKET do
lookahead.skipParens()
lookahead.token == COLON
lookahead.token == COLON || lookahead.token == SUBTYPE

/* --------- OPERAND/OPERATOR STACK --------------------------------------- */

Expand Down Expand Up @@ -3232,7 +3232,7 @@ object Parsers {
case ENUM =>
enumDef(start, posMods(start, mods | Enum))
case GIVEN =>
instanceDef(start, mods, atSpan(in.skipToken()) { Mod.Given() })
givenDef(start, mods, atSpan(in.skipToken()) { Mod.Given() })
case _ =>
syntaxErrorOrIncomplete(ExpectedStartOfTopLevelDefinition())
EmptyTree
Expand Down Expand Up @@ -3336,19 +3336,13 @@ object Parsers {
syntaxError(em"extension clause must start with a single regular parameter", start)


/** OLD:
* GivenDef ::= [id] [DefTypeParamClause] GivenBody
* GivenBody ::= [‘as ConstrApp {‘,’ ConstrApp }] {GivenParamClause} [TemplateBody]
* | ‘as’ Type {GivenParamClause} ‘=’ Expr
* | ‘(’ DefParam ‘)’ TemplateBody
* NEW:
* GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr
/** GivenDef ::= [GivenSig (‘:’ | <:)] Type ‘=’ Expr
* | [GivenSig ‘:’] [ConstrApp {‘,’ ConstrApp }] [TemplateBody]
* | [id ‘:’] [ExtParamClause] TemplateBody
* GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause}
* ExtParamClause ::= [DefTypeParamClause] DefParamClause {GivenParamClause}
*/
def instanceDef(start: Offset, mods: Modifiers, instanceMod: Mod) = atSpan(start, nameStart) {
def givenDef(start: Offset, mods: Modifiers, instanceMod: Mod) = atSpan(start, nameStart) {
var mods1 = addMod(mods, instanceMod)
val hasGivenSig = followingIsGivenSig()
val name = if isIdent && hasGivenSig then ident() else EmptyTermName
Expand Down Expand Up @@ -3382,6 +3376,11 @@ object Parsers {
Nil
else
tokenSeparated(COMMA, constrApp)
else if in.token == SUBTYPE then
if !mods.is(Inline) then
syntaxError("`<:' is only allowed for given with `inline' modifier")
in.nextToken()
TypeBoundsTree(EmptyTree, toplevelTyp()) :: Nil
else if name.isEmpty && in.token != LBRACE then
tokenSeparated(COMMA, constrApp)
else Nil
Expand All @@ -3392,7 +3391,9 @@ object Parsers {
mods1 |= Final
DefDef(name, tparams, vparamss, parents.head, subExpr())
else
//println(i"given $name $hasExtensionParams $hasGivenSig")
parents match
case TypeBoundsTree(_, _) :: _ => syntaxError("`=' expected")
case _ =>
possibleTemplateStart()
if !hasExtensionParams then
tparams = tparams.map(tparam => tparam.withMods(tparam.mods | PrivateLocal))
Expand Down
8 changes: 8 additions & 0 deletions tests/neg/i7078.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
trait A
class B extends A

given g1 <: A = B() // error: `<:' is only allowed for given with `inline' modifier // error

inline given g2 <: A // error: <: A is not a class type
def foo = 2 // error: `=' expected

7 changes: 7 additions & 0 deletions tests/pos/i7078.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
trait A
class B extends A

inline given tc <: A = B()

val x: B = summon[A]