From fa271e24342831bb6d7b683fddd1a4a11825d39d Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Tue, 24 Sep 2013 15:09:16 +0200 Subject: SI-4742 Make -Xcheckinit aware of constants. Members defined as `final val x = ` are given a ConstantType. The constant is folded into the accessor method `x`, and the field itself is never initialized. (Related discussion: SI-4605) As such, -Xcheckinit spuriously warns when calling that accessor. This commit disables the checks for constants. This will also fix the checkinit build (failure tracked as SI-7839), which is the victim of this a spurious scolding. --- src/compiler/scala/tools/nsc/transform/Mixin.scala | 1 + test/files/run/t4742.flags | 1 + test/files/run/t4742.scala | 7 +++++++ 3 files changed, 9 insertions(+) create mode 100644 test/files/run/t4742.flags create mode 100644 test/files/run/t4742.scala diff --git a/src/compiler/scala/tools/nsc/transform/Mixin.scala b/src/compiler/scala/tools/nsc/transform/Mixin.scala index 7b545be07e..4eb8eb933c 100644 --- a/src/compiler/scala/tools/nsc/transform/Mixin.scala +++ b/src/compiler/scala/tools/nsc/transform/Mixin.scala @@ -89,6 +89,7 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL { settings.checkInit && sym.isGetter && !sym.isInitializedToDefault + && !isConstantType(sym.info.finalResultType) // SI-4742 && !sym.hasFlag(PARAMACCESSOR | SPECIALIZED | LAZY) && !sym.accessed.hasFlag(PRESUPER) && !sym.isOuterAccessor diff --git a/test/files/run/t4742.flags b/test/files/run/t4742.flags new file mode 100644 index 0000000000..ae08446055 --- /dev/null +++ b/test/files/run/t4742.flags @@ -0,0 +1 @@ +-Xcheckinit \ No newline at end of file diff --git a/test/files/run/t4742.scala b/test/files/run/t4742.scala new file mode 100644 index 0000000000..3b42c0c120 --- /dev/null +++ b/test/files/run/t4742.scala @@ -0,0 +1,7 @@ +trait T { val x: Int = 0 } +object O extends T { override final val x = 1 } + +object Test extends App { + // was throwing an UnitializedFieldError as constant 1 is folded into the accessor + assert((O: T).x == 1) +} -- cgit v1.2.3