Skip to content

Commit 1f4aad6

Browse files
Merge pull request #3065 from biboudis/fix-i3050
Fix #3050: Avoid retyping synthesised code
2 parents 1dda6e1 + 40a183a commit 1f4aad6

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class ReTyper extends Typer {
5858
}
5959

6060
override def typedUnApply(tree: untpd.UnApply, selType: Type)(implicit ctx: Context): UnApply = {
61-
val fun1 = typedExpr(tree.fun, AnyFunctionProto)
61+
val fun1 = typedUnadapted(tree.fun, AnyFunctionProto)
6262
val implicits1 = tree.implicits.map(typedExpr(_))
6363
val patterns1 = tree.patterns.mapconserve(pat => typed(pat, pat.tpe))
6464
untpd.cpy.UnApply(tree)(fun1, implicits1, patterns1).withType(tree.tpe)
@@ -104,4 +104,6 @@ class ReTyper extends Typer {
104104
Implicits.NoImplicitMatches
105105
override def checkCanEqual(ltp: Type, rtp: Type, pos: Position)(implicit ctx: Context): Unit = ()
106106
override def inlineExpansion(mdef: DefDef)(implicit ctx: Context): List[Tree] = mdef :: Nil
107+
108+
override protected def checkEqualityEvidence(tree: tpd.Tree, pt: Type)(implicit ctx: Context): Unit = ()
107109
}

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

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package typer
44

55
import core._
6-
import ast._
6+
import ast.{tpd, _}
77
import Trees._
88
import Constants._
99
import StdNames._
@@ -28,15 +28,17 @@ import EtaExpansion.etaExpand
2828
import dotty.tools.dotc.transform.Erasure.Boxing
2929
import util.Positions._
3030
import util.common._
31-
import util.{SourcePosition, Property}
31+
import util.{Property, SourcePosition}
32+
3233
import collection.mutable
3334
import annotation.tailrec
3435
import Implicits._
35-
import util.Stats.{track, record}
36-
import config.Printers.{typr, gadts}
36+
import util.Stats.{record, track}
37+
import config.Printers.{gadts, typr}
3738
import rewrite.Rewrites.patch
3839
import NavigateAST._
3940
import transform.SymUtils._
41+
4042
import language.implicitConversions
4143
import printing.SyntaxHighlighting._
4244

@@ -2126,17 +2128,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
21262128
typed(untpd.Select(untpd.TypedSplice(tree), nme.apply), pt)
21272129
}
21282130
else if (ctx.mode is Mode.Pattern) {
2129-
tree match {
2130-
case _: RefTree | _: Literal
2131-
if !isVarPattern(tree) &&
2132-
!(tree.tpe <:< pt)(ctx.addMode(Mode.GADTflexible)) =>
2133-
val cmp =
2134-
untpd.Apply(
2135-
untpd.Select(untpd.TypedSplice(tree), nme.EQ),
2136-
untpd.TypedSplice(dummyTreeOfType(pt)))
2137-
typedExpr(cmp, defn.BooleanType)(ctx.retractMode(Mode.Pattern))
2138-
case _ =>
2139-
}
2131+
checkEqualityEvidence(tree, pt)
21402132
tree
21412133
}
21422134
else if (tree.tpe <:< pt) {
@@ -2289,4 +2281,24 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
22892281
}
22902282
}
22912283
}
2284+
2285+
/** Check that `tree == x: pt` is typeable. Used when checking a pattern
2286+
* against a selector of type `pt`. This implementation accounts for
2287+
* user-defined definitions of `==`.
2288+
*
2289+
* Overwritten to no-op in ReTyper.
2290+
*/
2291+
protected def checkEqualityEvidence(tree: tpd.Tree, pt: Type)(implicit ctx: Context) : Unit = {
2292+
tree match {
2293+
case _: RefTree | _: Literal
2294+
if !isVarPattern(tree) &&
2295+
!(tree.tpe <:< pt) (ctx.addMode(Mode.GADTflexible)) =>
2296+
val cmp =
2297+
untpd.Apply(
2298+
untpd.Select(untpd.TypedSplice(tree), nme.EQ),
2299+
untpd.TypedSplice(dummyTreeOfType(pt)))
2300+
typedExpr(cmp, defn.BooleanType)
2301+
case _ =>
2302+
}
2303+
}
22922304
}

tests/pos/i3050.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
object Test {
2+
inline def openImpl(): Int =
3+
Some(42) match { case Some(i) => i }
4+
5+
def open(): Int = openImpl()
6+
7+
inline def openImpl2(): Int =
8+
Some(42) match { case None => 42 }
9+
10+
def open2(): Int = openImpl2()
11+
}

0 commit comments

Comments
 (0)