Skip to content

Commit c411955

Browse files
committed
Change rules for exports to make them more useful for enums
Two changes: - The forwarder alising a value definition gets a singleton type referring to the alias - Synthetic members are not exported by a wildcard export This means we can now safely write ``` enum MatchDegree { case NoMatch, ParamMatch, FullMatch } export MatchDegree._ ``` and export just the three enum values with the most precise types. (precision is necessary for exhaustiveness checks, among others).
1 parent b5a6963 commit c411955

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -976,12 +976,11 @@ class Namer { typer: Typer =>
976976
fwdInfo(path.tpe.select(mbr.symbol), mbr.info),
977977
coord = span)
978978
else {
979-
val maybeStable = if (mbr.symbol.isStableMember) StableRealizable else EmptyFlags
980-
ctx.newSymbol(
981-
cls, alias,
982-
Exported | Method | Final | maybeStable | mbr.symbol.flags & RetainedExportFlags,
983-
mbr.info.ensureMethodic,
984-
coord = span)
979+
val (maybeStable, mbrInfo) =
980+
if (mbr.symbol.isStableMember) (StableRealizable, ExprType(path.tpe.select(mbr.symbol)))
981+
else (EmptyFlags, mbr.info.ensureMethodic)
982+
val mbrFlags = Exported | Method | Final | maybeStable | mbr.symbol.flags & RetainedExportFlags
983+
ctx.newSymbol(cls, alias, mbrFlags, mbrInfo, coord = span)
985984
}
986985
val forwarderDef =
987986
if (forwarder.isType) tpd.TypeDef(forwarder.asType)
@@ -1010,7 +1009,8 @@ class Namer { typer: Typer =>
10101009
}
10111010

10121011
def addForwardersExcept(seen: List[TermName], span: Span): Unit =
1013-
for (mbr <- path.tpe.allMembers) {
1012+
for (mbr <- path.tpe.membersBasedOnFlags(
1013+
required = EmptyFlags, excluded = PrivateOrSynthetic)) {
10141014
val alias = mbr.name.toTermName
10151015
if (!seen.contains(alias)) addForwarder(alias, mbr, span)
10161016
}

docs/docs/reference/enums/desugarEnums.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ map into case classes or vals.
157157

158158
Non-generic enums `E` that define one or more singleton cases
159159
are called _enumerations_. Companion objects of enumerations define
160-
the following additional members.
160+
the following additional synthetic members.
161161

162162
- A method `valueOf(name: String): E`. It returns the singleton case value whose
163163
`toString` representation is `name`.

docs/docs/reference/other-new-features/export.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ of one of the following forms:
5454
- An _omitting selector_ `x => _` prevents `x` from being aliased by a subsequent
5555
wildcard selector.
5656
- A _wildcard selector_ creates aliases for all eligible members of `path` except for
57-
those members that are named by a previous simple, renaming, or omitting selector.
57+
synthetic members generated by the compiler and those members that are named by a previous simple, renaming, or omitting selector.
5858

5959
A member is _eligible_ if all of the following holds:
6060

@@ -75,7 +75,7 @@ Export aliases are always `final`. Aliases of delegates are again defines as del
7575
not marked `override`.
7676
- However, export aliases can implement deferred members of base classes.
7777

78-
Export aliases for value definitions are marked by the compiler as "stable". This means
78+
Export aliases for value definitions are marked by the compiler as "stable" and their result types are the singleton types of the aliased definitions. This means
7979
that they can be used as parts of stable identifier paths, even though they are technically methods. For instance, the following is OK:
8080
```scala
8181
class C { type T }

0 commit comments

Comments
 (0)