Skip to content

Commit c5921de

Browse files
committed
Make Typer.BindingPrec an enum
1 parent 9a26f44 commit c5921de

File tree

3 files changed

+32
-34
lines changed

3 files changed

+32
-34
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,23 +1317,24 @@ object messages {
13171317
|"""
13181318
}
13191319

1320-
case class AmbiguousImport(name: Name, newPrec: Int, prevPrec: Int, prevCtx: Context)(implicit ctx: Context)
1321-
extends Message(AmbiguousImportID) {
1320+
import typer.Typer.BindingPrec
13221321

1323-
import typer.Typer.BindingPrec
1322+
case class AmbiguousImport(name: Name, newPrec: BindingPrec, prevPrec: BindingPrec, prevCtx: Context)(implicit ctx: Context)
1323+
extends Message(AmbiguousImportID) {
13241324

13251325
/** A string which explains how something was bound; Depending on `prec` this is either
13261326
* imported by <tree>
13271327
* or defined in <symbol>
13281328
*/
1329-
private def bindingString(prec: Int, whereFound: Context, qualifier: String = "") = {
1329+
private def bindingString(prec: BindingPrec, whereFound: Context, qualifier: String = "") = {
13301330
val howVisible = prec match {
1331-
case BindingPrec.definition => "defined"
1332-
case BindingPrec.namedImport => "imported by name"
1333-
case BindingPrec.wildImport => "imported"
1334-
case BindingPrec.packageClause => "found"
1331+
case BindingPrec.Definition => "defined"
1332+
case BindingPrec.NamedImport => "imported by name"
1333+
case BindingPrec.WildImport => "imported"
1334+
case BindingPrec.PackageClause => "found"
1335+
case BindingPrec.NothingBound => assert(false)
13351336
}
1336-
if (BindingPrec.isImportPrec(prec)) {
1337+
if (prec.isImportPrec) {
13371338
ex"""$howVisible$qualifier by ${em"${whereFound.importInfo}"}"""
13381339
} else
13391340
ex"""$howVisible$qualifier in ${em"${whereFound.owner}"}"""

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,10 @@ object Typer {
4747
/** The precedence of bindings which determines which of several bindings will be
4848
* accessed by an Ident.
4949
*/
50-
object BindingPrec {
51-
val definition: Int = 4
52-
val namedImport: Int = 3
53-
val wildImport: Int = 2
54-
val packageClause: Int = 1
55-
val nothingBound: Int = 0
56-
def isImportPrec(prec: Int): Boolean = prec == namedImport || prec == wildImport
50+
enum BindingPrec {
51+
case NothingBound, PackageClause, WildImport, NamedImport, Definition
52+
53+
def isImportPrec = this == NamedImport || this == WildImport
5754
}
5855

5956
/** Assert tree has a position, unless it is empty or a typed splice */
@@ -149,7 +146,7 @@ class Typer extends Namer
149146
* @param prevCtx The context of the previous denotation,
150147
* or else `NoContext` if nothing was found yet.
151148
*/
152-
def findRefRecur(previous: Type, prevPrec: Int, prevCtx: Context)(implicit ctx: Context): Type = {
149+
def findRefRecur(previous: Type, prevPrec: BindingPrec, prevCtx: Context)(implicit ctx: Context): Type = {
153150
import BindingPrec._
154151

155152
/** Check that any previously found result from an inner context
@@ -161,11 +158,11 @@ class Typer extends Namer
161158
* previous and new contexts do not have the same scope, we select
162159
* the previous (inner) definition. This models what scalac does.
163160
*/
164-
def checkNewOrShadowed(found: Type, newPrec: Int, scala2pkg: Boolean = false)(implicit ctx: Context): Type =
161+
def checkNewOrShadowed(found: Type, newPrec: BindingPrec, scala2pkg: Boolean = false)(implicit ctx: Context): Type =
165162
if (!previous.exists || ctx.typeComparer.isSameRef(previous, found)) found
166163
else if ((prevCtx.scope eq ctx.scope) &&
167-
(newPrec == definition ||
168-
newPrec == namedImport && prevPrec == wildImport)) {
164+
(newPrec == Definition ||
165+
newPrec == NamedImport && prevPrec == WildImport)) {
169166
// special cases: definitions beat imports, and named imports beat
170167
// wildcard imports, provided both are in contexts with same scope
171168
found
@@ -252,9 +249,9 @@ class Typer extends Namer
252249
}
253250

254251
/** Would import of kind `prec` be not shadowed by a nested higher-precedence definition? */
255-
def isPossibleImport(prec: Int)(implicit ctx: Context) =
252+
def isPossibleImport(prec: BindingPrec)(implicit ctx: Context) =
256253
!noImports &&
257-
(prevPrec < prec || prevPrec == prec && (prevCtx.scope eq ctx.scope))
254+
(prevPrec.ordinal < prec.ordinal || prevPrec == prec && (prevCtx.scope eq ctx.scope))
258255

259256
@tailrec def loop(lastCtx: Context)(implicit ctx: Context): Type = {
260257
if (ctx.scope == null) previous
@@ -309,14 +306,14 @@ class Typer extends Namer
309306
effectiveOwner.thisType.select(name, defDenot)
310307
}
311308
if (!curOwner.is(Package) || isDefinedInCurrentUnit(defDenot))
312-
result = checkNewOrShadowed(found, definition) // no need to go further out, we found highest prec entry
309+
result = checkNewOrShadowed(found, Definition) // no need to go further out, we found highest prec entry
313310
else {
314311
if (ctx.scala2Mode && !foundUnderScala2.exists)
315-
foundUnderScala2 = checkNewOrShadowed(found, definition, scala2pkg = true)
312+
foundUnderScala2 = checkNewOrShadowed(found, Definition, scala2pkg = true)
316313
if (defDenot.symbol.is(Package))
317-
result = checkNewOrShadowed(previous orElse found, packageClause)
318-
else if (prevPrec < packageClause)
319-
result = findRefRecur(found, packageClause, ctx)(ctx.outer)
314+
result = checkNewOrShadowed(previous orElse found, PackageClause)
315+
else if (prevPrec.ordinal < PackageClause.ordinal)
316+
result = findRefRecur(found, PackageClause, ctx)(ctx.outer)
320317
}
321318
}
322319
}
@@ -329,14 +326,14 @@ class Typer extends Namer
329326
if (curImport.unimported.exists) unimported += curImport.unimported
330327
if (ctx.owner.is(Package) && curImport != null && curImport.isRootImport && previous.exists)
331328
previous // no more conflicts possible in this case
332-
else if (isPossibleImport(namedImport) && (curImport ne outer.importInfo)) {
329+
else if (isPossibleImport(NamedImport) && (curImport ne outer.importInfo)) {
333330
val namedImp = namedImportRef(curImport)
334331
if (namedImp.exists)
335-
findRefRecur(checkNewOrShadowed(namedImp, namedImport), namedImport, ctx)(outer)
336-
else if (isPossibleImport(wildImport) && !curImport.sym.isCompleting) {
332+
findRefRecur(checkNewOrShadowed(namedImp, NamedImport), NamedImport, ctx)(outer)
333+
else if (isPossibleImport(WildImport) && !curImport.sym.isCompleting) {
337334
val wildImp = wildImportRef(curImport)
338335
if (wildImp.exists)
339-
findRefRecur(checkNewOrShadowed(wildImp, wildImport), wildImport, ctx)(outer)
336+
findRefRecur(checkNewOrShadowed(wildImp, WildImport), WildImport, ctx)(outer)
340337
else {
341338
updateUnimported()
342339
loop(ctx)(outer)
@@ -356,7 +353,7 @@ class Typer extends Namer
356353
loop(NoContext)(ctx)
357354
}
358355

359-
findRefRecur(NoType, BindingPrec.nothingBound, NoContext)
356+
findRefRecur(NoType, BindingPrec.NothingBound, NoContext)
360357
}
361358

362359
/** Attribute an identifier consisting of a simple name or wildcard

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,8 @@ class ErrorMessagesTests extends ErrorMessagesTest {
500500
assertMessageCount(1, messages)
501501
val AmbiguousImport(name, newPrec, prevPrec, prevCtx) :: Nil = messages
502502
assertEquals("ToBeImported", name.show)
503-
assertEquals(namedImport, newPrec)
504-
assertEquals(namedImport, prevPrec)
503+
assertEquals(NamedImport, newPrec)
504+
assertEquals(NamedImport, prevPrec)
505505
}
506506

507507
@Test def methodDoesNotTakeParameters =

0 commit comments

Comments
 (0)