Skip to content

Commit

Permalink
Retain default parameters with export
Browse files Browse the repository at this point in the history
Default parameters need to have the `HasDefault` flag set to work properly, it
seems that without this flag they were still selected but only under join
compilation.

While we're at it, we define a full set of flags that might be exported. Note
that Given/Implicit/Erased were already set by `tpd.DefDef` and Inlined doesn't
seem to make a difference in practice since the body of the exported inline def
is manually constructed and doesn't contain proxies either way.
  • Loading branch information
smarter committed Apr 11, 2024
1 parent c7570c8 commit 1045571
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,9 @@ object Flags {
/** Flags retained in term export forwarders */
val RetainedExportTermFlags = Infix | Given | Implicit | Inline | Transparent | Erased | HasDefaultParams | NoDefaultParams | ExtensionMethod

/** Flags retained in parameters of term export forwarders */
val RetainedExportTermParamFlags = Given | Implicit | Erased | HasDefault | Inline

val MandatoryExportTermFlags = Exported | Method | Final

/** Flags retained in type export forwarders */
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1293,14 +1293,16 @@ class Namer { typer: Typer =>
getter => addForwarder(
getter.name.asTermName, getter.asSeenFrom(path.tpe), span))

// adding annotations at the parameter level
// adding annotations and flags at the parameter level
// TODO: This probably needs to be filtered to avoid adding some annotation
// such as MacroAnnotations
if sym.is(Method) then
for (orig, forwarded) <- sym.paramSymss.lazyZip(forwarder.paramSymss)
(origParameter, exportedParameter) <- orig.lazyZip(forwarded)
do
exportedParameter.addAnnotations(origParameter.annotations)
if exportedParameter.isTerm then
exportedParameter.setFlag(origParameter.flags & RetainedExportTermParamFlags)
end addForwarder

def addForwardersNamed(name: TermName, alias: TermName, span: Span): Unit =
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/export-param-flags/A_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object A:
def defaultParam(x: Int = 1) = x

object Exported:
export A.*
2 changes: 2 additions & 0 deletions tests/pos/export-param-flags/B_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
object B:
val x = Exported.defaultParam()
13 changes: 13 additions & 0 deletions tests/printing/export-param-flags.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[syntax trees at end of typer]] // tests/printing/export-param-flags.scala
package <empty> {
final lazy module val A: A = new A()
final module class A() extends Object() { this: A.type =>
inline def inlinedParam(inline x: Int): Int = x.+(x):Int
}
final lazy module val Exported: Exported = new Exported()
final module class Exported() extends Object() { this: Exported.type =>
export A.*
final inline def inlinedParam(inline x: Int): Int = A.inlinedParam(x)
}
}

5 changes: 5 additions & 0 deletions tests/printing/export-param-flags.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object A:
inline def inlinedParam(inline x: Int): Int = x + x

object Exported:
export A.*

0 comments on commit 1045571

Please sign in to comment.