Skip to content

Commit e58ece8

Browse files
committed
Don't instantiate variables to Nothing in canDefineFurther
The purpose of canDefineFurther is to discover new members. That is not helped by instantiating a type variable to `Nothing`. Avoiding the instantiation helpos error messages. There was one test (i864.scala) that fails now but compiled previously with a surprising instantiation to Nothing. I believe it is better to issue an error message in this case.
1 parent 2df18bf commit e58ece8

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

compiler/src/dotty/tools/dotc/typer/Inferencing.scala

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ object Inferencing {
3333
*/
3434
def isFullyDefined(tp: Type, force: ForceDegree.Value)(using Context): Boolean = {
3535
val nestedCtx = ctx.fresh.setNewTyperState()
36-
val result = new IsFullyDefinedAccumulator(force)(using nestedCtx).process(tp)
36+
val result =
37+
try new IsFullyDefinedAccumulator(force)(using nestedCtx).process(tp)
38+
catch case ex: StackOverflowError =>
39+
false // can happen for programs with illegal recusions, e.g. neg/recursive-lower-constraint.scala
3740
if (result) nestedCtx.typerState.commit()
3841
result
3942
}
@@ -43,7 +46,7 @@ object Inferencing {
4346
*/
4447
def canDefineFurther(tp: Type)(using Context): Boolean =
4548
val prevConstraint = ctx.typerState.constraint
46-
isFullyDefined(tp, force = ForceDegree.all)
49+
isFullyDefined(tp, force = ForceDegree.failBottom)
4750
&& (ctx.typerState.constraint ne prevConstraint)
4851

4952
/** The fully defined type, where all type variables are forced.

tests/neg/i4986a.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
|Cannot construct a collection of type List[String] with elements of type Int based on a collection of type List[Int]..
55
|I found:
66
|
7-
| collection.BuildFrom.buildFromIterableOps[Nothing, Nothing, Nothing]
7+
| collection.BuildFrom.buildFromIterableOps[CC, A0, A]
88
|
99
|But method buildFromIterableOps in trait BuildFromLowPriority2 does not match type collection.BuildFrom[List[Int], Int, List[String]].

tests/neg/i7056.check

+16-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
-- [E008] Not Found Error: tests/neg/i7056.scala:19:10 -----------------------------------------------------------------
1+
-- [E007] Type Mismatch Error: tests/neg/i7056.scala:19:8 --------------------------------------------------------------
22
19 |val z = x.idnt1 // error
3-
| ^^^^^^^
4-
| value idnt1 is not a member of B.
5-
| An extension method was tried, but could not be fully constructed:
3+
| ^
4+
| Found: (given_PartialId_B : B)
5+
| Required: PartialId[T]
66
|
7-
| given_T1_T[T](given_PartialId_B).idnt1()
7+
| where: T is a type variable with constraint <: A
8+
|
9+
|
10+
| Note: a match type could not be fully reduced:
11+
|
12+
| trying to reduce PartialId[T]
13+
| failed since selector T
14+
| matches none of the cases
15+
|
16+
| case B => T
17+
18+
longer explanation available when compiling with `-explain`

tests/pos/i864.scala renamed to tests/neg/i864.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ object C {
66
trait X[T]
77
implicit def u[A, B]: X[A | B] = new X[A | B] {}
88
def y[T](implicit x: X[T]): T = ???
9-
val x: a.type & b.type | b.type & c.type = y
9+
val x: a.type & b.type | b.type & c.type = y // error
1010
}

0 commit comments

Comments
 (0)