Skip to content

Commit 769cf0a

Browse files
authored
Merge pull request #429 from scala/backport-lts-3.3-23161
Backport "Fix SemantiDB production of method signature with shadowed parameters" to 3.3 LTS
2 parents c0cd408 + 04d4dc2 commit 769cf0a

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

compiler/src/dotty/tools/dotc/semanticdb/TypeOps.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ class TypeOps:
9696
// We try to find the "actual" binder of <y>: `inner`,
9797
// and register them to the symbol table with `(<y>, inner) -> <y>`
9898
// instead of `("y", outer) -> <y>`
99-
if lam.paramNames.contains(sym.name) then
99+
// We must also check for parameter shadowing such as def shadowParam(x: Int) = {val x = true}
100+
// We skip param symbol check if owner is not a LambdaType for proper MatchType paramRef entry in the paramRefSymtab
101+
// for more information: https://github.com/scala/scala3/pull/23161#discussion_r2097755983
102+
103+
if (sym.is(Flags.Param) || !sym.owner.info.isInstanceOf[LambdaType]) && lam.paramNames.contains(sym.name) then
100104
paramRefSymtab((lam, sym.name)) = sym
101105
else
102106
enterParamRef(lam.resType)

compiler/test/dotty/tools/dotc/config/ScalaSettingsTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class ScalaSettingsTests:
201201

202202
@Test def `illegal source versions are not accepted when parsing the settings`: Unit =
203203
for source <- SourceVersion.illegalInSettings do
204-
val settings = ScalaSettings
204+
val settings = new ScalaSettings
205205
val result = settings.processArguments(List("-source", source.toString()), true)
206206
assertEquals(0, result.warnings.length)
207207
assertEquals(1, result.errors.length)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package example
2+
3+
class Shadow/*<-example::Shadow#*/{
4+
5+
def shadowParam/*<-example::Shadow#shadowParam().*/(x/*<-example::Shadow#shadowParam().(x)*/: Int/*->scala::Int#*/)= {
6+
val x/*<-local0*/ = true
7+
}
8+
9+
def curriedParams/*<-example::Shadow#curriedParams().*/(x/*<-example::Shadow#curriedParams().(x)*/: Int/*->scala::Int#*/)(y/*<-example::Shadow#curriedParams().(y)*/: List/*->scala::package.List#*/[Int/*->scala::Int#*/]) = {
10+
val x/*<-local1*/ = "shadow"
11+
val y/*<-local2*/ = 1
12+
}
13+
14+
15+
def multiParams/*<-example::Shadow#multiParams().*/(x/*<-example::Shadow#multiParams().(x)*/: List/*->scala::package.List#*/[Int/*->scala::Int#*/], y/*<-example::Shadow#multiParams().(y)*/: String/*->scala::Predef.String#*/) = {
16+
val x/*<-local3*/ = 1
17+
val y/*<-local4*/ = List/*->scala::package.List.*/(1,2,3)
18+
}
19+
20+
21+
def shadowInParamBlock/*<-example::Shadow#shadowInParamBlock().*/(x/*<-example::Shadow#shadowInParamBlock().(x)*/: Int/*->scala::Int#*/, y/*<-example::Shadow#shadowInParamBlock().(y)*/: Int/*->scala::Int#*/) = {
22+
val y/*<-local5*/ = 1
23+
{
24+
val x/*<-local6*/ = 2
25+
}
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package example
2+
3+
class Shadow{
4+
5+
def shadowParam(x: Int)= {
6+
val x = true
7+
}
8+
9+
def curriedParams(x: Int)(y: List[Int]) = {
10+
val x = "shadow"
11+
val y = 1
12+
}
13+
14+
15+
def multiParams(x: List[Int], y: String) = {
16+
val x = 1
17+
val y = List(1,2,3)
18+
}
19+
20+
21+
def shadowInParamBlock(x: Int, y: Int) = {
22+
val y = 1
23+
{
24+
val x = 2
25+
}
26+
}
27+
}

tests/semanticdb/metac.expect

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3446,6 +3446,93 @@ Occurrences:
34463446
[13:17..13:17): <- selfs/C6#`<init>`().
34473447
[13:27..13:28): B -> selfs/B#
34483448

3449+
expect/ShadowedParameters.scala
3450+
-------------------------------
3451+
3452+
Summary:
3453+
Schema => SemanticDB v4
3454+
Uri => ShadowedParameters.scala
3455+
Text => empty
3456+
Language => Scala
3457+
Symbols => 20 entries
3458+
Occurrences => 31 entries
3459+
Diagnostics => 14 entries
3460+
Synthetics => 1 entries
3461+
3462+
Symbols:
3463+
example/Shadow# => class Shadow extends Object { self: Shadow => +5 decls }
3464+
example/Shadow#`<init>`(). => primary ctor <init> (): Shadow
3465+
example/Shadow#curriedParams(). => method curriedParams (param x: Int)(param y: List[Int]): Unit
3466+
example/Shadow#curriedParams().(x) => param x: Int
3467+
example/Shadow#curriedParams().(y) => param y: List[Int]
3468+
example/Shadow#multiParams(). => method multiParams (param x: List[Int], param y: String): Unit
3469+
example/Shadow#multiParams().(x) => param x: List[Int]
3470+
example/Shadow#multiParams().(y) => param y: String
3471+
example/Shadow#shadowInParamBlock(). => method shadowInParamBlock (param x: Int, param y: Int): Unit
3472+
example/Shadow#shadowInParamBlock().(x) => param x: Int
3473+
example/Shadow#shadowInParamBlock().(y) => param y: Int
3474+
example/Shadow#shadowParam(). => method shadowParam (param x: Int): Unit
3475+
example/Shadow#shadowParam().(x) => param x: Int
3476+
local0 => val local x: Boolean
3477+
local1 => val local x: String
3478+
local2 => val local y: Int
3479+
local3 => val local x: Int
3480+
local4 => val local y: List[Int]
3481+
local5 => val local y: Int
3482+
local6 => val local x: Int
3483+
3484+
Occurrences:
3485+
[0:8..0:15): example <- example/
3486+
[2:6..2:12): Shadow <- example/Shadow#
3487+
[4:4..4:4): <- example/Shadow#`<init>`().
3488+
[4:8..4:19): shadowParam <- example/Shadow#shadowParam().
3489+
[4:20..4:21): x <- example/Shadow#shadowParam().(x)
3490+
[4:23..4:26): Int -> scala/Int#
3491+
[5:12..5:13): x <- local0
3492+
[8:8..8:21): curriedParams <- example/Shadow#curriedParams().
3493+
[8:22..8:23): x <- example/Shadow#curriedParams().(x)
3494+
[8:25..8:28): Int -> scala/Int#
3495+
[8:30..8:31): y <- example/Shadow#curriedParams().(y)
3496+
[8:33..8:37): List -> scala/package.List#
3497+
[8:38..8:41): Int -> scala/Int#
3498+
[9:12..9:13): x <- local1
3499+
[10:12..10:13): y <- local2
3500+
[14:8..14:19): multiParams <- example/Shadow#multiParams().
3501+
[14:20..14:21): x <- example/Shadow#multiParams().(x)
3502+
[14:23..14:27): List -> scala/package.List#
3503+
[14:28..14:31): Int -> scala/Int#
3504+
[14:34..14:35): y <- example/Shadow#multiParams().(y)
3505+
[14:37..14:43): String -> scala/Predef.String#
3506+
[15:12..15:13): x <- local3
3507+
[16:12..16:13): y <- local4
3508+
[16:16..16:20): List -> scala/package.List.
3509+
[20:8..20:26): shadowInParamBlock <- example/Shadow#shadowInParamBlock().
3510+
[20:27..20:28): x <- example/Shadow#shadowInParamBlock().(x)
3511+
[20:30..20:33): Int -> scala/Int#
3512+
[20:35..20:36): y <- example/Shadow#shadowInParamBlock().(y)
3513+
[20:38..20:41): Int -> scala/Int#
3514+
[21:12..21:13): y <- local5
3515+
[23:16..23:17): x <- local6
3516+
3517+
Diagnostics:
3518+
[4:20..4:21): [warning] unused explicit parameter
3519+
[5:12..5:13): [warning] unused local definition
3520+
[8:22..8:23): [warning] unused explicit parameter
3521+
[8:30..8:31): [warning] unused explicit parameter
3522+
[9:12..9:13): [warning] unused local definition
3523+
[10:12..10:13): [warning] unused local definition
3524+
[14:20..14:21): [warning] unused explicit parameter
3525+
[14:34..14:35): [warning] unused explicit parameter
3526+
[15:12..15:13): [warning] unused local definition
3527+
[16:12..16:13): [warning] unused local definition
3528+
[20:27..20:28): [warning] unused explicit parameter
3529+
[20:35..20:36): [warning] unused explicit parameter
3530+
[21:12..21:13): [warning] unused local definition
3531+
[23:16..23:17): [warning] unused local definition
3532+
3533+
Synthetics:
3534+
[16:16..16:20):List => *.apply[Int]
3535+
34493536
expect/StructuralTypes.scala
34503537
----------------------------
34513538

0 commit comments

Comments
 (0)