Skip to content

Commit 875b9f8

Browse files
committed
Fix explanation for lower bounds failures
Fixes scala#15575
1 parent 69858b5 commit 875b9f8

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,9 @@ import transform.SymUtils._
14461446
}
14471447

14481448
class DoesNotConformToBound(tpe: Type, which: String, bound: Type)(using Context)
1449-
extends TypeMismatchMsg(tpe, bound)(DoesNotConformToBoundID) {
1449+
extends TypeMismatchMsg(
1450+
if which == "lower" then bound else tpe,
1451+
if which == "lower" then tpe else bound)(DoesNotConformToBoundID) {
14501452
def msg = em"Type argument ${tpe} does not conform to $which bound $bound"
14511453
}
14521454

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class CompilationTests {
159159
compileFile("tests/neg-custom-args/i12650.scala", allowDeepSubtypes),
160160
compileFile("tests/neg-custom-args/i9517.scala", defaultOptions.and("-Xprint-types")),
161161
compileFile("tests/neg-custom-args/i11637.scala", defaultOptions.and("-explain")),
162+
compileFile("tests/neg-custom-args/i15575.scala", defaultOptions.and("-explain")),
162163
compileFile("tests/neg-custom-args/interop-polytypes.scala", allowDeepSubtypes.and("-Yexplicit-nulls")),
163164
compileFile("tests/neg-custom-args/conditionalWarnings.scala", allowDeepSubtypes.and("-deprecation").and("-Xfatal-warnings")),
164165
compileFilesInDir("tests/neg-custom-args/isInstanceOf", allowDeepSubtypes and "-Xfatal-warnings"),

tests/neg-custom-args/i15575.check

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
-- [E057] Type Mismatch Error: tests/neg-custom-args/i15575.scala:3:27 -------------------------------------------------
2+
3 | def bar[T]: Unit = foo[T & Any] // error
3+
| ^
4+
| Type argument T & Any does not conform to lower bound Any
5+
|---------------------------------------------------------------------------------------------------------------------
6+
| Explanation (enabled by `-explain`)
7+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
8+
| I tried to show that
9+
| Any
10+
| conforms to
11+
| T & Any
12+
| but the comparison trace ended with `false`:
13+
|
14+
| ==> Any <: T & Any
15+
| ==> Any <: T
16+
| <== Any <: T = false
17+
| <== Any <: T & Any = false
18+
|
19+
| The tests were made under the empty constraint
20+
---------------------------------------------------------------------------------------------------------------------
21+
-- [E057] Type Mismatch Error: tests/neg-custom-args/i15575.scala:7:14 -------------------------------------------------
22+
7 | val _ = foo[String] // error
23+
| ^
24+
| Type argument String does not conform to lower bound CharSequence
25+
|---------------------------------------------------------------------------------------------------------------------
26+
| Explanation (enabled by `-explain`)
27+
|- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
28+
| I tried to show that
29+
| CharSequence
30+
| conforms to
31+
| String
32+
| but the comparison trace ended with `false`:
33+
|
34+
| ==> CharSequence <: String
35+
| ==> CharSequence <: String
36+
| <== CharSequence <: String = false
37+
| <== CharSequence <: String = false
38+
|
39+
| The tests were made under the empty constraint
40+
---------------------------------------------------------------------------------------------------------------------

tests/neg-custom-args/i15575.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test1:
2+
def foo[T >: Any]: Unit = ()
3+
def bar[T]: Unit = foo[T & Any] // error
4+
5+
object Test2:
6+
def foo[T >: CharSequence]: Unit = ()
7+
val _ = foo[String] // error

0 commit comments

Comments
 (0)