Skip to content

Commit

Permalink
= add VarianceSpec with a (commented out) failing test for #172
Browse files Browse the repository at this point in the history
  • Loading branch information
sirthias committed Oct 13, 2016
1 parent 61480a5 commit fc1acd8
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
Expand Up @@ -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]

Expand Down
92 changes: 92 additions & 0 deletions 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
}

0 comments on commit fc1acd8

Please sign in to comment.