Skip to content

Commit 5f3e0df

Browse files
Backport "Optimise SpaceEngine.signature" to 3.6.2 (#21899)
Backports #21791 to the 3.6.2 branch. PR submitted by the release tooling. [skip ci]
2 parents b04616a + 9ea1e9b commit 5f3e0df

File tree

8 files changed

+3102
-11
lines changed

8 files changed

+3102
-11
lines changed

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,9 @@ object TypeOps:
902902
}
903903

904904
val inferThisMap = new InferPrefixMap
905-
val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds, DepParamName.fresh(tparam.paramName)) }
905+
val tvars = tp1.etaExpand match
906+
case eta: TypeLambda => constrained(eta)
907+
case _ => Nil
906908
val protoTp1 = inferThisMap.apply(tp1).appliedTo(tvars)
907909

908910
if gadtSyms.nonEmpty then

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4395,9 +4395,11 @@ object Types extends TypeUtils {
43954395

43964396
/** Distributes Lambda inside type bounds. Examples:
43974397
*
4398-
* type T[X] = U becomes type T = [X] -> U
4399-
* type T[X] <: U becomes type T >: Nothing <: ([X] -> U)
4400-
* type T[X] >: L <: U becomes type T >: ([X] -> L) <: ([X] -> U)
4398+
* {{{
4399+
* type T[X] = U becomes type T = [X] =>> U
4400+
* type T[X] <: U becomes type T >: Nothing <: ([X] =>> U)
4401+
* type T[X] >: L <: U becomes type T >: ([X] =>> L) <: ([X] =>> U)
4402+
* }}}
44014403
*
44024404
* The variances of regular TypeBounds types, as well as of match aliases
44034405
* and of opaque aliases are always determined from the given parameters
@@ -4409,13 +4411,15 @@ object Types extends TypeUtils {
44094411
*
44104412
* Examples:
44114413
*
4414+
* {{{
44124415
* type T[X] >: A // X is invariant
44134416
* type T[X] <: List[X] // X is invariant
44144417
* type T[X] = List[X] // X is covariant (determined structurally)
44154418
* opaque type T[X] = List[X] // X is invariant
44164419
* opaque type T[+X] = List[X] // X is covariant
44174420
* type T[A, B] = A => B // A is contravariant, B is covariant (determined structurally)
44184421
* type T[A, +B] = A => B // A is invariant, B is covariant
4422+
* }}}
44194423
*/
44204424
def boundsFromParams[PI <: ParamInfo.Of[TypeName]](params: List[PI], bounds: TypeBounds)(using Context): TypeBounds = {
44214425
def expand(tp: Type, useVariances: Boolean) =
@@ -4702,6 +4706,7 @@ object Types extends TypeUtils {
47024706
type BT <: LambdaType
47034707
def paramNum: Int
47044708
def paramName: binder.ThisName = binder.paramNames(paramNum)
4709+
def paramInfo: binder.PInfo = binder.paramInfos(paramNum)
47054710

47064711
override def underlying(using Context): Type = {
47074712
// TODO: update paramInfos's type to nullable

compiler/src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,15 @@ object Formatting {
115115
given Show[Char] = ShowAny
116116
given Show[Boolean] = ShowAny
117117
given Show[Integer] = ShowAny
118+
given Show[Long] = ShowAny
118119
given Show[String] = ShowAny
119120
given Show[Class[?]] = ShowAny
120121
given Show[Throwable] = ShowAny
121122
given Show[StringBuffer] = ShowAny
122123
given Show[CompilationUnit] = ShowAny
123124
given Show[Phases.Phase] = ShowAny
124125
given Show[TyperState] = ShowAny
126+
given Show[Unit] = ShowAny
125127
given Show[config.ScalaVersion] = ShowAny
126128
given Show[io.AbstractFile] = ShowAny
127129
given Show[parsing.Scanners.Scanner] = ShowAny

compiler/src/dotty/tools/dotc/reporting/trace.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ trait TraceSyntax:
9696
(op: => T)(using Context): T =
9797
if ctx.mode.is(Mode.Printing) || !isForced && (printer eq Printers.noPrinter) then op
9898
else
99+
val start = System.nanoTime
99100
// Avoid evaluating question multiple time, since each evaluation
100101
// may cause some extra logging output.
101102
val q = question
@@ -109,7 +110,13 @@ trait TraceSyntax:
109110
def finalize(msg: String) =
110111
if !finalized then
111112
ctx.base.indent -= 1
112-
doLog(s"$margin$msg")
113+
val stop = System.nanoTime
114+
val diffNs = stop - start
115+
val diffS = (diffNs / 1000 / 1000).toInt / 1000.0
116+
if diffS > 0.1 then
117+
doLog(s"$margin$msg (${"%.2f".format(diffS)} s)")
118+
else
119+
doLog(s"$margin$msg")
113120
finalized = true
114121
try
115122
doLog(s"$margin$leading")

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import core.*
77
import Constants.*, Contexts.*, Decorators.*, Flags.*, NullOpsDecorator.*, Symbols.*, Types.*
88
import Names.*, NameOps.*, StdNames.*
99
import ast.*, tpd.*
10-
import config.Printers.*
10+
import config.Printers.exhaustivity
1111
import printing.{ Printer, * }, Texts.*
1212
import reporting.*
1313
import typer.*, Applications.*, Inferencing.*, ProtoTypes.*
@@ -524,14 +524,25 @@ object SpaceEngine {
524524
val mt: MethodType = unapp.widen match {
525525
case mt: MethodType => mt
526526
case pt: PolyType =>
527+
scrutineeTp match
528+
case AppliedType(tycon, targs)
529+
if unappSym.is(Synthetic)
530+
&& (pt.resultType.asInstanceOf[MethodType].paramInfos.head.typeConstructor eq tycon) =>
531+
// Special case synthetic unapply/unapplySeq's
532+
// Provided the shapes of the types match:
533+
// the scrutinee type being unapplied and
534+
// the unapply parameter type
535+
pt.instantiate(targs).asInstanceOf[MethodType]
536+
case _ =>
527537
val locked = ctx.typerState.ownedVars
528538
val tvars = constrained(pt)
529539
val mt = pt.instantiate(tvars).asInstanceOf[MethodType]
530-
scrutineeTp <:< mt.paramInfos(0)
540+
val unapplyArgType = mt.paramInfos.head
541+
scrutineeTp <:< unapplyArgType
531542
// force type inference to infer a narrower type: could be singleton
532543
// see tests/patmat/i4227.scala
533-
mt.paramInfos(0) <:< scrutineeTp
534-
maximizeType(mt.paramInfos(0), Spans.NoSpan)
544+
unapplyArgType <:< scrutineeTp
545+
maximizeType(unapplyArgType, Spans.NoSpan)
535546
if !(ctx.typerState.ownedVars -- locked).isEmpty then
536547
// constraining can create type vars out of wildcard types
537548
// (in legalBound, by using a LevelAvoidMap)
@@ -543,7 +554,7 @@ object SpaceEngine {
543554
// but I'd rather have an unassigned new-new type var, than an infinite loop.
544555
// After all, there's nothing strictly "wrong" with unassigned type vars,
545556
// it just fails TreeChecker's linting.
546-
maximizeType(mt.paramInfos(0), Spans.NoSpan)
557+
maximizeType(unapplyArgType, Spans.NoSpan)
547558
mt
548559
}
549560

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ trait ImportSuggestions:
264264
end importSuggestions
265265

266266
/** Reduce next timeout for import suggestions by the amount of time it took
267-
* for current search, but but never less than to half of the previous budget.
267+
* for current search, but never less than to half of the previous budget.
268268
*/
269269
private def reduceTimeBudget(used: Int)(using Context) =
270270
val run = ctx.run.nn

0 commit comments

Comments
 (0)