Skip to content

Commit d9fe503

Browse files
committed
Add a warning message for a pointless applied constructor type use
1 parent f7f49f9 commit d9fe503

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-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
@@ -225,6 +225,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
225225
case FormatInterpolationErrorID // errorNumber: 209
226226
case ValueClassCannotExtendAliasOfAnyValID // errorNumber: 210
227227
case MatchIsNotPartialFunctionID // errorNumber: 211
228+
case PointlessAppliedConstructorTypeID // errorNumber: 212
228229

229230
def errorNumber = ordinal - 1
230231

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3487,3 +3487,19 @@ class MatchIsNotPartialFunction(using Context) extends SyntaxMsg(MatchIsNotParti
34873487
|
34883488
|Efficient operations will use `applyOrElse` to avoid computing the match twice,
34893489
|but the `apply` body would be executed "per element" in the example."""
3490+
def msg(using Context) = errorText
3491+
def explain(using Context) = ""
3492+
3493+
final class PointlessAppliedConstructorType(tpt: untpd.Tree, args: List[untpd.Tree], tpe: Type)(using Context) extends TypeMsg(PointlessAppliedConstructorTypeID):
3494+
override protected def msg(using Context): String =
3495+
val act = i"$tpt(${args.map(_.show).mkString(", ")})"
3496+
i"""|Applied constructor type $act has no effect.
3497+
|The resulting type of $act is the same as its base type, namely: $tpe""".stripMargin
3498+
3499+
override protected def explain(using Context): String =
3500+
i"""|Applied constructor types are used to ascribe specialized types of constructor applications.
3501+
|To benefit from this feature, the constructor in question has to have a more specific type than the class itself.
3502+
|
3503+
|If you want to track a precise type of any of the class parameters, make sure to mark the parameter as `tracked`.
3504+
|Otherwise, you can safely remove the argument list from the type.
3505+
|"""

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,8 +1722,8 @@ trait Applications extends Compatibility {
17221722
def apply(tp: Type) = mapOver(tp.widenSkolem)
17231723
val preciseTp = widenSkolemsMap(tree1.tpe)
17241724
val classTp = typedType(tpt).tpe
1725-
if preciseTp frozen_=:= classTp then
1726-
report.warning(em"Blop blop")
1725+
if !preciseTp.isError && (preciseTp frozen_=:= classTp) then
1726+
report.warning(PointlessAppliedConstructorType(tpt, tree.args, classTp), tree.srcPos)
17271727
TypeTree(preciseTp)
17281728

17291729
/** 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)