Skip to content

Commit e54a934

Browse files
authored
Merge pull request #14730 from rochala/illegal-access-extension-error-message
Error message improvement for illegal access extension methods
2 parents ab885bf + 469e5ec commit e54a934

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ object ErrorReporting {
208208
i""".
209209
|Extension methods were tried, but the search failed with:
210210
|
211-
| ${nested.head.explanation}"""
211+
|${nested.head.explanation.indented(4)}"""
212212
else if tree.hasAttachment(desugar.MultiLineInfix) then
213213
i""".
214214
|Note that `${tree.name}` is treated as an infix operator in Scala 3.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,9 @@ trait Implicits:
11081108
ctx.reporter.removeBufferedMessages
11091109
adapted.tpe match {
11101110
case _: SearchFailureType => SearchFailure(adapted)
1111-
case _ =>
1111+
case error: PreviousErrorType if !adapted.symbol.isAccessibleFrom(cand.ref.prefix) =>
1112+
SearchFailure(adapted.withType(new NestedFailure(error.msg, pt)))
1113+
case _ =>
11121114
// Special case for `$conforms` and `<:<.refl`. Showing them to the users brings
11131115
// no value, so we instead report a `NoMatchingImplicitsFailure`
11141116
if (adapted.symbol == defn.Predef_conforms || adapted.symbol == defn.SubType_refl)

tests/neg/i12573.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- [E008] Not Found Error: tests/neg/i12573.scala:23:38 ----------------------------------------------------------------
2+
23 |val w: Value[8] = DFBits(Value[8](8)).getDFType.width // error
3+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4+
| value getDFType is not a member of DFBits[(8 : Int)].
5+
| Extension methods were tried, but the search failed with:
6+
|
7+
| method getDFType cannot be accessed as a member of DFType.type from module class i12573$package$.
8+
| Access to protected method getDFType not permitted because enclosing package object i12573$package
9+
| is not a subclass of object DFType where target is defined

tests/neg/i12573.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Value[T <: Int](val value: T)
2+
3+
sealed trait DFType:
4+
type Width <: Int
5+
val width: Value[Width]
6+
7+
object DFType:
8+
trait TC[T]:
9+
type Type <: DFType
10+
def apply(t: T): Type
11+
type Aux[T, Type0 <: DFType] = TC[T] { type Type = Type0 }
12+
transparent inline given ofDFType[T <: DFType]: TC[T] =
13+
new TC[T]:
14+
type Type = T
15+
def apply(t: T): Type = t
16+
17+
extension [T, Type <: DFType](t: T)(using tc: Aux[T, Type])
18+
protected def getDFType: Type = tc(t)
19+
20+
final case class DFBits[W <: Int](width: Value[W]) extends DFType:
21+
type Width = W
22+
23+
val w: Value[8] = DFBits(Value[8](8)).getDFType.width // error

0 commit comments

Comments
 (0)