From 6d22805793cd25427469cceb89258fdbca42630b Mon Sep 17 00:00:00 2001 From: Paul Phillips Date: Tue, 26 Oct 2010 06:00:20 +0000 Subject: Case accessors are always public else the patte... Case accessors are always public else the pattern matcher goes south. A more discriminating fix may be possible at some point, but it looks to be an involved endeavor. Closes #3714, review by odersky. --- .../tools/nsc/typechecker/SyntheticMethods.scala | 2 +- test/files/neg/bug3714-neg.check | 13 +++++++ test/files/neg/bug3714-neg.scala | 41 ++++++++++++++++++++++ test/files/run/bug3714.scala | 33 +++++++++++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/files/neg/bug3714-neg.check create mode 100644 test/files/neg/bug3714-neg.scala create mode 100644 test/files/run/bug3714.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala index 50e150c211..61894092f3 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SyntheticMethods.scala @@ -222,7 +222,7 @@ trait SyntheticMethods extends ast.TreeDSL { case DefDef(_, _, _, _, _, rhs) => var newAcc = tree.symbol.cloneSymbol newAcc.name = context.unit.fresh.newName(tree.symbol.pos.focus, tree.symbol.name + "$") - newAcc setFlag SYNTHETIC resetFlag (ACCESSOR | PARAMACCESSOR | PRIVATE) + newAcc setFlag SYNTHETIC resetFlag (ACCESSOR | PARAMACCESSOR | PRIVATE | PROTECTED) newAcc.privateWithin = NoSymbol newAcc = newAcc.owner.info.decls enter newAcc val result = typer typed { DEF(newAcc) === rhs.duplicate } diff --git a/test/files/neg/bug3714-neg.check b/test/files/neg/bug3714-neg.check new file mode 100644 index 0000000000..fab6623001 --- /dev/null +++ b/test/files/neg/bug3714-neg.check @@ -0,0 +1,13 @@ +bug3714-neg.scala:17: error: value break in class BreakImpl cannot be accessed in BreakImpl + Access to protected value break not permitted because + enclosing class object Test is not a subclass of + class BreakImpl where target is defined + case b: BreakImpl => b.break + ^ +bug3714-neg.scala:25: error: value break in class BreakImpl cannot be accessed in BreakImpl + Access to protected value break not permitted because + enclosing class object Test is not a subclass of + class BreakImpl where target is defined + case b: BreakImpl => b.break + ^ +two errors found diff --git a/test/files/neg/bug3714-neg.scala b/test/files/neg/bug3714-neg.scala new file mode 100644 index 0000000000..19bdebbfa9 --- /dev/null +++ b/test/files/neg/bug3714-neg.scala @@ -0,0 +1,41 @@ +// this is a slight negative twist on run/bug3714.scala. +trait Break { + protected val break: Int; +} + +class BreakImpl(protected val break: Int) extends Break { } +object BreakImpl { + def apply(x: Int): Break = new BreakImpl(x) + def unapply(x: Any) = x match { + case x: BreakImpl => Some(x.break) + case _ => None + } +} + +object Test { + def f1(x: Break) = x match { + case b: BreakImpl => b.break + case b => -1 + } + def f2(x: Break) = x match { + case BreakImpl(x) => x + case _ => -1 + } + def f3(x: Any) = x match { + case b: BreakImpl => b.break + case b => -1 + } + def f4(x: Any) = x match { + case BreakImpl(x) => x + case _ => -1 + } + + def main(args: Array[String]) { + val break = BreakImpl(22) + assert(f1(break) == 22) + assert(f2(break) == 22) + assert(f3(break) == 22) + assert(f4(break) == 22) + } +} + diff --git a/test/files/run/bug3714.scala b/test/files/run/bug3714.scala new file mode 100644 index 0000000000..2d600f97f1 --- /dev/null +++ b/test/files/run/bug3714.scala @@ -0,0 +1,33 @@ +trait Break { + protected val break: Int; +} + +case class BreakImpl(protected val break: Int) extends Break { } + +object Test { + // def f1(x: Break) = x match { + // case b: BreakImpl => b.break + // case b => -1 + // } + def f2(x: Break) = x match { + case BreakImpl(x) => x + case _ => -1 + } + // def f3(x: Any) = x match { + // case b: BreakImpl => b.break + // case b => -1 + // } + def f4(x: Any) = x match { + case BreakImpl(x) => x + case _ => -1 + } + + def main(args: Array[String]) { + val break = BreakImpl(22) + // assert(f1(break) == 22) + assert(f2(break) == 22) + // assert(f3(break) == 22) + assert(f4(break) == 22) + } +} + -- cgit v1.2.3