-
Notifications
You must be signed in to change notification settings - Fork 1.1k
SIP-53 - Quote pattern explicit type variable syntax #17362
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -1737,8 +1737,39 @@ object Parsers { | |
}) | ||
else t | ||
|
||
/** The block in a quote or splice */ | ||
def stagedBlock() = inBraces(block(simplify = true)) | ||
/** TypeBlock ::= {TypeBlockStat semi} Type | ||
*/ | ||
def typeBlock(): Tree = | ||
typeBlockStats() match | ||
case Nil => typ() | ||
case tdefs => Block(tdefs, typ()) | ||
|
||
def typeBlockStats(): List[Tree] = | ||
val tdefs = new ListBuffer[Tree] | ||
while in.token == TYPE do tdefs += typeBlockStat() | ||
tdefs.toList | ||
|
||
/** TypeBlockStat ::= ‘type’ {nl} TypeDcl | ||
*/ | ||
def typeBlockStat(): Tree = | ||
val mods = defAnnotsMods(BitSet()) | ||
val tdef = typeDefOrDcl(in.offset, in.skipToken(mods)) | ||
if in.token == SEMI then in.nextToken() | ||
if in.isNewLine then in.nextToken() | ||
tdef | ||
|
||
/** Quoted ::= ‘'’ ‘{’ Block ‘}’ | ||
* | ‘'’ ‘[’ TypeBlock ‘]’ | ||
*/ | ||
def quote(inPattern: Boolean): Tree = | ||
atSpan(in.skipToken()) { | ||
withinStaged(StageKind.Quoted | (if (inPattern) StageKind.QuotedPattern else 0)) { | ||
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. Is it possible to be StageKind.QuotedPattern but not StageKind.Quoted? 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. No. It would be possible to make all |
||
val body = | ||
if (in.token == LBRACKET) inBrackets(typeBlock()) | ||
else inBraces(block(simplify = true)) | ||
Quote(body, Nil) | ||
} | ||
} | ||
|
||
/** ExprSplice ::= ‘$’ spliceId -- if inside quoted block | ||
* | ‘$’ ‘{’ Block ‘}’ -- unless inside quoted pattern | ||
|
@@ -1754,7 +1785,8 @@ object Parsers { | |
val expr = | ||
if (in.name.length == 1) { | ||
in.nextToken() | ||
withinStaged(StageKind.Spliced)(if (inPattern) inBraces(pattern()) else stagedBlock()) | ||
val inPattern = (staged & StageKind.QuotedPattern) != 0 | ||
withinStaged(StageKind.Spliced)(inBraces(if inPattern then pattern() else block(simplify = true))) | ||
} | ||
else atSpan(in.offset + 1) { | ||
val id = Ident(in.name.drop(1)) | ||
|
@@ -2477,14 +2509,7 @@ object Parsers { | |
canApply = false | ||
blockExpr() | ||
case QUOTE => | ||
atSpan(in.skipToken()) { | ||
withinStaged(StageKind.Quoted | (if (location.inPattern) StageKind.QuotedPattern else 0)) { | ||
val body = | ||
if (in.token == LBRACKET) inBrackets(typ()) | ||
else stagedBlock() | ||
Quote(body, Nil) | ||
} | ||
} | ||
quote(location.inPattern) | ||
case NEW => | ||
canApply = false | ||
newExpr() | ||
|
@@ -3758,6 +3783,8 @@ object Parsers { | |
else makeTypeDef(bounds) | ||
case SEMI | NEWLINE | NEWLINES | COMMA | RBRACE | OUTDENT | EOF => | ||
makeTypeDef(typeBounds()) | ||
case _ if (staged & StageKind.QuotedPattern) != 0 => | ||
makeTypeDef(typeBounds()) | ||
case _ => | ||
syntaxErrorOrIncomplete(ExpectedTypeBoundOrEquals(in.token)) | ||
return EmptyTree // return to avoid setting the span to EmptyTree | ||
|
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
9 changes: 9 additions & 0 deletions
9
tests/neg-custom-args/no-experimental/sip-53-exprimental-a.scala
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,9 @@ | ||
import scala.quoted.* | ||
|
||
def foo(using Quotes): Unit = | ||
(??? : Type[?]) match | ||
case '[ (t, t, t) ] => // error // error | ||
'{ ??? : Any } match | ||
case '{ type u; $x: u } => // error | ||
case '{ type u; ($ls: List[u]).map($f: u => Int) } => // error // error | ||
|
8 changes: 8 additions & 0 deletions
8
tests/neg-custom-args/no-experimental/sip-53-exprimental-b.scala
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,8 @@ | ||
import scala.quoted.* | ||
|
||
def empty[K <: AnyKind : Type](using Quotes): Type[?] = | ||
Type.of[K] match | ||
case '[type t; `t`] => Type.of[t] // error | ||
case '[type f[X]; `f`] => Type.of[f] // error | ||
case '[type f[X <: Int, Y]; `f`] => Type.of[f] // error | ||
case '[type k <: AnyKind; `k` ] => Type.of[k] // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import scala.quoted.* | ||
def types(t: Type[?])(using Quotes) = t match { | ||
case '[ type t; Int ] => | ||
case '[ type t <: Int; Int ] => | ||
case '[ type t >: 1 <: Int; Int ] => | ||
case '[ type t = Int; Int ] => // error | ||
case '[ type t = scala.Int; Int ] => // error | ||
case '[ type f[t] <: List[Any]; Int ] => | ||
case '[ type f[t <: Int] <: List[Any]; Int ] => | ||
case '[ type f[t] = List[Any]; Int ] => // error | ||
} | ||
|
||
def expressions(x: Expr[Any])(using Quotes) = x match { | ||
case '{ type t; () } => | ||
case '{ type t <: Int; () } => | ||
case '{ type t >: 1 <: Int; () } => | ||
case '{ type t = Int; () } => // error | ||
case '{ type t = scala.Int; () } => // error | ||
case '{ type f[t] <: List[Any]; () } => | ||
case '{ type f[t <: Int] <: List[Any]; () } => | ||
case '{ type f[t] = List[Any]; () } => // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
-- Warning: tests/neg-macros/quote-type-variable-no-inference.scala:5:17 ----------------------------------------------- | ||
5 | case '[ F[t, t] ] => // warn // error | ||
| ^ | ||
| Ignored bound <: Double | ||
| | ||
| Consider defining bounds explicitly `'{ type t <: Int & Double; ... }` | ||
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference.scala:5:15 ---------------------------- | ||
5 | case '[ F[t, t] ] => // warn // error | ||
| ^ | ||
| Type argument t does not conform to upper bound Double | ||
| | ||
| longer explanation available when compiling with `-explain` |
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,8 @@ | ||
import scala.quoted.* | ||
|
||
def test(x: Type[?])(using Quotes) = | ||
x match | ||
case '[ F[t, t] ] => // warn // error | ||
case '[ type u <: Int & Double; F[u, u] ] => | ||
|
||
type F[x <: Int, y <: Double] |
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
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.
Could this be:
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.
That does not work.