-
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
Open
tanishiking
wants to merge
3
commits into
scala:main
Choose a base branch
from
tanishiking:dont-rename
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+114
−11
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,24 +49,21 @@ object GenericSignatures { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val builder = new StringBuilder(64) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val isTraitSignature = sym0.enclosingClass.is(Trait) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Collect class-level type parameter names to avoid conflicts with method-level type parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val usedNames = collection.mutable.Set.empty[String] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if(sym0.is(Method)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sym0.enclosingClass.typeParams.foreach { tp => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedNames += sanitizeName(tp.name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Track class type parameter names that are shadowed by method type parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Used to trigger renaming of method type parameters to avoid conflicts | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val shadowedClassTypeParamNames = collection.mutable.Set.empty[String] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val methodTypeParamRenaming = collection.mutable.Map.empty[String, String] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def freshTypeParamName(sanitizedName: String): String = { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !usedNames.contains(sanitizedName) then sanitizedName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !shadowedClassTypeParamNames.contains(sanitizedName) then sanitizedName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var i = 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| var newName = sanitizedName + i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while usedNames.contains(newName) do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| while shadowedClassTypeParamNames.contains(newName) do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| i += 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newName = sanitizedName + i | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| methodTypeParamRenaming(sanitizedName) = newName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedNames += newName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shadowedClassTypeParamNames += newName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| newName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -347,7 +344,22 @@ object GenericSignatures { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case mtd: MethodOrPoly => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val (tparams, vparams, rte) = collectMethodParams(mtd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (toplevel && !sym0.isConstructor) polyParamSig(tparams) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (toplevel && !sym0.isConstructor) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (sym0.is(Method)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val (usedMethodTypeParamNames, usedClassTypeParams) = collectUsedTypeParams(vparams :+ rte, sym0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val methodTypeParamNames = tparams.map(tp => sanitizeName(tp.paramName.lastPart)).toSet | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // Only add class type parameters to shadowedClassTypeParamNames if they are: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 1. Referenced in the method signature, AND | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // 2. Shadowed by a method type parameter with the same name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // This will trigger renaming of the method type parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedClassTypeParams.foreach { classTypeParam => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val classTypeParamName = sanitizeName(classTypeParam.name) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if methodTypeParamNames.contains(classTypeParamName) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| shadowedClassTypeParamNames += classTypeParamName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| polyParamSig(tparams) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder.append('(') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for vparam <- vparams do jsig1(vparam) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| builder.append(')') | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -448,6 +460,12 @@ object GenericSignatures { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (initialSymbol.is(Method) && initialSymbol.typeParams.contains(sym)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private def isTypeParameterInMethSig(sym: Symbol, initialSymbol: Symbol)(using Context) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| !sym.maybeOwner.isTypeParam && | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| sym.isTypeParam && ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (initialSymbol.is(Method) && initialSymbol.typeParams.contains(sym)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // @M #2585 when generating a java generic signature that includes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // a selection of an inner class p.I, (p = `pre`, I = `cls`) must | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // rewrite to p'.I, where p' refers to the class that directly defines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -528,4 +546,27 @@ object GenericSignatures { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val rte = recur(mtd) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (tparams.toList, vparams.toList, rte) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end collectMethodParams | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Collect type parameters that are actually used in the given types. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private def collectUsedTypeParams(types: List[Type], initialSymbol: Symbol)(using Context): (Set[Name], Set[Symbol]) = | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val usedMethodTypeParamNames = collection.mutable.Set.empty[Name] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val usedClassTypeParams = collection.mutable.Set.empty[Symbol] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val collector = new TypeTraverser: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def traverse(tp: Type) = tp.dealias match | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case ref @ TypeParamRef(_: PolyType, _) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedMethodTypeParamNames += ref.paramName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case TypeRef(pre, _) => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| val sym = tp.typeSymbol | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if isTypeParameterInMethSig(sym, initialSymbol) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedMethodTypeParamNames += sym.name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else if sym.isTypeParam && sym.isContainedIn(initialSymbol.topLevelClass) then | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| usedClassTypeParams += sym | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| traverse(pre) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should probably not be in the |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| case _ => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| traverseChildren(tp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| types.foreach(collector.traverse) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+554
to
+569
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| (usedMethodTypeParamNames.toSet, usedClassTypeParams.toSet) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| end collectUsedTypeParams | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
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}") | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems name shadowing can also happen for nested classes, though that's a lot more exotic
the class
C$Ihas signautre<T:TT;>Ljava/lang/Object;, which I guess is also wrong? Either way, I think we don't need to fix this in this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. In that case, the renaming strategy for methods in nested classes will be more complicated. I won't fix it in this PR. Instead, I gonna rebase #24621 on top of this change and address it there.