Skip to content

Properly desugar inline given .. with .. #14284

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

Merged
merged 5 commits into from
Jan 27, 2022
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
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ object desugar {
DefDef(
className.toTermName, joinParams(constrTparams, defParamss),
classTypeRef, creatorExpr)
.withMods(companionMods | mods.flags.toTermFlags & GivenOrImplicit | Final)
.withMods(companionMods | mods.flags.toTermFlags & (GivenOrImplicit | Inline) | Final)
.withSpan(cdef.span) :: Nil
}

Expand All @@ -809,7 +809,9 @@ object desugar {
Nil
}
}
val classMods = if mods.is(Given) then mods | Synthetic else mods
if mods.isAllOf(Given | Inline | Transparent) then
report.error("inline given instances cannot be trasparent", cdef)
val classMods = if mods.is(Given) then mods &~ (Inline | Transparent) | Synthetic else mods
cpy.TypeDef(cdef: TypeDef)(
name = className,
rhs = cpy.Template(impl)(constr, parents1, clsDerived, self1,
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3626,7 +3626,7 @@ object Parsers {
val templ =
if isStatSep || isStatSeqEnd then Template(constr, parents, Nil, EmptyValDef, Nil)
else withTemplate(constr, parents)
if noParams then ModuleDef(name, templ)
if noParams && !mods.is(Inline) then ModuleDef(name, templ)
else TypeDef(name.toTypeName, templ)
end gdef
finalizeDef(gdef, mods1, start)
Expand Down
22 changes: 22 additions & 0 deletions docs/docs/reference/contextual/givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ transparent inline given mkAnnotations[A, T]: Annotations[A, T] = ${

Since `mkAnnotations` is `transparent`, the type of an application is the type of its right-hand side, which can be a proper subtype of the declared result type `Annotations[A, T]`.

Given instances can have the `inline` but not `transparent` modifiers as their type is already known from the signature.
Example:

```scala
trait Show[T] {
inline def show(x: T): String
}

inline given Show[Foo] with {
/*transparent*/ inline def show(x: Foo): String = ${ ... }
}
Comment on lines +111 to +113
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example explains inline given but also uses inline on the method definition even though these two concepts are orthogonal, couldn't the example use a non-inline def to be kept simple?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could not come up with a useful example of inlining those givens that did not involve also inlining the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the only reason why we did not disallow inline given .. with .. is for the case where it contains an inline method (as I originally intended in #14207).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the doc describe a concrete situation where it's useful then? I imagine it's needed for the method call to actually be inlined but that should be shown explicitly with an example because it's not obvious

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine it's needed for the method call to actually be inlined but that should be shown explicitly with an example because it's not obvious

The use case of the issue at the origin of this PR was compile-time typeclass. The goal is to allow, as you said, to fully inline the method call. For example, I use it for type refinements/constraints.


def app =
// inlines `show` method call and removes the call to `given Show[Foo]`
summon[Show[Foo]].show(foo)
```
Note that the inline methods within the given instances may be `transparent`.

The inlining of given instances will not inline/duplicate the implementation of the given, it will just inline the instantiation of that instance.
This is used to help dead code elimination of the given instances that are not used after inlining.


## Pattern-Bound Given Instances

Given instances can also appear in patterns. Example:
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i14177a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.compiletime.*

trait C[A]

inline given [Tup <: Tuple]: C[Tup] with
val cs = summonAll[Tuple.Map[Tup, C]] // error cannot reduce inline match with
15 changes: 15 additions & 0 deletions tests/neg/i14177c.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class T

transparent inline given fail1: T with // error
val cs = scala.compiletime.summonAll[EmptyTuple]
transparent inline given fail2[X]: T with // error
val cs = scala.compiletime.summonAll[EmptyTuple]
transparent inline given fail3(using DummyImplicit): T with // error
val cs = scala.compiletime.summonAll[EmptyTuple]

transparent inline given ok1: T = new T:
val cs = scala.compiletime.summonAll[EmptyTuple]
transparent inline given ok2[X]: T = new T:
val cs = scala.compiletime.summonAll[EmptyTuple]
transparent inline given ok3(using DummyImplicit): T = new T:
val cs = scala.compiletime.summonAll[EmptyTuple]
15 changes: 15 additions & 0 deletions tests/pos/i14177b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class T

inline given fail1: T with
Copy link
Member

@smarter smarter Jan 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does transparent inline work / do anything useful here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sure that we do not hit the previous issue where the summonAll was not inlined due to the inline flag that was kept by mistake on the class fail1.

Copy link
Member

@smarter smarter Jan 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, there are no testcases with transparent in this pr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I completely missed the transparent. I will add some test cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transparent is failing for the wrong reason. I do not see how transparent would be useful here because we already have the precise type. I will disallow this combination and make sure the error message is correct.

val cs = scala.compiletime.summonAll[EmptyTuple]
inline given fail2[X]: T with
val cs = scala.compiletime.summonAll[EmptyTuple]
inline given fail3(using DummyImplicit): T with
val cs = scala.compiletime.summonAll[EmptyTuple]

inline given ok1: T = new T:
val cs = scala.compiletime.summonAll[EmptyTuple]
inline given ok2[X]: T = new T:
val cs = scala.compiletime.summonAll[EmptyTuple]
inline given ok3(using DummyImplicit): T = new T:
val cs = scala.compiletime.summonAll[EmptyTuple]
13 changes: 13 additions & 0 deletions tests/pos/i14282.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
trait Foo[A] {
inline def foo(): Unit
}

inline given FooA[A]: Foo[A] with {
inline def foo(): Unit = println()
}
def test1 = FooA.foo()

inline given FooInt: Foo[Int] with {
inline def foo(): Unit = println()
}
def test2 = FooInt.foo()