Skip to content
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

Keep tree of type ascriptions of quote pattern splices #18412

Merged
merged 1 commit into from
Aug 17, 2023
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
11 changes: 1 addition & 10 deletions compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -492,18 +492,9 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
)
case Block(_, Closure(_, _, tpt)) if ExpandSAMs.needsWrapperClass(tpt.tpe) =>
superAcc.withInvalidCurrentClass(super.transform(tree))
case _: Quote =>
case _: Quote | _: QuotePattern =>
ctx.compilationUnit.needsStaging = true
super.transform(tree)
case _: QuotePattern =>
if !ctx.reporter.errorsReported then
Checking.checkAppliedTypesIn(TypeTree(tree.tpe).withSpan(tree.span))
ctx.compilationUnit.needsStaging = true
super.transform(tree)
case tree: SplicePattern =>
if !ctx.reporter.errorsReported then
Checking.checkAppliedTypesIn(TypeTree(tree.tpe).withSpan(tree.span))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a workaround that we added to check the types of the splices because we removed them in QuotesAndSplices. Now those types are checked directly on their ASTs.

super.transform(tree)
case tree =>
super.transform(tree)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ object SpaceEngine {
def isIrrefutableQuotePattern(pat: tpd.QuotePattern, pt: Type)(using Context): Boolean = {
if pat.body.isType then pat.bindings.isEmpty && pt =:= pat.tpe
else pat.body match
case _: SplicePattern => pat.bindings.isEmpty && pt <:< pat.tpe
case _: SplicePattern | Typed(_: SplicePattern, _) => pat.bindings.isEmpty && pt <:< pat.tpe
case _ => false
}

Expand Down
6 changes: 0 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,8 @@ trait QuotesAndSplices {
pat.symbol.addAnnotation(Annotation(New(ref(defn.QuotedRuntimePatterns_fromAboveAnnot.typeRef)).withSpan(pat.span)))
allTypeBindings += pat
TypeTree(pat.symbol.typeRef).withSpan(pat.span)

case Typed(splice: SplicePattern, tpt) if !tpt.tpe.derivesFrom(defn.RepeatedParamClass) =>
// We drop the type ascription because it is redundant, the SplicePattern contains the same type
transform(tpt) // Collect type bindings
splice
case _: SplicePattern =>
tree

case _ =>
super.transform(tree)
}
Expand Down
6 changes: 3 additions & 3 deletions tests/neg-macros/quote-type-variable-no-inference-2.check
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
|
| Consider defining bounds explicitly:
| '{ type t <: Int & Double; ... }
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-2.scala:5:12 --------------------------
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-2.scala:5:20 --------------------------
5 | case '{ $_ : F[t, t]; () } => // warn // error
| ^
| Type argument t does not conform to upper bound Double in inferred type F[t, t]
| ^
| Type argument t does not conform to upper bound Double
|
| longer explanation available when compiling with `-explain`
6 changes: 3 additions & 3 deletions tests/neg-macros/quote-type-variable-no-inference-3.check
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
|
| Consider defining bounds explicitly:
| '{ type u <: Comparable[u] & Comparable[Any]; ... }
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-3.scala:5:12 --------------------------
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference-3.scala:5:20 --------------------------
5 | case '{ $_ : F[t, t]; () } => // warn // error
| ^
| Type argument t does not conform to upper bound Comparable[t] in inferred type F[t, t]
| ^
| Type argument t does not conform to upper bound Comparable[t]
|
| longer explanation available when compiling with `-explain`
10 changes: 2 additions & 8 deletions tests/neg-macros/quote-type-variable-no-inference.check
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
-- Warning: tests/neg-macros/quote-type-variable-no-inference.scala:5:17 -----------------------------------------------
5 | case '[ F[t, t] ] => // warn // error // error
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:9 -----------------------------
5 | case '[ F[t, t] ] => // warn // error // error
| ^
|Type argument t does not conform to upper bound Double in subpart F[t, t] of inferred type scala.quoted.Type[F[t, t]]
|
| longer explanation available when compiling with `-explain`
-- [E057] Type Mismatch Error: tests/neg-macros/quote-type-variable-no-inference.scala:5:15 ----------------------------
5 | case '[ F[t, t] ] => // warn // error // error
5 | case '[ F[t, t] ] => // warn // error
| ^
| Type argument t does not conform to upper bound Double
|
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/quote-type-variable-no-inference.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import scala.quoted.*

def test(x: Type[?])(using Quotes) =
x match
case '[ F[t, t] ] => // warn // error // error
case '[ F[t, t] ] => // warn // error
case '[ type u <: Int & Double; F[u, u] ] =>

type F[x <: Int, y <: Double]
26 changes: 26 additions & 0 deletions tests/pos-macros/i18409.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// scalac: -Werror -Wunused:all

import scala.quoted.*

object model {
trait Transformer[Source, Dest] {
def transform(from: Source): Dest
}
object Transformer {
trait ForProduct[A, B] extends Transformer[A, B]
}
}

object Ops {
import model.Transformer // unused import false-positive

def unapply(using Quotes)(term: quotes.reflect.Term): Option[String] = {
term.asExpr match {
case '{
($transformer: Transformer.ForProduct[a, b]).transform($appliedTo)
} =>
Some("")
case other => None
}
}
}
Loading