Skip to content

Commit f647651

Browse files
authored
Add better error reporting for inlined non-immutable paths (#21639)
Fixes #21538
2 parents 3f4fee2 + 000f484 commit f647651

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/reporting/messages.scala

+13-1
Original file line numberDiff line numberDiff line change
@@ -1817,13 +1817,25 @@ class SuperCallsNotAllowedInlineable(symbol: Symbol)(using Context)
18171817
}
18181818

18191819
class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathID):
1820-
def msg(using Context) = i"$tp is not a valid $usage, since it is not an immutable path"
1820+
def msg(using Context) = i"$tp is not a valid $usage, since it is not an immutable path" + inlineParamAddendum
18211821
def explain(using Context) =
18221822
i"""An immutable path is
18231823
| - a reference to an immutable value, or
18241824
| - a reference to `this`, or
18251825
| - a selection of an immutable path with an immutable value."""
18261826

1827+
def inlineParamAddendum(using Context) =
1828+
val sym = tp.termSymbol
1829+
if sym.isAllOf(Flags.InlineParam) then
1830+
i"""
1831+
|Inline parameters are not considered immutable paths and cannot be used as
1832+
|singleton types.
1833+
|
1834+
|Hint: Removing the `inline` qualifier from the `${sym.name}` parameter
1835+
|may help resolve this issue."""
1836+
else ""
1837+
1838+
18271839
class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
18281840
extends SyntaxMsg(WrongNumberOfParametersID) {
18291841
def msg(using Context) = s"Wrong number of parameters, expected: $expectedCount"

Diff for: tests/neg/21538.check

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- [E083] Type Error: tests/neg/21538.scala:3:45 -----------------------------------------------------------------------
2+
3 |inline def foo[V](inline value: V)(using Bar[value.type]) : Unit = {} // error
3+
| ^^^^^^^^^^
4+
| (value : V) is not a valid singleton type, since it is not an immutable path
5+
| Inline parameters are not considered immutable paths and cannot be used as
6+
| singleton types.
7+
|
8+
| Hint: Removing the `inline` qualifier from the `value` parameter
9+
| may help resolve this issue.
10+
|
11+
| longer explanation available when compiling with `-explain`

Diff for: tests/neg/21538.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trait Bar[T]
2+
given [T]: Bar[T] with {}
3+
inline def foo[V](inline value: V)(using Bar[value.type]) : Unit = {} // error

0 commit comments

Comments
 (0)