Skip to content

Check transparent inline type argument bounds #12334

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 1 commit into from
May 6, 2021
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
8 changes: 8 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ object Inliner {
case Apply(fn, args) =>
cpy.Apply(tree)(liftBindings(fn, liftPos), args)
case TypeApply(fn, args) =>
fn.tpe.widenTermRefExpr match
case tp: PolyType =>
val targBounds = tp.instantiateParamInfos(args.map(_.tpe))
for (arg, bounds: TypeBounds) <- args.zip(targBounds) if !bounds.contains(arg.tpe) do
val boundsStr =
if bounds == TypeBounds.empty then " <: Any. Note that this type is higher-kinded."
else bounds.show
report.error(em"${arg.tpe} does not conform to bound$boundsStr", arg)
cpy.TypeApply(tree)(liftBindings(fn, liftPos), args)
case Select(qual, name) =>
cpy.Select(tree)(liftBindings(qual, liftPos), name)
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i10552a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
transparent inline def foo[T]: Int = 10

def test =
foo[List] // error
24 changes: 24 additions & 0 deletions tests/neg/i10552b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- Error: tests/neg/i10552b.scala:10:17 --------------------------------------------------------------------------------
10 | println(foo1["hi"]) // error
| ^^^^
| ("hi" : String) does not conform to bound <: Int
-- Error: tests/neg/i10552b.scala:11:17 --------------------------------------------------------------------------------
11 | println(foo1[String]) // error
| ^^^^^^
| String does not conform to bound <: Int
-- Error: tests/neg/i10552b.scala:12:17 --------------------------------------------------------------------------------
12 | println(foo1[Any]) // error
| ^^^
| Any does not conform to bound <: Int
-- Error: tests/neg/i10552b.scala:13:17 --------------------------------------------------------------------------------
13 | println(foo1[AnyKind]) // error
| ^^^^^^^
| AnyKind does not conform to bound <: Int
-- Error: tests/neg/i10552b.scala:15:17 --------------------------------------------------------------------------------
15 | println(foo2["hi"]) // error
| ^^^^
| ("hi" : String) does not conform to bound >: Int <: Int
-- Error: tests/neg/i10552b.scala:17:17 --------------------------------------------------------------------------------
17 | println(foo3[X]) // error
| ^
| Foo.this.X does not conform to bound <: Any. Note that this type is higher-kinded.
17 changes: 17 additions & 0 deletions tests/neg/i10552b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Foo:
transparent inline def foo1[A <: Int]: Int = valueOf[A]
transparent inline def foo2[A >: Int <: Int]: Int = valueOf[A]
transparent inline def foo3[A]: Int = ???

type X >: AnyKind <: AnyKind

def run =
println(foo1[Int])
println(foo1["hi"]) // error
println(foo1[String]) // error
println(foo1[Any]) // error
println(foo1[AnyKind]) // error

println(foo2["hi"]) // error

println(foo3[X]) // error
2 changes: 0 additions & 2 deletions tests/run-macros/from-type.check
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Some(true)
Some(false)
Some(1)
Some(2)
Some(3)
Some(4)
Some(5)
Expand Down
8 changes: 3 additions & 5 deletions tests/run-macros/from-type/Test_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
@main def Test: Unit =
testValueOfType[true]
testValueOfType[false]
testValueOfByte[1]
testValueOfShort[2]
// TODO support Byte and short literal types
// testValueOfType[1b]
// testValueOfType[2s]
testValueOfType[3]
testValueOfType[4]
testValueOfType[5L]
Expand All @@ -29,6 +30,3 @@
testValueOfType[Null]
testValueOfType[Any]
testValueOfType[Some[1]]

transparent inline def testValueOfByte[B <: Byte] = testValueOfType[B]
transparent inline def testValueOfShort[S <: Short] = testValueOfType[S]