Skip to content

Commit c2fd6a2

Browse files
committed
Add a warning message for a pointless applied constructor type use
1 parent 5f09ece commit c2fd6a2

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

Diff for: compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

+1
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
221221
case GivenSearchPriorityID // errorNumber: 205
222222
case EnumMayNotBeValueClassesID // errorNumber: 206
223223
case IllegalUnrollPlacementID // errorNumber: 207
224+
case PointlessAppliedConstructorTypeID // errorNumber: 208
224225

225226
def errorNumber = ordinal - 1
226227

Diff for: compiler/src/dotty/tools/dotc/reporting/messages.scala

+14
Original file line numberDiff line numberDiff line change
@@ -3433,3 +3433,17 @@ extends DeclarationMsg(IllegalUnrollPlacementID):
34333433

34343434
def explain(using Context) = ""
34353435
end IllegalUnrollPlacement
3436+
3437+
final class PointlessAppliedConstructorType(tpt: untpd.Tree, args: List[untpd.Tree], tpe: Type)(using Context) extends TypeMsg(PointlessAppliedConstructorTypeID):
3438+
override protected def msg(using Context): String =
3439+
val act = i"$tpt(${args.map(_.show).mkString(", ")})"
3440+
i"""|Applied constructor type $act has no effect.
3441+
|The resulting type of $act is the same as its base type, namely: $tpe""".stripMargin
3442+
3443+
override protected def explain(using Context): String =
3444+
i"""|Applied constructor types are used to ascribe specialized types of constructor applications.
3445+
|To benefit from this feature, the constructor in question has to have a more specific type than the class itself.
3446+
|
3447+
|If you want to track a precise type of any of the class parameters, make sure to mark the parameter as `tracked`.
3448+
|Otherwise, you can safely remove the argument list from the type.
3449+
|"""

Diff for: compiler/src/dotty/tools/dotc/typer/Applications.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1701,8 +1701,8 @@ trait Applications extends Compatibility {
17011701
def apply(tp: Type) = mapOver(tp.widenSkolem)
17021702
val preciseTp = widenSkolemsMap(tree1.tpe)
17031703
val classTp = typedType(tpt).tpe
1704-
if preciseTp frozen_=:= classTp then
1705-
report.warning(em"Blop blop")
1704+
if !preciseTp.isError && (preciseTp frozen_=:= classTp) then
1705+
report.warning(PointlessAppliedConstructorType(tpt, tree.args, classTp), tree.srcPos)
17061706
TypeTree(preciseTp)
17071707

17081708
/** Is given method reference applicable to argument trees `args`?

Diff for: tests/neg/applied_constructor_types.check

-3
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44
| Not found: type f
55
|
66
| longer explanation available when compiling with `-explain`
7-
Blop blop
87
-- [E006] Not Found Error: tests/neg/applied_constructor_types.scala:9:10 ----------------------------------------------
98
9 | val v2: id(1) = f(1) // error
109
| ^^
1110
| Not found: type id - did you mean is?
1211
|
1312
| longer explanation available when compiling with `-explain`
14-
Blop blop
1513
-- [E006] Not Found Error: tests/neg/applied_constructor_types.scala:10:10 ---------------------------------------------
1614
10 | val v3: idDependent(1) = f(1) // error
1715
| ^^^^^^^^^^^
1816
| Not found: type idDependent
1917
|
2018
| longer explanation available when compiling with `-explain`
21-
Blop blop

Diff for: tests/warn/applied_constructor_types.check

+7-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
TODO
1+
-- [E208] Type Warning: tests/warn/applied_constructor_types.scala:6:10 ------------------------------------------------
2+
6 | val v1: UnspecificBox(4) = UnspecificBox(4) // warn
3+
| ^^^^^^^^^^^^^^^^
4+
| Applied constructor type UnspecificBox(4) has no effect.
5+
| The resulting type of UnspecificBox(4) is the same as its base type, namely: UnspecificBox
6+
|
7+
| longer explanation available when compiling with `-explain`

0 commit comments

Comments
 (0)