-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Only rename method type params shadowing enclosing class type params #24684
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
Merged
+157
−11
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a93c849
Only rename method type params shadowing enclosing class type params
tanishiking 3d4d397
refactor: use TypeTraverser to collect used type params
tanishiking 9b85e44
Add another test case that shadows class's type param
tanishiking 7cc63d4
Use Type.foreachPart collectUsedTypeParams
tanishiking e7fa00c
refactor: simplify collectUsedTypeParams
tanishiking 1e392ee
Add regression tests for #24619
tanishiking File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| noShadow: public scala.Tuple3<T, U, V> Outer$Middle$Inner.noShadow(T,U,V) | ||
| shadowOuter: public <T1> T Outer$Middle$Inner.shadowOuter() | ||
| shadowMiddle: public <U1> U Outer$Middle$Inner.shadowMiddle() | ||
| shadowInner: public <V1> V Outer$Middle$Inner.shadowInner() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| class Outer[T]: | ||
| def outerValue: T = ??? | ||
|
|
||
| class Middle[U]: | ||
| def middleValue: U = ??? | ||
|
|
||
| class Inner[V]: | ||
| def innerValue: V = ??? | ||
| def noShadow(x: T, y: U, z: V): (T, U, V) = (x, y, z) | ||
| def shadowOuter[T] = outerValue | ||
| def shadowMiddle[U] = middleValue | ||
| def shadowInner[V] = innerValue | ||
|
|
||
| @main def Test(): Unit = | ||
| val innerMethods = classOf[Outer[_]].getDeclaredClasses() | ||
| .find(_.getName.contains("Middle")).get.getDeclaredClasses() | ||
| .find(_.getName.contains("Inner")).get.getDeclaredMethods() | ||
|
|
||
| printMethodSig(innerMethods, "noShadow") | ||
| printMethodSig(innerMethods, "shadowOuter") | ||
| printMethodSig(innerMethods, "shadowMiddle") | ||
| printMethodSig(innerMethods, "shadowInner") | ||
|
|
||
| def printMethodSig(methods: Array[java.lang.reflect.Method], name: String): Unit = | ||
| methods.find(_.getName.endsWith(name)).foreach { m => | ||
| println(s"$name: ${m.toGenericString}") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| bar: public <T> T Foo.bar(T) | ||
| baz: public <T> T Foo.baz(T,Foo<T>) | ||
| qux: public <U> U Foo.qux(U,Foo<T>) | ||
| quux: public <T,U> scala.Tuple2<T, U> Foo.quux(T,U,Foo<T>) | ||
| copy: public <T> Bar<T> Bar.copy(T) | ||
| copy$default$1: public <T1> T Bar.copy$default$1() | ||
| m: public <T1> T C.m() | ||
| compose: public <A1> scala.Function1<A1, B> JavaPartialFunction.compose(scala.Function1<A1, A>) | ||
| compose: public <R> scala.PartialFunction<R, B> JavaPartialFunction.compose(scala.PartialFunction<R, A>) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // All of type params shouldn't rename | ||
| class Foo[T]: | ||
| def bar[T](x: T): T = x | ||
| def baz[T](x: T, y: Foo[T]): T = x | ||
| def qux[U](x: U, y: Foo[T]): U = x | ||
| def quux[T, U](x: T, y: U, z: Foo[T]): (T, U) = (x, y) | ||
|
|
||
| // https://github.com/scala/scala3/issues/24671 | ||
| final case class Bar[+T](t: T) | ||
|
|
||
| // `m: public <T1> T C.m()` rather than `m: public <T> T C.m()` | ||
| // that was wrong and could be crashed with | ||
| // `val c = C[String]; String x = c.<Object>m();`. | ||
| abstract class C[T]: | ||
| def x: T | ||
| def m[T] = x | ||
|
|
||
| // https://github.com/scala/scala3/issues/24134 | ||
| // The mixin forwarders for compose in Function1 method has signature | ||
| // `def compose[A](g: A => T1): A => R` | ||
| // Where the JavaPartialFunction[A, B] has type parameter A (name clash), | ||
| // The type parameter A in method should be renamed to avoid name duplication. | ||
| abstract class JavaPartialFunction[A, B] extends PartialFunction[A, B] | ||
|
|
||
| @main def Test(): Unit = | ||
| val fooMethods = classOf[Foo[_]].getDeclaredMethods() | ||
| printMethodSig(fooMethods, "bar") | ||
| printMethodSig(fooMethods, "baz") | ||
| printMethodSig(fooMethods, "qux") | ||
| printMethodSig(fooMethods, "quux") | ||
|
|
||
| val barMethods = classOf[Bar[_]].getDeclaredMethods() | ||
| printMethodSig(barMethods, "copy") | ||
| // copy$default$1 have `<T1> T Bar.copy$default$1` rather than `<T> T Bar.copy$default$1` | ||
| // as reported in https://github.com/scala/scala3/issues/24671 | ||
| // The type parameter rename occurs because the return type T refers the enclosing class's type param T. | ||
| printMethodSig(barMethods, "copy$default$1") | ||
|
|
||
| val cMethods = classOf[C[_]].getDeclaredMethods() | ||
| printMethodSig(cMethods, "m") | ||
|
|
||
| val jpfMethods = classOf[JavaPartialFunction[_, _]].getDeclaredMethods() | ||
| printMethodSigs(jpfMethods, "compose") | ||
|
|
||
| def printMethodSig(methods: Array[java.lang.reflect.Method], name: String): Unit = | ||
| methods.find(_.getName.endsWith(name)).foreach { m => | ||
| println(s"$name: ${m.toGenericString}") | ||
| } | ||
|
|
||
| def printMethodSigs(methods: Array[java.lang.reflect.Method], name: String): Unit = | ||
| methods.filter(_.getName == name).sortBy(_.toGenericString).foreach { m => | ||
| println(s"$name: ${m.toGenericString}") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| class Container[A]: | ||
| abstract class JavaPartialFunction[B] extends PartialFunction[A, B] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class Main { | ||
| public static void main(String[] args) { | ||
| Container<String> container = new Container<>(); | ||
| Container<String>.JavaPartialFunction<Integer> pf = container.new JavaPartialFunction<Integer>() { | ||
| @Override | ||
| public boolean isDefinedAt(String x) { | ||
| return x != null && !x.isEmpty(); | ||
| } | ||
| @Override | ||
| public Integer apply(String x) { | ||
| return x.length(); | ||
| } | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.