From 689a69e2f3829bd0f45b7da298e184c1508e1e63 Mon Sep 17 00:00:00 2001 From: rochala Date: Mon, 21 Mar 2022 13:34:24 +0100 Subject: [PATCH] add proper error for illegal access for extension methods --- .../tools/dotc/typer/ErrorReporting.scala | 2 +- .../dotty/tools/dotc/typer/Implicits.scala | 4 +++- tests/neg/i12573.check | 9 ++++++++ tests/neg/i12573.scala | 23 +++++++++++++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i12573.check create mode 100644 tests/neg/i12573.scala diff --git a/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala b/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala index 73ceda9223fd..97b5f9bb6680 100644 --- a/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala +++ b/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala @@ -208,7 +208,7 @@ object ErrorReporting { i""". |Extension methods were tried, but the search failed with: | - | ${nested.head.explanation}""" + |${nested.head.explanation.indented(4)}""" else if tree.hasAttachment(desugar.MultiLineInfix) then i""". |Note that `${tree.name}` is treated as an infix operator in Scala 3. diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index fc1fd57c7bd8..4fae9264939d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -1108,7 +1108,9 @@ trait Implicits: ctx.reporter.removeBufferedMessages adapted.tpe match { case _: SearchFailureType => SearchFailure(adapted) - case _ => + case error: PreviousErrorType if !adapted.symbol.isAccessibleFrom(cand.ref.prefix) => + SearchFailure(adapted.withType(new NestedFailure(error.msg, pt))) + case _ => // Special case for `$conforms` and `<:<.refl`. Showing them to the users brings // no value, so we instead report a `NoMatchingImplicitsFailure` if (adapted.symbol == defn.Predef_conforms || adapted.symbol == defn.SubType_refl) diff --git a/tests/neg/i12573.check b/tests/neg/i12573.check new file mode 100644 index 000000000000..d250f4beabbe --- /dev/null +++ b/tests/neg/i12573.check @@ -0,0 +1,9 @@ +-- [E008] Not Found Error: tests/neg/i12573.scala:23:38 ---------------------------------------------------------------- +23 |val w: Value[8] = DFBits(Value[8](8)).getDFType.width // error + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | value getDFType is not a member of DFBits[(8 : Int)]. + | Extension methods were tried, but the search failed with: + | + | method getDFType cannot be accessed as a member of DFType.type from module class i12573$package$. + | Access to protected method getDFType not permitted because enclosing package object i12573$package + | is not a subclass of object DFType where target is defined \ No newline at end of file diff --git a/tests/neg/i12573.scala b/tests/neg/i12573.scala new file mode 100644 index 000000000000..c45ef6974a84 --- /dev/null +++ b/tests/neg/i12573.scala @@ -0,0 +1,23 @@ +class Value[T <: Int](val value: T) + +sealed trait DFType: + type Width <: Int + val width: Value[Width] + +object DFType: + trait TC[T]: + type Type <: DFType + def apply(t: T): Type + type Aux[T, Type0 <: DFType] = TC[T] { type Type = Type0 } + transparent inline given ofDFType[T <: DFType]: TC[T] = + new TC[T]: + type Type = T + def apply(t: T): Type = t + + extension [T, Type <: DFType](t: T)(using tc: Aux[T, Type]) + protected def getDFType: Type = tc(t) + +final case class DFBits[W <: Int](width: Value[W]) extends DFType: + type Width = W + +val w: Value[8] = DFBits(Value[8](8)).getDFType.width // error \ No newline at end of file