Skip to content

Commit 239254a

Browse files
authored
Merge pull request #9621 from dotty-staging/reduce-closures
Some more reductions of allocations
2 parents 4c76d89 + 8e17b76 commit 239254a

File tree

7 files changed

+20
-16
lines changed

7 files changed

+20
-16
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ object Types {
143143
final def exists: Boolean = this.ne(NoType)
144144

145145
/** This type, if it exists, otherwise `that` type */
146-
def orElse(that: => Type): Type = if (exists) this else that
146+
inline def orElse(inline that: => Type): Type = if (exists) this else that
147147

148148
/** Is this type a value type? */
149149
final def isValueType: Boolean = this.isInstanceOf[ValueType]

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,11 +1357,10 @@ trait Applications extends Compatibility {
13571357
followApply && tp.member(nme.apply).hasAltWith(d => p(TermRef(tp, nme.apply, d)))
13581358
}
13591359

1360-
/** Does `tp` have an extension method named `extension_name` with this-argument `argType` and
1361-
* result matching `resultType`?
1360+
/** Does `tp` have an extension method named `xname` with this-argument `argType` and
1361+
* result matching `resultType`? `xname` is supposed to start with `extension_`.
13621362
*/
1363-
def hasExtensionMethod(tp: Type, name: TermName, argType: Type, resultType: Type)(using Context) = {
1364-
val xname = name.toExtensionName
1363+
def hasExtensionMethodNamed(tp: Type, xname: TermName, argType: Type, resultType: Type)(using Context) = {
13651364
def qualifies(mbr: Denotation) =
13661365
mbr.exists && isApplicableType(tp.select(xname, mbr), argType :: Nil, resultType)
13671366
tp.memberBasedOnFlags(xname, required = ExtensionMethod) match {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ object Checking {
433433
def checkNoConflict(flag1: FlagSet, flag2: FlagSet, msg: => String) =
434434
if (sym.isAllOf(flag1 | flag2)) fail(msg)
435435
def checkCombination(flag1: FlagSet, flag2: FlagSet) =
436-
checkNoConflict(flag1, flag2, i"illegal combination of modifiers: `${flag1.flagsString}` and `${flag2.flagsString}` for: $sym")
436+
if sym.isAllOf(flag1 | flag2) then fail(i"illegal combination of modifiers: `${flag1.flagsString}` and `${flag2.flagsString}` for: $sym")
437437
def checkApplicable(flag: FlagSet, ok: Boolean) =
438438
if (!ok && !sym.is(Synthetic))
439439
fail(i"modifier `${flag.flagsString}` is not allowed for this definition")

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ object Implicits:
6969
* method with the selecting name? False otherwise.
7070
*/
7171
def hasExtMethod(tp: Type, expected: Type)(using Context) = expected match
72-
case SelectionProto(name, _, _, _) =>
73-
tp.memberBasedOnFlags(name.toExtensionName, required = ExtensionMethod).exists
72+
case selProto @ SelectionProto(_: TermName, _, _, _) =>
73+
tp.memberBasedOnFlags(selProto.extensionName, required = ExtensionMethod).exists
7474
case _ => false
7575

7676
def strictEquality(using Context): Boolean =
@@ -1034,9 +1034,9 @@ trait Implicits:
10341034
pt, locked)
10351035
}
10361036
pt match
1037-
case SelectionProto(name: TermName, mbrType, _, _) if cand.isExtension =>
1037+
case selProto @ SelectionProto(_: TermName, mbrType, _, _) if cand.isExtension =>
10381038
def tryExtension(using Context) =
1039-
extMethodApply(untpd.Select(untpdGenerated, name.toExtensionName), argument, mbrType)
1039+
extMethodApply(untpd.Select(untpdGenerated, selProto.extensionName), argument, mbrType)
10401040
if cand.isConversion then
10411041
val extensionCtx, conversionCtx = ctx.fresh.setNewTyperState()
10421042
val extensionResult = tryExtension(using extensionCtx)

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ object ProtoTypes {
129129
abstract case class SelectionProto(name: Name, memberProto: Type, compat: Compatibility, privateOK: Boolean)
130130
extends CachedProxyType with ProtoType with ValueTypeOrProto {
131131

132+
private var myExtensionName: TermName = null
133+
def extensionName: TermName =
134+
if myExtensionName == null then myExtensionName = name.toExtensionName
135+
myExtensionName
136+
132137
/** Is the set of members of this type unknown? This is the case if:
133138
* 1. The type has Nothing or Wildcard as a prefix or underlying type
134139
* 2. The type has an uninstantiated TypeVar as a prefix or underlying type,
@@ -416,8 +421,8 @@ object ProtoTypes {
416421
def isMatchedBy(tp: Type, keepConstraint: Boolean)(using Context): Boolean =
417422
ctx.typer.isApplicableType(tp, argType :: Nil, resultType) || {
418423
resType match {
419-
case SelectionProto(name: TermName, mbrType, _, _) =>
420-
ctx.typer.hasExtensionMethod(tp, name, argType, mbrType)
424+
case selProto @ SelectionProto(_: TermName, mbrType, _, _) =>
425+
ctx.typer.hasExtensionMethodNamed(tp, selProto.extensionName, argType, mbrType)
421426
//.reporting(i"has ext $tp $name $argType $mbrType: $result")
422427
case _ =>
423428
false

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ class Typer extends Namer
24322432
case tree: untpd.Bind => typedBind(tree, pt)
24332433
case tree: untpd.ValDef =>
24342434
if (tree.isEmpty) tpd.EmptyValDef
2435-
else typedValDef(tree, sym)(using ctx.localContext(tree, sym).setNewScope)
2435+
else typedValDef(tree, sym)(using ctx.localContext(tree, sym))
24362436
case tree: untpd.DefDef =>
24372437
val typer1 = localTyper(sym)
24382438
typer1.typedDefDef(tree, sym)(using ctx.localContext(tree, sym).setTyper(typer1))
@@ -3468,10 +3468,10 @@ class Typer extends Namer
34683468

34693469
// try an extension method in scope
34703470
pt match {
3471-
case SelectionProto(name, mbrType, _, _) =>
3471+
case selProto @ SelectionProto(_: TermName, mbrType, _, _) =>
34723472
def tryExtension(using Context): Tree =
34733473
try
3474-
findRef(name.toExtensionName, WildcardType, ExtensionMethod, tree.srcPos) match {
3474+
findRef(selProto.extensionName, WildcardType, ExtensionMethod, tree.srcPos) match {
34753475
case ref: TermRef =>
34763476
extMethodApply(untpd.ref(ref).withSpan(tree.span), tree, mbrType)
34773477
case _ => EmptyTree

tests/neg/i7813.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
implicit def f[X <: Undefined](implicit a: Int): X = ??? // error
2-
def g(arg: h.NonExistent): Int = ???
2+
def g(arg: h.NonExistent): Int = ??? // error
33
val h: Int = ???

0 commit comments

Comments
 (0)