From fc1acd8930672cfd4e994b88e640c86cc4f49e20 Mon Sep 17 00:00:00 2001 From: Mathias Date: Thu, 13 Oct 2016 14:02:40 +0200 Subject: [PATCH] = add VarianceSpec with a (commented out) failing test for #172 --- .../org/parboiled2/support/TailSwitch.scala | 2 +- .../scala/org/parboiled2/VarianceSpec.scala | 92 +++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 parboiled-core/src/test/scala/org/parboiled2/VarianceSpec.scala diff --git a/parboiled-core/src/main/scala/org/parboiled2/support/TailSwitch.scala b/parboiled-core/src/main/scala/org/parboiled2/support/TailSwitch.scala index 225d6d3d..bbffbd2d 100644 --- a/parboiled-core/src/main/scala/org/parboiled2/support/TailSwitch.scala +++ b/parboiled-core/src/main/scala/org/parboiled2/support/TailSwitch.scala @@ -42,7 +42,7 @@ object TailSwitch { // else if (LI <: T) RI.reverse ::: R // else if (LI <: HNil) rec(L, HNil, T, TI.tail, R, RI) // else if (TI <: HNil) rec(L, LI.tail, T, HNil, R, LI.head :: RI) - // rec(L, LI.tail, T, TI.tail, R, LI.head :: RI) + // else rec(L, LI.tail, T, TI.tail, R, LI.head :: RI) // rec(L, L, T, T, R, HNil) sealed trait Aux[L <: HList, LI <: HList, T <: HList, TI <: HList, R <: HList, RI <: HList, Out <: HList] diff --git a/parboiled-core/src/test/scala/org/parboiled2/VarianceSpec.scala b/parboiled-core/src/test/scala/org/parboiled2/VarianceSpec.scala new file mode 100644 index 00000000..045c287e --- /dev/null +++ b/parboiled-core/src/test/scala/org/parboiled2/VarianceSpec.scala @@ -0,0 +1,92 @@ +/* + * Copyright (C) 2009-2013 Mathias Doenitz, Alexander Myltsev + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.parboiled2 + +import shapeless.test.illTyped +import shapeless._ + +//// pure compile-time-only test +class VarianceSpec { + + // the Parsing DSL should + { + + // honor contravariance on the 1st type param of the `Rule` type + { + // valid example + test { + abstract class Par extends Parser { + def A: Rule2[String, Int] = ??? + def B: PopRule[Any :: HNil] = ??? + def C: Rule1[String] = rule { A ~ B } + } + () + } + + // TODO: fix https://github.com/sirthias/parboiled2/issues/172 and re-enable +// //invalid example 1 +// test { +// abstract class Par extends Parser { +// def A: Rule1[Any] = ??? +// def B: PopRule[Int :: HNil] = ??? +// } +// illTyped("""class P extends Par { def C = rule { A ~ B } }""", "Illegal rule composition") +// } + + // invalid example 2 + test { + abstract class Par extends Parser { + def A: Rule2[String, Any] = ??? + def B: PopRule[Int :: HNil] = ??? + } + illTyped("""class P extends Par { def C = rule { A ~ B } }""", "Illegal rule composition") + } + + // invalid example 3 + test { + abstract class Par extends Parser { + def A: Rule1[String] = ??? + def B: PopRule[Int :: HNil] = ??? + } + illTyped("""class P extends Par { def C = rule { A ~ B } }""", "Illegal rule composition") + } + } + + // honor covariance on the 2nd type param of the `Rule` type + { + // valid example + test { + abstract class Par extends Parser { + def A: Rule0 = ??? + def B: Rule1[Int] = ??? + def C: Rule1[Any] = rule { A ~ B } + } + } + + // invalid example + test { + abstract class Par extends Parser { + def A: Rule0 = ??? + def B: Rule1[Any] = ??? + } + illTyped("""class P extends Par { def C: Rule1[Int] = rule { A ~ B } }""", "type mismatch;.*") + } + } + } + + def test(x: Any): Unit = () // prevent "a pure expression does nothing in statement position" warnings +}