Skip to content

Commit 82c54a9

Browse files
committed
Back to non-synthetic, handle only exported types
1 parent c8e0ae7 commit 82c54a9

File tree

10 files changed

+113
-57
lines changed

10 files changed

+113
-57
lines changed

compiler/src/dotty/tools/dotc/core/Flags.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ object Flags {
363363
val (Enum @ _, EnumVal @ _, _) = newFlags(40, "enum")
364364

365365
/** An export forwarder */
366-
val (Exported @ _, _, _) = newFlags(41, "exported")
366+
val (Exported @ _, ExportedTerm @ _, ExportedType @ _) = newFlags(41, "exported")
367367

368368
/** Labeled with `erased` modifier (erased value or class) */
369369
val (Erased @ _, _, _) = newFlags(42, "erased")

compiler/src/dotty/tools/dotc/core/Symbols.scala

+2
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ object Symbols extends SymUtils {
332332
this
333333
else if (denot.is(ModuleVal))
334334
this.moduleClass.sourceSymbol // The module val always has a zero-extent position
335+
else if denot.is(ExportedType) then
336+
denot.info.dropAlias.asInstanceOf[NamedType].symbol.sourceSymbol
335337
else if (denot.is(Synthetic)) {
336338
val linked = denot.linkedClass
337339
if (linked.exists && !linked.is(Synthetic))

compiler/src/dotty/tools/dotc/interactive/SourceTree.scala

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ case class SourceTree(tree: tpd.Import | tpd.NameTree, source: SourceFile) {
4242
(treeSpan.end - nameLength, treeSpan.end)
4343
Span(start, end, start)
4444
}
45-
source.atSpan(position)
45+
// Don't widen the span, only narrow.
46+
// E.g. The star in a wildcard export is 1 character,
47+
// and that is the span of the type alias that results from it
48+
// but the name may very well be larger, which we don't want.
49+
val span1 = if treeSpan.contains(position) then position else treeSpan
50+
source.atSpan(span1)
4651
}
4752
case _ =>
4853
NoSourcePosition

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ class Namer { typer: Typer =>
12391239
(EmptyFlags, mbrInfo)
12401240
var flagMask = RetainedExportFlags
12411241
if sym.isTerm then flagMask |= HasDefaultParams | NoDefaultParams
1242-
var mbrFlags = Exported | Synthetic | Method | Final | maybeStable | sym.flags & flagMask
1242+
var mbrFlags = Exported | Method | Final | maybeStable | sym.flags & flagMask
12431243
if sym.is(ExtensionMethod) || pathMethod.exists then
12441244
mbrFlags |= ExtensionMethod
12451245
val forwarderName = checkNoConflict(alias, isPrivate = false, span)

presentation-compiler/src/main/dotty/tools/pc/MetalsInteractive.scala

+4-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import dotty.tools.dotc.core.Flags.*
1010
import dotty.tools.dotc.core.Names.Name
1111
import dotty.tools.dotc.core.StdNames
1212
import dotty.tools.dotc.core.Symbols.*
13-
import dotty.tools.dotc.core.Types.*
13+
import dotty.tools.dotc.core.Types.Type
1414
import dotty.tools.dotc.interactive.SourceTree
1515
import dotty.tools.dotc.util.SourceFile
1616
import dotty.tools.dotc.util.SourcePosition
@@ -205,17 +205,9 @@ object MetalsInteractive:
205205
Nil
206206

207207
case path @ head :: tail =>
208-
if head.symbol.is(Exported) then
209-
head.symbol.info match
210-
case TypeAlias(target: NamedType) =>
211-
val ss = target.symbol.sourceSymbol // exported type
212-
List((ss, ss.info))
213-
case info => info.finalResultType match
214-
case target: NamedType =>
215-
val ss = target.symbol.sourceSymbol // exported term
216-
List((ss, ss.info))
217-
case _ =>
218-
enclosingSymbolsWithExpressionType(tail, pos, indexed, skipCheckOnName)
208+
if head.symbol.is(ExportedType) then
209+
val sym = head.symbol.sourceSymbol
210+
List((sym, sym.info))
219211
else if head.symbol.is(Synthetic) then
220212
enclosingSymbolsWithExpressionType(
221213
tail,

presentation-compiler/test/dotty/tools/pc/tests/definition/PcDefinitionSuite.scala

+69-1
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
199199
|""".stripMargin
200200
)
201201

202-
@Test def `export` =
202+
@Test def `exportType1` =
203203
check(
204204
"""object enumerations:
205205
| trait <<SymbolKind>>
@@ -217,6 +217,74 @@ class PcDefinitionSuite extends BasePcDefinitionSuite:
217217
|""".stripMargin
218218
)
219219

220+
@Test def `exportType1Wild` =
221+
check(
222+
"""object enumerations:
223+
| trait <<SymbolKind>>
224+
| trait CymbalKind
225+
|
226+
|object all:
227+
| export enumerations.SymbolKind
228+
|
229+
|@main def hello =
230+
| import all.SymbolKind
231+
| import enumerations.CymbalKind
232+
|
233+
| val x = new Symbo@@lKind {}
234+
| val y = new CymbalKind {}
235+
|""".stripMargin
236+
)
237+
238+
@Test def `exportTerm1` =
239+
check(
240+
"""class BitMap
241+
|class Scanner:
242+
| def scan(): BitMap = ???
243+
|class Copier:
244+
| private val scanUnit = new Scanner
245+
| export scanUnit.<<scan>>
246+
| def t1 = sc@@an()
247+
|""".stripMargin
248+
)
249+
250+
@Test def `exportTerm2` =
251+
check(
252+
"""class BitMap
253+
|class Scanner:
254+
| def scan(): BitMap = ???
255+
|class Copier:
256+
| private val scanUnit = new Scanner
257+
| export scanUnit.<<scan>>
258+
|class Test:
259+
| def t2(cpy: Copier) = cpy.sc@@an()
260+
|""".stripMargin
261+
)
262+
263+
@Test def `exportTerm1Wild` =
264+
check(
265+
"""class BitMap
266+
|class Scanner:
267+
| def scan(): BitMap = ???
268+
|class Copier:
269+
| private val scanUnit = new Scanner
270+
| export scanUnit.<<*>>
271+
| def t1 = sc@@an()
272+
|""".stripMargin
273+
)
274+
275+
@Test def `exportTerm2Wild` =
276+
check(
277+
"""class BitMap
278+
|class Scanner:
279+
| def scan(): BitMap = ???
280+
|class Copier:
281+
| private val scanUnit = new Scanner
282+
| export scanUnit.<<*>>
283+
|class Test:
284+
| def t2(cpy: Copier) = cpy.sc@@an()
285+
|""".stripMargin
286+
)
287+
220288
@Test def `named-arg-local` =
221289
check(
222290
"""|

tests/neg/export-leaks.check

-12
This file was deleted.

tests/neg/export-leaks.scala

+3-13
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@ object Signature {
66
object O1 {
77
private[Signature] def bar: T = ???
88
}
9-
export O1._
9+
export O1._ // error: non-private method bar refers to private type T
1010

1111
object O2 {
1212
private[Signature] val foo: T = ???
1313
}
14-
export O2._
15-
16-
object PosTest:
17-
def main(args: Array[String]): Unit =
18-
val t1 = bar // ok
19-
val t2 = foo // ok
20-
}
21-
22-
object Test:
23-
def main(args: Array[String]): Unit =
24-
val t1 = bar // error: Not found: bar
25-
val t2 = foo // error: Not found: foo
14+
export O2._ // error: non-private method foo refers to private type T
15+
}

tests/neg/exports.check

+23-12
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,16 @@
77
| ^^^^^^^^^^^^^^
88
| no eligible member scanAll at this.scanUnit
99
| this.scanUnit.scanAll cannot be exported because it is not accessible
10-
-- [E120] Naming Error: tests/neg/exports.scala:28:8 -------------------------------------------------------------------
11-
28 | def status: List[String] = printUnit.status ++ scanUnit.status // error: double definition w/ printUnit.status
12-
| ^
13-
| Double definition:
14-
| final def status: List[String] in class Copier at line 23 and
15-
| def status: List[String] in class Copier at line 28
16-
| have the same type after erasure.
17-
|
18-
| Consider adding a @targetName annotation to one of the conflicting definitions
19-
| for disambiguation.
10+
-- Error: tests/neg/exports.scala:25:21 --------------------------------------------------------------------------------
11+
25 | export printUnit.bitmap // error: no eligible member
12+
| ^
13+
| non-private given instance bitmap in class Copier refers to private value printUnit
14+
| in its type signature => object Copier.this.printUnit.bitmap
2015
-- [E120] Naming Error: tests/neg/exports.scala:23:33 ------------------------------------------------------------------
2116
23 | export printUnit.{stat => _, _} // error: double definition
2217
| ^
2318
| Double definition:
24-
| final def status: List[String] in class Copier at line 24 and
19+
| def status: List[String] in class Copier at line 28 and
2520
| final def status: List[String] in class Copier at line 23
2621
| have the same type after erasure.
2722
|
@@ -31,12 +26,22 @@
3126
24 | export scanUnit._ // error: double definition
3227
| ^
3328
| Double definition:
34-
| final def status: List[String] in class Copier at line 26 and
29+
| final def status: List[String] in class Copier at line 23 and
3530
| final def status: List[String] in class Copier at line 24
3631
| have the same type after erasure.
3732
|
3833
| Consider adding a @targetName annotation to one of the conflicting definitions
3934
| for disambiguation.
35+
-- [E120] Naming Error: tests/neg/exports.scala:26:21 ------------------------------------------------------------------
36+
26 | export printUnit.status // error: double definition
37+
| ^
38+
| Double definition:
39+
| final def status: List[String] in class Copier at line 24 and
40+
| final def status: List[String] in class Copier at line 26
41+
| have the same type after erasure.
42+
|
43+
| Consider adding a @targetName annotation to one of the conflicting definitions
44+
| for disambiguation.
4045
-- Error: tests/neg/exports.scala:35:24 --------------------------------------------------------------------------------
4146
35 | export this.{concat => ++} // error: no eligible member
4247
| ^^^^^^^^^^^^
@@ -47,6 +52,12 @@
4752
| ^^^
4853
| no eligible member foo at this.foo
4954
| this.foo.foo cannot be exported because it is already a member of class Foo
55+
-- [E120] Naming Error: tests/neg/exports.scala:46:15 ------------------------------------------------------------------
56+
46 | export bar._ // error: double definition
57+
| ^
58+
| Double definition:
59+
| val bar: Bar in class Baz at line 45 and
60+
| final def bar: (Baz.this.bar.bar : => (Baz.this.bar.baz.bar : Bar)) in class Baz at line 46
5061
-- [E083] Type Error: tests/neg/exports.scala:57:11 --------------------------------------------------------------------
5162
57 | export printer.* // error: not stable
5263
| ^^^^^^^

tests/neg/exports.scala

+4-4
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
export scanUnit.{scanAll => foo} // error: no eligible member
2323
export printUnit.{stat => _, _} // error: double definition
2424
export scanUnit._ // error: double definition
25-
export printUnit.bitmap
26-
export printUnit.status
25+
export printUnit.bitmap // error: no eligible member
26+
export printUnit.status // error: double definition
2727

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

3131
trait IterableOps[+A, +CC[_], +C] {
@@ -43,7 +43,7 @@
4343

4444
class Baz {
4545
val bar: Bar = new Bar
46-
export bar._
46+
export bar._ // error: double definition
4747
}
4848
class Bar {
4949
val baz: Baz = new Baz

0 commit comments

Comments
 (0)