Skip to content

Commit 0a80777

Browse files
committed
Nowarn non-private implicit val class params
Dowarn protected param accessor of given class, which is further constrained to be protected only for simply named params that the user might reference.
1 parent a25fe5e commit 0a80777

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -4363,7 +4363,7 @@ object Parsers {
43634363
else
43644364
// structural instance
43654365
val vparamss1 = vparamss.nestedMap: vparam =>
4366-
if vparam.mods.is(Private)
4366+
if vparam.mods.is(Private) && !vparam.name.isInstanceOf[DerivedName]
43674367
then vparam.withMods(vparam.mods &~ PrivateLocal | Protected)
43684368
else vparam
43694369
val constr = makeConstructor(tparams, vparamss1)

compiler/src/dotty/tools/dotc/transform/CheckUnused.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,10 @@ object CheckUnused:
585585
val alias = m.owner.info.member(sym.name)
586586
if alias.exists then
587587
val aliasSym = alias.symbol
588-
if aliasSym.is(ParamAccessor) && !infos.refs(alias.symbol) then
588+
val checking =
589+
aliasSym.isAllOf(PrivateParamAccessor, butNot = CaseAccessor)
590+
|| aliasSym.isAllOf(Protected | ParamAccessor, butNot = CaseAccessor) && m.owner.is(Given)
591+
if checking && !infos.refs(alias.symbol) then
589592
warnAt(pos)(UnusedSymbol.implicitParams)
590593
else
591594
warnAt(pos)(UnusedSymbol.implicitParams)

tests/warn/i15503f.scala

+32
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,35 @@ object Unmatched:
5252
case Ident(name) =>
5353
case _ =>
5454
e
55+
56+
trait Ctx
57+
case class K(i: Int)(using val ctx: Ctx) // nowarn
58+
class L(val i: Int)(using val ctx: Ctx) // nowarn
59+
class M(val i: Int)(using ctx: Ctx) // warn
60+
61+
package givens:
62+
63+
trait X:
64+
def doX: Int
65+
66+
trait Y:
67+
def doY: String
68+
69+
given X:
70+
def doX = 7
71+
72+
given X => Y: // warn protected param to given class
73+
def doY = "7"
74+
/* desugared. It is protected so that its type can be used in member defs without leaking.
75+
* possibly it should be protected only for named parameters.
76+
given class given_Y(using x$1: givens.X) extends Object(), givens.Y {
77+
protected given val x$1: givens.X
78+
def doY: String = "7"
79+
}
80+
final given def given_Y(using x$1: givens.X): givens.given_Y =
81+
new givens.given_Y(using x$1)()
82+
*/
83+
84+
given namely: (x: X) => Y: // warn protected param to given class
85+
def doY = "8"
86+
end givens

0 commit comments

Comments
 (0)