Skip to content

Commit 341afb3

Browse files
tudorv91bishabosha
andcommitted
Update compiler/src/dotty/tools/dotc/core/NameOps.scala
Co-authored-by: Jamie Thompson <[email protected]>
1 parent a17b774 commit 341afb3

File tree

4 files changed

+43
-19
lines changed

4 files changed

+43
-19
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,26 +147,32 @@ object desugar {
147147

148148
// ----- Desugar methods -------------------------------------------------
149149

150+
/** Setter generation is needed for:
151+
* - non-private class members
152+
* - all trait members
153+
* - all package object members
154+
*/
155+
def isSetterNeeded(valDef: ValDef)(using Context): Boolean = {
156+
val mods = valDef.mods
157+
mods.is(Mutable)
158+
&& ctx.owner.isClass
159+
&& (!mods.is(Private) || ctx.owner.is(Trait) || ctx.owner.isPackageObject)
160+
}
161+
150162
/** var x: Int = expr
151163
* ==>
152164
* def x: Int = expr
153165
* def x_=($1: <TypeTree()>): Unit = ()
154166
*
155-
* Generate the setter only for
156-
* - non-private class members
157-
* - all trait members
158-
* - all package object members
167+
* Generate setter where needed
159168
*/
160169
def valDef(vdef0: ValDef)(using Context): Tree = {
161170
val vdef @ ValDef(_, tpt, rhs) = vdef0
162171
val mods = vdef.mods
163-
val setterNeeded =
164-
mods.is(Mutable)
165-
&& ctx.owner.isClass
166-
&& (!mods.is(Private) || ctx.owner.is(Trait) || ctx.owner.isPackageObject)
172+
167173
val valName = normalizeName(vdef, tpt).asTermName
168174

169-
if (setterNeeded) {
175+
if (isSetterNeeded(vdef)) {
170176
// TODO: copy of vdef as getter needed?
171177
// val getter = ValDef(mods, name, tpt, rhs) withPos vdef.pos?
172178
// right now vdef maps via expandedTree to a thicket which concerns itself.
@@ -937,7 +943,7 @@ object desugar {
937943
name = name.errorName
938944
}
939945
mdef match {
940-
case vdef: ValDef if name.isExtension && vdef.mods.is(Mutable) =>
946+
case vdef: ValDef if name.isExtension && isSetterNeeded(vdef) =>
941947
report.error(em"illegal setter name: `extension_=`", errPos)
942948
case memDef if name.isExtensionName && !mdef.mods.is(ExtensionMethod) =>
943949
report.error(em"illegal name: $name may not start with `extension_`", errPos)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ object NameOps {
134134
}
135135

136136
/** Does the name match `extension`? */
137-
def isExtension: Boolean = name.toString == "extension"
137+
def isExtension: Boolean = name match
138+
case name: SimpleName =>
139+
name.length == "extension".length && name.startsWith("extension")
140+
case _ => false
138141

139142
/** Does this name start with `extension_`? */
140143
def isExtensionName: Boolean = name match

tests/neg/illegal-extension.check

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
| ^^^^^^^^^^^^^
88
| illegal name: extension_val may not start with `extension_`
99
-- Error: tests/neg/illegal-extension.scala:5:14 -----------------------------------------------------------------------
10-
5 | private var extension = Nil // error: not allowed because it matches `extension`
10+
5 | private var extension = Nil // error: illegal setter name: `extension_=`
1111
| ^^^^^^^^^
12-
| illegal variable name: `extension`
13-
-- Error: tests/neg/illegal-extension.scala:8:23 -----------------------------------------------------------------------
14-
8 |extension (x: Any) def extension_foo: String = "foo" // error: illegal name: extension_foo may not start with `extension_`
15-
| ^^^^^^^^^^^^^
16-
| illegal name: extension_foo may not start with `extension_`
12+
| illegal setter name: `extension_=`
13+
-- Error: tests/neg/illegal-extension.scala:16:23 ----------------------------------------------------------------------
14+
16 |extension (x: Any) def extension_foo: String = "foo" // error: illegal name: extension_foo may not start with `extension_`
15+
| ^^^^^^^^^^^^^
16+
| illegal name: extension_foo may not start with `extension_`
17+
-- Error: tests/neg/illegal-extension.scala:9:6 ------------------------------------------------------------------------
18+
9 | var extension = 1337 // error: illegal setter name: `extension_=`
19+
| ^^^^^^^^^
20+
| illegal setter name: `extension_=`

tests/neg/illegal-extension.scala

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,19 @@ trait A {
22
def extension_n: String = "illegal method" // error: illegal name: extension_n may not start with `extension_`
33
type extension_type = Int // allowed because it's a type alias
44
val extension_val = 23 // error: illegal name: extension_val may not start with `extension_`
5-
private var extension = Nil // error: not allowed because it matches `extension`
5+
private var extension = Nil // error: illegal setter name: `extension_=`
6+
}
7+
8+
class B {
9+
var extension = 1337 // error: illegal setter name: `extension_=`
10+
}
11+
12+
class C {
13+
private var extension = "OK" // allowed because it does not require a setter
614
}
715

816
extension (x: Any) def extension_foo: String = "foo" // error: illegal name: extension_foo may not start with `extension_`
9-
extension (some: Any) def valid_extension_name: String = "bar"
17+
extension (some: Any) def valid_extension_name: String = {
18+
var extension = "foo" // `extension` name allowed because it doesn't require a setter
19+
s"$extension bar"
20+
}

0 commit comments

Comments
 (0)