Skip to content

Improve type test error message position #12255

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
Apr 28, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ object TypeTestsCasts {
val argType = tree.args.head.tpe
val isTrusted = tree.hasAttachment(PatternMatcher.TrustedTypeTestKey)
if (!isTrusted && !checkable(expr.tpe, argType, tree.span))
report.warning(i"the type test for $argType cannot be checked at runtime", tree.srcPos)
report.warning(i"the type test for $argType cannot be checked at runtime", expr.srcPos)
transformTypeTest(expr, tree.args.head.tpe, flagUnrelated = true)
}
else if (sym.isTypeCast)
Expand Down
8 changes: 8 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i12253.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Error: tests/neg-custom-args/fatal-warnings/i12253.scala:11:10 ------------------------------------------------------
11 | case extractors.InlinedLambda(_, Select(_, name)) => Expr(name) // error // error
| ^
| the type test for extractors.q2.reflect.Term cannot be checked at runtime
-- Error: tests/neg-custom-args/fatal-warnings/i12253.scala:11:38 ------------------------------------------------------
11 | case extractors.InlinedLambda(_, Select(_, name)) => Expr(name) // error // error
| ^
| the type test for q1.reflect.Select cannot be checked at runtime
Copy link
Contributor

Choose a reason for hiding this comment

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

For future consideration: Maybe it's better for the unapplys to take a less specific type to avoid such warnings.

28 changes: 28 additions & 0 deletions tests/neg-custom-args/fatal-warnings/i12253.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import scala.quoted.{given, *}
import deriving.*, compiletime.*

object MacroUtils:
transparent inline def extractNameFromSelector[To, T](inline code: To => T) = ${extractNameFromSelectorImpl('code)}

def extractNameFromSelectorImpl[To: Type, T: Type](code: Expr[To => T])(using q1: Quotes): Expr[String] =
import quotes.reflect.*
val extractors = new Extractors
code.asTerm match
case extractors.InlinedLambda(_, Select(_, name)) => Expr(name) // error // error
case t => report.throwError(s"Illegal argument to extractor: ${code.show}, in tasty: $t")

class Extractors(using val q2: Quotes):
//attempt to strip away consecutive inlines in AST and extract only final lambda
import quotes.reflect.*

object InlinedLambda:
def unapply(arg: Term): Option[(List[ValDef], Term)] =
arg match
case Inlined(_, _, Lambda(vals, term)) => Some((vals, term))
case Inlined(_, _, nested) => InlinedLambda.unapply(nested)
case t => None
end InlinedLambda

end Extractors
end MacroUtils