Skip to content

Commit 14307b2

Browse files
authored
Backport "Improve parameter type inference error messaging" to LTS (#19119)
Backports #18190 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 202e0d2 + c408bd2 commit 14307b2

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,14 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
196196
case AmbiguousExtensionMethodID // errorNumber 180
197197
case UnqualifiedCallToAnyRefMethodID // errorNumber: 181
198198
case NotConstantID // errorNumber: 182
199+
case ClosureCannotHaveInternalParameterDependenciesID // errorNumber: 183
200+
case MatchTypeNoCasesID // errorNumber: 184
201+
case UnimportedAndImportedID // errorNumber: 185
202+
case ImplausiblePatternWarningID // errorNumber: 186
199203
case SynchronizedCallOnBoxedClassID // errorNumber: 187
204+
case VarArgsParamCannotBeGivenID // errorNumber: 188
205+
case ExtractorNotFoundID // errorNumber: 189
206+
case PureUnitExpressionID // errorNumber: 190
200207

201208
def errorNumber = ordinal - 1
202209

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,23 +160,27 @@ extends SyntaxMsg(CaseClassMissingParamListID) {
160160

161161
class AnonymousFunctionMissingParamType(param: untpd.ValDef,
162162
tree: untpd.Function,
163-
pt: Type)
163+
inferredType: Type,
164+
expectedType: Type,
165+
)
164166
(using Context)
165167
extends TypeMsg(AnonymousFunctionMissingParamTypeID) {
166168
def msg(using Context) = {
167169
val ofFun =
168170
if param.name.is(WildcardParamName)
169171
|| (MethodType.syntheticParamNames(tree.args.length + 1) contains param.name)
170-
then i" of expanded function:\n$tree"
172+
then i"\n\nIn expanded function:\n$tree"
171173
else ""
172174

173175
val inferred =
174-
if (pt == WildcardType) ""
175-
else i"\nWhat I could infer was: $pt"
176+
if (inferredType == WildcardType) ""
177+
else i"\n\nPartially inferred type for the parameter: $inferredType"
176178

177-
i"""Missing parameter type
178-
|
179-
|I could not infer the type of the parameter ${param.name}$ofFun.$inferred"""
179+
val expected =
180+
if (expectedType == WildcardType) ""
181+
else i"\n\nExpected type for the whole anonymous function: $expectedType"
182+
183+
i"Could not infer type for parameter ${param.name} of anonymous function$ofFun$inferred$expected"
180184
}
181185

182186
def explain(using Context) = ""

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1612,7 +1612,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
16121612
else
16131613
inferredFromTarget(param, formal, calleeType, isErased, paramIndex).orElse(
16141614
if knownFormal then formal0
1615-
else errorType(AnonymousFunctionMissingParamType(param, tree, formal), param.srcPos)
1615+
else errorType(AnonymousFunctionMissingParamType(param, tree, inferredType = formal, expectedType = pt), param.srcPos)
16161616
)
16171617
val paramTpt = untpd.TypedSplice(
16181618
(if knownFormal then InferredTypeTree() else untpd.TypeTree())

tests/neg/i11350.check

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
-- [E081] Type Error: tests/neg/i11350.scala:1:39 ----------------------------------------------------------------------
22
1 |class A1[T](action: A1[T] ?=> String = "") // error
33
| ^
4-
| Missing parameter type
4+
| Could not infer type for parameter evidence$1 of anonymous function
55
|
6-
| I could not infer the type of the parameter evidence$1.
7-
| What I could infer was: A1[<?>]
6+
| Partially inferred type for the parameter: A1[<?>]
7+
|
8+
| Expected type for the whole anonymous function: (A1[<?>]) ?=> String
89
-- [E081] Type Error: tests/neg/i11350.scala:2:39 ----------------------------------------------------------------------
910
2 |class A2[T](action: A1[T] ?=> String = summon[A1[T]]) // error
1011
| ^
11-
| Missing parameter type
12+
| Could not infer type for parameter evidence$2 of anonymous function
13+
|
14+
| Partially inferred type for the parameter: A1[<?>]
1215
|
13-
| I could not infer the type of the parameter evidence$2.
14-
| What I could infer was: A1[<?>]
16+
| Expected type for the whole anonymous function: (A1[<?>]) ?=> String

tests/neg/i11561.check

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

0 commit comments

Comments
 (0)