From e52ac88c4db7b0d2851cb63773e6520da859cb59 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 8 Jul 2019 21:27:46 +0200 Subject: [PATCH 1/4] Handle multiple `val _ = ...` definitions These need to be expanded to pattern definitions, not ValDefs, otherwise duplicate definition errors result. --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 2 +- compiler/src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- tests/pos/wildcardDefs.scala | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 tests/pos/wildcardDefs.scala diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 22c4daab4e87..35e62d523894 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -1616,7 +1616,7 @@ object desugar { */ private object IdPattern { def unapply(tree: Tree)(implicit ctx: Context): Option[VarInfo] = tree match { - case id: Ident => Some(id, TypeTree()) + case id: Ident if id.name != nme.WILDCARD => Some(id, TypeTree()) case Typed(id: Ident, tpt) => Some((id, tpt)) case _ => None } diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index f3a067ec13f1..1d6f048df18f 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -2574,7 +2574,7 @@ object Parsers { } } else EmptyTree lhs match { - case (id @ Ident(name: TermName)) :: Nil => + case (id @ Ident(name: TermName)) :: Nil if name != nme.WILDCARD => val vdef = ValDef(name, tpt, rhs) if (isBackquoted(id)) vdef.pushAttachment(Backquoted, ()) finalizeDef(vdef, mods, start) diff --git a/tests/pos/wildcardDefs.scala b/tests/pos/wildcardDefs.scala new file mode 100644 index 000000000000..3a6d2c1aa696 --- /dev/null +++ b/tests/pos/wildcardDefs.scala @@ -0,0 +1,4 @@ +object Test { + val _ = 2 + val _ = 3 +} \ No newline at end of file From 38e47df6523f07ed612c47f3338d1cbcb2010723 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Tue, 9 Jul 2019 14:06:08 +0200 Subject: [PATCH 2/4] Specialize types of inline values Specialize types of ibline values even if a wider type was gven in an inherited definition. This generalizes a previous scheme for final vals. --- compiler/src/dotty/tools/dotc/typer/Namer.scala | 11 ++--------- tests/pos/givenFallback.scala | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) create mode 100644 tests/pos/givenFallback.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 9a1f89ca7650..8b6f5fa2e256 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -1320,15 +1320,8 @@ class Namer { typer: Typer => def cookedRhsType = deskolemize(dealiasIfUnit(widenRhs(rhsType))) def lhsType = fullyDefinedType(cookedRhsType, "right-hand side", mdef.span) //if (sym.name.toString == "y") println(i"rhs = $rhsType, cooked = $cookedRhsType") - if (inherited.exists) { - if (sym.is(Final, butNot = Method)) { - val tp = lhsType - if (tp.isInstanceOf[ConstantType]) - tp // keep constant types that fill in for a non-constant (to be revised when inline has landed). - else inherited - } - else inherited - } + if (inherited.exists) + if (isInlineVal) lhsType else inherited else { if (sym.is(Implicit)) mdef match { diff --git a/tests/pos/givenFallback.scala b/tests/pos/givenFallback.scala new file mode 100644 index 000000000000..d9cd5c664533 --- /dev/null +++ b/tests/pos/givenFallback.scala @@ -0,0 +1,15 @@ +trait TC[T] { def x: Int; def y: Int = 0 } + +given [T] as TC[T] { + inline val x = 1 +} + +given as TC[Int] { + inline val x = 2 + inline override val y = 3 +} + +object Test extends App { + val z: 2 = the[TC[Int]].x + val _: 3 = the[TC[Int]].y +} \ No newline at end of file From 98e0404333a43ae1bc4fb7fca8726184b83be711 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Wed, 10 Jul 2019 11:31:43 +0200 Subject: [PATCH 3/4] Fix Scala2 unpickler's handling of package references --- .../dotc/core/unpickleScala2/Scala2Unpickler.scala | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 9b4b626655fe..d3b2dca997df 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -690,6 +690,15 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas } else tp1 } + /** Read type ref, mapping a TypeRef to a package to the package's ThisType + * Packae references should be TermRefs or ThisTypes but it was observed that + * nsc sometimes pickles them as TypeRefs instead. + */ + private def readPrefix()(implicit ctx: Context): Type = readTypeRef() match { + case pre: TypeRef if pre.symbol.is(Package) => pre.symbol.thisType + case pre => pre + } + /** Read a type * * @param forceProperType is used to ease the transition to NullaryMethodTypes (commentmarker: NMT_TRANSITION) @@ -707,7 +716,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas case THIStpe => readSymbolRef().thisType case SINGLEtpe => - val pre = readTypeRef() + val pre = readPrefix() val sym = readDisambiguatedSymbolRef(_.info.isParameterless) pre.select(sym) case SUPERtpe => @@ -717,7 +726,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas case CONSTANTtpe => ConstantType(readConstantRef()) case TYPEREFtpe => - var pre = readTypeRef() + var pre = readPrefix() val sym = readSymbolRef() pre match { case thispre: ThisType => From e457160af129f59fcb8a967403d82cb9bbcf8084 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Thu, 11 Jul 2019 13:45:07 +0200 Subject: [PATCH 4/4] Fix typo in doc --- .../dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index d3b2dca997df..7e671d19c810 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -691,7 +691,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas } /** Read type ref, mapping a TypeRef to a package to the package's ThisType - * Packae references should be TermRefs or ThisTypes but it was observed that + * Package references should be TermRefs or ThisTypes but it was observed that * nsc sometimes pickles them as TypeRefs instead. */ private def readPrefix()(implicit ctx: Context): Type = readTypeRef() match {