Skip to content

Some usability improvements relating to errors #23370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Better error message if inferred type does not conform to externally …
…visible type.
  • Loading branch information
odersky committed Jun 15, 2025
commit 7a3550ff1fae795f7423fd06dd33fdcc578a0e68
12 changes: 11 additions & 1 deletion compiler/src/dotty/tools/dotc/cc/CheckCaptures.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,16 @@ class CheckCaptures extends Recheck, SymTransformer:
// too annoying. This is a hole since a defualt getter's result type
// might leak into a type variable.

def fail(tree: Tree, expected: Type, addenda: Addenda): Unit =
def maybeResult = if sym.is(Method) then " result" else ""
report.error(
em"""$sym needs an explicit$maybeResult type because the inferred type does not conform to
|the type that is externally visible in other compilation units.
|
| Inferred type : ${tree.tpe}
| Externally visible type: $expected""",
tree.srcPos)

def addenda(expected: Type) = new Addenda:
override def toAdd(using Context) =
def result = if tree.isInstanceOf[ValDef] then"" else " result"
Expand All @@ -1083,7 +1093,7 @@ class CheckCaptures extends Recheck, SymTransformer:
val expected = tpt.tpe.dropAllRetains
todoAtPostCheck += { () =>
withCapAsRoot:
checkConformsExpr(tp, expected, tree.rhs, addenda(expected))
testAdapted(tp, expected, tree.rhs, addenda(expected))(fail)
// The check that inferred <: expected is done after recheck so that it
// does not interfere with normal rechecking by constraining capture set variables.
}
Expand Down
72 changes: 24 additions & 48 deletions tests/neg-custom-args/captures/i15116.check
Original file line number Diff line number Diff line change
@@ -1,72 +1,48 @@
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15116.scala:5:13 ----------------------------------------
-- Error: tests/neg-custom-args/captures/i15116.scala:5:13 -------------------------------------------------------------
5 | val x = Foo(m) // error
| ^^^^^^
| Found: test.Foo{val m: (Bar.this.m² : test.STR^)}^{Bar.this.m²}
| Required: test.Foo
| value x needs an explicit type because the inferred type does not conform to
| the type that is externally visible in other compilation units.
|
| Inferred type : test.Foo{val m: (Bar.this.m² : test.STR^)}^{Bar.this.m²}
| Externally visible type: test.Foo
|
| where: ^ refers to a fresh root capability in the type of value m²
| m is a value in class Foo
| m² is a value in class Bar
|
|
| Note that the expected type test.Foo
| is the previously inferred type of value x
| which is also the type seen in separately compiled sources.
| The new inferred type test.Foo{val m: (Bar.this.m : test.STR^)}^{Bar.this.m}
| must conform to this type.
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15116.scala:7:13 ----------------------------------------
-- Error: tests/neg-custom-args/captures/i15116.scala:7:13 -------------------------------------------------------------
7 | val x = Foo(m) // error
| ^^^^^^
| Found: test.Foo{val m: (Baz.this.m² : test.STR^)}^{Baz.this.m²}
| Required: test.Foo
| value x needs an explicit type because the inferred type does not conform to
| the type that is externally visible in other compilation units.
|
| Inferred type : test.Foo{val m: (Baz.this.m² : test.STR^)}^{Baz.this.m²}
| Externally visible type: test.Foo
|
| where: ^ refers to a fresh root capability in the type of value m²
| m is a value in class Foo
| m² is a value in trait Baz
|
|
| Note that the expected type test.Foo
| is the previously inferred type of value x
| which is also the type seen in separately compiled sources.
| The new inferred type test.Foo{val m: (Baz.this.m : test.STR^)}^{Baz.this.m}
| must conform to this type.
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15116.scala:9:13 ----------------------------------------
-- Error: tests/neg-custom-args/captures/i15116.scala:9:13 -------------------------------------------------------------
9 | val x = Foo(m) // error
| ^^^^^^
| Found: test.Foo{val m: (Bar1.this.m² : test.STR^)}^{Bar1.this.m²}
| Required: test.Foo
| value x needs an explicit type because the inferred type does not conform to
| the type that is externally visible in other compilation units.
|
| Inferred type : test.Foo{val m: (Bar1.this.m² : test.STR^)}^{Bar1.this.m²}
| Externally visible type: test.Foo
|
| where: ^ refers to a fresh root capability in the type of value m²
| m is a value in class Foo
| m² is a value in class Bar1
|
|
| Note that the expected type test.Foo
| is the previously inferred type of value x
| which is also the type seen in separately compiled sources.
| The new inferred type test.Foo{val m: (Bar1.this.m : test.STR^)}^{Bar1.this.m}
| must conform to this type.
|
| longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: tests/neg-custom-args/captures/i15116.scala:11:13 ---------------------------------------
-- Error: tests/neg-custom-args/captures/i15116.scala:11:13 ------------------------------------------------------------
11 | val x = Foo(m) // error
| ^^^^^^
| Found: test.Foo{val m: (Baz2.this.m² : test.STR^)}^{Baz2.this.m²}
| Required: test.Foo
| value x needs an explicit type because the inferred type does not conform to
| the type that is externally visible in other compilation units.
|
| Inferred type : test.Foo{val m: (Baz2.this.m² : test.STR^)}^{Baz2.this.m²}
| Externally visible type: test.Foo
|
| where: ^ refers to a fresh root capability in the type of value m²
| m is a value in class Foo
| m² is a value in trait Baz2
|
|
| Note that the expected type test.Foo
| is the previously inferred type of value x
| which is also the type seen in separately compiled sources.
| The new inferred type test.Foo{val m: (Baz2.this.m : test.STR^)}^{Baz2.this.m}
| must conform to this type.
|
| longer explanation available when compiling with `-explain`