Skip to content

Commit

Permalink
Back to non-synthetic, handle only exported types
Browse files Browse the repository at this point in the history
  • Loading branch information
dwijnand committed Jan 19, 2024
1 parent 6aadd94 commit 03c9b3e
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 57 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ object Flags {
val (Enum @ _, EnumVal @ _, _) = newFlags(40, "enum")

/** An export forwarder */
val (Exported @ _, _, _) = newFlags(41, "exported")
val (Exported @ _, ExportedTerm @ _, ExportedType @ _) = newFlags(41, "exported")

/** Labeled with `erased` modifier (erased value or class) */
val (Erased @ _, _, _) = newFlags(42, "erased")
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Symbols.scala
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ object Symbols extends SymUtils {
this
else if (denot.is(ModuleVal))
this.moduleClass.sourceSymbol // The module val always has a zero-extent position
else if denot.is(ExportedType) then
denot.info.dropAlias.asInstanceOf[NamedType].symbol.sourceSymbol
else if (denot.is(Synthetic)) {
val linked = denot.linkedClass
if (linked.exists && !linked.is(Synthetic))
Expand Down
7 changes: 6 additions & 1 deletion compiler/src/dotty/tools/dotc/interactive/SourceTree.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ case class SourceTree(tree: tpd.Import | tpd.NameTree, source: SourceFile) {
(treeSpan.end - nameLength, treeSpan.end)
Span(start, end, start)
}
source.atSpan(position)
// Don't widen the span, only narrow.
// E.g. The star in a wildcard export is 1 character,
// and that is the span of the type alias that results from it
// but the name may very well be larger, which we don't want.
val span1 = if treeSpan.contains(position) then position else treeSpan
source.atSpan(span1)
}
case _ =>
NoSourcePosition
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1247,7 +1247,7 @@ class Namer { typer: Typer =>
(EmptyFlags, mbrInfo)
var flagMask = RetainedExportFlags
if sym.isTerm then flagMask |= HasDefaultParams | NoDefaultParams
var mbrFlags = Exported | Synthetic | Method | Final | maybeStable | sym.flags & flagMask
var mbrFlags = Exported | Method | Final | maybeStable | sym.flags & flagMask
if sym.is(ExtensionMethod) || pathMethod.exists then
mbrFlags |= ExtensionMethod
val forwarderName = checkNoConflict(alias, isPrivate = false, span)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import dotty.tools.dotc.core.Flags.*
import dotty.tools.dotc.core.Names.Name
import dotty.tools.dotc.core.StdNames
import dotty.tools.dotc.core.Symbols.*
import dotty.tools.dotc.core.Types.*
import dotty.tools.dotc.core.Types.Type
import dotty.tools.dotc.interactive.SourceTree
import dotty.tools.dotc.util.SourceFile
import dotty.tools.dotc.util.SourcePosition
Expand Down Expand Up @@ -205,17 +205,9 @@ object MetalsInteractive:
Nil

case path @ head :: tail =>
if head.symbol.is(Exported) then
head.symbol.info match
case TypeAlias(target: NamedType) =>
val ss = target.symbol.sourceSymbol // exported type
List((ss, ss.info))
case info => info.finalResultType match
case target: NamedType =>
val ss = target.symbol.sourceSymbol // exported term
List((ss, ss.info))
case _ =>
enclosingSymbolsWithExpressionType(tail, pos, indexed, skipCheckOnName)
if head.symbol.is(ExportedType) then
val sym = head.symbol.sourceSymbol
List((sym, sym.info))
else if head.symbol.is(Synthetic) then
enclosingSymbolsWithExpressionType(
tail,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
|""".stripMargin
)

@Test def `export` =
@Test def `exportType1` =
check(
"""object enumerations:
| trait <<SymbolKind>>
Expand All @@ -217,6 +217,74 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
|""".stripMargin
)

@Test def `exportType1Wild` =
check(
"""object enumerations:
| trait <<SymbolKind>>
| trait CymbalKind
|
|object all:
| export enumerations.SymbolKind
|
|@main def hello =
| import all.SymbolKind
| import enumerations.CymbalKind
|
| val x = new Symbo@@lKind {}
| val y = new CymbalKind {}
|""".stripMargin
)

@Test def `exportTerm1` =
check(
"""class BitMap
|class Scanner:
| def scan(): BitMap = ???
|class Copier:
| private val scanUnit = new Scanner
| export scanUnit.<<scan>>
| def t1 = sc@@an()
|""".stripMargin
)

@Test def `exportTerm2` =
check(
"""class BitMap
|class Scanner:
| def scan(): BitMap = ???
|class Copier:
| private val scanUnit = new Scanner
| export scanUnit.<<scan>>
|class Test:
| def t2(cpy: Copier) = cpy.sc@@an()
|""".stripMargin
)

@Test def `exportTerm1Wild` =
check(
"""class BitMap
|class Scanner:
| def scan(): BitMap = ???
|class Copier:
| private val scanUnit = new Scanner
| export scanUnit.<<*>>
| def t1 = sc@@an()
|""".stripMargin
)

@Test def `exportTerm2Wild` =
check(
"""class BitMap
|class Scanner:
| def scan(): BitMap = ???
|class Copier:
| private val scanUnit = new Scanner
| export scanUnit.<<*>>
|class Test:
| def t2(cpy: Copier) = cpy.sc@@an()
|""".stripMargin
)

@Test def `named-arg-local` =
check(
"""|
Expand Down
12 changes: 0 additions & 12 deletions tests/neg/export-leaks.check

This file was deleted.

16 changes: 3 additions & 13 deletions tests/neg/export-leaks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@ object Signature {
object O1 {
private[Signature] def bar: T = ???
}
export O1._
export O1._ // error: non-private method bar refers to private type T

object O2 {
private[Signature] val foo: T = ???
}
export O2._

object PosTest:
def main(args: Array[String]): Unit =
val t1 = bar // ok
val t2 = foo // ok
}

object Test:
def main(args: Array[String]): Unit =
val t1 = bar // error: Not found: bar
val t2 = foo // error: Not found: foo
export O2._ // error: non-private method foo refers to private type T
}
35 changes: 23 additions & 12 deletions tests/neg/exports.check
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,16 @@
| ^^^^^^^^^^^^^^
| no eligible member scanAll at this.scanUnit
| this.scanUnit.scanAll cannot be exported because it is not accessible
-- [E120] Naming Error: tests/neg/exports.scala:28:8 -------------------------------------------------------------------
28 | def status: List[String] = printUnit.status ++ scanUnit.status // error: double definition w/ printUnit.status
| ^
| Double definition:
| final def status: List[String] in class Copier at line 23 and
| def status: List[String] in class Copier at line 28
| have the same type after erasure.
|
| Consider adding a @targetName annotation to one of the conflicting definitions
| for disambiguation.
-- Error: tests/neg/exports.scala:25:21 --------------------------------------------------------------------------------
25 | export printUnit.bitmap // error: no eligible member
| ^
| non-private given instance bitmap in class Copier refers to private value printUnit
| in its type signature => object Copier.this.printUnit.bitmap
-- [E120] Naming Error: tests/neg/exports.scala:23:33 ------------------------------------------------------------------
23 | export printUnit.{stat => _, _} // error: double definition
| ^
| Double definition:
| final def status: List[String] in class Copier at line 24 and
| def status: List[String] in class Copier at line 28 and
| final def status: List[String] in class Copier at line 23
| have the same type after erasure.
|
Expand All @@ -31,12 +26,22 @@
24 | export scanUnit._ // error: double definition
| ^
| Double definition:
| final def status: List[String] in class Copier at line 26 and
| final def status: List[String] in class Copier at line 23 and
| final def status: List[String] in class Copier at line 24
| have the same type after erasure.
|
| Consider adding a @targetName annotation to one of the conflicting definitions
| for disambiguation.
-- [E120] Naming Error: tests/neg/exports.scala:26:21 ------------------------------------------------------------------
26 | export printUnit.status // error: double definition
| ^
| Double definition:
| final def status: List[String] in class Copier at line 24 and
| final def status: List[String] in class Copier at line 26
| have the same type after erasure.
|
| Consider adding a @targetName annotation to one of the conflicting definitions
| for disambiguation.
-- Error: tests/neg/exports.scala:35:24 --------------------------------------------------------------------------------
35 | export this.{concat => ++} // error: no eligible member
| ^^^^^^^^^^^^
Expand All @@ -47,6 +52,12 @@
| ^^^
| no eligible member foo at this.foo
| this.foo.foo cannot be exported because it is already a member of class Foo
-- [E120] Naming Error: tests/neg/exports.scala:46:15 ------------------------------------------------------------------
46 | export bar._ // error: double definition
| ^
| Double definition:
| val bar: Bar in class Baz at line 45 and
| final def bar: (Baz.this.bar.bar : => (Baz.this.bar.baz.bar : Bar)) in class Baz at line 46
-- [E083] Type Error: tests/neg/exports.scala:57:11 --------------------------------------------------------------------
57 | export printer.* // error: not stable
| ^^^^^^^
Expand Down
8 changes: 4 additions & 4 deletions tests/neg/exports.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
export scanUnit.{scanAll => foo} // error: no eligible member
export printUnit.{stat => _, _} // error: double definition
export scanUnit._ // error: double definition
export printUnit.bitmap
export printUnit.status
export printUnit.bitmap // error: no eligible member
export printUnit.status // error: double definition

def status: List[String] = printUnit.status ++ scanUnit.status // error: double definition w/ printUnit.status
def status: List[String] = printUnit.status ++ scanUnit.status
}

trait IterableOps[+A, +CC[_], +C] {
Expand All @@ -43,7 +43,7 @@

class Baz {
val bar: Bar = new Bar
export bar._
export bar._ // error: double definition
}
class Bar {
val baz: Baz = new Baz
Expand Down

0 comments on commit 03c9b3e

Please sign in to comment.