Skip to content

Commit d709c85

Browse files
committed
Add a warning message for a pointless applied constructor type use
1 parent 92ba51d commit d709c85

File tree

5 files changed

+24
-6
lines changed

5 files changed

+24
-6
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
223223
case IllegalUnrollPlacementID // errorNumber: 207
224224
case ExtensionHasDefaultID // errorNumber: 208
225225
case FormatInterpolationErrorID // errorNumber: 209
226+
case PointlessAppliedConstructorTypeID // errorNumber: 210
226227

227228
def errorNumber = ordinal - 1
228229

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3449,3 +3449,17 @@ class BadFormatInterpolation(errorText: String)(using Context) extends Message(F
34493449
def kind = MessageKind.Interpolation
34503450
def msg(using Context) = errorText
34513451
def explain(using Context) = ""
3452+
3453+
final class PointlessAppliedConstructorType(tpt: untpd.Tree, args: List[untpd.Tree], tpe: Type)(using Context) extends TypeMsg(PointlessAppliedConstructorTypeID):
3454+
override protected def msg(using Context): String =
3455+
val act = i"$tpt(${args.map(_.show).mkString(", ")})"
3456+
i"""|Applied constructor type $act has no effect.
3457+
|The resulting type of $act is the same as its base type, namely: $tpe""".stripMargin
3458+
3459+
override protected def explain(using Context): String =
3460+
i"""|Applied constructor types are used to ascribe specialized types of constructor applications.
3461+
|To benefit from this feature, the constructor in question has to have a more specific type than the class itself.
3462+
|
3463+
|If you want to track a precise type of any of the class parameters, make sure to mark the parameter as `tracked`.
3464+
|Otherwise, you can safely remove the argument list from the type.
3465+
|"""

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

Lines changed: 2 additions & 2 deletions
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`?

tests/neg/applied_constructor_types.check

Lines changed: 0 additions & 3 deletions
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
Lines changed: 7 additions & 1 deletion
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)