Skip to content

Commit 1f01f71

Browse files
committed
Only refine type of inline vals marked as transparent
1 parent f5c758f commit 1f01f71

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

+12-10
Original file line numberDiff line numberDiff line change
@@ -887,17 +887,19 @@ trait Checking {
887887
*/
888888
def checkInlineConformant(tpt: Tree, tree: Tree, sym: Symbol)(using Context): Tree = {
889889
if sym.is(Inline, butNot = DeferredOrTermParamOrAccessor) && !ctx.erasedTypes && !ctx.inInlineMethod then
890-
tree.tpe.widenTermRefExpr.dealias match
891-
case tp: ConstantType =>
892-
// final vals can be marked inline even if they're not pure, see Typer#patchFinalVals
893-
val purityLevel = if (sym.is(Final)) Idempotent else Pure
894-
if !(exprPurity(tree) >= purityLevel) then
895-
ctx.error("inline value must be pure", tree.sourcePos)
896-
if tree.tpe =:= tpt.tpe then tpt
897-
else TypeTree(tree.tpe).withSpan(tpt.span)
890+
// final vals can be marked inline even if they're not pure, see Typer#patchFinalVals
891+
val purityLevel = if (sym.is(Final)) Idempotent else Pure
892+
if !(exprPurity(tree) >= purityLevel) then
893+
ctx.error("inline value must be pure", tree.sourcePos)
894+
tpt
895+
else tpt.tpe.widenTermRefExpr.dealias match
896+
case tp: ConstantType => tpt
898897
case _ =>
899-
ctx.error("inline value must have a literal constant type", tree.sourcePos)
900-
tpt
898+
if sym.is(Transparent) then
899+
TypeTree(tree.tpe).withSpan(tpt.span)
900+
else
901+
ctx.error("inline value must have a literal constant type", tree.sourcePos)
902+
tpt
901903
else
902904
tpt
903905
}

tests/neg/i8841.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Foo {
2-
inline val log1 : Boolean = false
2+
inline val log1 : Boolean = false // error
33
inline val log2 = true: Boolean // error
44
inline val log3: false = { println(); false } // error
55
}

tests/neg/inline-val.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

22
inline val a = 1 : Int // error
3-
inline val b: Int = 1
3+
inline val b: Int = 1 // error
44
inline val c = b // error

tests/pos/inline-vals.scala

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ object Constants extends InlineConstants {
2323
}
2424

2525
class Constants2 {
26-
inline val myInlinedBoolean: Boolean = true
27-
inline val myInlinedByte: Byte = 1
28-
inline val myInlinedShort: Short = 2
29-
inline val myInlinedInt: Int = 3
30-
inline val myInlinedLong: Long = 4
31-
inline val myInlinedFloat: Float = 5
32-
inline val myInlinedDouble: Double = 6
33-
inline val myInlinedChar: Char = 'a'
34-
inline val myInlinedString: String = "abc"
35-
}
26+
transparent inline val myInlinedBoolean: Boolean = true
27+
transparent inline val myInlinedByte: Byte = 1
28+
transparent inline val myInlinedShort: Short = 2
29+
transparent inline val myInlinedInt: Int = 3
30+
transparent inline val myInlinedLong: Long = 4
31+
transparent inline val myInlinedFloat: Float = 5
32+
transparent inline val myInlinedDouble: Double = 6
33+
transparent inline val myInlinedChar: Char = 'a'
34+
transparent inline val myInlinedString: String = "abc"
35+
}

0 commit comments

Comments
 (0)