Skip to content

Commit 69c800b

Browse files
authored
Merge pull request #12334 from dotty-staging/fix-#10552
Check transparent inline type argument bounds
2 parents dfcc26a + 92c6de8 commit 69c800b

File tree

6 files changed

+56
-7
lines changed

6 files changed

+56
-7
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ object Inliner {
125125
case Apply(fn, args) =>
126126
cpy.Apply(tree)(liftBindings(fn, liftPos), args)
127127
case TypeApply(fn, args) =>
128+
fn.tpe.widenTermRefExpr match
129+
case tp: PolyType =>
130+
val targBounds = tp.instantiateParamInfos(args.map(_.tpe))
131+
for (arg, bounds: TypeBounds) <- args.zip(targBounds) if !bounds.contains(arg.tpe) do
132+
val boundsStr =
133+
if bounds == TypeBounds.empty then " <: Any. Note that this type is higher-kinded."
134+
else bounds.show
135+
report.error(em"${arg.tpe} does not conform to bound$boundsStr", arg)
128136
cpy.TypeApply(tree)(liftBindings(fn, liftPos), args)
129137
case Select(qual, name) =>
130138
cpy.Select(tree)(liftBindings(qual, liftPos), name)

tests/neg/i10552a.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
transparent inline def foo[T]: Int = 10
2+
3+
def test =
4+
foo[List] // error

tests/neg/i10552b.check

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Error: tests/neg/i10552b.scala:10:17 --------------------------------------------------------------------------------
2+
10 | println(foo1["hi"]) // error
3+
| ^^^^
4+
| ("hi" : String) does not conform to bound <: Int
5+
-- Error: tests/neg/i10552b.scala:11:17 --------------------------------------------------------------------------------
6+
11 | println(foo1[String]) // error
7+
| ^^^^^^
8+
| String does not conform to bound <: Int
9+
-- Error: tests/neg/i10552b.scala:12:17 --------------------------------------------------------------------------------
10+
12 | println(foo1[Any]) // error
11+
| ^^^
12+
| Any does not conform to bound <: Int
13+
-- Error: tests/neg/i10552b.scala:13:17 --------------------------------------------------------------------------------
14+
13 | println(foo1[AnyKind]) // error
15+
| ^^^^^^^
16+
| AnyKind does not conform to bound <: Int
17+
-- Error: tests/neg/i10552b.scala:15:17 --------------------------------------------------------------------------------
18+
15 | println(foo2["hi"]) // error
19+
| ^^^^
20+
| ("hi" : String) does not conform to bound >: Int <: Int
21+
-- Error: tests/neg/i10552b.scala:17:17 --------------------------------------------------------------------------------
22+
17 | println(foo3[X]) // error
23+
| ^
24+
| Foo.this.X does not conform to bound <: Any. Note that this type is higher-kinded.

tests/neg/i10552b.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Foo:
2+
transparent inline def foo1[A <: Int]: Int = valueOf[A]
3+
transparent inline def foo2[A >: Int <: Int]: Int = valueOf[A]
4+
transparent inline def foo3[A]: Int = ???
5+
6+
type X >: AnyKind <: AnyKind
7+
8+
def run =
9+
println(foo1[Int])
10+
println(foo1["hi"]) // error
11+
println(foo1[String]) // error
12+
println(foo1[Any]) // error
13+
println(foo1[AnyKind]) // error
14+
15+
println(foo2["hi"]) // error
16+
17+
println(foo3[X]) // error

tests/run-macros/from-type.check

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
Some(true)
22
Some(false)
3-
Some(1)
4-
Some(2)
53
Some(3)
64
Some(4)
75
Some(5)

tests/run-macros/from-type/Test_2.scala

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
@main def Test: Unit =
33
testValueOfType[true]
44
testValueOfType[false]
5-
testValueOfByte[1]
6-
testValueOfShort[2]
5+
// TODO support Byte and short literal types
6+
// testValueOfType[1b]
7+
// testValueOfType[2s]
78
testValueOfType[3]
89
testValueOfType[4]
910
testValueOfType[5L]
@@ -29,6 +30,3 @@
2930
testValueOfType[Null]
3031
testValueOfType[Any]
3132
testValueOfType[Some[1]]
32-
33-
transparent inline def testValueOfByte[B <: Byte] = testValueOfType[B]
34-
transparent inline def testValueOfShort[S <: Short] = testValueOfType[S]

0 commit comments

Comments
 (0)