Skip to content

Commit 9b3cdc2

Browse files
committed
Properly desugar inline given .. with ..
```scala inline given Foo with { .. } ``` now becomes ```scala inline given def given_Foo: Foo with {..} = new Foo class given_Foo extends Foo with { } ``` There are no creation of `lazy vals` when we have `inline`. We inline the `new givne_Foo` only, the class does not get inlined. This is useful to remove the instatiation of the given instance when the methods are inline themselfs. Fixes #14282 Fixes #14177
1 parent eed6820 commit 9b3cdc2

File tree

5 files changed

+37
-3
lines changed

5 files changed

+37
-3
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ object desugar {
783783
DefDef(
784784
className.toTermName, joinParams(constrTparams, defParamss),
785785
classTypeRef, creatorExpr)
786-
.withMods(companionMods | mods.flags.toTermFlags & GivenOrImplicit | Final)
786+
.withMods(companionMods | mods.flags.toTermFlags & (GivenOrImplicit | Inline) | Final)
787787
.withSpan(cdef.span) :: Nil
788788
}
789789

@@ -809,7 +809,7 @@ object desugar {
809809
Nil
810810
}
811811
}
812-
val classMods = if mods.is(Given) then mods | Synthetic else mods
812+
val classMods = if mods.is(Given) then mods &~ Inline | Synthetic else mods
813813
cpy.TypeDef(cdef: TypeDef)(
814814
name = className,
815815
rhs = cpy.Template(impl)(constr, parents1, clsDerived, self1,

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3626,7 +3626,7 @@ object Parsers {
36263626
val templ =
36273627
if isStatSep || isStatSeqEnd then Template(constr, parents, Nil, EmptyValDef, Nil)
36283628
else withTemplate(constr, parents)
3629-
if noParams then ModuleDef(name, templ)
3629+
if noParams && !mods.is(Inline) then ModuleDef(name, templ)
36303630
else TypeDef(name.toTypeName, templ)
36313631
end gdef
36323632
finalizeDef(gdef, mods1, start)

tests/neg/i14177a.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.compiletime.*
2+
3+
trait C[A]
4+
5+
inline given [Tup <: Tuple]: C[Tup] with
6+
val cs = summonAll[Tuple.Map[Tup, C]] // error cannot reduce inline match with

tests/pos/i14177b.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class T
2+
3+
inline given fail1: T with
4+
val cs = scala.compiletime.summonAll[EmptyTuple]
5+
inline given fail2[X]: T with
6+
val cs = scala.compiletime.summonAll[EmptyTuple]
7+
inline given fail3(using DummyImplicit): T with
8+
val cs = scala.compiletime.summonAll[EmptyTuple]
9+
10+
inline given ok1: T = new T:
11+
val cs = scala.compiletime.summonAll[EmptyTuple]
12+
inline given ok2[X]: T = new T:
13+
val cs = scala.compiletime.summonAll[EmptyTuple]
14+
inline given ok3(using DummyImplicit): T = new T:
15+
val cs = scala.compiletime.summonAll[EmptyTuple]

tests/pos/i14282.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait Foo[A] {
2+
inline def foo(): Unit
3+
}
4+
5+
inline given FooA[A]: Foo[A] with {
6+
inline def foo(): Unit = println()
7+
}
8+
def test1 = FooA.foo()
9+
10+
inline given FooInt: Foo[Int] with {
11+
inline def foo(): Unit = println()
12+
}
13+
def test2 = FooInt.foo()

0 commit comments

Comments
 (0)