Skip to content

Fix #1567: Widen private constructor in value class #1595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 20, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/dotty/tools/dotc/transform/ExtensionMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ import SymUtils._
* in [[ElimErasedValueType]].
* This is different from the implementation of value classes in Scala 2
* (see SIP-15) which uses `asInstanceOf` which does not typecheck.
*
* Finally, if the constructor of a value class is private pr protected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: pr -> or

* it is widened to public.
*/
class ExtensionMethods extends MiniPhaseTransform with DenotTransformer with FullParameterization { thisTransformer =>

Expand Down Expand Up @@ -96,11 +99,18 @@ class ExtensionMethods extends MiniPhaseTransform with DenotTransformer with Ful
case _ =>
moduleClassSym
}
case ref: SymDenotation
if isMethodWithExtension(ref) && ref.hasAnnotation(defn.TailrecAnnot) =>
val ref1 = ref.copySymDenotation()
ref1.removeAnnotation(defn.TailrecAnnot)
ref1
case ref: SymDenotation =>
if (isMethodWithExtension(ref) && ref.hasAnnotation(defn.TailrecAnnot)) {
val ref1 = ref.copySymDenotation()
ref1.removeAnnotation(defn.TailrecAnnot)
ref1
}
else if (ref.isConstructor && isDerivedValueClass(ref.owner) && ref.is(AccessFlags)) {
val ref1 = ref.copySymDenotation()
ref1.resetFlag(AccessFlags)
ref1
}
else ref
case _ =>
ref
}
Expand Down
6 changes: 6 additions & 0 deletions tests/pos/1567/PosZInt_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
final class PosZInt private (val value: Int) extends AnyVal

object PosZInt {
def from(value: Int): Option[PosZInt] =
if (value >= 0) Some(new PosZInt(value)) else None
}
3 changes: 3 additions & 0 deletions tests/pos/1567/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Test {
scala.util.Try(PosZInt.from(1).get)
}