From 9b6184bf0f87847b552ef64a019e1f6d0167a8c8 Mon Sep 17 00:00:00 2001 From: EnzeXing Date: Fri, 3 Nov 2023 12:18:04 -0400 Subject: [PATCH 1/4] Fix i18628 --- .../src/dotty/tools/dotc/transform/init/Objects.scala | 2 +- tests/init-global/pos/i18628.scala | 7 +++++++ tests/init-global/pos/i18628_2.scala | 7 +++++++ tests/init-global/pos/i18628_3.scala | 9 +++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 tests/init-global/pos/i18628.scala create mode 100644 tests/init-global/pos/i18628_2.scala create mode 100644 tests/init-global/pos/i18628_3.scala diff --git a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala index 8556ced63008..54712c3b12fb 100644 --- a/compiler/src/dotty/tools/dotc/transform/init/Objects.scala +++ b/compiler/src/dotty/tools/dotc/transform/init/Objects.scala @@ -589,7 +589,7 @@ object Objects: values.map(ref => ref.widen(height)).join case Fun(code, thisV, klass, env) => - Fun(code, thisV.widenRefOrCold(height), klass, env.widen(height)) + Fun(code, thisV.widenRefOrCold(height), klass, env.widen(height - 1)) case ref @ OfClass(klass, outer, _, args, env) => val outer2 = outer.widen(height - 1) diff --git a/tests/init-global/pos/i18628.scala b/tests/init-global/pos/i18628.scala new file mode 100644 index 000000000000..8ea91a381276 --- /dev/null +++ b/tests/init-global/pos/i18628.scala @@ -0,0 +1,7 @@ +object Test: + class Box(val x: Int) + + def recur(a: => Box, b: => Box): Int = + a.x + recur(a, b) + b.x + + recur(Box(1), Box(2)) \ No newline at end of file diff --git a/tests/init-global/pos/i18628_2.scala b/tests/init-global/pos/i18628_2.scala new file mode 100644 index 000000000000..272207fc1fd1 --- /dev/null +++ b/tests/init-global/pos/i18628_2.scala @@ -0,0 +1,7 @@ +object Test: + class Box(val x: Int) + + def recur(a: => Box, b: Box): Int = + a.x + recur(a, b) + b.x + + recur(Box(1), Box(2)) diff --git a/tests/init-global/pos/i18628_3.scala b/tests/init-global/pos/i18628_3.scala new file mode 100644 index 000000000000..22ac88cfc39d --- /dev/null +++ b/tests/init-global/pos/i18628_3.scala @@ -0,0 +1,9 @@ +import scala.annotation.init.widen + +object Test: + class Box(val x: Int) + + def recur(a: => Box, b: => Box): Int = + a.x + recur(a: @widen(5), b: @widen(5)) + b.x + + recur(Box(1), Box(2)) \ No newline at end of file From 20e2ca349f644bff42092fd406bf71bb62a25801 Mon Sep 17 00:00:00 2001 From: EnzeXing Date: Fri, 3 Nov 2023 12:26:50 -0400 Subject: [PATCH 2/4] move test --- tests/init-global/{pos => neg}/i18628.scala | 2 +- tests/init-global/{pos => neg}/i18628_2.scala | 2 +- tests/init-global/{pos => neg}/i18628_3.scala | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename tests/init-global/{pos => neg}/i18628.scala (73%) rename tests/init-global/{pos => neg}/i18628_2.scala (73%) rename tests/init-global/{pos => neg}/i18628_3.scala (70%) diff --git a/tests/init-global/pos/i18628.scala b/tests/init-global/neg/i18628.scala similarity index 73% rename from tests/init-global/pos/i18628.scala rename to tests/init-global/neg/i18628.scala index 8ea91a381276..a89e98e8bd97 100644 --- a/tests/init-global/pos/i18628.scala +++ b/tests/init-global/neg/i18628.scala @@ -2,6 +2,6 @@ object Test: class Box(val x: Int) def recur(a: => Box, b: => Box): Int = - a.x + recur(a, b) + b.x + a.x + recur(a, b) + b.x // error recur(Box(1), Box(2)) \ No newline at end of file diff --git a/tests/init-global/pos/i18628_2.scala b/tests/init-global/neg/i18628_2.scala similarity index 73% rename from tests/init-global/pos/i18628_2.scala rename to tests/init-global/neg/i18628_2.scala index 272207fc1fd1..4ad428035441 100644 --- a/tests/init-global/pos/i18628_2.scala +++ b/tests/init-global/neg/i18628_2.scala @@ -2,6 +2,6 @@ object Test: class Box(val x: Int) def recur(a: => Box, b: Box): Int = - a.x + recur(a, b) + b.x + a.x + recur(a, b) + b.x // error recur(Box(1), Box(2)) diff --git a/tests/init-global/pos/i18628_3.scala b/tests/init-global/neg/i18628_3.scala similarity index 70% rename from tests/init-global/pos/i18628_3.scala rename to tests/init-global/neg/i18628_3.scala index 22ac88cfc39d..563d55b26f23 100644 --- a/tests/init-global/pos/i18628_3.scala +++ b/tests/init-global/neg/i18628_3.scala @@ -4,6 +4,6 @@ object Test: class Box(val x: Int) def recur(a: => Box, b: => Box): Int = - a.x + recur(a: @widen(5), b: @widen(5)) + b.x + a.x + recur(a: @widen(5), b: @widen(5)) + b.x // error recur(Box(1), Box(2)) \ No newline at end of file From 2d1d8aa62f5b0894f86b56f066c737bff8968f4e Mon Sep 17 00:00:00 2001 From: EnzeXing Date: Wed, 8 Nov 2023 15:04:54 -0500 Subject: [PATCH 3/4] Add original test for i18628 --- tests/init-global/pos/i18628.scala | 91 ++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/init-global/pos/i18628.scala diff --git a/tests/init-global/pos/i18628.scala b/tests/init-global/pos/i18628.scala new file mode 100644 index 000000000000..71d9a1ed30a8 --- /dev/null +++ b/tests/init-global/pos/i18628.scala @@ -0,0 +1,91 @@ +abstract class Reader[+T] { + def first: T + + def rest: Reader[T] + + def atEnd: Boolean +} + +trait Parsers { + type Elem + type Input = Reader[Elem] + + sealed abstract class ParseResult[+T] { + val successful: Boolean + + def map[U](f: T => U): ParseResult[U] + + def flatMapWithNext[U](f: T => Input => ParseResult[U]): ParseResult[U] + } + + sealed abstract class NoSuccess(val msg: String) extends ParseResult[Nothing] { // when we don't care about the difference between Failure and Error + val successful = false + + def map[U](f: Nothing => U) = this + + def flatMapWithNext[U](f: Nothing => Input => ParseResult[U]): ParseResult[U] + = this + } + + case class Failure(override val msg: String) extends NoSuccess(msg) + + case class Error(override val msg: String) extends NoSuccess(msg) + + case class Success[+T](result: T, val next: Input) extends ParseResult[T] { + val successful = true + + def map[U](f: T => U) = Success(f(result), next) + + def flatMapWithNext[U](f: T => Input => ParseResult[U]): ParseResult[U] = f(result)(next) match { + case s @ Success(result, rest) => Success(result, rest) + case f: Failure => f + case e: Error => e + } + } + + case class ~[+a, +b](_1: a, _2: b) { + override def toString = s"(${_1}~${_2})" + } + + abstract class Parser[+T] extends (Input => ParseResult[T]) { + def apply(in: Input): ParseResult[T] + + def ~ [U](q: => Parser[U]): Parser[~[T, U]] = { + (for(a <- this; b <- q) yield new ~(a,b)) + } + + def flatMap[U](f: T => Parser[U]): Parser[U] + = Parser{ in => this(in) flatMapWithNext(f)} + + def map[U](f: T => U): Parser[U] //= flatMap{x => success(f(x))} + = Parser{ in => this(in) map(f)} + + def ^^ [U](f: T => U): Parser[U] = map(f) + } + + def Parser[T](f: Input => ParseResult[T]): Parser[T] + = new Parser[T]{ def apply(in: Input) = f(in) } + + def accept(e: Elem): Parser[Elem] = acceptIf(_ == e)("'"+e+"' expected but " + _ + " found") + + def acceptIf(p: Elem => Boolean)(err: Elem => String): Parser[Elem] = Parser { in => + if (in.atEnd) Failure("end of input") + else if (p(in.first)) Success(in.first, in.rest) + else Failure(err(in.first)) + } +} + + +object grammars3 extends Parsers { + type Elem = String + + val a: Parser[String] = accept("a") + val b: Parser[String] = accept("b") + + val AnBnCn: Parser[List[String]] = { + repMany(a,b) + } + + def repMany[T](p: => Parser[T], q: => Parser[T]): Parser[List[T]] = + p~repMany(p,q)~q ^^ {case x~xs~y => x::xs:::(y::Nil)} +} \ No newline at end of file From 2c582c3aac703b288c15e22b2fca7d58bd69ca50 Mon Sep 17 00:00:00 2001 From: EnzeXing Date: Wed, 8 Nov 2023 16:35:35 -0500 Subject: [PATCH 4/4] Update failing minimized test --- tests/init-global/neg/i18628.scala | 2 +- tests/init-global/neg/i18628_3.scala | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/init-global/neg/i18628.scala b/tests/init-global/neg/i18628.scala index a89e98e8bd97..798b3204338c 100644 --- a/tests/init-global/neg/i18628.scala +++ b/tests/init-global/neg/i18628.scala @@ -2,6 +2,6 @@ object Test: class Box(val x: Int) def recur(a: => Box, b: => Box): Int = - a.x + recur(a, b) + b.x // error + a.x + recur(a, b) + b.x // error // error recur(Box(1), Box(2)) \ No newline at end of file diff --git a/tests/init-global/neg/i18628_3.scala b/tests/init-global/neg/i18628_3.scala index 563d55b26f23..101674cffb6f 100644 --- a/tests/init-global/neg/i18628_3.scala +++ b/tests/init-global/neg/i18628_3.scala @@ -4,6 +4,6 @@ object Test: class Box(val x: Int) def recur(a: => Box, b: => Box): Int = - a.x + recur(a: @widen(5), b: @widen(5)) + b.x // error + a.x + recur(a: @widen(5), b: @widen(5)) + b.x // error // error recur(Box(1), Box(2)) \ No newline at end of file