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

Skip to content

Commit 92bd67a

Browse files
committed
ApplyImplicitView is Applied attachment
1 parent 66a9ac7 commit 92bd67a

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

src/compiler/scala/tools/nsc/typechecker/RefChecks.scala

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import scala.annotation._
1717
import scala.collection.mutable
1818
import scala.collection.mutable.ListBuffer
1919
import scala.reflect.internal.util.CodeAction
20-
import scala.tools.nsc.Reporting.WarningCategory
20+
import scala.tools.nsc.Reporting.WarningCategory, WarningCategory._
2121
import scala.tools.nsc.settings.ScalaVersion
2222
import scala.tools.nsc.settings.NoScalaVersion
2323
import symtab.Flags._
@@ -994,11 +994,13 @@ abstract class RefChecks extends Transform {
994994
def apply(tp: Type) = mapOver(tp).normalize
995995
}
996996

997-
def checkImplicitViewOptionApply(pos: Position, fun: Tree, argss: List[List[Tree]]): Unit = if (settings.warnOptionImplicit) argss match {
998-
case List(List(view: ApplyImplicitView)) if fun.symbol == currentRun.runDefinitions.Option_apply =>
999-
refchecksWarning(pos, s"Suspicious application of an implicit view (${view.fun}) in the argument to Option.apply.", WarningCategory.LintOptionImplicit) // scala/bug#6567
1000-
case _ =>
1001-
}
997+
def checkImplicitViewOptionApply(pos: Position, fun: Tree, argss: List[List[Tree]]): Unit =
998+
if (settings.warnOptionImplicit && fun.symbol == currentRun.runDefinitions.Option_apply)
999+
argss match {
1000+
case (((view @ Apply(coercion, _)) :: Nil) :: Nil) if view.hasAttachment[AppliedImplicitView.type] =>
1001+
refchecksWarning(pos, s"Suspicious application of an implicit view ($coercion) in the argument to Option.apply.", LintOptionImplicit) // scala/bug#6567
1002+
case _ =>
1003+
}
10021004

10031005
private def isObjectOrAnyComparisonMethod(sym: Symbol) = sym match {
10041006
case Object_eq | Object_ne | Object_== | Object_!= | Any_== | Any_!= => true
@@ -2074,7 +2076,7 @@ abstract class RefChecks extends Transform {
20742076
def checkImplicitlyAdaptedBlockResult(t: Tree): Unit = {
20752077
def loop(t: Tree): Unit =
20762078
t match {
2077-
case Apply(coercion, _) if t.isInstanceOf[ApplyImplicitView] =>
2079+
case Apply(coercion, _) if t.hasAttachment[AppliedImplicitView.type] =>
20782080
coercion.symbol.paramLists match {
20792081
case (p :: Nil) :: _ if p.isByNameParam => refchecksWarning(t.pos, s"Block result expression was adapted via implicit conversion (${coercion.symbol}) taking a by-name parameter; only the result was passed, not the entire block.", WarningCategory.LintBynameImplicit)
20802082
case _ =>

src/compiler/scala/tools/nsc/typechecker/Typers.scala

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
12161216
if (settings.logImplicitConv.value) context.echo(tree.pos, msg)
12171217
else debuglog(msg)
12181218

1219-
val viewApplied = new ApplyImplicitView(coercion, List(tree)) setPos tree.pos
1219+
val viewApplied = ApplyImplicitView(coercion, List(tree)).setPos(tree.pos)
12201220
val silentContext = context.makeImplicit(context.ambiguousErrors)
12211221
val typedView = newTyper(silentContext).typed(viewApplied, mode, pt)
12221222

@@ -1383,7 +1383,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
13831383
&& !qtpe.isError
13841384
&& !qtpe.typeSymbol.isBottomClass
13851385
&& !qtpe.isWildcard
1386-
&& !qual.isInstanceOf[ApplyImplicitView] // don't chain views
1386+
&& !qual.hasAttachment[AppliedImplicitView.type] // don't chain views
13871387
&& (context.implicitsEnabled || context.enrichmentEnabled)
13881388
// Elaborating `context.implicitsEnabled`:
13891389
// don't try to adapt a top-level type that's the subject of an implicit search
@@ -1410,7 +1410,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
14101410
if (currentRun.isScala3 && coercion.symbol == currentRun.runDefinitions.Predef_any2stringaddMethod)
14111411
if (!currentRun.sourceFeatures.any2StringAdd)
14121412
runReporting.warning(qual.pos, s"Converting to String for concatenation is not supported in Scala 3 (or with -Xsource-features:any2stringadd).", Scala3Migration, coercion.symbol)
1413-
typedQualifier(atPos(qual.pos)(new ApplyImplicitView(coercion, List(qual))))
1413+
typedQualifier(atPos(qual.pos)(ApplyImplicitView(coercion, List(qual))))
14141414
}
14151415
}
14161416
else qual
@@ -3680,8 +3680,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
36803680
val treeInfo.Applied(_, _, argss) = appl
36813681
val needsAdjust =
36823682
argss.find {
3683-
case (aiv: ApplyImplicitView) :: Nil =>
3684-
aiv.args match {
3683+
case (view @ Apply(_, viewed)) :: Nil if view.hasAttachment[AppliedImplicitView.type] =>
3684+
viewed match {
36853685
case Block(_ :: _, _) :: Nil => true
36863686
case _ => false
36873687
}
@@ -5224,9 +5224,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
52245224
fn.symbol.paramLists match {
52255225
case (h :: Nil) :: Nil if !h.isByNameParam && (!isUserRassocArg(arg) || needsRewrite(arg)) =>
52265226
val arg1 = arg match {
5227-
case view: ApplyImplicitView if usesStab(view.fun) =>
5228-
new ApplyImplicitView(view.fun, rewriteRightAssoc(view.args.head) :: Nil)
5229-
.setType(view.tpe).setPos(view.pos)
5227+
case view @ Apply(coercion, coerced :: Nil) if view.hasAttachment[AppliedImplicitView.type] && usesStab(coercion) =>
5228+
treeCopy.Apply(view, coercion, rewriteRightAssoc(coerced) :: Nil)
52305229
case arg => rewriteRightAssoc(arg)
52315230
}
52325231
treeCopy.Apply(applied, fn, arg1 :: Nil)
@@ -6358,8 +6357,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
63586357
// if in the midst of series of apply, possibly a qualifier was adapted, possibly an rassoc arg.
63596358
// the expression to rewrite with added stabilizers is different for each case.
63606359
val result = adapted match {
6361-
case view: ApplyImplicitView if shouldInsertRassocs =>
6362-
new ApplyImplicitView(view.fun, addStabilizers(view.args.head) :: Nil)
6360+
case view @ Apply(coercion, coerced :: Nil) if view.hasAttachment[AppliedImplicitView.type] && shouldInsertRassocs =>
6361+
treeCopy.Apply(view, coercion, addStabilizers(coerced) :: Nil)
63636362
case _ if shouldInsertStabilizers => addStabilizers(adapted)
63646363
case _ => adapted
63656364
}

src/reflect/scala/reflect/internal/Importers.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,6 @@ trait Importers { to: SymbolTable =>
382382
case from.Apply(fun, args) => their match {
383383
case _: from.ApplyToImplicitArgs =>
384384
new ApplyToImplicitArgs(importTree(fun), args map importTree)
385-
case _: from.ApplyImplicitView =>
386-
new ApplyImplicitView(importTree(fun), args map importTree)
387385
case _ =>
388386
new Apply(importTree(fun), args map importTree)
389387
}

src/reflect/scala/reflect/internal/StdAttachments.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,7 @@ trait StdAttachments {
188188
case object RightAssociative extends PlainAttachment
189189
/** Arg to right associative infix application is candidate for rewriting. */
190190
case object RightAssociativeArg extends PlainAttachment
191+
192+
/** Application is an implicit view. */
193+
case object AppliedImplicitView extends PlainAttachment
191194
}

src/reflect/scala/reflect/internal/Trees.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -809,9 +809,7 @@ trait Trees extends api.Trees {
809809
// copying trees will all too easily forget to distinguish subclasses
810810
class ApplyToImplicitArgs(fun: Tree, args: List[Tree]) extends Apply(fun, args)
811811

812-
// TODO remove this class, add a tree attachment to Apply to track whether implicits were involved
813-
// copying trees will all too easily forget to distinguish subclasses
814-
class ApplyImplicitView(fun: Tree, args: List[Tree]) extends Apply(fun, args)
812+
def ApplyImplicitView(fun: Tree, args: List[Tree]) = Apply(fun, args).updateAttachment(AppliedImplicitView)
815813

816814
def ApplyConstructor(tpt: Tree, args: List[Tree]) = Apply(Select(New(tpt), nme.CONSTRUCTOR), args)
817815

@@ -1114,7 +1112,6 @@ trait Trees extends api.Trees {
11141112
def Apply(tree: Tree, fun: Tree, args: List[Tree]) =
11151113
(tree match { // TODO: use a tree attachment to track whether this is an apply to implicit args or a view
11161114
case _: ApplyToImplicitArgs => new ApplyToImplicitArgs(fun, args)
1117-
case _: ApplyImplicitView => new ApplyImplicitView(fun, args)
11181115
// TODO: ApplyConstructor ???
11191116
case self.pendingSuperCall => self.pendingSuperCall
11201117
case _ => new Apply(fun, args)

src/reflect/scala/reflect/runtime/JavaUniverseForce.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ trait JavaUniverseForce { self: runtime.JavaUniverse =>
9393
this.BooleanParameterType
9494
this.RightAssociative
9595
this.RightAssociativeArg
96+
this.AppliedImplicitView
9697
this.noPrint
9798
this.typeDebug
9899
// inaccessible: this.posAssigner

src/scaladoc/scala/tools/nsc/doc/model/ModelFactoryImplicitSupport.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ trait ModelFactoryImplicitSupport {
192192

193193
// type the view application so we get the exact type of the result (not the formal type)
194194
val viewTree = result.tree.setType(viewSimplifiedType)
195-
val appliedTree = new ApplyImplicitView(viewTree, List(Ident("<argument>") setType viewTree.tpe.paramTypes.head))
195+
val appliedTree = ApplyImplicitView(viewTree, List(Ident("<argument>") setType viewTree.tpe.paramTypes.head))
196196
val appliedTreeTyped: Tree = {
197197
val newContext = context.makeImplicit(context.ambiguousErrors)
198198
newContext.macrosEnabled = false

0 commit comments

Comments
 (0)