Skip to content

Commit

Permalink
Add better error reporting for inlined non-immutable paths
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Bovel <[email protected]>
  • Loading branch information
HarrisL2 and mbovel committed Sep 24, 2024
1 parent ba9e59a commit 000f484
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1817,13 +1817,25 @@ class SuperCallsNotAllowedInlineable(symbol: Symbol)(using Context)
}

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

def inlineParamAddendum(using Context) =
val sym = tp.termSymbol
if sym.isAllOf(Flags.InlineParam) then
i"""
|Inline parameters are not considered immutable paths and cannot be used as
|singleton types.
|
|Hint: Removing the `inline` qualifier from the `${sym.name}` parameter
|may help resolve this issue."""
else ""


class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
extends SyntaxMsg(WrongNumberOfParametersID) {
def msg(using Context) = s"Wrong number of parameters, expected: $expectedCount"
Expand Down
11 changes: 11 additions & 0 deletions tests/neg/21538.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- [E083] Type Error: tests/neg/21538.scala:3:45 -----------------------------------------------------------------------
3 |inline def foo[V](inline value: V)(using Bar[value.type]) : Unit = {} // error
| ^^^^^^^^^^
| (value : V) is not a valid singleton type, since it is not an immutable path
| Inline parameters are not considered immutable paths and cannot be used as
| singleton types.
|
| Hint: Removing the `inline` qualifier from the `value` parameter
| may help resolve this issue.
|
| longer explanation available when compiling with `-explain`
3 changes: 3 additions & 0 deletions tests/neg/21538.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
trait Bar[T]
given [T]: Bar[T] with {}
inline def foo[V](inline value: V)(using Bar[value.type]) : Unit = {} // error

0 comments on commit 000f484

Please sign in to comment.