forked from scala/scala3
/
Typer.scala
2060 lines (1897 loc) · 88.6 KB
/
Typer.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
package dotty.tools
package dotc
package typer
import core._
import ast._
import Trees._
import Constants._
import StdNames._
import Scopes._
import Denotations._
import ProtoTypes._
import Contexts._
import Comments._
import Symbols._
import Types._
import SymDenotations._
import Annotations._
import Names._
import NameOps._
import Flags._
import Decorators._
import ErrorReporting._
import Checking._
import Inferencing._
import EtaExpansion.etaExpand
import dotty.tools.dotc.transform.Erasure.Boxing
import util.Positions._
import util.common._
import util.{SourcePosition, Property}
import collection.mutable
import annotation.tailrec
import Implicits._
import util.Stats.{track, record}
import config.Printers.{typr, gadts}
import rewrite.Rewrites.patch
import NavigateAST._
import transform.SymUtils._
import language.implicitConversions
import printing.SyntaxHighlighting._
object Typer {
/** The precedence of bindings which determines which of several bindings will be
* accessed by an Ident.
*/
object BindingPrec {
val definition = 4
val namedImport = 3
val wildImport = 2
val packageClause = 1
val nothingBound = 0
def isImportPrec(prec: Int) = prec == namedImport || prec == wildImport
}
/** Assert tree has a position, unless it is empty or a typed splice */
def assertPositioned(tree: untpd.Tree)(implicit ctx: Context) =
if (!tree.isEmpty && !tree.isInstanceOf[untpd.TypedSplice] && ctx.typerState.isGlobalCommittable)
assert(tree.pos.exists, s"position not set for $tree # ${tree.uniqueId}")
private val ExprOwner = new Property.Key[Symbol]
}
class Typer extends Namer with TypeAssigner with Applications with Implicits with Dynamic with Checking with Docstrings {
import Typer._
import tpd.{cpy => _, _}
import untpd.cpy
import Dynamic.isDynamicMethod
import reporting.diagnostic.Message
import reporting.diagnostic.messages._
/** A temporary data item valid for a single typed ident:
* The set of all root import symbols that have been
* encountered as a qualifier of an import so far.
* Note: It would be more proper to move importedFromRoot into typedIdent.
* We should check that this has no performance degradation, however.
*/
private var unimported: Set[Symbol] = Set()
/** Temporary data item for single call to typed ident:
* This symbol would be found under Scala2 mode, but is not
* in dotty (because dotty conforms to spec section 2
* wrt to package member resolution but scalac doe not).
*/
private var foundUnderScala2: Type = NoType
def newLikeThis: Typer = new Typer
/** Attribute an identifier consisting of a simple name or wildcard
*
* @param tree The tree representing the identifier.
* Transformations: (1) Prefix class members with this.
* (2) Change imported symbols to selections.
* (3) Change pattern Idents id (but not wildcards) to id @ _
*/
def typedIdent(tree: untpd.Ident, pt: Type)(implicit ctx: Context): Tree = track("typedIdent") {
val refctx = ctx
val name = tree.name
val noImports = ctx.mode.is(Mode.InPackageClauseName)
/** Method is necessary because error messages need to bind to
* to typedIdent's context which is lost in nested calls to findRef
*/
def error(msg: => Message, pos: Position) = ctx.error(msg, pos)
/** Does this identifier appear as a constructor of a pattern? */
def isPatternConstr =
if (ctx.mode.isExpr && (ctx.outer.mode is Mode.Pattern))
ctx.outer.tree match {
case Apply(`tree`, _) => true
case _ => false
}
else false
/** A symbol qualifies if it really exists and is not a package class.
* In addition, if we are in a constructor of a pattern, we ignore all definitions
* which are methods and not accessors (note: if we don't do that
* case x :: xs in class List would return the :: method).
*
* Package classes are part of their parent's scope, because otherwise
* we could not reload them via `_.member`. On the other hand, accessing a
* package as a type from source is always an error.
*/
def qualifies(denot: Denotation): Boolean =
reallyExists(denot) &&
!(pt.isInstanceOf[UnapplySelectionProto] &&
(denot.symbol is (Method, butNot = Accessor))) &&
!(denot.symbol is PackageClass)
/** Find the denotation of enclosing `name` in given context `ctx`.
* @param previous A denotation that was found in a more deeply nested scope,
* or else `NoDenotation` if nothing was found yet.
* @param prevPrec The binding precedence of the previous denotation,
* or else `nothingBound` if nothing was found yet.
* @param prevCtx The context of the previous denotation,
* or else `NoContext` if nothing was found yet.
*/
def findRef(previous: Type, prevPrec: Int, prevCtx: Context)(implicit ctx: Context): Type = {
import BindingPrec._
/** A string which explains how something was bound; Depending on `prec` this is either
* imported by <tree>
* or defined in <symbol>
*/
def bindingString(prec: Int, whereFound: Context, qualifier: String = "") =
if (prec == wildImport || prec == namedImport) {
ex"""imported$qualifier by ${hl"${whereFound.importInfo.toString}"}"""
} else
ex"""defined$qualifier in ${hl"${whereFound.owner.toString}"}"""
/** Check that any previously found result from an inner context
* does properly shadow the new one from an outer context.
* @param found The newly found result
* @param newPrec Its precedence
* @param scala2pkg Special mode where we check members of the same package, but defined
* in different compilation units under Scala2. If set, and the
* previous and new contexts do not have the same scope, we select
* the previous (inner) definition. This models what scalac does.
*/
def checkNewOrShadowed(found: Type, newPrec: Int, scala2pkg: Boolean = false)(implicit ctx: Context): Type =
if (!previous.exists || ctx.typeComparer.isSameRef(previous, found)) found
else if ((prevCtx.scope eq ctx.scope) &&
(newPrec == definition ||
newPrec == namedImport && prevPrec == wildImport)) {
// special cases: definitions beat imports, and named imports beat
// wildcard imports, provided both are in contexts with same scope
found
}
else {
if (!scala2pkg && !previous.isError && !found.isError) {
error(
ex"""|reference to `$name` is ambiguous
|it is both ${bindingString(newPrec, ctx, "")}
|and ${bindingString(prevPrec, prevCtx, " subsequently")}""",
tree.pos)
}
previous
}
/** The type representing a named import with enclosing name when imported
* from given `site` and `selectors`.
*/
def namedImportRef(imp: ImportInfo)(implicit ctx: Context): Type = {
val Name = name.toTermName.decode
def recur(selectors: List[untpd.Tree]): Type = selectors match {
case selector :: rest =>
def checkUnambiguous(found: Type) = {
val other = recur(selectors.tail)
if (other.exists && found.exists && (found != other))
error(em"reference to `$name` is ambiguous; it is imported twice in ${ctx.tree}",
tree.pos)
found
}
def selection(name: Name) =
if (imp.sym.isCompleting) {
ctx.warning(i"cyclic ${imp.sym}, ignored", tree.pos)
NoType
}
else if (unimported.nonEmpty && unimported.contains(imp.site.termSymbol))
NoType
else {
// Pass refctx so that any errors are reported in the context of the
// reference instead of the
checkUnambiguous(selectionType(imp.site, name, tree.pos)(refctx))
}
selector match {
case Thicket(fromId :: Ident(Name) :: _) =>
val Ident(from) = fromId
selection(if (name.isTypeName) from.toTypeName else from)
case Ident(Name) =>
selection(name)
case _ =>
recur(rest)
}
case nil =>
NoType
}
recur(imp.selectors)
}
/** The type representing a wildcard import with enclosing name when imported
* from given import info
*/
def wildImportRef(imp: ImportInfo)(implicit ctx: Context): Type = {
if (imp.isWildcardImport) {
val pre = imp.site
if (!unimported.contains(pre.termSymbol) &&
!imp.excluded.contains(name.toTermName) &&
name != nme.CONSTRUCTOR) {
val denot = pre.member(name).accessibleFrom(pre)(refctx)
if (reallyExists(denot)) return pre.select(name, denot)
}
}
NoType
}
/** Is (some alternative of) the given predenotation `denot`
* defined in current compilation unit?
*/
def isDefinedInCurrentUnit(denot: Denotation)(implicit ctx: Context): Boolean = denot match {
case MultiDenotation(d1, d2) => isDefinedInCurrentUnit(d1) || isDefinedInCurrentUnit(d2)
case denot: SingleDenotation => denot.symbol.sourceFile == ctx.source.file
}
/** Is `denot` the denotation of a self symbol? */
def isSelfDenot(denot: Denotation)(implicit ctx: Context) = denot match {
case denot: SymDenotation => denot is SelfName
case _ => false
}
/** Would import of kind `prec` be not shadowed by a nested higher-precedence definition? */
def isPossibleImport(prec: Int)(implicit ctx: Context) =
!noImports &&
(prevPrec < prec || prevPrec == prec && (prevCtx.scope eq ctx.scope))
@tailrec def loop(implicit ctx: Context): Type = {
if (ctx.scope == null) previous
else {
val outer = ctx.outer
var result: Type = NoType
// find definition
if ((ctx.scope ne outer.scope) || (ctx.owner ne outer.owner)) {
val defDenot = ctx.denotNamed(name)
if (qualifies(defDenot)) {
val curOwner = ctx.owner
val found =
if (isSelfDenot(defDenot)) curOwner.enclosingClass.thisType
else curOwner.thisType.select(name, defDenot)
if (!(curOwner is Package) || isDefinedInCurrentUnit(defDenot))
result = checkNewOrShadowed(found, definition) // no need to go further out, we found highest prec entry
else {
if (ctx.scala2Mode && !foundUnderScala2.exists)
foundUnderScala2 = checkNewOrShadowed(found, definition, scala2pkg = true)
if (defDenot.symbol is Package)
result = checkNewOrShadowed(previous orElse found, packageClause)
else if (prevPrec < packageClause)
result = findRef(found, packageClause, ctx)(outer)
}
}
}
if (result.exists) result
else { // find import
val curImport = ctx.importInfo
def updateUnimported() =
if (curImport.unimported.exists) unimported += curImport.unimported
if (ctx.owner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
previous // no more conflicts possible in this case
else if (isPossibleImport(namedImport) && (curImport ne outer.importInfo)) {
val namedImp = namedImportRef(curImport)
if (namedImp.exists)
findRef(checkNewOrShadowed(namedImp, namedImport), namedImport, ctx)(outer)
else if (isPossibleImport(wildImport) && !curImport.sym.isCompleting) {
val wildImp = wildImportRef(curImport)
if (wildImp.exists)
findRef(checkNewOrShadowed(wildImp, wildImport), wildImport, ctx)(outer)
else {
updateUnimported()
loop(outer)
}
}
else {
updateUnimported()
loop(outer)
}
}
else loop(outer)
}
}
}
loop
}
// begin typedIdent
def kind = if (name.isTermName) "" else "type "
typr.println(s"typed ident $kind$name in ${ctx.owner}")
if (ctx.mode is Mode.Pattern) {
if (name == nme.WILDCARD)
return tree.withType(pt)
if (isVarPattern(tree) && name.isTermName)
return typed(desugar.patternVar(tree), pt)
}
val rawType = {
val saved1 = unimported
val saved2 = foundUnderScala2
unimported = Set.empty
foundUnderScala2 = NoType
try {
var found = findRef(NoType, BindingPrec.nothingBound, NoContext)
if (foundUnderScala2.exists && !(foundUnderScala2 =:= found)) {
ctx.migrationWarning(
ex"""Name resolution will change.
| currently selected : $foundUnderScala2
| in the future, without -language:Scala2: $found""", tree.pos)
found = foundUnderScala2
}
found
}
finally {
unimported = saved1
foundUnderScala2 = saved2
}
}
val ownType =
if (rawType.exists)
ensureAccessible(rawType, superAccess = false, tree.pos)
else
errorType(new MissingIdent(tree, kind, name.show), tree.pos)
val tree1 = ownType match {
case ownType: NamedType if !prefixIsElidable(ownType) =>
ref(ownType).withPos(tree.pos)
case _ =>
tree.withType(ownType)
}
checkValue(tree1, pt)
}
private def typedSelect(tree: untpd.Select, pt: Type, qual: Tree)(implicit ctx: Context): Select =
healNonvariant(
checkValue(assignType(cpy.Select(tree)(qual, tree.name), qual), pt),
pt)
/** Let `tree = p.n` where `p: T`. If tree's type is an unsafe instantiation
* (see TypeOps#asSeenFrom for how this can happen), rewrite the prefix `p`
* to `(p: <unknown skolem of type T>)` and try again with the new (stable)
* prefix. If the result has another unsafe instantiation, raise an error.
*/
private def healNonvariant[T <: Tree](tree: T, pt: Type)(implicit ctx: Context): T =
if (ctx.unsafeNonvariant == ctx.runId && tree.tpe.widen.hasUnsafeNonvariant)
tree match {
case tree @ Select(qual, _) if !qual.tpe.isStable =>
val alt = typedSelect(tree, pt, Typed(qual, TypeTree(SkolemType(qual.tpe.widen))))
typr.println(i"healed type: ${tree.tpe} --> $alt")
alt.asInstanceOf[T]
case _ =>
ctx.error(ex"unsafe instantiation of type ${tree.tpe}", tree.pos)
tree
}
else tree
def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = track("typedSelect") {
def typeSelectOnTerm(implicit ctx: Context): Tree = {
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos)
val select = typedSelect(tree, pt, qual1)
if (select.tpe ne TryDynamicCallType) select
else if (pt.isInstanceOf[PolyProto] || pt.isInstanceOf[FunProto] || pt == AssignProto) select
else typedDynamicSelect(tree, Nil, pt)
}
def typeSelectOnType(qual: untpd.Tree)(implicit ctx: Context) =
typedSelect(untpd.cpy.Select(tree)(qual, tree.name.toTypeName), pt)
def tryJavaSelectOnType(implicit ctx: Context): Tree = tree.qualifier match {
case Select(qual, name) => typeSelectOnType(untpd.Select(qual, name.toTypeName))
case Ident(name) => typeSelectOnType(untpd.Ident(name.toTypeName))
case _ => errorTree(tree, "cannot convert to type selection") // will never be printed due to fallback
}
def selectWithFallback(fallBack: Context => Tree) =
tryAlternatively(typeSelectOnTerm(_))(fallBack)
if (tree.qualifier.isType) {
val qual1 = typedType(tree.qualifier, selectionProto(tree.name, pt, this))
assignType(cpy.Select(tree)(qual1, tree.name), qual1)
}
else if (ctx.compilationUnit.isJava && tree.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.
selectWithFallback(tryJavaSelectOnType(_)) // !!! possibly exponential bcs of qualifier retyping
else if (tree.name == nme.withFilter && tree.getAttachment(desugar.MaybeFilter).isDefined)
selectWithFallback {
implicit ctx =>
typedSelect(untpd.cpy.Select(tree)(tree.qualifier, nme.filter), pt) // !!! possibly exponential bcs of qualifier retyping
}
else
typeSelectOnTerm(ctx)
}
def typedThis(tree: untpd.This)(implicit ctx: Context): Tree = track("typedThis") {
assignType(tree)
}
def typedSuper(tree: untpd.Super, pt: Type)(implicit ctx: Context): Tree = track("typedSuper") {
val qual1 = typed(tree.qual)
val inConstrCall = pt match {
case pt: SelectionProto if pt.name == nme.CONSTRUCTOR => true
case _ => false
}
pt match {
case pt: SelectionProto if pt.name.isTypeName =>
qual1 // don't do super references for types; they are meaningless anyway
case _ =>
assignType(cpy.Super(tree)(qual1, tree.mix), qual1, inConstrCall)
}
}
def typedLiteral(tree: untpd.Literal)(implicit ctx: Context) = track("typedLiteral") {
assignType(tree)
}
def typedNew(tree: untpd.New, pt: Type)(implicit ctx: Context) = track("typedNew") {
tree.tpt match {
case templ: untpd.Template =>
import untpd._
templ.parents foreach {
case parent: RefTree =>
typedAheadImpl(parent, tree => inferTypeParams(typedType(tree), pt))
case _ =>
}
val x = tpnme.ANON_CLASS
val clsDef = TypeDef(x, templ).withFlags(Final)
typed(cpy.Block(tree)(clsDef :: Nil, New(Ident(x), Nil)), pt)
case _ =>
var tpt1 = typedType(tree.tpt)
tpt1 = tpt1.withType(ensureAccessible(tpt1.tpe, superAccess = false, tpt1.pos))
tpt1.tpe.dealias match {
case TypeApplications.EtaExpansion(tycon) => tpt1 = tpt1.withType(tycon)
case _ =>
}
checkClassType(tpt1.tpe, tpt1.pos, traitReq = false, stablePrefixReq = true)
tpt1 match {
case AppliedTypeTree(_, targs) =>
for (targ @ TypeBoundsTree(_, _) <- targs)
ctx.error("type argument must be fully defined", targ.pos)
case _ =>
}
assignType(cpy.New(tree)(tpt1), tpt1)
// todo in a later phase: checkInstantiatable(cls, tpt1.pos)
}
}
def typedTyped(tree: untpd.Typed, pt: Type)(implicit ctx: Context): Tree = track("typedTyped") {
/* Handles three cases:
* @param ifPat how to handle a pattern (_: T)
* @param ifExpr how to handle an expression (e: T)
* @param wildName what name `w` to use in the rewriting of
* (x: T) to (x @ (w: T)). This is either `_` or `_*`.
*/
def cases(ifPat: => Tree, ifExpr: => Tree, wildName: TermName) = tree.expr match {
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && isVarPattern(id) =>
if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) ifPat
else {
import untpd._
typed(Bind(id.name, Typed(Ident(wildName), tree.tpt)).withPos(id.pos), pt)
}
case _ => ifExpr
}
def ascription(tpt: Tree, isWildcard: Boolean) = {
val underlyingTreeTpe =
if (isRepeatedParamType(tpt)) TypeTree(defn.SeqType.appliedTo(pt :: Nil))
else tpt
val expr1 =
if (isRepeatedParamType(tpt)) tree.expr.withType(defn.SeqType.appliedTo(pt :: Nil))
else if (isWildcard) tree.expr.withType(tpt.tpe)
else typed(tree.expr, tpt.tpe.widenSkolem)
assignType(cpy.Typed(tree)(expr1, tpt), underlyingTreeTpe)
}
if (untpd.isWildcardStarArg(tree))
cases(
ifPat = ascription(TypeTree(defn.RepeatedParamType.appliedTo(pt)), isWildcard = true),
ifExpr = seqToRepeated(typedExpr(tree.expr, defn.SeqType)),
wildName = nme.WILDCARD_STAR)
else {
def typedTpt = checkSimpleKinded(typedType(tree.tpt))
def handlePattern: Tree = {
val tpt1 = typedTpt
// special case for an abstract type that comes with a class tag
tpt1.tpe.dealias match {
case tref: TypeRef if !tref.symbol.isClass && !ctx.isAfterTyper =>
inferImplicit(defn.ClassTagType.appliedTo(tref),
EmptyTree, tpt1.pos)(ctx.retractMode(Mode.Pattern)) match {
case SearchSuccess(arg, _, _, _) =>
return typed(untpd.Apply(untpd.TypedSplice(arg), tree.expr), pt)
case _ =>
}
case _ =>
if (!ctx.isAfterTyper) tpt1.tpe.<:<(pt)(ctx.addMode(Mode.GADTflexible))
}
ascription(tpt1, isWildcard = true)
}
cases(
ifPat = handlePattern,
ifExpr = ascription(typedTpt, isWildcard = false),
wildName = nme.WILDCARD)
}
}
def typedNamedArg(tree: untpd.NamedArg, pt: Type)(implicit ctx: Context) = track("typedNamedArg") {
val arg1 = typed(tree.arg, pt)
assignType(cpy.NamedArg(tree)(tree.name, arg1), arg1)
}
def typedAssign(tree: untpd.Assign, pt: Type)(implicit ctx: Context) = track("typedAssign") {
tree.lhs match {
case lhs @ Apply(fn, args) =>
typed(cpy.Apply(lhs)(untpd.Select(fn, nme.update), args :+ tree.rhs), pt)
case untpd.TypedSplice(Apply(MaybePoly(Select(fn, app), targs), args)) if app == nme.apply =>
val rawUpdate: untpd.Tree = untpd.Select(untpd.TypedSplice(fn), nme.update)
val wrappedUpdate =
if (targs.isEmpty) rawUpdate
else untpd.TypeApply(rawUpdate, targs map (untpd.TypedSplice(_)))
val appliedUpdate = cpy.Apply(fn)(wrappedUpdate, (args map (untpd.TypedSplice(_))) :+ tree.rhs)
typed(appliedUpdate, pt)
case lhs =>
val lhsCore = typedUnadapted(lhs, AssignProto)
def lhs1 = typed(untpd.TypedSplice(lhsCore))
def canAssign(sym: Symbol) = // allow assignments from the primary constructor to class fields
sym.is(Mutable, butNot = Accessor) ||
ctx.owner.isPrimaryConstructor && !sym.is(Method) && sym.owner == ctx.owner.owner ||
ctx.owner.name.isTraitSetterName || ctx.owner.isStaticConstructor
lhsCore.tpe match {
case ref: TermRef if canAssign(ref.symbol) =>
assignType(cpy.Assign(tree)(lhs1, typed(tree.rhs, ref.info)))
case _ =>
def reassignmentToVal =
errorTree(cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)),
"reassignment to val")
lhsCore.tpe match {
case ref: TermRef => // todo: further conditions to impose on getter?
val pre = ref.prefix
val setterName = ref.name.setterName
val setter = pre.member(setterName)
lhsCore match {
case lhsCore: RefTree if setter.exists =>
val setterTypeRaw = pre.select(setterName, setter)
val setterType = ensureAccessible(setterTypeRaw, isSuperSelection(lhsCore), tree.pos)
val lhs2 = healNonvariant(
untpd.rename(lhsCore, setterName).withType(setterType), WildcardType)
typedUnadapted(cpy.Apply(tree)(untpd.TypedSplice(lhs2), tree.rhs :: Nil))
case _ =>
reassignmentToVal
}
case TryDynamicCallType =>
typedDynamicAssign(tree, pt)
case tpe =>
reassignmentToVal
}
}
}
}
def typedBlockStats(stats: List[untpd.Tree])(implicit ctx: Context): (Context, List[tpd.Tree]) =
(indexAndAnnotate(stats), typedStats(stats, ctx.owner))
def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context) = track("typedBlock") {
val (exprCtx, stats1) = typedBlockStats(tree.stats)
val ept =
if (tree.isInstanceOf[untpd.InfixOpBlock])
// Right-binding infix operations are expanded to InfixBlocks, which may be followed by arguments.
// Example: `(a /: bs)(op)` expands to `{ val x = a; bs./:(x) } (op)` where `{...}` is an InfixBlock.
pt
else pt.notApplied
val expr1 = typedExpr(tree.expr, ept)(exprCtx)
ensureNoLocalRefs(
assignType(cpy.Block(tree)(stats1, expr1), stats1, expr1), pt, localSyms(stats1))
}
def escapingRefs(block: Tree, localSyms: => List[Symbol])(implicit ctx: Context): collection.Set[NamedType] = {
lazy val locals = localSyms.toSet
block.tpe namedPartsWith (tp => locals.contains(tp.symbol))
}
/** Check that expression's type can be expressed without references to locally defined
* symbols. The following two remedies are tried before giving up:
* 1. If the expected type of the expression is fully defined, pick it as the
* type of the result expressed by adding a type ascription.
* 2. If (1) fails, force all type variables so that the block's type is
* fully defined and try again.
*/
protected def ensureNoLocalRefs(tree: Tree, pt: Type, localSyms: => List[Symbol], forcedDefined: Boolean = false)(implicit ctx: Context): Tree = {
def ascribeType(tree: Tree, pt: Type): Tree = tree match {
case block @ Block(stats, expr) =>
val expr1 = ascribeType(expr, pt)
cpy.Block(block)(stats, expr1) withType expr1.tpe // no assignType here because avoid is redundant
case _ =>
Typed(tree, TypeTree(pt.simplified))
}
val leaks = escapingRefs(tree, localSyms)
if (leaks.isEmpty) tree
else if (isFullyDefined(pt, ForceDegree.none)) ascribeType(tree, pt)
else if (!forcedDefined) {
fullyDefinedType(tree.tpe, "block", tree.pos)
val tree1 = ascribeType(tree, avoid(tree.tpe, localSyms))
ensureNoLocalRefs(tree1, pt, localSyms, forcedDefined = true)
} else
errorTree(tree,
em"local definition of ${leaks.head.name} escapes as part of expression's type ${tree.tpe}"/*; full type: ${result.tpe.toString}"*/)
}
def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context): Tree = track("typedIf") {
val cond1 = typed(tree.cond, defn.BooleanType)
val thenp1 = typed(tree.thenp, pt.notApplied)
val elsep1 = typed(tree.elsep orElse (untpd.unitLiteral withPos tree.pos), pt.notApplied)
val thenp2 :: elsep2 :: Nil = harmonize(thenp1 :: elsep1 :: Nil)
assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2)
}
private def decomposeProtoFunction(pt: Type, defaultArity: Int)(implicit ctx: Context): (List[Type], Type) = pt match {
case _ if defn.isFunctionType(pt) =>
// if expected parameter type(s) are wildcards, approximate from below.
// if expected result type is a wildcard, approximate from above.
// this can type the greatest set of admissible closures.
(pt.dealias.argTypesLo.init, pt.dealias.argTypesHi.last)
case SAMType(meth) =>
val mt @ MethodType(_, paramTypes) = meth.info
(paramTypes, mt.resultType)
case _ =>
(List.range(0, defaultArity) map alwaysWildcardType, WildcardType)
}
def typedFunction(tree: untpd.Function, pt: Type)(implicit ctx: Context) = track("typedFunction") {
val untpd.Function(args, body) = tree
if (ctx.mode is Mode.Type) {
val funCls =
if (tree.isInstanceOf[untpd.ImplicitFunction]) defn.ImplicitFunctionClass(args.length)
else defn.FunctionClass(args.length)
typed(cpy.AppliedTypeTree(tree)(
untpd.TypeTree(funCls.typeRef), args :+ body), pt)
}
else {
val params = args.asInstanceOf[List[untpd.ValDef]]
pt match {
case pt: TypeVar if untpd.isFunctionWithUnknownParamType(tree) =>
// try to instantiate `pt` if this is possible. If it does not
// work the error will be reported later in `inferredParam`,
// when we try to infer the parameter type.
isFullyDefined(pt, ForceDegree.noBottom)
case _ =>
}
val (protoFormals, protoResult) = decomposeProtoFunction(pt, params.length)
def refersTo(arg: untpd.Tree, param: untpd.ValDef): Boolean = arg match {
case Ident(name) => name == param.name
case _ => false
}
/** The function body to be returned in the closure. Can become a TypedSplice
* of a typed expression if this is necessary to infer a parameter type.
*/
var fnBody = tree.body
/** If function is of the form
* (x1, ..., xN) => f(x1, ..., XN)
* the type of `f`, otherwise NoType. (updates `fnBody` as a side effect).
*/
def calleeType: Type = fnBody match {
case Apply(expr, args) if (args corresponds params)(refersTo) =>
expr match {
case untpd.TypedSplice(expr1) =>
expr1.tpe
case _ =>
val protoArgs = args map (_ withType WildcardType)
val callProto = FunProto(protoArgs, WildcardType, this)
val expr1 = typedExpr(expr, callProto)
fnBody = cpy.Apply(fnBody)(untpd.TypedSplice(expr1), args)
expr1.tpe
}
case _ =>
NoType
}
/** Two attempts: First, if expected type is fully defined pick this one.
* Second, if function is of the form
* (x1, ..., xN) => f(x1, ..., XN)
* and f has a method type MT, pick the corresponding parameter type in MT,
* if this one is fully defined.
* If both attempts fail, issue a "missing parameter type" error.
*/
def inferredParamType(param: untpd.ValDef, formal: Type): Type = {
if (isFullyDefined(formal, ForceDegree.noBottom)) return formal
calleeType.widen match {
case mtpe: MethodType =>
val pos = params indexWhere (_.name == param.name)
if (pos < mtpe.paramTypes.length) {
val ptype = mtpe.paramTypes(pos)
if (isFullyDefined(ptype, ForceDegree.noBottom)) return ptype
}
case _ =>
}
val ofFun =
if (nme.syntheticParamNames(args.length + 1) contains param.name)
i" of expanded function $tree"
else
""
errorType(i"missing parameter type for parameter ${param.name}$ofFun, expected = $pt", param.pos)
}
def protoFormal(i: Int): Type =
if (protoFormals.length == params.length) protoFormals(i)
else errorType(i"wrong number of parameters, expected: ${protoFormals.length}", tree.pos)
/** Is `formal` a product type which is elementwise compatible with `params`? */
def ptIsCorrectProduct(formal: Type) = {
val pclass = defn.ProductNType(params.length).symbol
isFullyDefined(formal, ForceDegree.noBottom) &&
formal.derivesFrom(pclass) &&
formal.baseArgTypes(pclass).corresponds(params) {
(argType, param) =>
param.tpt.isEmpty || argType <:< typedAheadType(param.tpt).tpe
}
}
val desugared =
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
desugar.makeTupledFunction(params, fnBody)
}
else {
val inferredParams: List[untpd.ValDef] =
for ((param, i) <- params.zipWithIndex) yield
if (!param.tpt.isEmpty) param
else cpy.ValDef(param)(
tpt = untpd.TypeTree(
inferredParamType(param, protoFormal(i)).underlyingIfRepeated(isJava = false)))
// Define result type of closure as the expected type, thereby pushing
// down any implicit searches. We do this even if the expected type is not fully
// defined, which is a bit of a hack. But it's needed to make the following work
// (see typers.scala and printers/PlainPrinter.scala for examples).
//
// def double(x: Char): String = s"$x$x"
// "abc" flatMap double
//
val resultTpt = protoResult match {
case WildcardType(_) => untpd.TypeTree()
case _ => untpd.TypeTree(protoResult)
}
val inlineable = pt.hasAnnotation(defn.InlineParamAnnot)
desugar.makeClosure(inferredParams, fnBody, resultTpt, inlineable)
}
typed(desugared, pt)
}
}
def typedClosure(tree: untpd.Closure, pt: Type)(implicit ctx: Context): Tree = track("typedClosure") {
val env1 = tree.env mapconserve (typed(_))
val meth1 = typedUnadapted(tree.meth)
val target =
if (tree.tpt.isEmpty)
meth1.tpe.widen match {
case mt: MethodType =>
pt match {
case SAMType(meth) if !defn.isFunctionType(pt) && mt <:< meth.info =>
if (!isFullyDefined(pt, ForceDegree.all))
ctx.error(ex"result type of closure is an underspecified SAM type $pt", tree.pos)
TypeTree(pt)
case _ =>
if (!mt.isDependent) EmptyTree
else throw new java.lang.Error(i"internal error: cannot turn dependent method type $mt into closure, position = ${tree.pos}, raw type = ${mt.toString}") // !!! DEBUG. Eventually, convert to an error?
}
case tp =>
throw new java.lang.Error(i"internal error: closing over non-method $tp, pos = ${tree.pos}")
}
else typed(tree.tpt)
//println(i"typing closure $tree : ${meth1.tpe.widen}")
assignType(cpy.Closure(tree)(env1, meth1, target), meth1, target)
}
def typedMatch(tree: untpd.Match, pt: Type)(implicit ctx: Context) = track("typedMatch") {
tree.selector match {
case EmptyTree =>
val (protoFormals, _) = decomposeProtoFunction(pt, 1)
val unchecked = pt <:< defn.PartialFunctionType
typed(desugar.makeCaseLambda(tree.cases, protoFormals.length, unchecked) withPos tree.pos, pt)
case _ =>
val sel1 = typedExpr(tree.selector)
val selType = widenForMatchSelector(
fullyDefinedType(sel1.tpe, "pattern selector", tree.pos))
val cases1 = typedCases(tree.cases, selType, pt.notApplied)
val cases2 = harmonize(cases1).asInstanceOf[List[CaseDef]]
assignType(cpy.Match(tree)(sel1, cases2), cases2)
}
}
def typedCases(cases: List[untpd.CaseDef], selType: Type, pt: Type)(implicit ctx: Context) = {
/** gadtSyms = "all type parameters of enclosing methods that appear
* non-variantly in the selector type" todo: should typevars
* which appear with variances +1 and -1 (in different
* places) be considered as well?
*/
val gadtSyms: Set[Symbol] = ctx.traceIndented(i"GADT syms of $selType", gadts) {
val accu = new TypeAccumulator[Set[Symbol]] {
def apply(tsyms: Set[Symbol], t: Type): Set[Symbol] = {
val tsyms1 = t match {
case tr: TypeRef if (tr.symbol is TypeParam) && tr.symbol.owner.isTerm && variance == 0 =>
tsyms + tr.symbol
case _ =>
tsyms
}
foldOver(tsyms1, t)
}
}
accu(Set.empty, selType)
}
cases mapconserve (typedCase(_, pt, selType, gadtSyms))
}
/** Type a case. Overridden in ReTyper, that's why it's separate from
* typedCases.
*/
def typedCase(tree: untpd.CaseDef, pt: Type, selType: Type, gadtSyms: Set[Symbol])(implicit ctx: Context): CaseDef = track("typedCase") {
val originalCtx = ctx
/** - replace all references to symbols associated with wildcards by their GADT bounds
* - enter all symbols introduced by a Bind in current scope
*/
val indexPattern = new TreeMap {
val elimWildcardSym = new TypeMap {
def apply(t: Type) = t match {
case ref @ TypeRef(_, tpnme.WILDCARD) if ctx.gadt.bounds.contains(ref.symbol) =>
ctx.gadt.bounds(ref.symbol)
case TypeAlias(ref @ TypeRef(_, tpnme.WILDCARD)) if ctx.gadt.bounds.contains(ref.symbol) =>
ctx.gadt.bounds(ref.symbol)
case _ =>
mapOver(t)
}
}
override def transform(trt: Tree)(implicit ctx: Context) =
super.transform(trt.withType(elimWildcardSym(trt.tpe))) match {
case b: Bind =>
if (ctx.scope.lookup(b.name) == NoSymbol) ctx.enter(b.symbol)
else ctx.error(new DuplicateBind(b, tree), b.pos)
b.symbol.info = elimWildcardSym(b.symbol.info)
b
case t => t
}
}
def caseRest(pat: Tree)(implicit ctx: Context) = {
val pat1 = indexPattern.transform(pat)
val guard1 = typedExpr(tree.guard, defn.BooleanType)
val body1 = ensureNoLocalRefs(typedExpr(tree.body, pt), pt, ctx.scope.toList)
.ensureConforms(pt)(originalCtx) // insert a cast if body does not conform to expected type if we disregard gadt bounds
assignType(cpy.CaseDef(tree)(pat1, guard1, body1), body1)
}
val gadtCtx =
if (gadtSyms.isEmpty) ctx
else {
val c = ctx.fresh.setFreshGADTBounds
for (sym <- gadtSyms)
if (!c.gadt.bounds.contains(sym))
c.gadt.setBounds(sym, TypeBounds.empty)
c
}
val pat1 = typedPattern(tree.pat, selType)(gadtCtx)
caseRest(pat1)(gadtCtx.fresh.setNewScope)
}
def typedReturn(tree: untpd.Return)(implicit ctx: Context): Return = track("typedReturn") {
def returnProto(owner: Symbol, locals: Scope): Type =
if (owner.isConstructor) defn.UnitType
else owner.info match {
case info: PolyType =>
val tparams = locals.toList.takeWhile(_ is TypeParam)
assert(info.paramNames.length == tparams.length,
i"return mismatch from $owner, tparams = $tparams, locals = ${locals.toList}%, %")
info.instantiate(tparams.map(_.typeRef)).finalResultType
case info =>
info.finalResultType
}
def enclMethInfo(cx: Context): (Tree, Type) = {
val owner = cx.owner
if (cx == NoContext || owner.isType) {
ctx.error("return outside method definition", tree.pos)
(EmptyTree, WildcardType)
}
else if (owner != cx.outer.owner && owner.isRealMethod) {
if (owner.isInlineMethod)
(EmptyTree, errorType(em"no explicit return allowed from inline $owner", tree.pos))
else if (!owner.isCompleted)
(EmptyTree, errorType(em"$owner has return statement; needs result type", tree.pos))
else {
val from = Ident(TermRef(NoPrefix, owner.asTerm))
val proto = returnProto(owner, cx.scope)
(from, proto)
}
}
else enclMethInfo(cx.outer)
}
val (from, proto) =
if (tree.from.isEmpty) enclMethInfo(ctx)
else {
val from = tree.from.asInstanceOf[tpd.Tree]
val proto =
if (ctx.erasedTypes) from.symbol.info.finalResultType
else WildcardType // We cannot reliably detect the internal type view of polymorphic or dependent methods
// because we do not know the internal type params and method params.
// Hence no adaptation is possible, and we assume WildcardType as prototype.
(from, proto)
}
val expr1 = typedExpr(tree.expr orElse untpd.unitLiteral.withPos(tree.pos), proto)
assignType(cpy.Return(tree)(expr1, from))
}
def typedTry(tree: untpd.Try, pt: Type)(implicit ctx: Context): Try = track("typedTry") {
val expr1 = typed(tree.expr, pt.notApplied)
val cases1 = typedCases(tree.cases, defn.ThrowableType, pt.notApplied)
val finalizer1 = typed(tree.finalizer, defn.UnitType)
val expr2 :: cases2x = harmonize(expr1 :: cases1)
val cases2 = cases2x.asInstanceOf[List[CaseDef]]
assignType(cpy.Try(tree)(expr2, cases2, finalizer1), expr2, cases2)
}
def typedThrow(tree: untpd.Throw)(implicit ctx: Context): Tree = track("typedThrow") {
val expr1 = typed(tree.expr, defn.ThrowableType)
Throw(expr1).withPos(tree.pos)
}
def typedSeqLiteral(tree: untpd.SeqLiteral, pt: Type)(implicit ctx: Context): SeqLiteral = track("typedSeqLiteral") {
val proto1 = pt.elemType match {
case NoType => WildcardType
case bounds: TypeBounds => WildcardType(bounds)
case elemtp => elemtp
}
val elems1 = tree.elems mapconserve (typed(_, proto1))
val proto2 = // the computed type of the `elemtpt` field
if (!tree.elemtpt.isEmpty) WildcardType
else if (isFullyDefined(proto1, ForceDegree.none)) proto1
else if (tree.elems.isEmpty && tree.isInstanceOf[Trees.JavaSeqLiteral[_]])
defn.ObjectType // generic empty Java varargs are of type Object[]
else ctx.typeComparer.lub(elems1.tpes)
val elemtpt1 = typed(tree.elemtpt, proto2)
assignType(cpy.SeqLiteral(tree)(elems1, elemtpt1), elems1, elemtpt1)
}
def typedInlined(tree: untpd.Inlined, pt: Type)(implicit ctx: Context): Inlined = {
val (exprCtx, bindings1) = typedBlockStats(tree.bindings)
val expansion1 = typed(tree.expansion, pt)(inlineContext(tree.call)(exprCtx))
assignType(cpy.Inlined(tree)(tree.call, bindings1.asInstanceOf[List[MemberDef]], expansion1),
bindings1, expansion1)
}
def typedTypeTree(tree: untpd.TypeTree, pt: Type)(implicit ctx: Context): TypeTree = track("typedTypeTree") {
tree match {
case tree: untpd.DerivedTypeTree =>
tree.ensureCompletions
try
TypeTree(tree.derivedType(tree.attachment(untpd.OriginalSymbol))) withPos tree.pos
// btw, no need to remove the attachment. The typed
// tree is different from the untyped one, so the
// untyped tree is no longer accessed after all
// accesses with typedTypeTree are done.