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

Unnecessary field in subclass, parameter alias not detected #12995

Open
lrytz opened this issue May 6, 2024 · 1 comment
Open

Unnecessary field in subclass, parameter alias not detected #12995

lrytz opened this issue May 6, 2024 · 1 comment
Milestone

Comments

@lrytz
Copy link
Member

lrytz commented May 6, 2024

On 2.13.14

abstract class A(val id: String)

// `B.id` has alias `A.id`, no field in `B`
abstract class B(override val id: String) extends A(id)

// `idC` has no alias, unnecessary field in `C`
class C(idC: String) extends B(idC) { def m = idC }

Class C gets a field. If B's parameter has a different name aliasing is identified, C gets no field.

@lrytz lrytz added this to the Backlog milestone May 6, 2024
@lrytz
Copy link
Member Author

lrytz commented May 6, 2024

The issue is here
https://github.com/scala/scala/blob/v2.13.14/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L6554

        val superClazz = sym.superClass

          superAcc.initialize.alias // Is the param accessor is an alias for a field further up  the class hierarchy?
            orElse (superAcc getterIn superAcc.owner) // otherwise, lookup the accessor for the super
            filter (alias => superClazz.info.nonPrivateMember(alias.name) == alias) // the accessor must be public
  • superAcc is the field B.id
  • superAcc.initialize.alias is the getter A.id
  • superClazz.info.nonPrivateMember(alias.name) gives the getter override B.id, which is not A.id, so the alias is discarded

Maybe it's an overisght, we could change it to alias.owner.info.nonPrivateMember; but I'm not entirely sure because the generated bytecode is INVOKESPECIAL A.id (super call to A.id), but we have C extends B extends A, where B overrides id. Would need to dig to know if that's valid.

On the other hand, generating a super call instead of a virtual call might a bug on its own (#9330 (comment)).

@SethTisue SethTisue changed the title Unnecessary field in sublcass, parameter alias not detected Unnecessary field in subclass, parameter alias not detected May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant