Skip to content

Commit e1a4941

Browse files
authored
Survive inaccessible types when computing implicit scope (#21589)
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
2 parents eb42a4d + cd9a7c5 commit e1a4941

File tree

7 files changed

+51
-10
lines changed

7 files changed

+51
-10
lines changed

compiler/src/dotty/tools/dotc/core/TypeErrors.scala

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ class MissingType(val pre: Type, val name: Name)(using Context) extends TypeErro
7373
case _ if givenSelf.exists && givenSelf.member(name).exists =>
7474
i"""$name exists as a member of the self type $givenSelf of $cls
7575
|but it cannot be called on a receiver whose type does not extend $cls"""
76+
case _ if pre.baseClasses.exists(_.findMember(name, pre, Private, EmptyFlags).exists) =>
77+
i"$name is a private member in a base class"
7678
case _ =>
7779
missingClassFile
7880

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -3292,14 +3292,14 @@ object UnusedSymbol {
32923292

32933293
class NonNamedArgumentInJavaAnnotation(using Context) extends SyntaxMsg(NonNamedArgumentInJavaAnnotationID):
32943294

3295-
override protected def msg(using Context): String =
3295+
override protected def msg(using Context): String =
32963296
"Named arguments are required for Java defined annotations"
32973297
+ Message.rewriteNotice("This", version = SourceVersion.`3.6-migration`)
32983298

3299-
override protected def explain(using Context): String =
3299+
override protected def explain(using Context): String =
33003300
i"""Starting from Scala 3.6.0, named arguments are required for Java defined annotations.
3301-
|Java defined annotations don't have an exact constructor representation
3302-
|and we previously relied on the order of the fields to create one.
3301+
|Java defined annotations don't have an exact constructor representation
3302+
|and we previously relied on the order of the fields to create one.
33033303
|One possible issue with this representation is the reordering of the fields.
33043304
|Lets take the following example:
33053305
|

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

+4
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,10 @@ trait ImplicitRunInfo:
821821
override def stopAt = StopAt.Static
822822
private val seen = util.HashSet[Type]()
823823

824+
override def derivedTypeBounds(tp: TypeBounds, lo: Type, hi: Type): Type =
825+
if lo.exists && hi.exists then super.derivedTypeBounds(tp, lo, hi)
826+
else NoType // Survive inaccessible types, for instance in i21543.scala.
827+
824828
def applyToUnderlying(t: TypeProxy) =
825829
if seen.contains(t) then
826830
WildcardType

tests/neg/i20554-a.check

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
| Explanation (enabled by `-explain`)
88
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99
| Starting from Scala 3.6.0, named arguments are required for Java defined annotations.
10-
| Java defined annotations don't have an exact constructor representation
11-
| and we previously relied on the order of the fields to create one.
10+
| Java defined annotations don't have an exact constructor representation
11+
| and we previously relied on the order of the fields to create one.
1212
| One possible issue with this representation is the reordering of the fields.
1313
| Lets take the following example:
1414
|
@@ -29,8 +29,8 @@
2929
| Explanation (enabled by `-explain`)
3030
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3131
| Starting from Scala 3.6.0, named arguments are required for Java defined annotations.
32-
| Java defined annotations don't have an exact constructor representation
33-
| and we previously relied on the order of the fields to create one.
32+
| Java defined annotations don't have an exact constructor representation
33+
| and we previously relied on the order of the fields to create one.
3434
| One possible issue with this representation is the reordering of the fields.
3535
| Lets take the following example:
3636
|

tests/neg/i20554-b.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
| Explanation (enabled by `-explain`)
88
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
99
| Starting from Scala 3.6.0, named arguments are required for Java defined annotations.
10-
| Java defined annotations don't have an exact constructor representation
11-
| and we previously relied on the order of the fields to create one.
10+
| Java defined annotations don't have an exact constructor representation
11+
| and we previously relied on the order of the fields to create one.
1212
| One possible issue with this representation is the reordering of the fields.
1313
| Lets take the following example:
1414
|

tests/neg/i21543.check

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i21543.scala:10:15 ------------------------------------------------------------
2+
10 | Cmd(List("1", "2")) // error // error
3+
| ^^^
4+
| Found: ("1" : String)
5+
| Required: Event
6+
|
7+
| Note that I could not resolve reference Event.
8+
| Event is a private member in a base class
9+
|
10+
|
11+
| longer explanation available when compiling with `-explain`
12+
-- [E007] Type Mismatch Error: tests/neg/i21543.scala:10:20 ------------------------------------------------------------
13+
10 | Cmd(List("1", "2")) // error // error
14+
| ^^^
15+
| Found: ("2" : String)
16+
| Required: Event
17+
|
18+
| Note that I could not resolve reference Event.
19+
| Event is a private member in a base class
20+
|
21+
|
22+
| longer explanation available when compiling with `-explain`

tests/neg/i21543.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object CompilerCrash {
2+
trait Scope {
3+
private type Event = String
4+
5+
case class Cmd(events: List[Event])
6+
}
7+
8+
new Scope {
9+
val commands = List(
10+
Cmd(List("1", "2")) // error // error
11+
)
12+
}
13+
}

0 commit comments

Comments
 (0)