Skip to content

Commit

Permalink
Make message more obvious in the myopic case
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Oct 22, 2024
1 parent 82aab28 commit 81291f1
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
case NonNamedArgumentInJavaAnnotationID // errorNumber: 201
case QuotedTypeMissingID // errorNumber: 202
case AmbiguousNamedTupleAssignmentID // errorNumber: 203
case DeprecatedNamedInfixArgID // errorNumber: 204 - used ONLY in LTS

def errorNumber = ordinal - 1

Expand Down
8 changes: 7 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ object ErrorReporting {
case tp => i" and expected result type $tp"
}
i"(${tp.typedArgs().tpes}%, %)$result"
s"arguments ${argStr(tp)}"
def hasNames = tp.args.exists:
case tree: untpd.Tuple => tree.trees.exists:
case NamedArg(_, _) => true
case _ => false
case _ => false
val addendum = if hasNames then " (a named tuple)" else ""
s"arguments ${argStr(tp)}$addendum"
case _ =>
i"expected type $tp"
}
Expand Down
5 changes: 3 additions & 2 deletions docs/_docs/reference/other-new-features/named-tuples.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ val persons: List[Person] = ...
val minors = persons.filter: p =>
p.age < 18
```
Named bindings in tuples are similar to function parameters and arguments. We use `name: Type` for element types and `name = value` for element values. It is illegal to mix named and unnamed elements in a tuple, or to use the same same
name for two different elements.
Named bindings in tuples are similar to function parameters and arguments.
We use `name: Type` for element types and `name = value` for element values.
It is illegal to mix named and unnamed elements in a tuple, or to use the same name for two different elements.

Fields of named tuples can be selected by their name, as in the line `p.age < 18` above.

Expand Down
13 changes: 13 additions & 0 deletions tests/neg/infix-named-args.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- [E134] Type Error: tests/neg/infix-named-args.scala:2:13 ------------------------------------------------------------
2 | def f = 42 + (x = 1) // error // a named tuple!
| ^^^^
| None of the overloaded alternatives of method + in class Int with types
| (x: Double): Double
| (x: Float): Float
| (x: Long): Long
| (x: Int): Int
| (x: Char): Int
| (x: Short): Int
| (x: Byte): Int
| (x: String): String
| match arguments ((x : Int)) (a named tuple)
7 changes: 7 additions & 0 deletions tests/neg/infix-named-args.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class C {
def f = 42 + (x = 1) // error // a named tuple!
def multi(x: Int, y: Int): Int = x + y
def **(x: Int, y: Int): Int = x + y
def g = new C() `multi` (x = 42, y = 27) // werror // werror // not actually a tuple! appearances to the contrary
def h = new C() ** (x = 42, y = 27) // werror // werror
}
7 changes: 0 additions & 7 deletions tests/warn/infix-named-args.scala

This file was deleted.

0 comments on commit 81291f1

Please sign in to comment.