Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retain default parameters with export #20167

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*