Skip to content

Commit 12a25f2

Browse files
author
Niklas Vest
committed
Place warning at annotation arg literal
1 parent 2a21f02 commit 12a25f2

File tree

4 files changed

+40
-58
lines changed

4 files changed

+40
-58
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,12 +2391,9 @@ import ast.tpd
23912391
def explain = ""
23922392
}
23932393

2394-
class InvalidReferenceInImplicitNotFoundAnnotation(typeVar: String,full: String, owner: String)(using Context)
2394+
class InvalidReferenceInImplicitNotFoundAnnotation(typeVar: String, owner: String)(using Context)
23952395
extends ReferenceMsg(InvalidReferenceInImplicitNotFoundAnnotationID) {
2396-
def msg = em"""|Invalid reference to a type variable "${hl(typeVar)}" found
2397-
| in the user-defined error message ${hl("\"" + full + "\"")}
2398-
| in the @${ctx.definitions.ImplicitNotFoundAnnot.name.show} annotation argument.
2399-
|
2396+
def msg = em"""|Invalid reference to a type variable "${hl(typeVar)}" found in the annotation argument.
24002397
|The variable does not occur in the signature of ${hl(owner)}.
24012398
|""".stripMargin
24022399
def explain = ""

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,14 +936,14 @@ object RefChecks {
936936
private def checkImplicitNotFoundAnnotation(tree: Template, sd: SymDenotation)(using Context): Unit =
937937
for
938938
annotation <- sd.annotations if annotation.symbol == ctx.definitions.ImplicitNotFoundAnnot
939-
arg <- annotation.arguments.collect { case Literal(c: Constant) => c.stringValue }
939+
(arg, l) <- annotation.arguments.collect { case l@Literal(c: Constant) => (c.stringValue, l) }
940940
do
941941
// matches quoted references such as "${(A)}", "${(Abc)}", etc.
942942
val reference = """(?<=\$\{)[a-zA-Z]+(?=\})""".r
943943
val matches = reference.findAllMatchIn(arg)
944944
for Match(ref) <- matches if !sd.typeParams.exists(_.denot.name.show == ref) do
945-
val msg = InvalidReferenceInImplicitNotFoundAnnotation(ref, arg, sd.name.show)
946-
val pos = ctx.source.atSpan(sd.symbol.span)
945+
val msg = InvalidReferenceInImplicitNotFoundAnnotation(ref, sd.name.show)
946+
val pos = ctx.source.atSpan(l.span)
947947
report.warning(msg, pos)
948948

949949
}
Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,25 @@
1-
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:6:6 ----------------------------------------
2-
6 |class ShouldWarn1[A] // error
3-
| ^
4-
| Invalid reference to a type variable "B" found
5-
| in the user-defined error message "An implicit ShouldWarn1[${B}] is not in scope"
6-
| in the @implicitNotFound annotation argument.
7-
|
8-
| The variable does not occur in the signature of ShouldWarn1.
9-
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:10:6 ---------------------------------------
10-
10 |trait ShouldWarn2[B] // error
11-
| ^
12-
| Invalid reference to a type variable "A" found
13-
| in the user-defined error message "An implicit ShouldWarn2[${A}] is not in scope"
14-
| in the @implicitNotFound annotation argument.
15-
|
16-
| The variable does not occur in the signature of ShouldWarn2.
17-
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:14:6 ---------------------------------------
18-
14 |trait ShouldWarn3[B, C] // error
19-
| ^
20-
| Invalid reference to a type variable "A" found
21-
| in the user-defined error message "An implicit ShouldWarn3[${A},${B}] is not in scope"
22-
| in the @implicitNotFound annotation argument.
23-
|
24-
| The variable does not occur in the signature of ShouldWarn3.
25-
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:18:6 ---------------------------------------
26-
18 |class ShouldWarn4[C, D] // error
27-
| ^
28-
| Invalid reference to a type variable "A" found
29-
| in the user-defined error message "An implicit ShouldWarn4[${A},${B}] is not in scope"
30-
| in the @implicitNotFound annotation argument.
31-
|
32-
| The variable does not occur in the signature of ShouldWarn4.
33-
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:22:6 ---------------------------------------
34-
22 |class ShouldWarn5[C, D] // error
35-
| ^
36-
| Invalid reference to a type variable "Abc" found
37-
| in the user-defined error message "An implicit ShouldWarn5[${C},${Abc}] is not in scope"
38-
| in the @implicitNotFound annotation argument.
39-
|
40-
| The variable does not occur in the signature of ShouldWarn5.
1+
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:5:29 ---------------------------------------
2+
5 |@annotation.implicitNotFound("An implicit ShouldWarn1[${B}] is not in scope") // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| Invalid reference to a type variable "B" found in the annotation argument.
5+
| The variable does not occur in the signature of ShouldWarn1.
6+
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:9:29 ---------------------------------------
7+
9 |@annotation.implicitNotFound("An implicit ShouldWarn2[${A}] is not in scope") // error
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9+
| Invalid reference to a type variable "A" found in the annotation argument.
10+
| The variable does not occur in the signature of ShouldWarn2.
11+
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:13:29 --------------------------------------
12+
13 |@annotation.implicitNotFound("An implicit ShouldWarn3[${A},${B}] is not in scope") // error
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| Invalid reference to a type variable "A" found in the annotation argument.
15+
| The variable does not occur in the signature of ShouldWarn3.
16+
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:17:29 --------------------------------------
17+
17 |@annotation.implicitNotFound("An implicit ShouldWarn4[${A},${B}] is not in scope") // error
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
19+
| Invalid reference to a type variable "A" found in the annotation argument.
20+
| The variable does not occur in the signature of ShouldWarn4.
21+
-- [E157] Reference Error: tests/neg-custom-args/fatal-warnings/i4008.scala:21:29 --------------------------------------
22+
21 |@annotation.implicitNotFound("An implicit ShouldWarn5[${C},${Abc}] is not in scope") // error
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
| Invalid reference to a type variable "Abc" found in the annotation argument.
25+
| The variable does not occur in the signature of ShouldWarn5.

tests/neg-custom-args/fatal-warnings/i4008.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22

33

44
// class, 1TP, invalid ref
5-
@annotation.implicitNotFound("An implicit ShouldWarn1[${B}] is not in scope")
6-
class ShouldWarn1[A] // error
5+
@annotation.implicitNotFound("An implicit ShouldWarn1[${B}] is not in scope") // error
6+
class ShouldWarn1[A]
77

88
// trait, 1TP, invalid ref
9-
@annotation.implicitNotFound("An implicit ShouldWarn2[${A}] is not in scope")
10-
trait ShouldWarn2[B] // error
9+
@annotation.implicitNotFound("An implicit ShouldWarn2[${A}] is not in scope") // error
10+
trait ShouldWarn2[B]
1111

1212
// trait, 2TP, 1 invalid ref
13-
@annotation.implicitNotFound("An implicit ShouldWarn3[${A},${B}] is not in scope")
14-
trait ShouldWarn3[B, C] // error
13+
@annotation.implicitNotFound("An implicit ShouldWarn3[${A},${B}] is not in scope") // error
14+
trait ShouldWarn3[B, C]
1515

1616
// class, 2TP, 2 invalid refs
17-
@annotation.implicitNotFound("An implicit ShouldWarn4[${A},${B}] is not in scope")
18-
class ShouldWarn4[C, D] // error
17+
@annotation.implicitNotFound("An implicit ShouldWarn4[${A},${B}] is not in scope") // error
18+
class ShouldWarn4[C, D]
1919

2020
// class, 2TP, 1 invalid multi-char refs
21-
@annotation.implicitNotFound("An implicit ShouldWarn5[${C},${Abc}] is not in scope")
22-
class ShouldWarn5[C, D] // error
21+
@annotation.implicitNotFound("An implicit ShouldWarn5[${C},${Abc}] is not in scope") // error
22+
class ShouldWarn5[C, D]
2323

2424
// trait, 1TP, valid ref
2525
@annotation.implicitNotFound("An implicit ShouldntWarn1[${A}] is not in scope")

0 commit comments

Comments
 (0)