Skip to content

Commit 1a16654

Browse files
committed
Improve error message about missing type of context function parameter
Also, change formatting of msg closer to what it was before. I find that more legible.
1 parent 38559d7 commit 1a16654

File tree

6 files changed

+58
-32
lines changed

6 files changed

+58
-32
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Contexts._
77
import Decorators._, Symbols._, Names._, NameOps._, Types._, Flags._, Phases._
88
import Denotations.SingleDenotation
99
import SymDenotations.SymDenotation
10-
import NameKinds.WildcardParamName
10+
import NameKinds.{WildcardParamName, ContextFunctionParamName}
1111
import parsing.Scanners.Token
1212
import parsing.Tokens
1313
import printing.Highlighting._
@@ -166,21 +166,24 @@ class AnonymousFunctionMissingParamType(param: untpd.ValDef,
166166
(using Context)
167167
extends TypeMsg(AnonymousFunctionMissingParamTypeID) {
168168
def msg(using Context) = {
169-
val ofFun =
169+
val paramDescription =
170170
if param.name.is(WildcardParamName)
171-
|| (MethodType.syntheticParamNames(tree.args.length + 1) contains param.name)
172-
then i"\n\nIn expanded function:\n$tree"
171+
|| param.name.is(ContextFunctionParamName)
172+
|| MethodType.syntheticParamNames(tree.args.length + 1).contains(param.name)
173+
then i"\nin expanded function:\n $tree"
173174
else ""
174175

175176
val inferred =
176-
if (inferredType == WildcardType) ""
177-
else i"\n\nPartially inferred type for the parameter: $inferredType"
177+
if inferredType == WildcardType then ""
178+
else i"\nWhat I could infer was: $inferredType"
178179

179180
val expected =
180-
if (expectedType == WildcardType) ""
181-
else i"\n\nExpected type for the whole anonymous function: $expectedType"
181+
if expectedType == WildcardType then ""
182+
else i"\nExpected type for the whole anonymous function:\n $expectedType"
182183

183-
i"Could not infer type for parameter ${param.name} of anonymous function$ofFun$inferred$expected"
184+
i"""Missing parameter type
185+
|
186+
|I could not infer the type of the parameter ${param.name}$paramDescription$inferred$expected"""
184187
}
185188

186189
def explain(using Context) = ""

tests/neg/i11350.check

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
-- [E081] Type Error: tests/neg/i11350.scala:1:39 ----------------------------------------------------------------------
22
1 |class A1[T](action: A1[T] ?=> String = "") // error
33
| ^
4-
| Could not infer type for parameter contextual$1 of anonymous function
4+
| Missing parameter type
55
|
6-
| Partially inferred type for the parameter: A1[<?>]
7-
|
8-
| Expected type for the whole anonymous function: (A1[<?>]) ?=> String
6+
| I could not infer the type of the parameter contextual$1
7+
| in expanded function:
8+
| contextual$1 ?=> ""
9+
| What I could infer was: A1[<?>]
10+
| Expected type for the whole anonymous function:
11+
| (A1[<?>]) ?=> String
912
-- [E081] Type Error: tests/neg/i11350.scala:2:39 ----------------------------------------------------------------------
1013
2 |class A2[T](action: A1[T] ?=> String = summon[A1[T]]) // error
1114
| ^
12-
| Could not infer type for parameter contextual$2 of anonymous function
13-
|
14-
| Partially inferred type for the parameter: A1[<?>]
15+
| Missing parameter type
1516
|
16-
| Expected type for the whole anonymous function: (A1[<?>]) ?=> String
17+
| I could not infer the type of the parameter contextual$2
18+
| in expanded function:
19+
| contextual$2 ?=> summon[A1[T]]
20+
| What I could infer was: A1[<?>]
21+
| Expected type for the whole anonymous function:
22+
| (A1[<?>]) ?=> String

tests/neg/i11561.check

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
-- [E081] Type Error: tests/neg/i11561.scala:2:32 ----------------------------------------------------------------------
22
2 | val updateText1 = copy(text = _) // error
33
| ^
4-
| Could not infer type for parameter _$1 of anonymous function
4+
| Missing parameter type
55
|
6-
| In expanded function:
7-
| _$1 => State.this.text = _$1
8-
|
9-
| Expected type for the whole anonymous function: String
6+
| I could not infer the type of the parameter _$1
7+
| in expanded function:
8+
| _$1 => State.this.text = _$1
9+
| Expected type for the whole anonymous function:
10+
| String
1011
-- [E052] Type Error: tests/neg/i11561.scala:3:30 ----------------------------------------------------------------------
1112
3 | val updateText2 = copy(text = (_: String)) // error
1213
| ^^^^^^^^^^^^^^^^^^

tests/neg/i17183.check

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
-- [E081] Type Error: tests/neg/i17183.scala:11:24 ---------------------------------------------------------------------
22
11 |def test = Context(f = (_, _) => ???) // error // error
33
| ^
4-
| Could not infer type for parameter _$1 of anonymous function
4+
| Missing parameter type
55
|
6-
| In expanded function:
7-
| (_$1, _$2) => ???
8-
|
9-
| Expected type for the whole anonymous function: MyFunc
6+
| I could not infer the type of the parameter _$1
7+
| in expanded function:
8+
| (_$1, _$2) => ???
9+
| Expected type for the whole anonymous function:
10+
| MyFunc
1011
-- [E081] Type Error: tests/neg/i17183.scala:11:27 ---------------------------------------------------------------------
1112
11 |def test = Context(f = (_, _) => ???) // error // error
1213
| ^
13-
| Could not infer type for parameter _$2 of anonymous function
14-
|
15-
| In expanded function:
16-
| (_$1, _$2) => ???
14+
| Missing parameter type
1715
|
18-
| Expected type for the whole anonymous function: MyFunc
16+
| I could not infer the type of the parameter _$2
17+
| in expanded function:
18+
| (_$1, _$2) => ???
19+
| Expected type for the whole anonymous function:
20+
| MyFunc

tests/neg/i18188.check

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- [E081] Type Error: tests/neg/i18188.scala:3:39 ----------------------------------------------------------------------
2+
3 |class A1[T](action: A1[T] ?=> String = "") // error
3+
| ^
4+
| Missing parameter type
5+
|
6+
| I could not infer the type of the parameter contextual$1
7+
| in expanded function:
8+
| contextual$1 ?=> ""
9+
| What I could infer was: dotty.tools.dotc.typer.A1[<?>]
10+
| Expected type for the whole anonymous function:
11+
| (dotty.tools.dotc.typer.A1[<?>]) ?=> String

tests/neg/i18188.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package dotty.tools.dotc.typer
2+
3+
class A1[T](action: A1[T] ?=> String = "") // error

0 commit comments

Comments
 (0)