Skip to content

Commit ab6d3e7

Browse files
HarrisL2WojciechMazur
authored andcommitted
Add better error reporting for inlined non-immutable paths
Co-authored-by: Matt Bovel <[email protected]> [Cherry-picked 000f484]
1 parent 051f2cb commit ab6d3e7

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1797,13 +1797,25 @@ class SuperCallsNotAllowedInlineable(symbol: Symbol)(using Context)
17971797
}
17981798

17991799
class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathID):
1800-
def msg(using Context) = i"$tp is not a valid $usage, since it is not an immutable path"
1800+
def msg(using Context) = i"$tp is not a valid $usage, since it is not an immutable path" + inlineParamAddendum
18011801
def explain(using Context) =
18021802
i"""An immutable path is
18031803
| - a reference to an immutable value, or
18041804
| - a reference to `this`, or
18051805
| - a selection of an immutable path with an immutable value."""
18061806

1807+
def inlineParamAddendum(using Context) =
1808+
val sym = tp.termSymbol
1809+
if sym.isAllOf(Flags.InlineParam) then
1810+
i"""
1811+
|Inline parameters are not considered immutable paths and cannot be used as
1812+
|singleton types.
1813+
|
1814+
|Hint: Removing the `inline` qualifier from the `${sym.name}` parameter
1815+
|may help resolve this issue."""
1816+
else ""
1817+
1818+
18071819
class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
18081820
extends SyntaxMsg(WrongNumberOfParametersID) {
18091821
def msg(using Context) = s"Wrong number of parameters, expected: $expectedCount"

tests/neg/21538.check

Lines changed: 11 additions & 0 deletions
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`

tests/neg/21538.scala

Lines changed: 3 additions & 0 deletions
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)