Skip to content

Commit b2f888e

Browse files
committed
Delay widening in derivedSelect when avoiding
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.
1 parent ef653b6 commit b2f888e

File tree

5 files changed

+28
-5
lines changed

5 files changed

+28
-5
lines changed

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -504,13 +504,15 @@ object TypeOps:
504504
override def derivedSelect(tp: NamedType, pre: Type) =
505505
if (pre eq tp.prefix)
506506
tp
507-
else tryWiden(tp, tp.prefix).orElse {
507+
else (if pre.isInstanceOf[AndType] then tryWiden(tp, tp.prefix) else NoType).orElse {
508508
if (tp.isTerm && variance > 0 && !pre.isSingleton)
509509
apply(tp.info.widenExpr)
510510
else if (upper(pre).member(tp.name).exists)
511511
super.derivedSelect(tp, pre)
512-
else
512+
else if (pre.isInstanceOf[AndType])
513513
range(defn.NothingType, defn.AnyType)
514+
else
515+
tryWiden(tp, tp.prefix).orElse(range(defn.NothingType, defn.AnyType))
514516
}
515517
end AvoidMap
516518

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5866,7 +5866,7 @@ object Types {
58665866
* underlying bounds to a range, otherwise return the expansion.
58675867
*/
58685868
def expandParam(tp: NamedType, pre: Type): Type =
5869-
tp.argForParam(pre) match {
5869+
tp.argForParam(pre, widenAbstract = true) match {
58705870
case arg @ TypeRef(pre, _) if pre.isArgPrefixOf(arg.symbol) =>
58715871
arg.info match {
58725872
case argInfo: TypeBounds => expandBounds(argInfo)

compiler/src/dotty/tools/io/AbstractFile.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,10 @@ abstract class AbstractFile extends Iterable[AbstractFile] {
260260

261261
// a race condition in creating the entry after the failed lookup may throw
262262
val path = jpath.resolve(name)
263-
if (isDir) Files.createDirectory(path)
264-
else Files.createFile(path)
263+
try
264+
if (isDir) Files.createDirectory(path)
265+
else Files.createFile(path)
266+
catch case _: FileAlreadyExistsException => ()
265267
new PlainFile(new File(path))
266268
case lookup => lookup
267269
}

tests/pos/i16435.avoid.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Foo:
2+
type Value
3+
def test: Option[Value] =
4+
val scr = {
5+
val self: Foo.this.type = this
6+
None: Option[self.Value]
7+
}
8+
scr

tests/pos/i16435.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// scalac: -Werror
2+
trait Base:
3+
type Value
4+
inline def oov: Option[Option[Value]] = None
5+
def get: Option[Value]
6+
7+
trait X extends Base:
8+
override def get: Option[Value] =
9+
oov match // was: match may not be exhaustive
10+
case Some(ov) => ov
11+
case None => None

0 commit comments

Comments
 (0)