From 37e4fe703a883098a4c0e02c91faf23a5681ccf0 Mon Sep 17 00:00:00 2001 From: odersky Date: Sun, 12 Mar 2023 20:01:12 +0100 Subject: [PATCH 1/2] Check trait constructor for accessibility even if not called at Typer This allows to restrict inheritance of traits even if the constructor is not called, or not called at Typer. Fixes #17089 --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 3 +++ tests/neg/i17089.scala | 4 ++++ 2 files changed, 7 insertions(+) create mode 100644 tests/neg/i17089.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 96149caf215d..f6048f6fb156 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -2511,6 +2511,9 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer checkSimpleKinded(parent) // allow missing type parameters if there are implicit arguments to pass // since we can infer type arguments from them + val constr = psym.primaryConstructor + if constr.exists then + ensureAccessible(constr.termRef, superAccess = true, tree.srcPos) else checkParentCall(result, cls) if cls is Case then diff --git a/tests/neg/i17089.scala b/tests/neg/i17089.scala new file mode 100644 index 000000000000..46968aa6f093 --- /dev/null +++ b/tests/neg/i17089.scala @@ -0,0 +1,4 @@ +object o: + trait T private[o]() + +def test = new o.T { } // error From 391730ae4df3c1a046b57734cb19b19fdbb5f4c8 Mon Sep 17 00:00:00 2001 From: odersky Date: Mon, 13 Mar 2023 10:34:50 +0100 Subject: [PATCH 2/2] Don't check parents of refinement classes for accessibility Only check trait constructor accessibility if the trait is a parent of some other class or trait. Refinements don't fall into this category. --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index f6048f6fb156..d45e24bf30e5 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -2512,7 +2512,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer // allow missing type parameters if there are implicit arguments to pass // since we can infer type arguments from them val constr = psym.primaryConstructor - if constr.exists then + if psym.is(Trait) && constr.exists && !cls.isRefinementClass then ensureAccessible(constr.termRef, superAccess = true, tree.srcPos) else checkParentCall(result, cls)