Skip to content

Commit

Permalink
Adapt tests for strict pattern binding warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
griggt committed May 11, 2022
1 parent 56c69bd commit 7eb227f
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 34 deletions.
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i3536_bind
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scala> val res10 @ Some(_) = Option(1)
scala> val res10 @ Some(_) = Option(1): @unchecked
val res10: Some[Int] = Some(1)
scala> res10
val res11: Some[Int] = Some(1)
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i3536_patterndef_some
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scala> val Some((res40, res41)) = Option((1, 0))
scala> val Some((res40, res41)) = Option((1, 0)): @unchecked
val res40: Int = 1
val res41: Int = 0
scala> res40
Expand Down
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/i3966
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scala> val List(x: _*) = List(1, 2)
scala> val List(x: _*) = List(1, 2): @unchecked
val x: Seq[Int] = List(1, 2)
2 changes: 1 addition & 1 deletion compiler/test-resources/repl/patdef
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
scala> val Const, x = 0
val Const: Int = 0
val x: Int = 0
scala> val (Const, List(`x`, _, a), b) = (0, List(0, 1337, 1), 2)
scala> val (Const, List(`x`, _, a), b) = (0, List(0, 1337, 1), 2): @unchecked
val a: Int = 1
val b: Int = 2
scala> val a@b = 0
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/dotc/TastyBootstrapTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class TastyBootstrapTests {
compileList("lib", librarySources,
defaultOptions.and("-Ycheck-reentrant",
"-language:experimental.erasedDefinitions", // support declaration of scala.compiletime.erasedValue
// "-source", "future", // TODO: re-enable once we allow : @unchecked in pattern definitions. Right now, lots of narrowing pattern definitions fail.
// "-source", "future", // TODO: re-enable once library uses updated syntax for vararg splices, wildcard imports, and import renaming
))(libGroup)

val tastyCoreSources = sources(Paths.get("tasty/src"))
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/dotc/ast/DesugarTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class DesugarTests extends DottyTest {
val ccTree = tree.find(tree => tree.symbol.name == typeName("Foo")).get
val List(_, foo) = defPath(ccTree.symbol, tree).map(_.symbol.info)

val x :: y :: rest = foo.decls.toList
val x :: y :: rest = foo.decls.toList: @unchecked

// Make sure we extracted the correct values from foo:
assert(x.name == termName("x"))
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/dotc/printing/PrinterTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PrinterTests extends DottyTest {

checkCompile("typer", source) { (tree, context) =>
given Context = context
val bar @ Trees.DefDef(_, _, _, _) = tree.find(tree => tree.symbol.name == termName("bar2")).get
val bar @ Trees.DefDef(_, _, _, _) = tree.find(tree => tree.symbol.name == termName("bar2")).get: @unchecked
assertEquals("Int & (Boolean | String)", bar.tpt.show)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class UserDefinedErrorMessages extends ErrorMessagesTest {
given Context = itcx

assertMessageCount(1, messages)
val (m: NoExplanation) :: Nil = messages
val (m: NoExplanation) :: Nil = messages: @unchecked

assertEquals(m.msg, "Could not prove Int =!= Int")
}
Expand All @@ -50,7 +50,7 @@ class UserDefinedErrorMessages extends ErrorMessagesTest {
given Context = itcx

assertMessageCount(1, messages)
val (m: NoExplanation) :: Nil = messages
val (m: NoExplanation) :: Nil = messages: @unchecked

assertEquals(m.msg, "Could not prove Int =!= Int")
}
Expand All @@ -75,7 +75,7 @@ class UserDefinedErrorMessages extends ErrorMessagesTest {
given Context = itcx

assertMessageCount(1, messages)
val (m: NoExplanation) :: Nil = messages
val (m: NoExplanation) :: Nil = messages: @unchecked

assertEquals(m.msg, "Could not prove Int =!= Int")
}
Expand All @@ -97,7 +97,7 @@ class UserDefinedErrorMessages extends ErrorMessagesTest {
given Context = itcx

assertMessageCount(1, messages)
val (m: NoExplanation) :: Nil = messages
val (m: NoExplanation) :: Nil = messages: @unchecked

assertEquals(m.msg, "msg A=Any")
}
Expand All @@ -119,7 +119,7 @@ class UserDefinedErrorMessages extends ErrorMessagesTest {
given Context = itcx

assertMessageCount(1, messages)
val (m: NoExplanation) :: Nil = messages
val (m: NoExplanation) :: Nil = messages: @unchecked

assertEquals(m.msg, "msg A=Any")
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,18 @@ class ReplCompilerTests extends ReplTest:
}

@Test def `i10214 must show classic MatchError` = initially {
run("val 1 = 2")
run("val 1 = 2: @unchecked")
assertEquals("scala.MatchError: 2 (of class java.lang.Integer)", storedOutput().linesIterator.next())
}
@Test def `i10214 must show useful regex MatchError` =
initially {
run("""val r = raw"\d+".r""")
} andThen {
run("""val r() = "abc"""")
run("""val r() = "abc": @unchecked""")
assertEquals("scala.MatchError: abc (of class java.lang.String)", storedOutput().linesIterator.drop(1).next())
}
@Test def `i10214 must show MatchError on literal type` = initially {
run("val (x: 1) = 2")
run("val (x: 1) = 2: @unchecked")
assertEquals("scala.MatchError: 2 (of class java.lang.Integer)", storedOutput().linesIterator.next())
}
@Test def `i12920 must truncate stack trace to user code` = initially {
Expand Down Expand Up @@ -328,7 +328,7 @@ class ReplCompilerTests extends ReplTest:
}

@Test def i14473 = initially {
run("""val (x,y) = if true then "hi" else (42,17)""")
run("""val (x,y) = (if true then "hi" else (42,17)): @unchecked""")
val all = lines()
assertEquals(2, all.length)
assertEquals("scala.MatchError: hi (of class java.lang.String)", all.head)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {
val target = wrappedTarget.symbol
val lookupRes = MemberLookup.lookupOpt(parseQuery(query), None)
assertTrue(s"Couldn't look up: $query", lookupRes.nonEmpty)
val Some((lookedUp, _, _)) = lookupRes
val Some((lookedUp, _, _)) = lookupRes: @unchecked
assertSame(query, target, lookedUp)
}

Expand All @@ -83,7 +83,7 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {
val target = sym.symbol
val lookupRes = MemberLookup.lookupOpt(parseQuery(query), None)
assertTrue(s"Couldn't look up: $query", lookupRes.nonEmpty)
val Some((_ , _, Some(owner))) = lookupRes
val Some((_ , _, Some(owner))) = lookupRes: @unchecked
assertSame(query, target, owner)
}
}
Expand All @@ -102,7 +102,7 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {
val lookupRes = MemberLookup.lookupOpt(parseQuery(query), Some(cls("scala.=:=").symbol))
assertTrue(s"Couldn't look up: $query", lookupRes.nonEmpty)
println(lookupRes)
val Some((_ , _, owner)) = lookupRes
val Some((_ , _, owner)) = lookupRes: @unchecked
assertSame(query, None, owner)
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {
)

cases.foreach { case ((Sym(owner), query), Sym(target)) =>
val Some((lookedUp, _, _)) = MemberLookup.lookup(parseQuery(query), owner)
val Some((lookedUp, _, _)) = MemberLookup.lookup(parseQuery(query), owner): @unchecked
assertSame(s"$owner / $query", target, lookedUp)
}
}
Expand All @@ -176,7 +176,7 @@ class LookupTestCases[Q <: Quotes](val q: Quotes) {
given q.type = q

def parseQuery(query: String): Query = {
val Right(parsed) = QueryParser(query).tryReadQuery()
val Right(parsed) = QueryParser(query).tryReadQuery(): @unchecked
parsed
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ class QueryParserTests {
private def parse(input: String) = QueryParser(input).tryReadQuery()

private def testSuccess(input: String, expected: Query) = {
val Right(got) = parse(input)
val Right(got) = parse(input): @unchecked
assertEquals(expected, got)
}

private def testFailAt(input: String, char: Int) = {
val Left(err) = parse(input)
val Left(err) = parse(input): @unchecked
assertEquals(s"expected to fail at $char : $input", char, err.at)
}
}
2 changes: 1 addition & 1 deletion tests/neg-macros/i6432/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Macro {
import quotes.reflect.*
sc match {
case '{ StringContext(${Varargs(parts)}*) } =>
for (part @ Expr(s) <- parts)
for (case part @ Expr(s) <- parts)
report.error(s, part.asTerm.pos)
}
'{}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i6432b/Macro_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Macro {
import quotes.reflect.*
sc match {
case '{ StringContext(${Varargs(parts)}*) } =>
for (part @ Expr(s) <- parts)
for (case part @ Expr(s) <- parts)
report.error(s, part.asTerm.pos)
}
'{}
Expand Down
16 changes: 16 additions & 0 deletions tests/neg/t5702-neg-bad-and-wild.check
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,19 @@
| Recursive value $1$ needs type
|
| longer explanation available when compiling with `-explain`
-- Warning: tests/neg/t5702-neg-bad-and-wild.scala:22:20 ---------------------------------------------------------------
22 | val K(x @ _*) = k
| ^
| pattern's type Int* does not match the right hand side expression's type Int
|
| If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
| which may result in a MatchError at runtime.
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
-- Warning: tests/neg/t5702-neg-bad-and-wild.scala:25:20 ---------------------------------------------------------------
25 | val (b, _ * ) = (5,6) // ok
| ^^^^^
| pattern's type Int* does not match the right hand side expression's type Int
|
| If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
| which may result in a MatchError at runtime.
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
2 changes: 1 addition & 1 deletion tests/patmat/for.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ object Test {
for ((a, b) <- l) yield a
}

def bar(xs: List[(Int, List[Int])]): Unit = for ( (_, x :: y :: xs) <- xs) yield x
def bar(xs: List[(Int, List[Int])]): Unit = for (case (_, x :: y :: xs) <- xs) yield x
}
2 changes: 1 addition & 1 deletion tests/pos-special/fatal-warnings/i6621.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ object Unapply {
}

object Test {
val Unapply(x, y) = ""
val Unapply(x, y) = "": @unchecked
}
3 changes: 2 additions & 1 deletion tests/pos/i14587.hard-union-tuples.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ class Test:
type Foo = Option[String] | Option[Int]

def test(foo: Foo) =
val (_, foo2: Foo) = // was: the type test for Test.this.Foo cannot be checked at runtime
val (_, foo2: Foo) = ( // was: the type test for Test.this.Foo cannot be checked at runtime
foo match
case Some(s: String) => ((), s)
case _ => ((), 0)
): @unchecked
foo2
6 changes: 3 additions & 3 deletions tests/semanticdb/expect/Synthetic.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Synthetic/*<-example::Synthetic#*/ {

// See https://github.com/scalameta/scalameta/issues/977
val Name/*<-example::Synthetic#Name.*/ = "name:(.*)".r/*->scala::collection::StringOps#r().*/
val x/*<-example::Synthetic#x.*/ #::/*->scala::package.`#::`.*/ xs/*<-example::Synthetic#xs.*/ = LazyList/*->scala::package.LazyList.*/(1, 2)
val Name/*->example::Synthetic#Name.*/(name/*<-example::Synthetic#name.*/) = "name:foo"
val x/*<-example::Synthetic#x.*/ #::/*->scala::package.`#::`.*/ xs/*<-example::Synthetic#xs.*/ = LazyList/*->scala::package.LazyList.*/(1, 2): @unchecked
val Name/*->example::Synthetic#Name.*/(name/*<-example::Synthetic#name.*/) = "name:foo": @unchecked
1 #:: 2 #:: LazyList/*->scala::package.LazyList.*/.empty/*->scala::collection::immutable::LazyList.empty().*/

val a1/*<-example::Synthetic#a1.*/ #::/*->scala::package.`#::`.*/ a2/*<-example::Synthetic#a2.*/ #::/*->scala::package.`#::`.*/ as/*<-example::Synthetic#as.*/ = LazyList/*->scala::package.LazyList.*/(1, 2)
val a1/*<-example::Synthetic#a1.*/ #::/*->scala::package.`#::`.*/ a2/*<-example::Synthetic#a2.*/ #::/*->scala::package.`#::`.*/ as/*<-example::Synthetic#as.*/ = LazyList/*->scala::package.LazyList.*/(1, 2): @unchecked

val lst/*<-example::Synthetic#lst.*/ = 1 #:: 2 #:: LazyList/*->scala::package.LazyList.*/.empty/*->scala::collection::immutable::LazyList.empty().*/

Expand Down
6 changes: 3 additions & 3 deletions tests/semanticdb/expect/Synthetic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class Synthetic {

// See https://github.com/scalameta/scalameta/issues/977
val Name = "name:(.*)".r
val x #:: xs = LazyList(1, 2)
val Name(name) = "name:foo"
val x #:: xs = LazyList(1, 2): @unchecked
val Name(name) = "name:foo": @unchecked
1 #:: 2 #:: LazyList.empty

val a1 #:: a2 #:: as = LazyList(1, 2)
val a1 #:: a2 #:: as = LazyList(1, 2): @unchecked

val lst = 1 #:: 2 #:: LazyList.empty

Expand Down

0 comments on commit 7eb227f

Please sign in to comment.