Skip to content

Commit 4f32c24

Browse files
committed
Convert some checking methods to take Positioned arguments
instead of SourcePos. It's more efficient since it does not need allocations.
1 parent 107fdb7 commit 4f32c24

File tree

6 files changed

+46
-46
lines changed

6 files changed

+46
-46
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
199199
case tree @ Select(qual, name) =>
200200
handleMeta(tree.symbol)
201201
if (name.isTypeName) {
202-
Checking.checkRealizable(qual.tpe, qual.sourcePos.focus)
202+
Checking.checkRealizable(qual.tpe, qual.posd)
203203
super.transform(tree)(ctx.addMode(Mode.Type))
204204
}
205205
else
@@ -219,7 +219,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
219219
case Select(nu: New, nme.CONSTRUCTOR) if isCheckable(nu) =>
220220
// need to check instantiability here, because the type of the New itself
221221
// might be a type constructor.
222-
Checking.checkInstantiable(tree.tpe, nu.sourcePos)
222+
Checking.checkInstantiable(tree.tpe, nu.posd)
223223
withNoCheckNews(nu :: Nil)(super.transform(app))
224224
case _ =>
225225
super.transform(app)
@@ -266,18 +266,18 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
266266
}
267267
processMemberDef(super.transform(tree))
268268
case tree: New if isCheckable(tree) =>
269-
Checking.checkInstantiable(tree.tpe, tree.sourcePos)
269+
Checking.checkInstantiable(tree.tpe, tree.posd)
270270
super.transform(tree)
271271
case tree: Closure if !tree.tpt.isEmpty =>
272-
Checking.checkRealizable(tree.tpt.tpe, tree.sourcePos, "SAM type")
272+
Checking.checkRealizable(tree.tpt.tpe, tree.posd, "SAM type")
273273
super.transform(tree)
274274
case tree @ Annotated(annotated, annot) =>
275275
cpy.Annotated(tree)(transform(annotated), transformAnnot(annot))
276276
case tree: AppliedTypeTree =>
277277
Checking.checkAppliedType(tree, boundsCheck = !ctx.mode.is(Mode.Pattern))
278278
super.transform(tree)
279279
case SingletonTypeTree(ref) =>
280-
Checking.checkRealizable(ref.tpe, ref.sourcePos.focus)
280+
Checking.checkRealizable(ref.tpe, ref.posd)
281281
super.transform(tree)
282282
case tree: TypeTree =>
283283
tree.withType(
@@ -289,7 +289,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
289289
case tree: AndTypeTree =>
290290
// Ideally, this should be done by Typer, but we run into cyclic references
291291
// when trying to typecheck self types which are intersections.
292-
Checking.checkNonCyclicInherited(tree.tpe, tree.left.tpe :: tree.right.tpe :: Nil, EmptyScope, tree.sourcePos)
292+
Checking.checkNonCyclicInherited(tree.tpe, tree.left.tpe :: tree.right.tpe :: Nil, EmptyScope, tree.posd)
293293
super.transform(tree)
294294
case tree: LambdaTypeTree =>
295295
VarianceChecker.checkLambda(tree)

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
890890
if (typedArgs.length <= pt.paramInfos.length && !isNamed)
891891
if (typedFn.symbol == defn.Predef_classOf && typedArgs.nonEmpty) {
892892
val arg = typedArgs.head
893-
checkClassType(arg.tpe, arg.sourcePos, traitReq = false, stablePrefixReq = false)
893+
checkClassType(arg.tpe, arg.posd, traitReq = false, stablePrefixReq = false)
894894
}
895895
case _ =>
896896
}

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,16 @@ object Checking {
7979
HKTypeLambda.fromParams(tparams, bound).appliedTo(args)
8080
if (boundsCheck) checkBounds(orderedArgs, bounds, instantiate)
8181

82-
def checkWildcardApply(tp: Type, pos: SourcePosition): Unit = tp match {
82+
def checkWildcardApply(tp: Type): Unit = tp match {
8383
case tp @ AppliedType(tycon, args) =>
8484
if (tycon.isLambdaSub && args.exists(_.isInstanceOf[TypeBounds]))
8585
ctx.errorOrMigrationWarning(
8686
ex"unreducible application of higher-kinded type $tycon to wildcard arguments",
87-
pos)
87+
tree.sourcePos)
8888
case _ =>
8989
}
9090
def checkValidIfApply(implicit ctx: Context): Unit =
91-
checkWildcardApply(tycon.tpe.appliedTo(args.map(_.tpe)), tree.sourcePos)
91+
checkWildcardApply(tycon.tpe.appliedTo(args.map(_.tpe)))
9292
checkValidIfApply(ctx.addMode(Mode.AllowLambdaWildcardApply))
9393
}
9494

@@ -118,28 +118,28 @@ object Checking {
118118
/** Check that `tp` refers to a nonAbstract class
119119
* and that the instance conforms to the self type of the created class.
120120
*/
121-
def checkInstantiable(tp: Type, pos: SourcePosition)(implicit ctx: Context): Unit =
121+
def checkInstantiable(tp: Type, posd: Positioned)(implicit ctx: Context): Unit =
122122
tp.underlyingClassRef(refinementOK = false) match {
123123
case tref: TypeRef =>
124124
val cls = tref.symbol
125125
if (cls.is(AbstractOrTrait))
126-
ctx.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), pos)
126+
ctx.error(CantInstantiateAbstractClassOrTrait(cls, isTrait = cls.is(Trait)), posd.sourcePos)
127127
if (!cls.is(Module)) {
128128
// Create a synthetic singleton type instance, and check whether
129129
// it conforms to the self type of the class as seen from that instance.
130130
val stp = SkolemType(tp)
131131
val selfType = cls.asClass.givenSelfType.asSeenFrom(stp, cls)
132132
if (selfType.exists && !(stp <:< selfType))
133-
ctx.error(DoesNotConformToSelfTypeCantBeInstantiated(tp, selfType), pos)
133+
ctx.error(DoesNotConformToSelfTypeCantBeInstantiated(tp, selfType), posd.sourcePos)
134134
}
135135
case _ =>
136136
}
137137

138138
/** Check that type `tp` is realizable. */
139-
def checkRealizable(tp: Type, pos: SourcePosition, what: String = "path")(implicit ctx: Context): Unit = {
139+
def checkRealizable(tp: Type, posd: Positioned, what: String = "path")(implicit ctx: Context): Unit = {
140140
val rstatus = realizability(tp)
141141
if (rstatus ne Realizable)
142-
ctx.errorOrMigrationWarning(em"$tp is not a legal $what\nsince it${rstatus.msg}", pos)
142+
ctx.errorOrMigrationWarning(em"$tp is not a legal $what\nsince it${rstatus.msg}", posd.sourcePos)
143143
}
144144

145145
/** A type map which checks that the only cycles in a type are F-bounds
@@ -332,7 +332,7 @@ object Checking {
332332
* unless a type with the same name aleadry appears in `decls`.
333333
* @return true iff no cycles were detected
334334
*/
335-
def checkNonCyclicInherited(joint: Type, parents: List[Type], decls: Scope, pos: SourcePosition)(implicit ctx: Context): Unit = {
335+
def checkNonCyclicInherited(joint: Type, parents: List[Type], decls: Scope, posd: Positioned)(implicit ctx: Context): Unit = {
336336
def qualifies(sym: Symbol) = sym.name.isTypeName && !sym.is(Private)
337337
val abstractTypeNames =
338338
for (parent <- parents; mbr <- parent.abstractTypeMembers if qualifies(mbr.symbol))
@@ -350,7 +350,7 @@ object Checking {
350350
}
351351
catch {
352352
case ex: RecursionOverflow =>
353-
ctx.error(em"cyclic reference involving type $name", pos)
353+
ctx.error(em"cyclic reference involving type $name", posd.sourcePos)
354354
false
355355
}
356356
}
@@ -562,8 +562,8 @@ trait Checking {
562562
def checkNonCyclic(sym: Symbol, info: TypeBounds, reportErrors: Boolean)(implicit ctx: Context): Type =
563563
Checking.checkNonCyclic(sym, info, reportErrors)
564564

565-
def checkNonCyclicInherited(joint: Type, parents: List[Type], decls: Scope, pos: SourcePosition)(implicit ctx: Context): Unit =
566-
Checking.checkNonCyclicInherited(joint, parents, decls, pos)
565+
def checkNonCyclicInherited(joint: Type, parents: List[Type], decls: Scope, posd: Positioned)(implicit ctx: Context): Unit =
566+
Checking.checkNonCyclicInherited(joint, parents, decls, posd)
567567

568568
/** Check that Java statics and packages can only be used in selections.
569569
*/
@@ -578,8 +578,8 @@ trait Checking {
578578
}
579579

580580
/** Check that type `tp` is stable. */
581-
def checkStable(tp: Type, pos: SourcePosition)(implicit ctx: Context): Unit =
582-
if (!tp.isStable) ctx.error(ex"$tp is not stable", pos)
581+
def checkStable(tp: Type, posd: Positioned)(implicit ctx: Context): Unit =
582+
if (!tp.isStable) ctx.error(ex"$tp is not stable", posd.sourcePos)
583583

584584
/** Check that all type members of `tp` have realizable bounds */
585585
def checkRealizableBounds(cls: Symbol, pos: SourcePosition)(implicit ctx: Context): Unit = {
@@ -594,14 +594,14 @@ trait Checking {
594594
* check that class prefix is stable.
595595
* @return `tp` itself if it is a class or trait ref, ObjectType if not.
596596
*/
597-
def checkClassType(tp: Type, pos: SourcePosition, traitReq: Boolean, stablePrefixReq: Boolean)(implicit ctx: Context): Type =
597+
def checkClassType(tp: Type, posd: Positioned, traitReq: Boolean, stablePrefixReq: Boolean)(implicit ctx: Context): Type =
598598
tp.underlyingClassRef(refinementOK = false) match {
599599
case tref: TypeRef =>
600-
if (traitReq && !(tref.symbol is Trait)) ctx.error(TraitIsExpected(tref.symbol), pos)
601-
if (stablePrefixReq && ctx.phase <= ctx.refchecksPhase) checkStable(tref.prefix, pos)
600+
if (traitReq && !(tref.symbol is Trait)) ctx.error(TraitIsExpected(tref.symbol), posd.sourcePos)
601+
if (stablePrefixReq && ctx.phase <= ctx.refchecksPhase) checkStable(tref.prefix, posd)
602602
tp
603603
case _ =>
604-
ctx.error(ex"$tp is not a class type", pos)
604+
ctx.error(ex"$tp is not a class type", posd.sourcePos)
605605
defn.ObjectType
606606
}
607607

@@ -634,7 +634,7 @@ trait Checking {
634634
* - it is defined in Predef
635635
* - it is the scala.reflect.Selectable.reflectiveSelectable conversion
636636
*/
637-
def checkImplicitConversionUseOK(sym: Symbol, pos: SourcePosition)(implicit ctx: Context): Unit = {
637+
def checkImplicitConversionUseOK(sym: Symbol, posd: Positioned)(implicit ctx: Context): Unit = {
638638
val conversionOK =
639639
!sym.exists ||
640640
sym.is(Synthetic) ||
@@ -643,7 +643,7 @@ trait Checking {
643643
sym.name == nme.reflectiveSelectable && sym.maybeOwner.maybeOwner.maybeOwner == defn.ScalaPackageClass
644644
if (!conversionOK)
645645
checkFeature(defn.LanguageModuleClass, nme.implicitConversions,
646-
i"Use of implicit conversion ${sym.showLocated}", NoSymbol, pos)
646+
i"Use of implicit conversion ${sym.showLocated}", NoSymbol, posd.sourcePos)
647647
}
648648

649649
/** Issue a feature warning if feature is not enabled */
@@ -890,14 +890,14 @@ trait Checking {
890890
}
891891

892892
/** Check that we are in an inline context (inside an inline method or in inline code) */
893-
def checkInInlineContext(what: String, pos: SourcePosition)(implicit ctx: Context): Unit =
893+
def checkInInlineContext(what: String, posd: Positioned)(implicit ctx: Context): Unit =
894894
if (!ctx.inInlineMethod && !ctx.isInlineContext) {
895895
val inInlineUnapply = ctx.owner.ownersIterator.exists(owner =>
896896
owner.name == nme.unapply && owner.is(Inline) && owner.is(Method))
897897
val msg =
898898
if (inInlineUnapply) "cannot be used in an inline unapply"
899899
else "can only be used in an inline method"
900-
ctx.error(em"$what $msg", pos)
900+
ctx.error(em"$what $msg", posd.sourcePos)
901901
}
902902

903903
/** Check that all case classes that extend `scala.Enum` are `enum` cases */
@@ -994,12 +994,12 @@ trait ReChecking extends Checking {
994994
trait NoChecking extends ReChecking {
995995
import tpd._
996996
override def checkNonCyclic(sym: Symbol, info: TypeBounds, reportErrors: Boolean)(implicit ctx: Context): Type = info
997-
override def checkNonCyclicInherited(joint: Type, parents: List[Type], decls: Scope, pos: SourcePosition)(implicit ctx: Context): Unit = ()
997+
override def checkNonCyclicInherited(joint: Type, parents: List[Type], decls: Scope, posd: Positioned)(implicit ctx: Context): Unit = ()
998998
override def checkValue(tree: Tree, proto: Type)(implicit ctx: Context): tree.type = tree
999-
override def checkStable(tp: Type, pos: SourcePosition)(implicit ctx: Context): Unit = ()
1000-
override def checkClassType(tp: Type, pos: SourcePosition, traitReq: Boolean, stablePrefixReq: Boolean)(implicit ctx: Context): Type = tp
999+
override def checkStable(tp: Type, posd: Positioned)(implicit ctx: Context): Unit = ()
1000+
override def checkClassType(tp: Type, posd: Positioned, traitReq: Boolean, stablePrefixReq: Boolean)(implicit ctx: Context): Type = tp
10011001
override def checkImplicitConversionDefOK(sym: Symbol)(implicit ctx: Context): Unit = ()
1002-
override def checkImplicitConversionUseOK(sym: Symbol, pos: SourcePosition)(implicit ctx: Context): Unit = ()
1002+
override def checkImplicitConversionUseOK(sym: Symbol, posd: Positioned)(implicit ctx: Context): Unit = ()
10031003
override def checkFeasibleParent(tp: Type, pos: SourcePosition, where: => String = "")(implicit ctx: Context): Type = tp
10041004
override def checkInlineConformant(tree: Tree, isFinal: Boolean, what: => String)(implicit ctx: Context): Unit = ()
10051005
override def checkNoDoubleDeclaration(cls: Symbol)(implicit ctx: Context): Unit = ()
@@ -1011,6 +1011,6 @@ trait NoChecking extends ReChecking {
10111011
override def checkCaseInheritance(parentSym: Symbol, caseCls: ClassSymbol, pos: SourcePosition)(implicit ctx: Context): Unit = ()
10121012
override def checkNoForwardDependencies(vparams: List[ValDef])(implicit ctx: Context): Unit = ()
10131013
override def checkMembersOK(tp: Type, pos: SourcePosition)(implicit ctx: Context): Type = tp
1014-
override def checkInInlineContext(what: String, pos: SourcePosition)(implicit ctx: Context): Unit = ()
1014+
override def checkInInlineContext(what: String, posd: Positioned)(implicit ctx: Context): Unit = ()
10151015
override def checkFeature(base: ClassSymbol, name: TermName, description: => String, featureUseSite: Symbol, pos: SourcePosition)(implicit ctx: Context): Unit = ()
10161016
}

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ class Namer { typer: Typer =>
887887
val ptype = parentType(parent)(ctx.superCallContext).dealiasKeepAnnots
888888
if (cls.isRefinementClass) ptype
889889
else {
890-
val pt = checkClassType(ptype, parent.sourcePos,
890+
val pt = checkClassType(ptype, parent.posd,
891891
traitReq = parent ne parents.head, stablePrefixReq = true)
892892
if (pt.derivesFrom(cls)) {
893893
val addendum = parent match {

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ class Typer extends Namer
432432
case _ => app
433433
}
434434
case qual1 =>
435-
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.sourcePos)
435+
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.posd)
436436
val select = typedSelect(tree, pt, qual1)
437437
if (select.tpe ne TryDynamicCallType) ConstFold(checkStableIdentPattern(select, pt))
438438
else if (pt.isInstanceOf[FunOrPolyProto] || pt == AssignProto) select
@@ -515,7 +515,7 @@ class Typer extends Namer
515515
case TypeApplications.EtaExpansion(tycon) => tpt1 = tpt1.withType(tycon)
516516
case _ =>
517517
}
518-
if (checkClassType(tpt1.tpe, tpt1.sourcePos, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)
518+
if (checkClassType(tpt1.tpe, tpt1.posd, traitReq = false, stablePrefixReq = true) eq defn.ObjectType)
519519
tpt1 = TypeTree(defn.ObjectType).withPosOf(tpt1)
520520

521521
tpt1 match {
@@ -724,7 +724,7 @@ class Typer extends Namer
724724
}
725725

726726
def typedIf(tree: untpd.If, pt: Type)(implicit ctx: Context): Tree = track("typedIf") {
727-
if (tree.isInline) checkInInlineContext("inline if", tree.sourcePos)
727+
if (tree.isInline) checkInInlineContext("inline if", tree.posd)
728728
val cond1 = typed(tree.cond, defn.BooleanType)
729729
val thenp2 :: elsep2 :: Nil = harmonic(harmonize, pt) {
730730
val thenp1 = typed(tree.thenp, pt.notApplied)
@@ -987,7 +987,7 @@ class Typer extends Namer
987987
tree.selector match {
988988
case EmptyTree =>
989989
if (tree.isInline) {
990-
checkInInlineContext("implicit match", tree.sourcePos)
990+
checkInInlineContext("implicit match", tree.posd)
991991
val cases1 = tree.cases.mapconserve {
992992
case cdef @ CaseDef(pat @ Typed(Ident(nme.WILDCARD), _), _, _) =>
993993
// case _ : T --> case evidence$n : T
@@ -1002,7 +1002,7 @@ class Typer extends Namer
10021002
typed(desugar.makeCaseLambda(tree.cases, protoFormals.length, unchecked).withPosOf(tree), pt)
10031003
}
10041004
case _ =>
1005-
if (tree.isInline) checkInInlineContext("inline match", tree.sourcePos)
1005+
if (tree.isInline) checkInInlineContext("inline match", tree.posd)
10061006
val sel1 = typedExpr(tree.selector)
10071007
val selType = fullyDefinedType(sel1.tpe, "pattern selector", tree.span).widen
10081008
typedMatchFinish(tree, sel1, selType, tree.cases, pt)
@@ -1240,7 +1240,7 @@ class Typer extends Namer
12401240

12411241
def typedSingletonTypeTree(tree: untpd.SingletonTypeTree)(implicit ctx: Context): SingletonTypeTree = track("typedSingletonTypeTree") {
12421242
val ref1 = typedExpr(tree.ref)
1243-
checkStable(ref1.tpe, tree.sourcePos)
1243+
checkStable(ref1.tpe, tree.posd)
12441244
assignType(cpy.SingletonTypeTree(tree)(ref1), ref1)
12451245
}
12461246

@@ -1659,7 +1659,7 @@ class Typer extends Namer
16591659
cls, isRequired, cdef.sourcePos)
16601660
}
16611661

1662-
checkNonCyclicInherited(cls.thisType, cls.classParents, cls.info.decls, cdef.sourcePos)
1662+
checkNonCyclicInherited(cls.thisType, cls.classParents, cls.info.decls, cdef.posd)
16631663

16641664
// check value class constraints
16651665
checkDerivedValueClass(cls, body1)
@@ -1740,8 +1740,8 @@ class Typer extends Namer
17401740

17411741
def typedImport(imp: untpd.Import, sym: Symbol)(implicit ctx: Context): Import = track("typedImport") {
17421742
val expr1 = typedExpr(imp.expr, AnySelectionProto)
1743-
checkStable(expr1.tpe, imp.expr.sourcePos)
1744-
if (!ctx.isAfterTyper) checkRealizable(expr1.tpe, imp.expr.sourcePos)
1743+
checkStable(expr1.tpe, imp.expr.posd)
1744+
if (!ctx.isAfterTyper) checkRealizable(expr1.tpe, imp.expr.posd)
17451745
assignType(cpy.Import(imp)(expr1, imp.selectors), sym)
17461746
}
17471747

@@ -2659,7 +2659,7 @@ class Typer extends Namer
26592659
case SearchSuccess(inferred: Applications.ExtMethodApply, _, _) =>
26602660
inferred // nothing to check or adapt for extension method applications
26612661
case SearchSuccess(inferred, _, _) =>
2662-
checkImplicitConversionUseOK(inferred.symbol, tree.sourcePos)
2662+
checkImplicitConversionUseOK(inferred.symbol, tree.posd)
26632663
readapt(inferred)(ctx.retractMode(Mode.ImplicitsEnabled))
26642664
case failure: SearchFailure =>
26652665
if (pt.isInstanceOf[ProtoType] && !failure.isAmbiguous)

tests/neg/ski.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ trait S2[x <: Term, y <: Term] extends Term {
1818
}
1919
trait S3[x <: Term, y <: Term, z <: Term] extends Term {
2020
type ap[v <: Term] = eval#ap[v] // error: not a legal path
21-
type eval = x#ap[z]#ap[y#ap[z]]#eval // error: not a legal path // error: not a legal path
21+
type eval = x#ap[z]#ap[y#ap[z]]#eval // error: not a legal path
2222
}
2323

2424
// The K combinator

0 commit comments

Comments
 (0)