-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Typer.scala
3170 lines (2927 loc) · 140 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.{tpd, _}
import Trees._
import Constants._
import StdNames._
import Scopes._
import Denotations._
import ProtoTypes._
import Contexts._
import Symbols._
import Types._
import SymDenotations._
import Annotations._
import Names._
import NameOps._
import NameKinds._
import Flags._
import Decorators._
import ErrorReporting._
import Checking._
import Inferencing._
import EtaExpansion.etaExpand
import util.Spans._
import util.common._
import util.Property
import Applications.{ExtMethodApply, IntegratedTypeArgs, productSelectorTypes, wrapDefs}
import collection.mutable
import annotation.tailrec
import Implicits._
import util.Stats.{record, track}
import config.Printers.{gadts, typr}
import rewrites.Rewrites.patch
import NavigateAST._
import dotty.tools.dotc.transform.{PCPCheckAndHeal, Staging, TreeMapWithStages}
import dotty.tools.dotc.core.StagingContext._
import transform.SymUtils._
import transform.TypeUtils._
import reporting.trace
object Typer {
/** The precedence of bindings which determines which of several bindings will be
* accessed by an Ident.
*/
object BindingPrec {
val definition: Int = 4
val namedImport: Int = 3
val wildImport: Int = 2
val packageClause: Int = 1
val nothingBound: Int = 0
def isImportPrec(prec: Int): Boolean = 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): Unit =
if (!tree.isEmpty && !tree.isInstanceOf[untpd.TypedSplice] && ctx.typerState.isGlobalCommittable)
assert(tree.span.exists, i"position not set for $tree # ${tree.uniqueId} of ${tree.getClass} in ${tree.source}")
/** A context property that indicates the owner of any expressions to be typed in the context
* if that owner is different from the context's owner. Typically, a context with a class
* as owner would have a local dummy as ExprOwner value.
*/
private val ExprOwner = new Property.Key[Symbol]
/** An attachment on a Select node with an `apply` field indicating that the `apply`
* was inserted by the Typer.
*/
private val InsertedApply = new Property.Key[Unit]
/** An attachment on a tree `t` occurring as part of a `t()` where
* the `()` was dropped by the Typer.
*/
private val DroppedEmptyArgs = new Property.Key[Unit]
/** An attachment that indicates a failed conversion or extension method
* search was tried on a tree. This will in some cases be reported in error messages
*/
private[typer] val HiddenSearchFailure = new Property.Key[SearchFailure]
}
class Typer extends Namer
with TypeAssigner
with Applications
with Implicits
with Inferencing
with Dynamic
with Checking
with Deriving {
import Typer._
import tpd.{cpy => _, _}
import untpd.cpy
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[this] 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[this] var foundUnderScala2: Type = NoType
def newLikeThis: Typer = new Typer
/** Find the type of an identifier with given `name` in given context `ctx`.
* @param name the name of the identifier
* @param pt the expected type
* @param required flags the result's symbol must have
* @param posd indicates position to use for error reporting
*/
def findRef(name: Name, pt: Type, required: FlagSet, posd: Positioned)(implicit ctx: Context): Type = {
val refctx = ctx
val noImports = ctx.mode.is(Mode.InPackageClauseName)
/** 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 findRefRecur(previous: Type, prevPrec: Int, prevCtx: Context)(implicit ctx: Context): Type = {
import BindingPrec._
/** 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) {
refctx.error(AmbiguousImport(name, newPrec, prevPrec, prevCtx), posd.sourcePos)
}
previous
}
def selection(imp: ImportInfo, name: Name, checkBounds: Boolean) =
if (imp.sym.isCompleting) {
ctx.warning(i"cyclic ${imp.sym}, ignored", posd.sourcePos)
NoType
} else if (unimported.nonEmpty && unimported.contains(imp.site.termSymbol))
NoType
else {
val pre = imp.site
var reqd = required
var excl = EmptyFlags
if (imp.importDelegate) reqd |= Delegate else excl |= Delegate
var denot = pre.memberBasedOnFlags(name, reqd, excl).accessibleFrom(pre)(refctx)
if (checkBounds && imp.impliedBound.exists)
denot = denot.filterWithPredicate(mbr =>
NoViewsAllowed.normalizedCompatible(mbr.info, imp.impliedBound, keepConstraint = false))
// Pass refctx so that any errors are reported in the context of the
// reference instead of the
if (reallyExists(denot)) pre.select(name, denot) else NoType
}
/** 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
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))
refctx.error(em"reference to `$name` is ambiguous; it is imported twice", posd.sourcePos)
found
}
def unambiguousSelection(name: Name) =
checkUnambiguous(selection(imp, name, checkBounds = false))
selector match {
case Thicket(Ident(from) :: Ident(Name) :: _) =>
unambiguousSelection(if (name.isTypeName) from.toTypeName else from)
case Ident(Name) =>
unambiguousSelection(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 && !imp.excluded.contains(name.toTermName) && name != nme.CONSTRUCTOR)
selection(imp, name, checkBounds = imp.importDelegate)
else 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.source == ctx.compilationUnit.source
}
/** 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(lastCtx: Context)(implicit ctx: Context): Type = {
if (ctx.scope == null) previous
else {
var result: Type = NoType
val curOwner = ctx.owner
/** Is curOwner a package object that should be skipped?
* A package object should always be skipped if we look for a term.
* That way we make sure we consider all overloaded alternatives of
* a definition, even if they are in different source files.
* If we are looking for a type, a package object should ne skipped
* only if it does not contain opaque definitions. Package objects
* with opaque definitions are significant, since opaque aliases
* are only seen if the prefix is the this-type of the package object.
*/
def isTransparentPackageObject =
curOwner.isPackageObject && (name.isTermName || !curOwner.is(Opaque))
// Can this scope contain new definitions? This is usually the first
// context where either the scope or the owner changes wrt the
// context immediately nested in it. But for package contexts, it's
// the opposite: the last context before the package changes. This distinction
// is made so that top-level imports following a package clause are
// logically nested in that package clause. Finally, for the root package
// we switch back to the original test. This means that top-level packages in
// the root package take priority over root imports. For instance,
// a top-level io package takes priority over scala.io.
// It would be nice if we could drop all this complication, and
// always use the second condition. Unfortunately compileStdLib breaks
// with an error on CI which I cannot replicate locally (not even
// with the exact list of files given).
val isNewDefScope =
if (curOwner.is(Package) && !curOwner.isRoot) curOwner ne ctx.outer.owner
else ((ctx.scope ne lastCtx.scope) || (curOwner ne lastCtx.owner)) &&
!isTransparentPackageObject
if (isNewDefScope) {
val defDenot = ctx.denotNamed(name, required)
if (qualifies(defDenot)) {
val found =
if (isSelfDenot(defDenot)) curOwner.enclosingClass.thisType
else {
val effectiveOwner =
if (curOwner.isTerm && defDenot.symbol.isType)
// Don't mix NoPrefix and thisType prefixes, since type comparer
// would not detect types to be compatible. Note: If we replace the
// 2nd condition by `defDenot.symbol.maybeOwner.isType` we get lots
// of failures in the `tastyBootstrap` test. Trying to compile these
// files in isolation works though.
// TODO: Investigate why that happens.
defDenot.symbol.owner
else
curOwner
effectiveOwner.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 = findRefRecur(found, packageClause, ctx)(ctx.outer)
}
}
}
if (result.exists) result
else { // find import
val outer = ctx.outer
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)
findRefRecur(checkNewOrShadowed(namedImp, namedImport), namedImport, ctx)(outer)
else if (isPossibleImport(wildImport) && !curImport.sym.isCompleting) {
val wildImp = wildImportRef(curImport)
if (wildImp.exists)
findRefRecur(checkNewOrShadowed(wildImp, wildImport), wildImport, ctx)(outer)
else {
updateUnimported()
loop(ctx)(outer)
}
}
else {
updateUnimported()
loop(ctx)(outer)
}
}
else loop(ctx)(outer)
}
}
}
// begin findRefRecur
loop(NoContext)(ctx)
}
findRefRecur(NoType, BindingPrec.nothingBound, NoContext)
}
/** 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 name = tree.name
// 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 (untpd.isVarPattern(tree) && name.isTermName)
return typed(desugar.patternVar(tree), pt)
}
// Shortcut for the root package, this is not just a performance
// optimization, it also avoids forcing imports thus potentially avoiding
// cyclic references.
if (name == nme.ROOTPKG)
return tree.withType(defn.RootPackage.termRef)
val rawType = {
val saved1 = unimported
val saved2 = foundUnderScala2
unimported = Set.empty
foundUnderScala2 = NoType
try {
var found = findRef(name, pt, EmptyFlags, tree.posd)
if (foundUnderScala2.exists && !(foundUnderScala2 =:= found)) {
ctx.migrationWarning(
ex"""Name resolution will change.
| currently selected : $foundUnderScala2
| in the future, without -language:Scala2: $found""", tree.sourcePos)
found = foundUnderScala2
}
found
}
finally {
unimported = saved1
foundUnderScala2 = saved2
}
}
val ownType =
if (rawType.exists)
ensureAccessible(rawType, superAccess = false, tree.sourcePos)
else if (name == nme._scope) {
// gross hack to support current xml literals.
// awaiting a better implicits based solution for library-supported xml
return ref(defn.XMLTopScopeModuleRef)
}
else if (name.toTermName == nme.ERROR)
UnspecifiedErrorType
else if (ctx.owner.isConstructor && ctx.mode.is(Mode.InSuperCall) &&
ctx.owner.owner.unforcedDecls.lookup(tree.name).exists) {
// When InSuperCall mode and in a constructor we are in the arguments
// of a this(...) constructor call
errorType(ex"$tree is not accessible from constructor arguments", tree.sourcePos)
} else
errorType(new MissingIdent(tree, kind, name.show), tree.sourcePos)
val tree1 = ownType match {
case ownType: NamedType if !prefixIsElidable(ownType) =>
ref(ownType).withSpan(tree.span)
case _ =>
tree.withType(ownType)
}
checkStableIdentPattern(tree1, pt)
checkValue(tree1, pt)
}
/** Check that a stable identifier pattern is indeed stable (SLS 8.1.5)
*/
private def checkStableIdentPattern(tree: Tree, pt: Type)(implicit ctx: Context): tree.type = {
if (ctx.mode.is(Mode.Pattern) &&
!tree.isType &&
!pt.isInstanceOf[ApplyingProto] &&
!tree.tpe.isStable &&
!isWildcardArg(tree))
ctx.error(StableIdentPattern(tree, pt), tree.sourcePos)
tree
}
def typedSelect(tree: untpd.Select, pt: Type, qual: Tree)(implicit ctx: Context): Tree = qual match {
case qual @ IntegratedTypeArgs(app) =>
pt.revealIgnored match {
case _: PolyProto => qual // keep the IntegratedTypeArgs to strip at next typedTypeApply
case _ => app
}
case qual =>
if (tree.name.isTypeName) checkStable(qual.tpe, qual.sourcePos)
val select = checkValue(assignType(cpy.Select(tree)(qual, tree.name), qual), pt)
if (select.tpe ne TryDynamicCallType) ConstFold(checkStableIdentPattern(select, pt))
else if (pt.isInstanceOf[FunOrPolyProto] || pt == AssignProto) select
else typedDynamicSelect(tree, Nil, pt)
}
def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = track("typedSelect") {
def typeSelectOnTerm(implicit ctx: Context): Tree =
typedSelect(tree, pt, typedExpr(tree.qualifier, selectionProto(tree.name, pt, this)))
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
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
}
val enclosingInlineable = ctx.owner.ownersIterator.findSymbol(_.isInlineMethod)
if (enclosingInlineable.exists && !PrepareInlineable.isLocal(qual1.symbol, enclosingInlineable))
ctx.error(SuperCallsNotAllowedInlineable(enclosingInlineable), tree.sourcePos)
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): Tree = track("typedLiteral") {
val tree1 = assignType(tree)
if (ctx.mode.is(Mode.Type)) tpd.SingletonTypeTree(tree1) // this ensures that tree is classified as a type tree
else tree1
}
def typedNew(tree: untpd.New, pt: Type)(implicit ctx: Context): Tree = track("typedNew") {
tree.tpt match {
case templ: untpd.Template =>
import untpd._
var templ1 = templ
def isEligible(tp: Type) = tp.exists && !tp.typeSymbol.is(Final) && !tp.isRef(defn.AnyClass)
if (templ1.parents.isEmpty &&
isFullyDefined(pt, ForceDegree.noBottom) &&
isSkolemFree(pt) &&
isEligible(pt.underlyingClassRef(refinementOK = false)))
templ1 = cpy.Template(templ)(parents = untpd.TypeTree(pt) :: Nil)
templ1.parents foreach {
case parent: RefTree =>
typedAhead(parent, tree => inferTypeParams(typedType(tree), pt))
case _ =>
}
val x = tpnme.ANON_CLASS
val clsDef = TypeDef(x, templ1).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.sourcePos))
if (checkClassType(typeOfNew(tpt1), tpt1.sourcePos, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)
tpt1 = TypeTree(defn.ObjectType).withSpan(tpt1.span)
tpt1 match {
case AppliedTypeTree(_, targs) =>
for (targ @ TypeBoundsTree(_, _) <- targs)
ctx.error(WildcardOnTypeArgumentNotAllowedOnNew(), targ.sourcePos)
case _ =>
}
assignType(cpy.New(tree)(tpt1), tpt1)
}
}
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) && untpd.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)).withSpan(tree.span), 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)) {
def typedWildcardStarArgExpr = {
val ptArg =
if (ctx.mode.is(Mode.QuotedPattern)) pt.underlyingIfRepeated(isJava = false)
else WildcardType
val tpdExpr = typedExpr(tree.expr, ptArg)
tpdExpr.tpe.widenDealias match {
case defn.ArrayOf(_) =>
val starType = defn.ArrayType.appliedTo(WildcardType)
val exprAdapted = adapt(tpdExpr, starType)
arrayToRepeated(exprAdapted)
case _ =>
val starType = defn.SeqType.appliedTo(defn.AnyType)
val exprAdapted = adapt(tpdExpr, starType)
seqToRepeated(exprAdapted)
}
}
cases(
ifPat = ascription(TypeTree(defn.RepeatedParamType.appliedTo(pt)), isWildcard = true),
ifExpr = typedWildcardStarArgExpr,
wildName = nme.WILDCARD_STAR)
}
else {
def typedTpt = checkSimpleKinded(typedType(tree.tpt))
def handlePattern: Tree = {
val tpt1 = typedTpt
if (!ctx.isAfterTyper && pt != defn.ImplicitScrutineeTypeRef)
ctx.addMode(Mode.GadtConstraintInference).typeComparer.constrainPatternType(tpt1.tpe, pt)
// special case for an abstract type that comes with a class tag
tryWithClassTag(ascription(tpt1, isWildcard = true), pt)
}
cases(
ifPat = handlePattern,
ifExpr = ascription(typedTpt, isWildcard = false),
wildName = nme.WILDCARD)
}
}
/** For a typed tree `e: T`, if `T` is an abstract type for which an implicit class tag `ctag`
* exists, rewrite to `ctag(e)`.
* @pre We are in pattern-matching mode (Mode.Pattern)
*/
def tryWithClassTag(tree: Typed, pt: Type)(implicit ctx: Context): Tree = tree.tpt.tpe.dealias match {
case tref: TypeRef if !tref.symbol.isClass && !ctx.isAfterTyper =>
require(ctx.mode.is(Mode.Pattern))
inferImplicit(defn.ClassTagType.appliedTo(tref),
EmptyTree, tree.tpt.span)(ctx.retractMode(Mode.Pattern)) match {
case SearchSuccess(clsTag, _, _) =>
typed(untpd.Apply(untpd.TypedSplice(clsTag), untpd.TypedSplice(tree.expr)), pt)
case _ =>
tree
}
case _ => tree
}
def typedNamedArg(tree: untpd.NamedArg, pt: Type)(implicit ctx: Context): NamedArg = 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): Tree = track("typedAssign") {
tree.lhs match {
case lhs @ Apply(fn, args) =>
typed(untpd.Apply(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 =
untpd.Apply(wrappedUpdate, (args map (untpd.TypedSplice(_))) :+ tree.rhs)
typed(appliedUpdate, pt)
case lhs =>
val locked = ctx.typerState.ownedVars
val lhsCore = typedUnadapted(lhs, AssignProto, locked)
def lhs1 = typed(untpd.TypedSplice(lhsCore), WildcardType, locked)
def reassignmentToVal =
errorTree(cpy.Assign(tree)(lhsCore, typed(tree.rhs, lhs1.tpe.widen)),
ReassignmentToVal(lhsCore.symbol.name))
def canAssign(sym: Symbol) =
sym.is(Mutable, butNot = Accessor) ||
ctx.owner.isPrimaryConstructor && !sym.is(Method) && sym.owner == ctx.owner.owner ||
// allow assignments from the primary constructor to class fields
ctx.owner.name.is(TraitSetterName) || ctx.owner.isStaticConstructor
lhsCore.tpe match {
case ref: TermRef =>
val lhsVal = lhsCore.denot.suchThat(!_.is(Method))
if (canAssign(lhsVal.symbol)) {
// lhsBounds: (T .. Any) as seen from lhs prefix, where T is the type of lhsVal.symbol
// This ensures we do the as-seen-from on T with variance -1. Test case neg/i2928.scala
val lhsBounds =
TypeBounds.lower(lhsVal.symbol.info).asSeenFrom(ref.prefix, lhsVal.symbol.owner)
assignType(cpy.Assign(tree)(lhs1, typed(tree.rhs, lhsBounds.loBound)))
}
else {
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.sourcePos)
val lhs2 = untpd.rename(lhsCore, setterName).withType(setterType)
typedUnadapted(untpd.Apply(untpd.TypedSplice(lhs2), tree.rhs :: Nil), WildcardType, locked)
case _ =>
reassignmentToVal
}
}
case TryDynamicCallType =>
typedDynamicAssign(tree, pt)
case tpe =>
reassignmentToVal
}
}
}
def typedBlockStats(stats: List[untpd.Tree])(implicit ctx: Context): (Context, List[tpd.Tree]) =
(index(stats), typedStats(stats, ctx.owner))
def typedBlock(tree: untpd.Block, pt: Type)(implicit ctx: Context): Tree = track("typedBlock") {
val (exprCtx, stats1) = typedBlockStats(tree.stats)
val expr1 = typedExpr(tree.expr, pt.dropIfProto)(exprCtx)
ensureNoLocalRefs(
cpy.Block(tree)(stats1, expr1).withType(expr1.tpe), 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))
}
/** Ensure that an expression's type can be expressed without references to locally defined
* symbols. This is done by adding a type ascription of a widened type that does
* not refer to the locally defined symbols. The widened type is computed using
* `TyperAssigner#avoid`. However, if the expected type is fully defined and not
* a supertype of the widened type, we ascribe with the expected type instead.
*
* There's a special case having to do with anonymous classes. Sometimes the
* expected type of a block is the anonymous class defined inside it. In that
* case there's technically a leak which is not removed by the ascription.
*/
protected def ensureNoLocalRefs(tree: Tree, pt: Type, localSyms: => List[Symbol])(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))
}
def noLeaks(t: Tree): Boolean = escapingRefs(t, localSyms).isEmpty
if (noLeaks(tree)) tree
else {
fullyDefinedType(tree.tpe, "block", tree.span)
var avoidingType = avoid(tree.tpe, localSyms)
val ptDefined = isFullyDefined(pt, ForceDegree.none)
if (ptDefined && !(avoidingType.widenExpr <:< pt)) avoidingType = pt
val tree1 = ascribeType(tree, avoidingType)
assert(ptDefined || noLeaks(tree1) || tree1.tpe.isErroneous,
// `ptDefined` needed because of special case of anonymous classes
i"leak: ${escapingRefs(tree1, localSyms).toList}%, % in $tree1")
tree1
}
}
def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context): Tree = track("typedIf") {
if (tree.isInline) checkInInlineContext("inline if", tree.posd)
val cond1 = typed(tree.cond, defn.BooleanType)
if (tree.elsep.isEmpty) {
val thenp1 = typed(tree.thenp, defn.UnitType)
val elsep1 = tpd.unitLiteral.withSpan(tree.span.endPos)
cpy.If(tree)(cond1, thenp1, elsep1).withType(defn.UnitType)
}
else {
val thenp1 :: elsep1 :: Nil = harmonic(harmonize, pt)(
(tree.thenp :: tree.elsep :: Nil).map(typed(_, pt.dropIfProto)))
assignType(cpy.If(tree)(cond1, thenp1, elsep1), thenp1, elsep1)
}
}
/** Decompose function prototype into a list of parameter prototypes and a result prototype
* tree, using WildcardTypes where a type is not known.
* For the result type 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
*/
private def decomposeProtoFunction(pt: Type, defaultArity: Int)(implicit ctx: Context): (List[Type], untpd.Tree) = {
def typeTree(tp: Type) = tp match {
case _: WildcardType => untpd.TypeTree()
case _ => untpd.TypeTree(tp)
}
pt.stripTypeVar.dealias match {
case pt1 if defn.isNonRefinedFunction(pt1) =>
// 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.
(pt1.argTypesLo.init, typeTree(pt1.argTypesHi.last))
case SAMType(sam @ MethodTpe(_, formals, restpe)) =>
(formals,
if (sam.isResultDependent)
untpd.DependentTypeTree(syms => restpe.substParams(sam, syms.map(_.termRef)))
else
typeTree(restpe))
case tp: TypeParamRef =>
decomposeProtoFunction(ctx.typerState.constraint.entry(tp).bounds.hi, defaultArity)
case _ =>
(List.tabulate(defaultArity)(alwaysWildcardType), untpd.TypeTree())
}
}
def typedFunction(tree: untpd.Function, pt: Type)(implicit ctx: Context): Tree = track("typedFunction") {
if (ctx.mode is Mode.Type) typedFunctionType(tree, pt)
else typedFunctionValue(tree, pt)
}
def typedFunctionType(tree: untpd.Function, pt: Type)(implicit ctx: Context): Tree = {
val untpd.Function(args, body) = tree
var funFlags = tree match {
case tree: untpd.FunctionWithMods => tree.mods.flags
case _ => EmptyFlags
}
if (funFlags.is(Erased) && args.isEmpty) {
ctx.error("An empty function cannot not be erased", tree.sourcePos)
funFlags = funFlags &~ Erased
}
val funCls = defn.FunctionClass(args.length,
isContextual = funFlags.is(Given), isErased = funFlags.is(Erased))
/** Typechecks dependent function type with given parameters `params` */
def typedDependent(params: List[ValDef])(implicit ctx: Context): Tree = {
completeParams(params)
val params1 = params.map(typedExpr(_).asInstanceOf[ValDef])
if (!funFlags.isEmpty)
params1.foreach(_.symbol.setFlag(funFlags))
val resultTpt = typed(body)
val companion = MethodType.companion(
isContextual = funFlags.is(Given), isErased = funFlags.is(Erased))
val mt = companion.fromSymbols(params1.map(_.symbol), resultTpt.tpe)
if (mt.isParamDependent)
ctx.error(i"$mt is an illegal function type because it has inter-parameter dependencies", tree.sourcePos)
val resTpt = TypeTree(mt.nonDependentResultApprox).withSpan(body.span)
val typeArgs = params1.map(_.tpt) :+ resTpt
val tycon = TypeTree(funCls.typeRef)
val core = assignType(cpy.AppliedTypeTree(tree)(tycon, typeArgs), tycon, typeArgs)
val appMeth = ctx.newSymbol(ctx.owner, nme.apply, Synthetic | Method | Deferred, mt, coord = body.span)
val appDef = assignType(
untpd.DefDef(appMeth.name, Nil, List(params1), resultTpt, EmptyTree),
appMeth).withSpan(body.span)
RefinedTypeTree(core, List(appDef), ctx.owner.asClass)
}
args match {
case ValDef(_, _, _) :: _ =>
typedDependent(args.asInstanceOf[List[ValDef]])(
ctx.fresh.setOwner(ctx.newRefinedClassSymbol(tree.span)).setNewScope)
case _ =>
typed(cpy.AppliedTypeTree(tree)(untpd.TypeTree(funCls.typeRef), args :+ body), pt)
}
}
def typedFunctionValue(tree: untpd.Function, pt: Type)(implicit ctx: Context): Tree = {
val untpd.Function(params: List[untpd.ValDef] @unchecked, body) = tree
val isContextual = tree match {
case tree: untpd.FunctionWithMods => tree.mods.is(Given)
case _ => false
}
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, resultTpt) = 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
/** A map from parameter names to unique positions where the parameter
* appears in the argument list of an application.
*/
var paramIndex = Map[Name, Int]()
/** If parameter `param` appears exactly once as an argument in `args`,
* the singleton list consisting of its position in `args`, otherwise `Nil`.
*/
def paramIndices(param: untpd.ValDef, args: List[untpd.Tree]): List[Int] = {
def loop(args: List[untpd.Tree], start: Int): List[Int] = args match {
case arg :: args1 =>
val others = loop(args1, start + 1)
if (refersTo(arg, param)) start :: others else others
case _ => Nil
}
val allIndices = loop(args, 0)
if (allIndices.length == 1) allIndices else Nil
}
/** If function is of the form
* (x1, ..., xN) => f(... x1, ..., XN, ...)
* where each `xi` occurs exactly once in the argument list of `f` (in
* any order), the type of `f`, otherwise NoType.
* Updates `fnBody` and `paramIndex` as a side effect.
* @post: If result exists, `paramIndex` is defined for the name of
* every parameter in `params`.
*/
def calleeType: Type = fnBody match {
case app @ Apply(expr, args) =>
paramIndex = {
for (param <- params; idx <- paramIndices(param, args))
yield param.name -> idx
}.toMap
if (paramIndex.size == params.length)
expr match {
case untpd.TypedSplice(expr1) =>
expr1.tpe
case _ =>
val protoArgs = args map (_ withType WildcardType)
val callProto = FunProto(protoArgs, WildcardType)(this, app.isGivenApply)
val expr1 = typedExpr(expr, callProto)
fnBody = cpy.Apply(fnBody)(untpd.TypedSplice(expr1), args)
expr1.tpe
}
else NoType
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, ...)
* where each `xi` occurs exactly once in the argument list of `f` (in
* any order), 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 = paramIndex(param.name)
if (pos < mtpe.paramInfos.length) {
val ptype = mtpe.paramInfos(pos)
if (isFullyDefined(ptype, ForceDegree.noBottom) && !ptype.isRepeatedParam)
return ptype
}
case _ =>
}
errorType(AnonymousFunctionMissingParamType(param, params, tree, formal), param.sourcePos)
}
def protoFormal(i: Int): Type =
if (protoFormals.length == params.length) protoFormals(i)
else errorType(WrongNumberOfParameters(protoFormals.length), tree.sourcePos)
/** Is `formal` a product type which is elementwise compatible with `params`? */
def ptIsCorrectProduct(formal: Type) = {
isFullyDefined(formal, ForceDegree.noBottom) &&
(defn.isProductSubType(formal) || formal.derivesFrom(defn.PairClass)) &&
productSelectorTypes(formal, tree.sourcePos).corresponds(params) {
(argType, param) =>
param.tpt.isEmpty || argType.widenExpr <:< typedAheadType(param.tpt).tpe
}
}
val desugared =
if (protoFormals.length == 1 && params.length != 1 && ptIsCorrectProduct(protoFormals.head)) {
val isGenericTuple = !protoFormals.head.derivesFrom(defn.ProductClass)
desugar.makeTupledFunction(params, fnBody, isGenericTuple)
}
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)))
desugar.makeClosure(inferredParams, fnBody, resultTpt, isContextual)
}
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(sam)
if !defn.isFunctionType(pt) && mt <:< sam =>
if (!isFullyDefined(pt, ForceDegree.all))