Skip to content

Commit

Permalink
Delay widening in derivedSelect when avoiding
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]`.

But also, widen when we're deriving to an intersection prefix...
Test cases: i16105,i16435,i2945,i8900,i8861.

And fix some kind of race condition in creating files/directories.
  • Loading branch information
dwijnand committed Dec 20, 2022
1 parent ef653b6 commit b2f888e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 5 deletions.
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,15 @@ object TypeOps:
override def derivedSelect(tp: NamedType, pre: Type) =
if (pre eq tp.prefix)
tp
else tryWiden(tp, tp.prefix).orElse {
else (if pre.isInstanceOf[AndType] then tryWiden(tp, tp.prefix) else NoType).orElse {
if (tp.isTerm && variance > 0 && !pre.isSingleton)
apply(tp.info.widenExpr)
else if (upper(pre).member(tp.name).exists)
super.derivedSelect(tp, pre)
else
else if (pre.isInstanceOf[AndType])
range(defn.NothingType, defn.AnyType)
else
tryWiden(tp, tp.prefix).orElse(range(defn.NothingType, defn.AnyType))
}
end AvoidMap

Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5866,7 +5866,7 @@ object Types {
* underlying bounds to a range, otherwise return the expansion.
*/
def expandParam(tp: NamedType, pre: Type): Type =
tp.argForParam(pre) match {
tp.argForParam(pre, widenAbstract = true) match {
case arg @ TypeRef(pre, _) if pre.isArgPrefixOf(arg.symbol) =>
arg.info match {
case argInfo: TypeBounds => expandBounds(argInfo)
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
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 b2f888e

Please # to comment.