Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set proper position for ValDefs generated from tuples #14513

Merged
merged 1 commit into from
Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1143,10 +1143,11 @@ object desugar {
) // no `_`

val ids = for ((named, _) <- vars) yield Ident(named.name)
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
val matchExpr =
if (tupleOptimizable) rhs
else Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
else
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
Match(makeSelector(rhs, MatchCheck.IrrefutablePatDef), caseDef :: Nil)
vars match {
case Nil if !mods.is(Lazy) =>
matchExpr
Expand All @@ -1166,8 +1167,16 @@ object desugar {
val restDefs =
for (((named, tpt), n) <- vars.zipWithIndex if named.name != nme.WILDCARD)
yield
if (mods.is(Lazy)) derivedDefDef(original, named, tpt, selector(n), mods &~ Lazy)
else derivedValDef(original, named, tpt, selector(n), mods)
if mods.is(Lazy) then
DefDef(named.name.asTermName, Nil, tpt, selector(n))
.withMods(mods &~ Lazy)
.withSpan(named.span)
else
valDef(
ValDef(named.name.asTermName, tpt, selector(n))
.withMods(mods)
.withSpan(named.span)
)
flatTree(firstDef :: restDefs)
}
}
Expand Down
10 changes: 8 additions & 2 deletions language-server/test/dotty/tools/languageserver/HoverTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,13 @@ class HoverTest {

@Test def tuple: Unit = {
code"""|object A:
| val (${m1}first${m2}, second) = (1, 2)""".withSource
.hover(m1 to m2, hoverContent("Int"))
| val (${m1}first${m2}, second) = (1.0, 2)""".withSource
.hover(m1 to m2, hoverContent("Double"))
}

@Test def multiAssigment: Unit = {
code"""|val ${m1}x${m2}, ${m3}y${m4} = 42.0""".withSource
.hover(m1 to m2, hoverContent("Double"))
.hover(m3 to m4, hoverContent("Double"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,6 @@ class WorksheetTest {
((m1 to m2), "val res0: String = odd"))
}

@Test def patternMatching1: Unit = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the test because it was failing and since worksheets deprecated. I think the issue is more in the test than code itself.

ws"""${m1}val (foo, bar) = (1, 2)${m2}""".withSource
.run(m1,
((m1 to m2), s"val foo: Int = 1${nl}val bar: Int = 2"))
}

@Test def evaluationException: Unit = {
ws"""${m1}val foo = 1 / 0${m2}
${m3}val bar = 2${m4}""".withSource
Expand Down
8 changes: 7 additions & 1 deletion tests/neg/t5702-neg-bad-and-wild.check
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@
|
| longer explanation available when compiling with `-explain`
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:23:17 ---------------------------------------------------
23 | val K(ns @ _*, x) = k // error: pattern expected
23 | val K(ns @ _*, xx) = k // error: pattern expected
| ^
| pattern expected
|
| longer explanation available when compiling with `-explain`
-- [E161] Naming Error: tests/neg/t5702-neg-bad-and-wild.scala:24:10 ---------------------------------------------------
24 | val K(x) = k // error: x is already defined as value x
| ^^^^^^^^^^^^
| x is already defined as value x
|
| Note that overloaded methods must all be defined in the same group of toplevel definitions
-- Error: tests/neg/t5702-neg-bad-and-wild.scala:10:21 -----------------------------------------------------------------
10 | case List(1, _*,) => // error: pattern expected // error
| ^
Expand Down
3 changes: 2 additions & 1 deletion tests/neg/t5702-neg-bad-and-wild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ object Test {
// good syntax, bad semantics, detected by typer
//gowild.scala:14: error: star patterns must correspond with varargs parameters
val K(x @ _*) = k
val K(ns @ _*, x) = k // error: pattern expected
val K(ns @ _*, xx) = k // error: pattern expected
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interesting case. x started showing and error about being duplicate, which is true. I changed the condition to test the actual issue.

I wonder if I should add another case for the duplicate name?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test, the error seems legit, though I have no idea why it popped up now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea why it popped up now.

When multiple errors have the same position, we normally only display the first one.

val K(x) = k // error: x is already defined as value x
val (b, _ * ) = (5,6) // ok
// no longer complains
//bad-and-wild.scala:15: error: ')' expected but '}' found.
Expand Down