Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 63f88a7

Browse files
committedApr 5, 2023
Warn on use of inferred quote type variable bounds
This kind of inference is not reliable in general. We can only consider the bounds from type constructor where the type variable is defined but any other constraints are ignored. Therefore it is not possible to properly infer the type bounds of the type variable. The solution is simple, write the bounds explicitly and just check that those bounds conform to the use site of the type variable.
1 parent d4a8600 commit 63f88a7

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed
 

‎compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ trait QuotesAndSplices {
160160
case _ => TypeBounds.empty
161161
val typeSym = newSymbol(spliceOwner(ctx), name, EmptyFlags, typeSymInfo, NoSymbol, tree.span)
162162
typeSym.addAnnotation(Annotation(New(ref(defn.QuotedRuntimePatterns_patternTypeAnnot.typeRef)).withSpan(tree.span)))
163+
if !(typeSymInfo =:= TypeBounds.empty) then
164+
report.warning(em"Type variable `$tree` has partially inferred bounds$pt.\n\nConsider defining bounds explicitly `'{ $typeSym$pt; ... }`", tree.srcPos)
163165
val pat = typedPattern(expr, defn.QuotedTypeClass.typeRef.appliedTo(typeSym.typeRef))(
164166
using spliceContext.retractMode(Mode.QuotedPattern).withOwner(spliceOwner(ctx)))
165167
pat.select(tpnme.Underlying)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- Error: tests/neg-custom-args/fatal-warnings/quote-type-var-with-bounds.scala:9:18 -----------------------------------
2+
9 | case '{ $x: C[t] } => // error
3+
| ^
4+
| Type variable `t` has partially inferred bounds <: Int.
5+
|
6+
| Consider defining bounds explicitly `'{ type t <: Int; ... }`
7+
-- Error: tests/neg-custom-args/fatal-warnings/quote-type-var-with-bounds.scala:10:18 ----------------------------------
8+
10 | case '{ $x: D[t] } => // error
9+
| ^
10+
| Type variable `t` has partially inferred bounds >: Null <: String.
11+
|
12+
| Consider defining bounds explicitly `'{ type t >: Null <: String; ... }`
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.quoted.*
2+
3+
class C[T <: Int]
4+
class D[T >: Null <: String]
5+
6+
def test(e: Expr[Any])(using Quotes) =
7+
e match
8+
case '{ $x: t } =>
9+
case '{ $x: C[t] } => // error
10+
case '{ $x: D[t] } => // error
11+
case '{ type t <: Int; $x: C[`t`] } =>

0 commit comments

Comments
 (0)
Please sign in to comment.