Skip to content

Commit

Permalink
Tweak AvoidMap's derivedSelect (#16563)
Browse files Browse the repository at this point in the history
Using the example from `pos/i16435.avoid`:

    class Foo:
      type Value
      def test: Option[Value] =
        val scr = {
          val self: Foo.this.type = this
          None: Option[self.Value]
        }
        scr

We want avoidance to return `Option[Value]`, aka
`Option[Foo.this.Value]`, rather than widening to `Option[Any]`.

Using test cases i16105,i16435,i2945,i8900,i8861 we don't tryWiden if
the derived prefix is a singleton.

And fix some kind of race condition in creating files/directories.
  • Loading branch information
smarter authored Jan 3, 2023
2 parents 805dda8 + 342ebf3 commit bcf44f8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ object TypeOps:
override def derivedSelect(tp: NamedType, pre: Type) =
if (pre eq tp.prefix)
tp
else tryWiden(tp, tp.prefix).orElse {
else (if pre.isSingleton then NoType else tryWiden(tp, tp.prefix)).orElse {
if (tp.isTerm && variance > 0 && !pre.isSingleton)
apply(tp.info.widenExpr)
else if (upper(pre).member(tp.name).exists)
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/io/AbstractFile.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,10 @@ abstract class AbstractFile extends Iterable[AbstractFile] {

// a race condition in creating the entry after the failed lookup may throw
val path = jpath.resolve(name)
if (isDir) Files.createDirectory(path)
else Files.createFile(path)
try
if (isDir) Files.createDirectory(path)
else Files.createFile(path)
catch case _: FileAlreadyExistsException => ()
new PlainFile(new File(path))
case lookup => lookup
}
Expand Down
13 changes: 13 additions & 0 deletions tests/pos/i16435.TreeUnpickler.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Derived from the CI failure in compiling TreeUnpickler.scala
class Foo
class Bar extends Foo:
type Self >: this.type <: Bar
final def meth(): Self = this

class Test:
def test(cond: Boolean, bar: Bar): Foo =
val res = bar
if cond then
res.meth()
else
res
8 changes: 8 additions & 0 deletions tests/pos/i16435.avoid.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Foo:
type Value
def test: Option[Value] =
val scr = {
val self: Foo.this.type = this
None: Option[self.Value]
}
scr
11 changes: 11 additions & 0 deletions tests/pos/i16435.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// scalac: -Werror
trait Base:
type Value
inline def oov: Option[Option[Value]] = None
def get: Option[Value]

trait X extends Base:
override def get: Option[Value] =
oov match // was: match may not be exhaustive
case Some(ov) => ov
case None => None

0 comments on commit bcf44f8

Please # to comment.