8000 Simplify decision whether to derive accessors · scala/scala@1db8a53 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1db8a53

Browse files
committed
Simplify decision whether to derive accessors
Originally (modulo renaming & reduction of double negation in previous commit): ``` def deriveAccessors(vd: ValDef) = vd.mods.isLazy || !( !owner.isClass || (vd.mods.isPrivateLocal && !vd.mods.isCaseAccessor) // this is an error -- now checking first || (vd.name startsWith nme.OUTER) || (context.unit.isJava) // pulled out to caller || isEnumConstant(vd) ) def deriveAccessorTrees(vd: ValDef) = !( (vd.mods.isPrivateLocal && !vd.mods.isLazy) // lazy was pulled out to outer disjunction || vd.symbol.isModuleVar // pulled out to caller || isEnumConstant(vd)) ``` With changes in comments above, these conditions are now captured by one method.
1 parent d140d60 commit 1db8a53

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1105,7 +1105,7 @@ trait ContextErrors {
11051105
def GetterDefinedTwiceError(getter: Symbol) =
11061106
issueSymbolTypeError(getter, getter+" is defined twice")
11071107

1108-
def ValOrValWithSetterSuffixError(tree: Tree) =
1108+
def ValOrVarWithSetterSuffixError(tree: Tree) =
11091109
issueNormalTypeError(tree, "Names of vals or vars may not end in `_='")
11101110

11111111
def PrivateThisCaseClassParameterError(tree: Tree) =

src/compiler/scala/tools/nsc/typechecker/MethodSynthesis.scala

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,12 @@ trait MethodSynthesis {
136136

137137
// TODO: why is all this symbol creation disconnected from addDerivedTrees,
138138
// which creates another list of Field/Getter/Setter factories???
139-
def enterGetterSetter(tree: ValDef) {
140-
val ValDef(mods, name, _, _) = tree
141-
if (nme.isSetterName(name))
142-
ValOrValWithSetterSuffixError(tree)
143-
139+
def enterGetterSetter(tree: ValDef): Unit = {
144140
tree.symbol =
145-
if (mods.isLazy) {
141+
if (tree.mods.isLazy) {
146142
val lazyValGetter = LazyValGetter(tree).createAndEnterSymbol()
147143
enterLazyVal(tree, lazyValGetter)
148144
} else {
149-
if (mods.isPrivateLocal)
150-
PrivateThisCaseClassParameterError(tree)
151145
val getter = Getter(tree)
152146
val getterSym = getter.createAndEnterSymbol()
153147

@@ -161,7 +155,6 @@ trait MethodSynthesis {
161155
else enterStrictVal(tree)
162156
}
163157

164-
165158
enterBeans(tree)
166159
}
167160

@@ -182,7 +175,7 @@ trait MethodSynthesis {
182175
}
183176

184177
def addDerivedTrees(typer: Typer, stat: Tree): List[Tree] = stat match {
185-
case vd @ ValDef(mods, name, tpt, rhs) if deriveAccessorTrees(vd) =>
178+
case vd @ ValDef(mods, name, tpt, rhs) if deriveAccessors(vd) && !vd.symbol.isModuleVar =>
186179
// If we don't save the annotations, they seem to wander off.
187180
val annotations = stat.symbol.initialize.annotations
188181
val trees = (

src/compiler/scala/tools/nsc/typechecker/Namers.scala

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,14 @@ trait Namers extends MethodSynthesis {
115115
TypeSigError(tree, ex)
116116
alt
117117
}
118-
// PRIVATE | LOCAL are fields generated for primary constructor arguments
119-
// @PP: ...or fields declared as private[this]. PARAMACCESSOR marks constructor arguments.
120-
// Neither gets accessors so the code is as far as I know still correct.
121-
def deriveAccessors(vd: ValDef) = vd.mods.isLazy || !(
122-
!owner.isClass
123-
|| (vd.mods.isPrivateLocal && !vd.mods.isCaseAccessor)
124-
|| (vd.name startsWith nme.OUTER)
125-
|| (context.unit.isJava)
126-
|| isEnumConstant(vd)
127-
)
128118

129-
def deriveAccessorTrees(vd: ValDef) = !(
130-
(vd.mods.isPrivateLocal && !vd.mods.isLazy) // all lazy vals need accessors, even private[this]
131-
|| vd.symbol.isModuleVar
132-
|| isEnumConstant(vd))
119+
// All lazy vals need accessors, including those owned by terms (e.g., in method) or private[this] in a class
120+
def deriveAccessors(vd: ValDef) = vd.mods.isLazy || (owner.isClass && deriveAccessorsInClass(vd))
121+
122+
private def deriveAccessorsInClass(vd: ValDef) =
123+
!vd.mods.isPrivateLocal && // note, private[this] lazy vals do get accessors -- see outer disjunction of deriveAccessors
124+
!(vd.name startsWith nme.OUTER) && // outer accessors are added later, in explicitouter
125+
!isEnumConstant(vd) // enums can only occur in classes, so only check here
133126

134127
/** Determines whether this field holds an enum constant.
135128
* To qualify, the following conditions must be met:
@@ -655,8 +648,14 @@ trait Namers extends MethodSynthesis {
655648
}
656649
}
657650

658-
def enterValDef(tree: ValDef) {
659-
if (deriveAccessors(tree)) enterGetterSetter(tree)
651+
def enterValDef(tree: ValDef): Unit = {
652+
val isScala = !context.unit.isJava
653+
if (isScala) {
654+
if (nme.isSetterName(tree.name)) ValOrVarWithSetterSuffixError(tree)
655+
if (tree.mods.isPrivateLocal && tree.mods.isCaseAccessor) PrivateThisCaseClassParameterError(tree)
656+
}
657+
658+
if (isScala && deriveAccessors(tree)) enterGetterSetter(tree)
660659
else assignAndEnterFinishedSymbol(tree)
661660

662661
if (isEnumConstant(tree))

0 commit comments

Comments
 (0)
0