Skip to content

Commit 1163435

Browse files
committed
Ignore ascriptions from PrepareInlinable.wrapRHS in InlineTyper
1 parent 5145154 commit 1163435

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

compiler/src/dotty/tools/dotc/inlines/Inliner.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1056,8 +1056,20 @@ class Inliner(val call: tpd.Tree)(using Context):
10561056
val meth = tree.symbol
10571057
if meth.isAllOf(DeferredInline) then
10581058
errorTree(tree, em"Deferred inline ${meth.showLocated} cannot be invoked")
1059-
else if Inlines.needsInlining(tree) then Inlines.inlineCall(simplify(tree, pt, locked))
1060-
else tree
1059+
else if Inlines.needsInlining(tree) then
1060+
StripInlineResultAscriptionMap().transform(Inlines.inlineCall(simplify(tree, pt, locked)))
1061+
else
1062+
tree
1063+
1064+
private class StripInlineResultAscriptionMap extends tpd.TreeMap:
1065+
override def transform(tree: Tree)(using Context): Tree =
1066+
tree match
1067+
case Typed(expr, _) if tree.hasAttachment(PrepareInlineable.InlineResultAscription) =>
1068+
expr
1069+
case tree: Inlined =>
1070+
super.transform(tree)
1071+
case _ =>
1072+
tree
10611073

10621074
override def typedUnadapted(tree: untpd.Tree, pt: Type, locked: TypeVars)(using Context): Tree =
10631075
super.typedUnadapted(tree, pt, locked) match

compiler/src/dotty/tools/dotc/inlines/PrepareInlineable.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ object PrepareInlineable {
3030

3131
private val InlineAccessorsKey = new Property.Key[InlineAccessors]
3232

33+
/** Indicates that an ascription was inserted by [[PrepareInlinable.wrapRHS]].
34+
* It is used to remove it [[Inliner.stripInlineResultAscription]].
35+
*/
36+
val InlineResultAscription = new Property.StickyKey[Unit]
37+
3338
def initContext(ctx: Context): Context =
3439
ctx.fresh.setProperty(InlineAccessorsKey, new InlineAccessors)
3540

@@ -246,7 +251,10 @@ object PrepareInlineable {
246251

247252
/** The type ascription `rhs: tpt`, unless `original` is `transparent`. */
248253
def wrapRHS(original: untpd.DefDef, tpt: Tree, rhs: Tree)(using Context): Tree =
249-
if original.mods.is(Transparent) then rhs else Typed(rhs, tpt)
254+
if original.mods.is(Transparent) then
255+
rhs
256+
else
257+
Typed(rhs, tpt).withAttachment(InlineResultAscription, ())
250258

251259
/** Return result of evaluating `op`, but drop `Inline` flag and `Body` annotation
252260
* of `sym` in case that leads to errors.

tests/pos/i24412.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
object test {
2+
import scala.compiletime.erasedValue
3+
4+
inline def contains[T <: Tuple, E]: Boolean = inline erasedValue[T] match {
5+
case _: EmptyTuple => false
6+
case _: (_ *: tail) => contains[tail, E]
7+
}
8+
inline def check[T <: Tuple]: Unit = {
9+
inline if contains[T, Long] && false then ???
10+
}
11+
12+
check[(String, Double)]
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
inline def f(): Long =
2+
1L
3+
4+
inline def g(): Long =
5+
inline val x = f()
6+
x
7+
8+
inline def h(): Long =
9+
inline if g() > 0L then 1L else 0L
10+
11+
@main def Test: Unit =
12+
assert(h() == 1L)

tests/run/i24420-inline-val.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
inline def f1(): Long =
2+
1L
3+
4+
inline def f2(): Long =
5+
inline val x = f1() + 1L
6+
x
7+
8+
inline def f3(): Long =
9+
inline val x = f1()
10+
x
11+
12+
inline def g1(): Boolean =
13+
true
14+
15+
inline def g2(): Long =
16+
inline if g1() then 1L else 2L
17+
18+
inline def g3(): Long =
19+
inline if f1() > 0L then 1L else 2L
20+
21+
@main def Test: Unit =
22+
assert(f2() == 2L)
23+
assert(f3() == 1L)
24+
assert(g2() == 1L)
25+
assert(g3() == 1L)

0 commit comments

Comments
 (0)