Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88d0d02

Browse files
committed
Minor refactor typedNewFun
1 parent 51dc961 commit 88d0d02

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/compiler/scala/tools/nsc/transform/UnCurry.scala

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ abstract class UnCurry extends InfoTransform
220220
// Normally, we can unwrap `() => cbn` to `cbn` where `cbn` refers to a CBN argument (typically `cbn` is an Ident),
221221
// because we know `cbn` will already be a `Function0` thunk. When we're targeting a SAM,
222222
// the types don't align and we must preserve the function wrapper.
223-
if (fun.vparams.isEmpty && isByNameRef(fun.body) && fun.attachments.get[SAMFunction].isEmpty) {
223+
if (fun.vparams.isEmpty && isByNameRef(fun.body) && !fun.attachments.contains[SAMFunction]) {
224224
noApply += fun.body
225225
fun.body
226226
}
@@ -236,27 +236,26 @@ abstract class UnCurry extends InfoTransform
236236
val newFun = deriveFunction(fun)(_ => localTyper.typedPos(fun.pos)(
237237
gen.mkForwarder(gen.mkAttributedRef(liftedMethod.symbol), (fun.vparams map (_.symbol)) :: Nil)
238238
))
239+
def typeNewFun = localTyper.typedPos(fun.pos)(Block(liftedMethod :: Nil, super.transform(newFun)))
239240

240241
if (!mustExpand) {
241242
liftedMethod.symbol.updateAttachment(DelambdafyTarget)
242243
liftedMethod.updateAttachment(DelambdafyTarget)
244+
typeNewFun
243245
}
244-
245-
val typedNewFun = localTyper.typedPos(fun.pos)(Block(liftedMethod :: Nil, super.transform(newFun)))
246-
if (mustExpand) {
247-
val Block(stats, expr : Function) = typedNewFun: @unchecked
248-
val expansion = gen.expandFunction(localTyper)(expr, inConstructorFlag)
249-
expansion match {
250-
case Block(ClassDef(_, _, _, Template(_, _, body)) :: Nil, _) =>
251-
body.last match {
252-
case DefDef(_, _, _, _, _, Apply(_, args)) => noApply.addAll(args) // samDef args are pass-thru
253-
case _ =>
254-
}
255-
case _ =>
256-
}
257-
treeCopy.Block(typedNewFun, stats, expansion)
258-
} else {
259-
typedNewFun
246+
else typeNewFun match {
247+
case typedNewFun @ Block(stats, expr: Function) =>
248+
val expansion = gen.expandFunction(localTyper)(expr, inConstructorFlag)
249+
expansion match {
250+
case Block(ClassDef(_, _, _, Template(_, _, body)) :: Nil, _) =>
251+
body.last match {
252+
case DefDef(_, _, _, _, _, Apply(_, args)) => noApply.addAll(args) // samDef args are pass-thru
253+
case _ =>
254+
}
255+
case _ =>
256+
}
257+
treeCopy.Block(typedNewFun, stats, expansion)
258+
case x => throw new MatchError(x)
260259
}
261260
}
262261

src/reflect/scala/reflect/macros/Attachments.scala

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,15 @@ abstract class Attachments { self =>
6666
}
6767

6868
/** Check underlying payload contains an instance of type `T`. */
69-
def contains[T: ClassTag]: Boolean =
70-
!isEmpty && (all exists matchesTag[T])
69+
def contains[T: ClassTag]: Boolean = !isEmpty && {
70+
val it = all.iterator
71+
val matchesTagFn = matchesTag[T]
72+
while (it.hasNext) { // OPT: hotspot, hand roll `Set.exists`.
73+
val datum = it.next()
74+
if (matchesTagFn(datum)) return true
75+
}
76+
false
77+
}
7178

7279
/** Creates a copy of this attachment with the payload slot of T added/updated with the provided value.
7380
* Replaces an existing payload of the same type, if exists.

0 commit comments

Comments
 (0)