Skip to content

Commit 8699a84

Browse files
committed
fix #14432: check if scala 2 case class is accessible
1 parent b636633 commit 8699a84

File tree

8 files changed

+90
-19
lines changed

8 files changed

+90
-19
lines changed

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

+33-19
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
275275
monoMap(mirroredType.resultType)
276276

277277
private def productMirror(mirroredType: Type, formal: Type, span: Span)(using Context): Tree =
278+
279+
/** for a case class, if it is Scala2x then
280+
* check if its constructor can be accessed
281+
* from the calling scope.
282+
*/
283+
def canAccessCtor(cls: Symbol): Boolean =
284+
!cls.is(Scala2x) || {
285+
val ctor = cls.primaryConstructor
286+
!ctor.isOneOf(Private | Protected) // we will never generate the mirror inside a Scala 2 class
287+
&& (!ctor.privateWithin.exists || ctx.owner.isContainedIn(ctor.privateWithin)) // check scope is compatible
288+
}
289+
278290
mirroredType match
279291
case AndType(tp1, tp2) =>
280292
productMirror(tp1, formal, span).orElse(productMirror(tp2, formal, span))
@@ -291,25 +303,27 @@ class Synthesizer(typer: Typer)(using @constructorOnly c: Context):
291303
modulePath.cast(mirrorType)
292304
else if mirroredType.classSymbol.isGenericProduct then
293305
val cls = mirroredType.classSymbol
294-
val accessors = cls.caseAccessors.filterNot(_.isAllOf(PrivateLocal))
295-
val elemLabels = accessors.map(acc => ConstantType(Constant(acc.name.toString)))
296-
val nestedPairs = TypeOps.nestedPairs(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
297-
val (monoType, elemsType) = mirroredType match
298-
case mirroredType: HKTypeLambda =>
299-
(mkMirroredMonoType(mirroredType), mirroredType.derivedLambdaType(resType = nestedPairs))
300-
case _ =>
301-
(mirroredType, nestedPairs)
302-
val elemsLabels = TypeOps.nestedPairs(elemLabels)
303-
checkRefinement(formal, tpnme.MirroredElemTypes, elemsType, span)
304-
checkRefinement(formal, tpnme.MirroredElemLabels, elemsLabels, span)
305-
val mirrorType =
306-
mirrorCore(defn.Mirror_ProductClass, monoType, mirroredType, cls.name, formal)
307-
.refinedWith(tpnme.MirroredElemTypes, TypeAlias(elemsType))
308-
.refinedWith(tpnme.MirroredElemLabels, TypeAlias(elemsLabels))
309-
val mirrorRef =
310-
if (cls.is(Scala2x)) anonymousMirror(monoType, ExtendsProductMirror, span)
311-
else companionPath(mirroredType, span)
312-
mirrorRef.cast(mirrorType)
306+
if !canAccessCtor(cls) then EmptyTree
307+
else
308+
val accessors = cls.caseAccessors.filterNot(_.isAllOf(PrivateLocal))
309+
val elemLabels = accessors.map(acc => ConstantType(Constant(acc.name.toString)))
310+
val nestedPairs = TypeOps.nestedPairs(accessors.map(mirroredType.resultType.memberInfo(_).widenExpr))
311+
val (monoType, elemsType) = mirroredType match
312+
case mirroredType: HKTypeLambda =>
313+
(mkMirroredMonoType(mirroredType), mirroredType.derivedLambdaType(resType = nestedPairs))
314+
case _ =>
315+
(mirroredType, nestedPairs)
316+
val elemsLabels = TypeOps.nestedPairs(elemLabels)
317+
checkRefinement(formal, tpnme.MirroredElemTypes, elemsType, span)
318+
checkRefinement(formal, tpnme.MirroredElemLabels, elemsLabels, span)
319+
val mirrorType =
320+
mirrorCore(defn.Mirror_ProductClass, monoType, mirroredType, cls.name, formal)
321+
.refinedWith(tpnme.MirroredElemTypes, TypeAlias(elemsType))
322+
.refinedWith(tpnme.MirroredElemLabels, TypeAlias(elemsLabels))
323+
val mirrorRef =
324+
if (cls.is(Scala2x)) anonymousMirror(monoType, ExtendsProductMirror, span)
325+
else companionPath(mirroredType, span)
326+
mirrorRef.cast(mirrorType)
313327
else EmptyTree
314328
end productMirror
315329

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import deriving.Mirror
2+
3+
val mFoo = summon[Mirror.Of[Foo]] // error: `Foo.<init>(Int)` is not accessible from `<empty>`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import deriving.Mirror
2+
3+
package example {
4+
val mFoo = summon[Mirror.Of[Foo]] // ok, we can access Foo's ctor from here.
5+
}
6+
7+
@main def Test: Unit =
8+
assert(example.mFoo.fromProduct(Some(23)) == example.Foo(23))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package example
2+
3+
import deriving.Mirror
4+
5+
val mFoo = summon[Mirror.Of[Foo]] // error: `Foo.<init>(Int)` is not accessible from any class.
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
val scala3Version = sys.props("plugin.scalaVersion")
2+
val scala2Version = sys.props("plugin.scala2Version")
3+
4+
lazy val lib1 = project.in(file("lib1"))
5+
.settings(
6+
scalaVersion := scala2Version
7+
)
8+
9+
lazy val lib2 = project.in(file("lib2"))
10+
.settings(
11+
scalaVersion := scala2Version
12+
)
13+
14+
lazy val app1fail = project.in(file("app1fail"))
15+
.dependsOn(lib1)
16+
.settings(
17+
scalaVersion := scala3Version
18+
)
19+
20+
lazy val app1ok = project.in(file("app1ok"))
21+
.dependsOn(lib1)
22+
.settings(
23+
scalaVersion := scala3Version
24+
)
25+
26+
lazy val app2fail = project.in(file("app2fail"))
27+
.dependsOn(lib2)
28+
.settings(
29+
scalaVersion := scala3Version
30+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package example
2+
3+
case class Foo private[example] (i: Int)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package example
2+
3+
case class Foo private (i: Int)

sbt-test/scala2-compat/i14432/test

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
> lib1/compile
2+
> lib2/compile
3+
-> app1fail/compile
4+
> app1ok/run
5+
-> app2fail/compile

0 commit comments

Comments
 (0)