/
SemanticdbAnalyzer.scala
1098 lines (1024 loc) · 52.1 KB
/
SemanticdbAnalyzer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// NOTE: has to be this package or otherwise we won't be able to access private[typechecker] methods
package scala
package tools.nsc
package typechecker
import scala.tools.nsc.typechecker.{Analyzer => NscAnalyzer}
import scala.reflect.internal.Mode._
import scala.reflect.internal.util._
import scala.reflect.internal.Flags._
import scala.meta.internal.semanticdb.scalac.ReflectionToolkit
// NOTE: grep for "scalac deviation" to see what exactly has been changed
// I had to copy/paste a lot of code from Namers and Typers, but only a small percentage is actually relevant
trait SemanticdbAnalyzer extends NscAnalyzer with ReflectionToolkit {
import global._
import definitions._
import TypersStats._
override def newNamer(context: Context) = new SemanticdbNamer(context)
class SemanticdbNamer(context0: Context) extends Namer(context0) {
override def selfTypeCompleter(tree: Tree) = mkTypeCompleter(tree) { sym =>
val tree1 = typer.typedType(tree)
//+scalac deviation
tree.rememberSelfTypeOf(tree1.asInstanceOf[TypeTree].original)
//-scalac deviation
val selftpe = tree1.tpe
sym setInfo {
if (selftpe.typeSymbol isNonBottomSubClass sym.owner) selftpe
else intersectionType(List(sym.owner.tpe, selftpe))
}
}
}
override def newTyper(context: Context) = new SemanticdbTyper(context)
class SemanticdbTyper(context0: Context) extends Typer(context0) {
import infer._
import TyperErrorGen._
import typeDebug.ptTree
import runDefinitions._
// TODO: No idea whether this is gonna work well in the batch compilation mode.
override def canTranslateEmptyListToNil = false
override def typed1(tree: Tree, mode: Mode, pt: Type): Tree = {
def typedSingletonTypeTree(tree: SingletonTypeTree) = {
val refTyped =
context.withImplicitsDisabled {
typed(tree.ref, MonoQualifierModes | mode.onlyTypePat, AnyRefTpe)
}
if (refTyped.isErrorTyped) {
setError(tree)
} else {
//+scalac deviation
tree setType refTyped.tpe.resultType rememberSingletonTypeTreeOf refTyped
//-scalac deviation
if (refTyped.isErrorTyped || treeInfo.admitsTypeSelection(refTyped)) tree
else UnstableTreeError(tree)
}
}
def tryTypedArgs(args: List[Tree], mode: Mode): Option[List[Tree]] = {
val c = context.makeSilent(reportAmbiguousErrors = false)
c.retyping = true
try {
val res = newTyper(c).typedArgs(args, mode)
if (c.reporter.hasErrors) None else Some(res)
} catch {
case ex: CyclicReference =>
throw ex
case te: TypeError =>
// @H some of typer errors can still leak,
// for instance in continuations
None
}
}
/* Try to apply function to arguments; if it does not work, try to convert Java raw to existentials, or try to
* insert an implicit conversion.
*/
def tryTypedApply(fun: Tree, args: List[Tree]): Tree = {
val start = if (Statistics.canEnable) Statistics.startTimer(failedApplyNanos) else null
def onError(typeErrors: Seq[AbsTypeError], warnings: Seq[(Position, String)]): Tree = {
if (Statistics.canEnable) Statistics.stopTimer(failedApplyNanos, start)
// If the problem is with raw types, convert to existentials and try again.
// See #4712 for a case where this situation arises,
if ((fun.symbol ne null) && fun.symbol.isJavaDefined) {
val newtpe = rawToExistential(fun.tpe)
if (fun.tpe ne newtpe) {
// println("late cooking: "+fun+":"+fun.tpe) // DEBUG
return tryTypedApply(fun setType newtpe, args)
}
}
def treesInResult(tree: Tree): List[Tree] = tree :: (tree match {
case Block(_, r) => treesInResult(r)
case Match(_, cases) => cases
case CaseDef(_, _, r) => treesInResult(r)
case Annotated(_, r) => treesInResult(r)
case If(_, t, e) => treesInResult(t) ++ treesInResult(e)
case Try(b, catches, _) => treesInResult(b) ++ catches
case Typed(r, Function(Nil, EmptyTree)) => treesInResult(r) // a method value
case Select(qual, name) => treesInResult(qual)
case Apply(fun, args) => treesInResult(fun) ++ args.flatMap(treesInResult)
case TypeApply(fun, args) => treesInResult(fun) ++ args.flatMap(treesInResult)
case _ => Nil
})
def errorInResult(tree: Tree) = treesInResult(tree) exists (err => typeErrors.exists(_.errPos == err.pos))
val retry = (typeErrors.forall(_.errPos != null)) && (fun :: tree :: args exists errorInResult)
typingStack.printTyping({
val funStr = ptTree(fun) + " and " + (args map ptTree mkString ", ")
if (retry) "second try: " + funStr
else "no second try: " + funStr + " because error not in result: " + typeErrors.head.errPos+"!="+tree.pos
})
if (retry) {
val Select(qual, name) = fun
tryTypedArgs(args, forArgMode(fun, mode)) match {
case Some(args1) if !args1.exists(arg => arg.exists(_.isErroneous)) =>
val qual1 =
if (!pt.isError) adaptToArguments(qual, name, args1, pt, reportAmbiguous = true, saveErrors = true)
else qual
if (qual1 ne qual) {
val tree1 = Apply(Select(qual1, name) setPos fun.pos, args1) setPos tree.pos
return context withinSecondTry typed1(tree1, mode, pt)
}
case _ => ()
}
}
typeErrors foreach context.issue
warnings foreach { case (p, m) => context.warning(p, m) }
setError(treeCopy.Apply(tree, fun, args))
}
silent(_.doTypedApply(tree, fun, args, mode, pt)) match {
case SilentResultValue(value) => value
case e: SilentTypeError => onError(e.errors, e.warnings)
}
}
def normalTypedApply(tree: Tree, fun: Tree, args: List[Tree]) = {
// TODO: replace `fun.symbol.isStable` by `treeInfo.isStableIdentifierPattern(fun)`
val stableApplication = (fun.symbol ne null) && fun.symbol.isMethod && fun.symbol.isStable
val funpt = if (mode.inPatternMode) pt else WildcardType
val appStart = if (Statistics.canEnable) Statistics.startTimer(failedApplyNanos) else null
val opeqStart = if (Statistics.canEnable) Statistics.startTimer(failedOpEqNanos) else null
def isConversionCandidate(qual: Tree, name: Name): Boolean =
!mode.inPatternMode && nme.isOpAssignmentName(TermName(name.decode)) && !qual.exists(_.isErroneous)
def reportError(error: SilentTypeError): Tree = {
error.reportableErrors foreach context.issue
error.warnings foreach { case (p, m) => context.warning(p, m) }
args foreach (arg => typed(arg, mode, ErrorType))
setError(tree)
}
def advice1(convo: Tree, errors: List[AbsTypeError], err: SilentTypeError): List[AbsTypeError] =
errors.map { e =>
if (e.errPos == tree.pos) {
val header = f"${e.errMsg}%n Expression does not convert to assignment because:%n "
val expansion = f"%n expansion: ${show(convo)}"
NormalTypeError(tree, err.errors.flatMap(_.errMsg.lines.toList).mkString(header, f"%n ", expansion))
} else e
}
def advice2(errors: List[AbsTypeError]): List[AbsTypeError] =
errors.map { e =>
if (e.errPos == tree.pos) {
val msg = f"${e.errMsg}%n Expression does not convert to assignment because receiver is not assignable."
NormalTypeError(tree, msg)
} else e
}
def onError(error: SilentTypeError): Tree = fun match {
case Select(qual, name) if isConversionCandidate(qual, name) =>
val qual1 = typedQualifier(qual)
if (treeInfo.isVariableOrGetter(qual1)) {
if (Statistics.canEnable) Statistics.stopTimer(failedOpEqNanos, opeqStart)
val erred = qual1.isErroneous || args.exists(_.isErroneous)
if (erred) reportError(error) else {
val convo = convertToAssignment(fun, qual1, name, args)
silent(op = _.typed1(convo, mode, pt)) match {
case SilentResultValue(t) => t
case err: SilentTypeError => reportError(SilentTypeError(advice1(convo, error.errors, err), error.warnings))
}
}
}
else {
if (Statistics.canEnable) Statistics.stopTimer(failedApplyNanos, appStart)
val Apply(Select(qual2, _), args2) = tree
val erred = qual2.isErroneous || args2.exists(_.isErroneous)
reportError {
if (erred) error else SilentTypeError(advice2(error.errors), error.warnings)
}
}
case _ =>
if (Statistics.canEnable) Statistics.stopTimer(failedApplyNanos, appStart)
reportError(error)
}
val silentResult = silent(
op = _.typed(fun, mode.forFunMode, funpt),
reportAmbiguousErrors = !mode.inExprMode && context.ambiguousErrors,
newtree = if (mode.inExprMode) tree else context.tree
)
silentResult match {
case SilentResultValue(fun1) =>
val fun2 = if (stableApplication) stabilizeFun(fun1, mode, pt) else fun1
if (Statistics.canEnable) Statistics.incCounter(typedApplyCount)
val noSecondTry = (
isPastTyper
|| context.inSecondTry
|| (fun2.symbol ne null) && fun2.symbol.isConstructor
|| isImplicitMethodType(fun2.tpe)
)
val isFirstTry = fun2 match {
case Select(_, _) => !noSecondTry && mode.inExprMode
case _ => false
}
if (isFirstTry)
tryTypedApply(fun2, args)
else
doTypedApply(tree, fun2, args, mode, pt)
case err: SilentTypeError => onError(err)
}
}
// convert new Array[T](len) to evidence[ClassTag[T]].newArray(len)
// convert new Array^N[T](len) for N > 1 to evidence[ClassTag[Array[...Array[T]...]]].newArray(len)
// where Array HK gets applied (N-1) times
object ArrayInstantiation {
def unapply(tree: Apply) = tree match {
case Apply(Select(New(tpt), name), arg :: Nil) if tpt.tpe != null && tpt.tpe.typeSymbol == ArrayClass =>
Some(tpt.tpe) collect {
case erasure.GenericArray(level, componentType) =>
val tagType = (1 until level).foldLeft(componentType)((res, _) => arrayType(res))
resolveClassTag(tree.pos, tagType) match {
case EmptyTree => MissingClassTagError(tree, tagType)
case tag => atPos(tree.pos)(new ApplyToImplicitArgs(Select(tag, nme.newArray), arg :: Nil))
}
}
case _ => None
}
}
def typedApply(tree: Apply) = tree match {
case Apply(Block(stats, expr), args) =>
typed1(atPos(tree.pos)(Block(stats, Apply(expr, args) setPos tree.pos.makeTransparent)), mode, pt)
case Apply(fun, args) =>
normalTypedApply(tree, fun, args) match {
case tree1 @ ArrayInstantiation(tree2) =>
if (tree2.isErrorTyped) tree2
//+scalac deviation
else typed(tree2, mode, pt).rememberNewArrayOf(tree1)
//-scalac deviation
case Apply(Select(fun, nme.apply), _) if treeInfo.isSuperConstrCall(fun) =>
TooManyArgumentListsForConstructor(tree) //SI-5696
case tree1 =>
tree1
}
}
def convertToAssignment(fun: Tree, qual: Tree, name: Name, args: List[Tree]): Tree = {
val prefix = name.toTermName stripSuffix nme.EQL
def mkAssign(vble: Tree): Tree =
Assign(
vble,
Apply(
Select(vble.duplicate, prefix) setPos fun.pos.focus, args) setPos tree.pos.makeTransparent
) setPos tree.pos
def mkUpdate(table: Tree, indices: List[Tree]) =
gen.evalOnceAll(table :: indices, context.owner, context.unit) {
case tab :: is =>
def mkCall(name: Name, extraArgs: Tree*) = (
Apply(
Select(tab(), name) setPos table.pos,
is.map(i => i()) ++ extraArgs
) setPos tree.pos
)
mkCall(
nme.update,
Apply(Select(mkCall(nme.apply), prefix) setPos fun.pos, args) setPos tree.pos
)
case _ => EmptyTree
}
val assignment = qual match {
case Ident(_) =>
mkAssign(qual)
case Select(qualqual, vname) =>
gen.evalOnce(qualqual, context.owner, context.unit) { qq =>
val qq1 = qq()
mkAssign(Select(qq1, vname) setPos qual.pos)
}
case Apply(fn, indices) =>
fn match {
case treeInfo.Applied(Select(table, nme.apply), _, _) => mkUpdate(table, indices)
case _ => UnexpectedTreeAssignmentConversionError(qual)
}
}
assignment
}
def typedAnnotated(atd: Annotated): Tree = {
val ann = atd.annot
val arg1 = typed(atd.arg, mode, pt)
/* mode for typing the annotation itself */
val annotMode = (mode &~ TYPEmode) | EXPRmode
def resultingTypeTree(tpe: Type) = {
// we need symbol-ful originals for reification
// hence we go the extra mile to hand-craft this guy
val original = arg1 match {
case tt @ TypeTree() if tt.original != null => Annotated(ann, tt.original)
// this clause is needed to correctly compile stuff like "new C @D" or "@(inline @getter)"
case _ => Annotated(ann, arg1)
}
original setType ann.tpe
val result = TypeTree(tpe) setOriginal original setPos tree.pos.focus
//+scalac deviation
result rememberAnnotatedOf treeCopy.Annotated(atd, tpe.annotations.head.original, arg1)
//-scalac deviation
}
if (arg1.isType) {
// make sure the annotation is only typechecked once
if (ann.tpe == null) {
val ainfo = typedAnnotation(ann, annotMode)
val atype = arg1.tpe.withAnnotation(ainfo)
if (ainfo.isErroneous)
// Erroneous annotations were already reported in typedAnnotation
arg1 // simply drop erroneous annotations
else {
ann setType atype
resultingTypeTree(atype)
}
} else {
// the annotation was typechecked before
resultingTypeTree(ann.tpe)
}
}
else {
if (ann.tpe == null) {
val annotInfo = typedAnnotation(ann, annotMode)
ann setType arg1.tpe.withAnnotation(annotInfo)
}
val atype = ann.tpe
Typed(arg1, resultingTypeTree(atype)) setPos tree.pos setType atype
}
}
// Lookup in the given class using the root mirror.
def lookupInOwner(owner: Symbol, name: Name): Symbol =
if (mode.inQualMode) rootMirror.missingHook(owner, name) else NoSymbol
// Lookup in the given qualifier. Used in last-ditch efforts by typedIdent and typedSelect.
def lookupInRoot(name: Name): Symbol = lookupInOwner(rootMirror.RootClass, name)
def lookupInEmpty(name: Name): Symbol = rootMirror.EmptyPackageClass.info member name
def lookupInQualifier(qual: Tree, name: Name): Symbol = (
if (name == nme.ERROR || qual.tpe.widen.isErroneous)
NoSymbol
else lookupInOwner(qual.tpe.typeSymbol, name) orElse {
NotAMemberError(tree, qual, name)
NoSymbol
}
)
/* Attribute a selection where `tree` is `qual.name`.
* `qual` is already attributed.
*/
def typedSelect(tree: Tree, qual: Tree, name: Name): Tree = {
val t = typedSelectInternal(tree, qual, name)
// Checking for OverloadedTypes being handed out after overloading
// resolution has already happened.
if (isPastTyper) t.tpe match {
case OverloadedType(pre, alts) =>
if (alts forall (s => (s.owner == ObjectClass) || (s.owner == AnyClass) || isPrimitiveValueClass(s.owner))) ()
else if (settings.debug) printCaller(
s"""|Select received overloaded type during $phase, but typer is over.
|If this type reaches the backend, we are likely doomed to crash.
|$t has these overloads:
|${alts map (s => " " + s.defStringSeenAs(pre memberType s)) mkString "\n"}
|""".stripMargin
)("")
case _ =>
}
t
}
def typedSelectInternal(tree: Tree, qual: Tree, name: Name): Tree = {
def asDynamicCall = dyna.mkInvoke(context, tree, qual, name) map { t =>
dyna.wrapErrors(t, (_.typed1(t, mode, pt)))
}
val sym = tree.symbol orElse member(qual, name) orElse {
// symbol not found? --> try to convert implicitly to a type that does have the required
// member. Added `| PATTERNmode` to allow enrichment in patterns (so we can add e.g., an
// xml member to StringContext, which in turn has an unapply[Seq] method)
if (name != nme.CONSTRUCTOR && mode.inAny(EXPRmode | PATTERNmode)) {
val qual1 = adaptToMemberWithArgs(tree, qual, name, mode, reportAmbiguous = true, saveErrors = true)
if ((qual1 ne qual) && !qual1.isErrorTyped)
return typed(treeCopy.Select(tree, qual1, name), mode, pt)
}
NoSymbol
}
if (phase.erasedTypes && qual.isInstanceOf[Super] && tree.symbol != NoSymbol)
qual setType tree.symbol.owner.tpe
if (!reallyExists(sym)) {
def handleMissing: Tree = {
def errorTree = missingSelectErrorTree(tree, qual, name)
def asTypeSelection = (
if (context.unit.isJava && name.isTypeName) {
// SI-3120 Java uses the same syntax, A.B, to express selection from the
// value A and from the type A. We have to try both.
atPos(tree.pos)(gen.convertToSelectFromType(qual, name)) match {
case EmptyTree => None
case tree1 => Some(typed1(tree1, mode, pt))
}
}
else None
)
debuglog(s"""
|qual=$qual:${qual.tpe}
|symbol=${qual.tpe.termSymbol.defString}
|scope-id=${qual.tpe.termSymbol.info.decls.hashCode}
|members=${qual.tpe.members mkString ", "}
|name=$name
|found=$sym
|owner=${context.enclClass.owner}
""".stripMargin)
// 1) Try converting a term selection on a java class into a type selection.
// 2) Try expanding according to Dynamic rules.
// 3) Try looking up the name in the qualifier.
asTypeSelection orElse asDynamicCall getOrElse (lookupInQualifier(qual, name) match {
case NoSymbol => setError(errorTree)
case found => typed1(tree setSymbol found, mode, pt)
})
}
handleMissing
}
else {
val tree1 = tree match {
case Select(_, _) => treeCopy.Select(tree, qual, name)
case SelectFromTypeTree(_, _) => treeCopy.SelectFromTypeTree(tree, qual, name)
}
val (result, accessibleError) = silent(_.asInstanceOf[SemanticdbTyper].makeAccessible(tree1, sym, qual.tpe, qual)) match {
case SilentTypeError(err: AccessTypeError) =>
(tree1, Some(err))
case SilentTypeError(err) =>
SelectWithUnderlyingError(tree, err)
return tree
case SilentResultValue(treeAndPre) =>
(stabilize(treeAndPre._1, treeAndPre._2, mode, pt), None)
}
result match {
// could checkAccessible (called by makeAccessible) potentially have skipped checking a type application in qual?
case SelectFromTypeTree(qual@TypeTree(), name) if qual.tpe.typeArgs.nonEmpty => // TODO: somehow the new qual is not checked in refchecks
treeCopy.SelectFromTypeTree(
result,
(TypeTreeWithDeferredRefCheck(){ () => val tp = qual.tpe; val sym = tp.typeSymbolDirect
// will execute during refchecks -- TODO: make private checkTypeRef in refchecks public and call that one?
checkBounds(qual, tp.prefix, sym.owner, sym.typeParams, tp.typeArgs, "")
qual // you only get to see the wrapped tree after running this check :-p
}) setType qual.tpe setPos qual.pos,
name)
case _ if accessibleError.isDefined =>
// don't adapt constructor, SI-6074
val qual1 = if (name == nme.CONSTRUCTOR) qual
else adaptToMemberWithArgs(tree, qual, name, mode, reportAmbiguous = false, saveErrors = false)
if (!qual1.isErrorTyped && (qual1 ne qual))
typed(Select(qual1, name) setPos tree.pos, mode, pt)
else
// before failing due to access, try a dynamic call.
asDynamicCall getOrElse {
context.issue(accessibleError.get)
setError(tree)
}
case _ =>
result
}
}
}
// the qualifier type of a supercall constructor is its first parent class
def typedSelectOrSuperQualifier(qual: Tree) =
context withinSuperInit typed(qual, PolyQualifierModes)
// temporarily use `filter` as an alternative for `withFilter`
def tryWithFilterAndFilter(tree: Select, qual: Tree): Tree = {
def warn(sym: Symbol) = context.deprecationWarning(tree.pos, sym, s"`withFilter' method does not yet exist on ${qual.tpe.widen}, using `filter' method instead")
silent(_ => typedSelect(tree, qual, nme.withFilter)) orElse { _ =>
silent(_ => typed1(Select(qual, nme.filter) setPos tree.pos, mode, pt)) match {
case SilentResultValue(res) => warn(res.symbol) ; res
case SilentTypeError(err) => WithFilterError(tree, err)
}
}
}
def typedSelectOrSuperCall(tree: Select) = tree match {
case Select(qual @ Super(_, _), nme.CONSTRUCTOR) =>
// the qualifier type of a supercall constructor is its first parent class
typedSelect(tree, typedSelectOrSuperQualifier(qual), nme.CONSTRUCTOR)
case Select(qual, name) =>
if (Statistics.canEnable) Statistics.incCounter(typedSelectCount)
val qualTyped = checkDead(typedQualifier(qual, mode))
val qualStableOrError = (
if (qualTyped.isErrorTyped || !name.isTypeName || treeInfo.admitsTypeSelection(qualTyped))
qualTyped
else
UnstableTreeError(qualTyped)
)
val tree1 = name match {
case nme.withFilter if !settings.future => tryWithFilterAndFilter(tree, qualStableOrError)
case _ => typedSelect(tree, qualStableOrError, name)
}
def sym = tree1.symbol
if (tree.isInstanceOf[PostfixSelect])
checkFeature(tree.pos, PostfixOpsFeature, name.decode)
if (sym != null && sym.isOnlyRefinementMember && !sym.isMacro)
checkFeature(tree1.pos, ReflectiveCallsFeature, sym.toString)
qualStableOrError.symbol match {
//+scalac deviation
case s: Symbol if s.isRootPackage => treeCopy.Ident(tree1, name).rememberSelectOf(tree1)
case _ => tree1
//-scalac deviation
}
}
tree match {
case tree: SingletonTypeTree => typedSingletonTypeTree(tree)
case tree: Apply => typedApply(tree)
case tree: Annotated => typedAnnotated(tree)
case tree: Select => typedSelectOrSuperCall(tree)
case _ => super.typed1(tree, mode, pt)
}
}
/** Make symbol accessible. This means:
* If symbol refers to package object, insert `.package` as second to last selector.
* (exception for some symbols in scala package which are dealiased immediately)
* Call checkAccessible, which sets tree's attributes.
* Also note that checkAccessible looks up sym on pre without checking that pre is well-formed
* (illegal type applications in pre will be skipped -- that's why typedSelect wraps the resulting tree in a TreeWithDeferredChecks)
* @return modified tree and new prefix type
*/
private def makeAccessible(tree: Tree, sym: Symbol, pre: Type, site: Tree): (Tree, Type) =
if (context.isInPackageObject(sym, pre.typeSymbol)) {
if (pre.typeSymbol == ScalaPackageClass && sym.isTerm) {
// short cut some aliases. It seems pattern matching needs this
// to notice exhaustiveness and to generate good code when
// List extractors are mixed with :: patterns. See Test5 in lists.scala.
//
// TODO SI-6609 Eliminate this special case once the old pattern matcher is removed.
def dealias(sym: Symbol) = {
//+scalac deviation
val tree1 = atPos(tree.pos.makeTransparent) {gen.mkAttributedRef(sym)} setPos tree.pos
tree1.rememberSelectOf(tree)
(tree1, sym.owner.thisType)
//-scalac deviation
}
sym.name match {
case nme.List => return dealias(ListModule)
case nme.Seq => return dealias(SeqModule)
case nme.Nil => return dealias(NilModule)
case _ =>
}
}
val qual = typedQualifier { atPos(tree.pos.makeTransparent) {
tree match {
case Ident(_) => Ident(rootMirror.getPackageObjectWithMember(pre, sym))
case Select(qual, _) => Select(qual, nme.PACKAGEkw)
case SelectFromTypeTree(qual, _) => Select(qual, nme.PACKAGEkw)
}
}}
val tree1 = atPos(tree.pos) {
tree match {
case Ident(name) => Select(qual, name)
case Select(_, name) => Select(qual, name)
case SelectFromTypeTree(_, name) => SelectFromTypeTree(qual, name)
}
}
(checkAccessible(tree1, sym, qual.tpe, qual), qual.tpe)
} else {
(checkAccessible(tree, sym, pre, site), pre)
}
override protected def typedTypeApply(tree: Tree, mode: Mode, fun: Tree, args: List[Tree]): Tree = fun.tpe match {
case OverloadedType(pre, alts) =>
inferPolyAlternatives(fun, mapList(args)(treeTpe))
// SI-8267 `memberType` can introduce existentials *around* a PolyType/MethodType, see AsSeenFromMap#captureThis.
// If we had selected a non-overloaded symbol, `memberType` would have been called in `makeAccessible`
// and the resulting existential type would have been skolemized in `adapt` *before* we typechecked
// the enclosing type-/ value- application.
//
// However, if the selection is overloaded, we defer calling `memberType` until we can select a single
// alternative here. It is therefore necessary to skolemize the existential here.
//
val fun1 = adaptAfterOverloadResolution(fun, mode.forFunMode | TAPPmode)
val tparams = fun1.symbol.typeParams //@M TODO: fun.symbol.info.typeParams ? (as in typedAppliedTypeTree)
val args1 = if (sameLength(args, tparams)) {
//@M: in case TypeApply we can't check the kind-arities of the type arguments,
// as we don't know which alternative to choose... here we do
map2Conserve(args, tparams) {
//@M! the polytype denotes the expected kind
(arg, tparam) => typedHigherKindedType(arg, mode, Kind.FromParams(tparam.typeParams))
}
} else // @M: there's probably something wrong when args.length != tparams.length... (triggered by bug #320)
// Martin, I'm using fake trees, because, if you use args or arg.map(typedType),
// inferPolyAlternatives loops... -- I have no idea why :-(
// ...actually this was looping anyway, see bug #278.
return TypedApplyWrongNumberOfTpeParametersError(fun, fun)
typedTypeApply(tree, mode, fun1, args1)
case SingleType(_, _) =>
typedTypeApply(tree, mode, fun setType fun.tpe.widen, args)
case PolyType(tparams, restpe) if tparams.nonEmpty =>
if (sameLength(tparams, args)) {
val targs = mapList(args)(treeTpe)
checkBounds(tree, NoPrefix, NoSymbol, tparams, targs, "")
if (isPredefClassOf(fun.symbol)) {
//+scalac deviation
val result = typedClassOf(tree, args.head, noGen = true)
val original = atPos(tree.pos)(TypeApply(fun, args))
result.rememberClassOf(original)
//-scalac deviation
} else {
if (!isPastTyper && fun.symbol == Any_isInstanceOf && targs.nonEmpty) {
val scrutineeType = fun match {
case Select(qual, _) => qual.tpe
case _ => AnyTpe
}
checkCheckable(tree, targs.head, scrutineeType, inPattern = false)
}
val resultpe = restpe.instantiateTypeParams(tparams, targs)
//@M substitution in instantiateParams needs to be careful!
//@M example: class Foo[a] { def foo[m[x]]: m[a] = error("") } (new Foo[Int]).foo[List] : List[Int]
//@M --> first, m[a] gets changed to m[Int], then m gets substituted for List,
// this must preserve m's type argument, so that we end up with List[Int], and not List[a]
//@M related bug: #1438
//println("instantiating type params "+restpe+" "+tparams+" "+targs+" = "+resultpe)
treeCopy.TypeApply(tree, fun, args) setType resultpe
}
}
else {
TypedApplyWrongNumberOfTpeParametersError(tree, fun)
}
case ErrorType =>
setError(treeCopy.TypeApply(tree, fun, args))
case _ =>
fun match {
// drop the application for an applyDynamic or selectDynamic call since it has been pushed down
case treeInfo.DynamicApplication(_, _) => fun
case _ => TypedApplyDoesNotTakeTpeParametersError(tree, fun)
}
}
override protected def typedExistentialTypeTree(tree: ExistentialTypeTree, mode: Mode): Tree = {
for (wc <- tree.whereClauses)
if (wc.symbol == NoSymbol) { namer enterSym wc; wc.symbol setFlag EXISTENTIAL }
else context.scope enter wc.symbol
val whereClauses1 = typedStats(tree.whereClauses, context.owner)
for (vd @ ValDef(_, _, _, _) <- whereClauses1)
if (vd.symbol.tpe.isVolatile)
AbstractionFromVolatileTypeError(vd)
val tpt1 = typedType(tree.tpt, mode)
existentialTransform(whereClauses1 map (_.symbol), tpt1.tpe)((tparams, tp) => {
val original = tpt1 match {
case tpt : TypeTree => atPos(tree.pos)(ExistentialTypeTree(tpt.original, tree.whereClauses))
case _ => {
debuglog(s"cannot reconstruct the original for $tree, because $tpt1 is not a TypeTree")
tree
}
}
val result = TypeTree(newExistentialType(tparams, tp)) setOriginal original
//+scalac deviation
result.rememberExistentialTypeTreeOf(treeCopy.ExistentialTypeTree(tree, tpt1, whereClauses1.asInstanceOf[List[MemberDef]]))
//-scalac deviation
}
)
}
/** Perform the following adaptations of expression, pattern or type `tree` wrt to
* given mode `mode` and given prototype `pt`:
* (-1) For expressions with annotated types, let AnnotationCheckers decide what to do
* (0) Convert expressions with constant types to literals (unless in interactive/scaladoc mode)
* (1) Resolve overloading, unless mode contains FUNmode
* (2) Apply parameterless functions
* (3) Apply polymorphic types to fresh instances of their type parameters and
* store these instances in context.undetparams,
* unless followed by explicit type application.
* (4) Do the following to unapplied methods used as values:
* (4.1) If the method has only implicit parameters pass implicit arguments
* (4.2) otherwise, if `pt` is a function type and method is not a constructor,
* convert to function by eta-expansion,
* (4.3) otherwise, if the method is nullary with a result type compatible to `pt`
* and it is not a constructor, apply it to ()
* otherwise issue an error
* (5) Convert constructors in a pattern as follows:
* (5.1) If constructor refers to a case class factory, set tree's type to the unique
* instance of its primary constructor that is a subtype of the expected type.
* (5.2) If constructor refers to an extractor, convert to application of
* unapply or unapplySeq method.
*
* (6) Convert all other types to TypeTree nodes.
* (7) When in TYPEmode but not FUNmode or HKmode, check that types are fully parameterized
* (7.1) In HKmode, higher-kinded types are allowed, but they must have the expected kind-arity
* (8) When in both EXPRmode and FUNmode, add apply method calls to values of object type.
* (9) If there are undetermined type variables and not POLYmode, infer expression instance
* Then, if tree's type is not a subtype of expected type, try the following adaptations:
* (10) If the expected type is Byte, Short or Char, and the expression
* is an integer fitting in the range of that type, convert it to that type.
* (11) Widen numeric literals to their expected type, if necessary
* (12) When in mode EXPRmode, convert E to { E; () } if expected type is scala.Unit.
* (13) When in mode EXPRmode, apply AnnotationChecker conversion if expected type is annotated.
* (14) When in mode EXPRmode, apply a view
* If all this fails, error
*/
override protected def adapt(tree: Tree, mode: Mode, pt: Type, original: Tree = EmptyTree): Tree = {
def hasUndets = context.undetparams.nonEmpty
def hasUndetsInMonoMode = hasUndets && !mode.inPolyMode
def adaptToImplicitMethod(mt: MethodType): Tree = {
if (hasUndets) { // (9) -- should revisit dropped condition `hasUndetsInMonoMode`
// dropped so that type args of implicit method are inferred even if polymorphic expressions are allowed
// needed for implicits in 2.8 collection library -- maybe once #3346 is fixed, we can reinstate the condition?
context.undetparams = inferExprInstance(tree, context.extractUndetparams(), pt,
// approximate types that depend on arguments since dependency on implicit argument is like dependency on type parameter
mt.approximate,
keepNothings = false,
useWeaklyCompatible = true) // #3808
}
// avoid throwing spurious DivergentImplicit errors
if (context.reporter.hasErrors)
setError(tree)
else
withCondConstrTyper(treeInfo.isSelfOrSuperConstrCall(tree))(typer1 =>
if (original != EmptyTree && pt != WildcardType) (
typer1 silent { tpr =>
val withImplicitArgs = tpr.applyImplicitArgs(tree)
if (tpr.context.reporter.hasErrors) tree // silent will wrap it in SilentTypeError anyway
else tpr.typed(withImplicitArgs, mode, pt)
}
orElse { _ =>
val resetTree = resetAttrs(original)
resetTree match {
case treeInfo.Applied(fun, targs, args) =>
if (fun.symbol != null && fun.symbol.isError)
// SI-9041 Without this, we leak error symbols past the typer!
// because the fallback typechecking notices the error-symbol,
// refuses to re-attempt typechecking, and presumes that someone
// else was responsible for issuing the related type error!
fun.setSymbol(NoSymbol)
case _ =>
}
debuglog(s"fallback on implicits: ${tree}/$resetTree")
val tree1 = typed(resetTree, mode)
// Q: `typed` already calls `pluginsTyped` and `adapt`. the only difference here is that
// we pass `EmptyTree` as the `original`. intended? added in 2009 (53d98e7d42) by martin.
tree1 setType pluginsTyped(tree1.tpe, this, tree1, mode, pt)
if (tree1.isEmpty) tree1 else adapt(tree1, mode, pt, EmptyTree)
}
)
else
typer1.typed(typer1.applyImplicitArgs(tree), mode, pt)
)
}
def instantiateToMethodType(mt: MethodType): Tree = {
val meth = tree match {
// a partial named application is a block (see comment in EtaExpansion)
case Block(_, tree1) => tree1.symbol
case _ => tree.symbol
}
if (!meth.isConstructor && (isFunctionType(pt) || samOf(pt).exists)) { // (4.2)
debuglog(s"eta-expanding $tree: ${tree.tpe} to $pt")
checkParamsConvertible(tree, tree.tpe)
val tree0 = etaExpand(context.unit, tree, this)
// #2624: need to infer type arguments for eta expansion of a polymorphic method
// context.undetparams contains clones of meth.typeParams (fresh ones were generated in etaExpand)
// need to run typer on tree0, since etaExpansion sets the tpe's of its subtrees to null
// can't type with the expected type, as we can't recreate the setup in (3) without calling typed
// (note that (3) does not call typed to do the polymorphic type instantiation --
// it is called after the tree has been typed with a polymorphic expected result type)
if (hasUndets)
instantiate(typed(tree0, mode), mode, pt)
else
typed(tree0, mode, pt)
}
else if (!meth.isConstructor && mt.params.isEmpty) // (4.3)
adapt(typed(Apply(tree, Nil) setPos tree.pos), mode, pt, original)
else if (context.implicitsEnabled)
MissingArgsForMethodTpeError(tree, meth)
else
setError(tree)
}
def adaptType(): Tree = {
// @M When not typing a type constructor (!context.inTypeConstructorAllowed)
// or raw type, types must be of kind *,
// and thus parameterized types must be applied to their type arguments
// @M TODO: why do kind-* tree's have symbols, while higher-kinded ones don't?
def properTypeRequired = (
tree.hasSymbolField
&& !context.inTypeConstructorAllowed
&& !context.unit.isJava
)
// @M: don't check tree.tpe.symbol.typeParams. check tree.tpe.typeParams!!!
// (e.g., m[Int] --> tree.tpe.symbol.typeParams.length == 1, tree.tpe.typeParams.length == 0!)
// @M: removed check for tree.hasSymbolField and replace tree.symbol by tree.tpe.symbol
// (TypeTree's must also be checked here, and they don't directly have a symbol)
def kindArityMismatch = (
context.inTypeConstructorAllowed
&& !sameLength(tree.tpe.typeParams, pt.typeParams)
)
// Note that we treat Any and Nothing as kind-polymorphic.
// We can't perform this check when typing type arguments to an overloaded method before the overload is resolved
// (or in the case of an error type) -- this is indicated by pt == WildcardType (see case TypeApply in typed1).
def kindArityMismatchOk = tree.tpe.typeSymbol match {
case NothingClass | AnyClass => true
case _ => pt == WildcardType
}
// todo. It would make sense when mode.inFunMode to instead use
// tree setType tree.tpe.normalize
// when typechecking, say, TypeApply(Ident(`some abstract type symbol`), List(...))
// because otherwise Ident will have its tpe set to a TypeRef, not to a PolyType, and `typedTypeApply` will fail
// but this needs additional investigation, because it crashes t5228, gadts1 and maybe something else
if (mode.inFunMode)
tree
else if (properTypeRequired && tree.symbol.typeParams.nonEmpty) // (7)
MissingTypeParametersError(tree)
else if (kindArityMismatch && !kindArityMismatchOk) // (7.1) @M: check kind-arity
KindArityMismatchError(tree, pt)
else tree match { // (6)
case TypeTree() => tree
case _ => TypeTree(tree.tpe) setOriginal tree
}
}
def insertApply(): Tree = {
assert(!context.inTypeConstructorAllowed, mode) //@M
val adapted = adaptToName(tree, nme.apply)
def stabilize0(pre: Type): Tree = stabilize(adapted, pre, MonoQualifierModes, WildcardType)
// TODO reconcile the overlap between Typers#stablize and TreeGen.stabilize
val qual = adapted match {
case This(_) =>
gen.stabilize(adapted)
case Ident(_) =>
val owner = adapted.symbol.owner
val pre =
if (owner.isPackageClass) owner.thisType
else if (owner.isClass) context.enclosingSubClassContext(owner).prefix
else NoPrefix
stabilize0(pre)
case Select(qualqual, _) =>
stabilize0(qualqual.tpe)
case other =>
other
}
typedPos(tree.pos, mode, pt) {
Select(qual setPos tree.pos.makeTransparent, nme.apply)
}
}
def adaptConstant(value: Constant): Tree = {
val sym = tree.symbol
if (sym != null && sym.isDeprecated)
context.deprecationWarning(tree.pos, sym)
val result = treeCopy.Literal(tree, value)
//+scalac deviation
result.rememberConstfoldOf(tree)
//-scalac deviation
}
// Ignore type errors raised in later phases that are due to mismatching types with existential skolems
// We have lift crashing in 2.9 with an adapt failure in the pattern matcher.
// Here's my hypothesis why this happens. The pattern matcher defines a variable of type
//
// val x: T = expr
//
// where T is the type of expr, but T contains existential skolems ts.
// In that case, this value definition does not typecheck.
// The value definition
//
// val x: T forSome { ts } = expr
//
// would typecheck. Or one can simply leave out the type of the `val`:
//
// val x = expr
//
// SI-6029 shows another case where we also fail (in uncurry), but this time the expected
// type is an existential type.
//
// The reason for both failures have to do with the way we (don't) transform
// skolem types along with the trees that contain them. We'd need a
// radically different approach to do it. But before investing a lot of time to
// to do this (I have already sunk 3 full days with in the end futile attempts
// to consistently transform skolems and fix 6029), I'd like to
// investigate ways to avoid skolems completely.
//
// upd. The same problem happens when we try to typecheck the result of macro expansion against its expected type
// (which is the return type of the macro definition instantiated in the context of expandee):
//
// Test.scala:2: error: type mismatch;
// found : $u.Expr[Class[_ <: Object]]
// required: reflect.runtime.universe.Expr[Class[?0(in value <local Test>)]] where type ?0(in value <local Test>) <: Object
// scala.reflect.runtime.universe.reify(new Object().getClass)
// ^
// Therefore following Martin's advice I use this logic to recover from skolem errors after macro expansions
// (by adding the ` || tree.attachments.get[MacroExpansionAttachment].isDefined` clause to the conditional above).
//
def adaptMismatchedSkolems() = {
def canIgnoreMismatch = (
!context.reportErrors && isPastTyper
|| tree.hasAttachment[MacroExpansionAttachment]
)
def bound = pt match {
case ExistentialType(qs, _) => qs
case _ => Nil
}
def msg = sm"""
|Recovering from existential or skolem type error in
| $tree
|with type: ${tree.tpe}
| pt: $pt
| context: ${context.tree}
| adapted
""".trim
val boundOrSkolems = if (canIgnoreMismatch) bound ++ pt.skolemsExceptMethodTypeParams else Nil
boundOrSkolems match {
case Nil => AdaptTypeError(tree, tree.tpe, pt) ; setError(tree)
case _ => logResult(msg)(adapt(tree, mode, deriveTypeWithWildcards(boundOrSkolems)(pt)))
}
}
def fallbackAfterVanillaAdapt(): Tree = {
def isPopulatedPattern = {
if ((tree.symbol ne null) && tree.symbol.isModule)
inferModulePattern(tree, pt)
isPopulated(tree.tpe, approximateAbstracts(pt))
}
if (mode.inPatternMode && isPopulatedPattern)
return tree
val tree1 = constfold(tree, pt) // (10) (11)
if (tree1.tpe <:< pt)
return adapt(tree1, mode, pt, original)
if (mode.typingExprNotFun) {
// The <: Any requirement inhibits attempts to adapt continuation types
// to non-continuation types.
if (tree.tpe <:< AnyTpe) pt.dealias match {
case TypeRef(_, UnitClass, _) => // (12)
if (!isPastTyper && settings.warnValueDiscard)
context.warning(tree.pos, "discarded non-Unit value")
return typedPos(tree.pos, mode, pt)(Block(List(tree), Literal(Constant(()))))
case TypeRef(_, sym, _) if isNumericValueClass(sym) && isNumericSubType(tree.tpe, pt) =>
if (!isPastTyper && settings.warnNumericWiden)
context.warning(tree.pos, "implicit numeric widening")
return typedPos(tree.pos, mode, pt)(Select(tree, "to" + sym.name))
case _ =>
}
if (pt.dealias.annotations.nonEmpty && canAdaptAnnotations(tree, this, mode, pt)) // (13)
return typed(adaptAnnotations(tree, this, mode, pt), mode, pt)
if (hasUndets)
return instantiate(tree, mode, pt)
if (context.implicitsEnabled && !pt.isError && !tree.isErrorTyped) {
// (14); the condition prevents chains of views
debuglog("inferring view from " + tree.tpe + " to " + pt)
inferView(tree, tree.tpe, pt, reportAmbiguous = true) match {
case EmptyTree =>
case coercion =>
def msg = "inferred view from " + tree.tpe + " to " + pt + " = " + coercion + ":" + coercion.tpe
if (settings.logImplicitConv)
context.echo(tree.pos, msg)
debuglog(msg)
val silentContext = context.makeImplicit(context.ambiguousErrors)
val res = newTyper(silentContext).typed(
new ApplyImplicitView(coercion, List(tree)) setPos tree.pos, mode, pt)
silentContext.reporter.firstError match {
case Some(err) => context.issue(err)