Skip to content

Commit ca5916e

Browse files
authored
Merge pull request #6824 from dotty-staging/specialize-inline-vals
Three tweaks
2 parents 15b9cd8 + e457160 commit ca5916e

File tree

6 files changed

+34
-13
lines changed

6 files changed

+34
-13
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ object desugar {
16161616
*/
16171617
private object IdPattern {
16181618
def unapply(tree: Tree)(implicit ctx: Context): Option[VarInfo] = tree match {
1619-
case id: Ident => Some(id, TypeTree())
1619+
case id: Ident if id.name != nme.WILDCARD => Some(id, TypeTree())
16201620
case Typed(id: Ident, tpt) => Some((id, tpt))
16211621
case _ => None
16221622
}

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,15 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
690690
} else tp1
691691
}
692692

693+
/** Read type ref, mapping a TypeRef to a package to the package's ThisType
694+
* Package references should be TermRefs or ThisTypes but it was observed that
695+
* nsc sometimes pickles them as TypeRefs instead.
696+
*/
697+
private def readPrefix()(implicit ctx: Context): Type = readTypeRef() match {
698+
case pre: TypeRef if pre.symbol.is(Package) => pre.symbol.thisType
699+
case pre => pre
700+
}
701+
693702
/** Read a type
694703
*
695704
* @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
707716
case THIStpe =>
708717
readSymbolRef().thisType
709718
case SINGLEtpe =>
710-
val pre = readTypeRef()
719+
val pre = readPrefix()
711720
val sym = readDisambiguatedSymbolRef(_.info.isParameterless)
712721
pre.select(sym)
713722
case SUPERtpe =>
@@ -717,7 +726,7 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
717726
case CONSTANTtpe =>
718727
ConstantType(readConstantRef())
719728
case TYPEREFtpe =>
720-
var pre = readTypeRef()
729+
var pre = readPrefix()
721730
val sym = readSymbolRef()
722731
pre match {
723732
case thispre: ThisType =>

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2574,7 +2574,7 @@ object Parsers {
25742574
}
25752575
} else EmptyTree
25762576
lhs match {
2577-
case (id @ Ident(name: TermName)) :: Nil =>
2577+
case (id @ Ident(name: TermName)) :: Nil if name != nme.WILDCARD =>
25782578
val vdef = ValDef(name, tpt, rhs)
25792579
if (isBackquoted(id)) vdef.pushAttachment(Backquoted, ())
25802580
finalizeDef(vdef, mods, start)

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,15 +1323,8 @@ class Namer { typer: Typer =>
13231323
def cookedRhsType = deskolemize(dealiasIfUnit(widenRhs(rhsType)))
13241324
def lhsType = fullyDefinedType(cookedRhsType, "right-hand side", mdef.span)
13251325
//if (sym.name.toString == "y") println(i"rhs = $rhsType, cooked = $cookedRhsType")
1326-
if (inherited.exists) {
1327-
if (sym.is(Final, butNot = Method)) {
1328-
val tp = lhsType
1329-
if (tp.isInstanceOf[ConstantType])
1330-
tp // keep constant types that fill in for a non-constant (to be revised when inline has landed).
1331-
else inherited
1332-
}
1333-
else inherited
1334-
}
1326+
if (inherited.exists)
1327+
if (isInlineVal) lhsType else inherited
13351328
else {
13361329
if (sym.is(Implicit))
13371330
mdef match {

tests/pos/givenFallback.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
trait TC[T] { def x: Int; def y: Int = 0 }
2+
3+
given [T] as TC[T] {
4+
inline val x = 1
5+
}
6+
7+
given as TC[Int] {
8+
inline val x = 2
9+
inline override val y = 3
10+
}
11+
12+
object Test extends App {
13+
val z: 2 = the[TC[Int]].x
14+
val _: 3 = the[TC[Int]].y
15+
}

tests/pos/wildcardDefs.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test {
2+
val _ = 2
3+
val _ = 3
4+
}

0 commit comments

Comments
 (0)