Skip to content

Commit 6792b64

Browse files
authored
Improve error message about missing type of context function parameter (#18788)
Also, change formatting of msg closer to what it was before. I find that more legible.
2 parents 5288dff + 1a16654 commit 6792b64

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._
@@ -167,21 +167,24 @@ class AnonymousFunctionMissingParamType(param: untpd.ValDef,
167167
(using Context)
168168
extends TypeMsg(AnonymousFunctionMissingParamTypeID) {
169169
def msg(using Context) = {
170-
val ofFun =
170+
val paramDescription =
171171
if param.name.is(WildcardParamName)
172-
|| (MethodType.syntheticParamNames(tree.args.length + 1) contains param.name)
173-
then i"\n\nIn expanded function:\n$tree"
172+
|| param.name.is(ContextFunctionParamName)
173+
|| MethodType.syntheticParamNames(tree.args.length + 1).contains(param.name)
174+
then i"\nin expanded function:\n $tree"
174175
else ""
175176

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

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

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

187190
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)