From e88947266b1eac1c8797c01768a81ddb781bbdc6 Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Tue, 3 Dec 2024 22:45:07 +0100 Subject: [PATCH 1/2] Survive inaccessible types when computing implicit scope Also: Give a better error message later when encountering a missing type that refers to a private member of a base class. The previous one was misleading since it referred to a potentially missing class file, which is certainly not the case here. Fixes #21543 [Cherry-picked 472555dca6866b4ae1fde35f3abe99579689b8eb][modified] --- compiler/src/dotty/tools/dotc/core/TypeErrors.scala | 2 ++ compiler/src/dotty/tools/dotc/typer/Implicits.scala | 4 ++++ tests/neg/i21543.scala | 13 +++++++++++++ 3 files changed, 19 insertions(+) create mode 100644 tests/neg/i21543.scala diff --git a/compiler/src/dotty/tools/dotc/core/TypeErrors.scala b/compiler/src/dotty/tools/dotc/core/TypeErrors.scala index 80dd412d0dbe..c7a9a232ed0d 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeErrors.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeErrors.scala @@ -73,6 +73,8 @@ class MissingType(val pre: Type, val name: Name)(using Context) extends TypeErro case _ if givenSelf.exists && givenSelf.member(name).exists => i"""$name exists as a member of the self type $givenSelf of $cls |but it cannot be called on a receiver whose type does not extend $cls""" + case _ if pre.baseClasses.exists(_.findMember(name, pre, Private, EmptyFlags).exists) => + i"$name is a private member in a base class" case _ => missingClassFile diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index 3329055fd5d1..378a33b1da45 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -781,6 +781,10 @@ trait ImplicitRunInfo: override def stopAt = StopAt.Static private val seen = util.HashSet[Type]() + override def derivedTypeBounds(tp: TypeBounds, lo: Type, hi: Type): Type = + if lo.exists && hi.exists then super.derivedTypeBounds(tp, lo, hi) + else NoType // Survive inaccessible types, for instance in i21543.scala. + def applyToUnderlying(t: TypeProxy) = if seen.contains(t) then WildcardType diff --git a/tests/neg/i21543.scala b/tests/neg/i21543.scala new file mode 100644 index 000000000000..98de8d3ec939 --- /dev/null +++ b/tests/neg/i21543.scala @@ -0,0 +1,13 @@ +object CompilerCrash { + trait Scope { + private type Event = String + + case class Cmd(events: List[Event]) + } + + new Scope { + val commands = List( + Cmd(List("1", "2")) + ) + } +} \ No newline at end of file From 0484e65078127272209dcda9a5d46606ebb4ab6e Mon Sep 17 00:00:00 2001 From: Wojciech Mazur Date: Tue, 3 Dec 2024 22:45:32 +0100 Subject: [PATCH 2/2] Fix check files [Cherry-picked cd9a7c58afaab6d07bab91ebc059d98313a4713d][modified] --- tests/neg/i21543.check | 22 ++++++++++++++++++++++ tests/neg/i21543.scala | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/neg/i21543.check diff --git a/tests/neg/i21543.check b/tests/neg/i21543.check new file mode 100644 index 000000000000..9fa9a7779d7a --- /dev/null +++ b/tests/neg/i21543.check @@ -0,0 +1,22 @@ +-- [E007] Type Mismatch Error: tests/neg/i21543.scala:10:15 ------------------------------------------------------------ +10 | Cmd(List("1", "2")) // error // error + | ^^^ + | Found: ("1" : String) + | Required: Event + | + | Note that I could not resolve reference Event. + | Event is a private member in a base class + | + | + | longer explanation available when compiling with `-explain` +-- [E007] Type Mismatch Error: tests/neg/i21543.scala:10:20 ------------------------------------------------------------ +10 | Cmd(List("1", "2")) // error // error + | ^^^ + | Found: ("2" : String) + | Required: Event + | + | Note that I could not resolve reference Event. + | Event is a private member in a base class + | + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i21543.scala b/tests/neg/i21543.scala index 98de8d3ec939..aaadce6d22b4 100644 --- a/tests/neg/i21543.scala +++ b/tests/neg/i21543.scala @@ -7,7 +7,7 @@ object CompilerCrash { new Scope { val commands = List( - Cmd(List("1", "2")) + Cmd(List("1", "2")) // error // error ) } } \ No newline at end of file