Skip to content

Fix #4754: Don't generate inline accessor for constants #4760

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
merged 4 commits into from
Jul 6, 2018

Conversation

allanrenucci
Copy link
Contributor

No description provided.

@allanrenucci allanrenucci changed the title Fix #4574: Don't generate inline accessor for constants Fix #4754: Don't generate inline accessor for constants Jul 4, 2018
@allanrenucci allanrenucci requested a review from odersky July 4, 2018 16:27
@allanrenucci
Copy link
Contributor Author

Does this hold for protected accessors too?

def needsAccessor(sym: Symbol)(implicit ctx: Context) =
sym.isTerm &&
(sym.is(AccessFlags) || sym.privateWithin.exists) &&
!sym.isContainedIn(inlineSym)
!sym.isContainedIn(inlineSym) &&
!sym.info.widenTermRefExpr.isInstanceOf[ConstantType]
Copy link
Member

@smarter smarter Jul 4, 2018

Choose a reason for hiding this comment

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

You also need to check isIdempotentExpr like we do in TreeInfo#constToLiteral which is called from FirstTransform. Alternatively you could just check is(Final) I think.

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 don't think so. Here sym refer to a definition not an expression like in TreeInfo#constToLiteral.

I should add a test for private methods though making sure they get an accessor

Copy link
Member

Choose a reason for hiding this comment

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

Right so I'd check is(Final)

Copy link
Contributor Author

@allanrenucci allanrenucci Jul 4, 2018

Choose a reason for hiding this comment

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

Can you elaborate why is(Final)?

private final val x: Int = 1 // won't be inlined by FirstTransform, so need accessor
private final def y: Int = 1 // won't be inlined either, need accessor

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the right test is `

!(sym.isStable && sym.info.widenTermRefExpr.isInstanceOf[ConstantType])

That excludes defs with constant result. These have to be called anyway for their side effects.

}

class Test {
(new Foo).foo(1)
Copy link
Member

Choose a reason for hiding this comment

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

This should be a run test that checks that no accessor method is present in the class using java reflection.

Copy link
Contributor

@odersky odersky left a comment

Choose a reason for hiding this comment

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

Otherwise LGTM

def needsAccessor(sym: Symbol)(implicit ctx: Context) =
sym.isTerm &&
(sym.is(AccessFlags) || sym.privateWithin.exists) &&
!sym.isContainedIn(inlineSym)
!sym.isContainedIn(inlineSym) &&
!sym.info.widenTermRefExpr.isInstanceOf[ConstantType]
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the right test is `

!(sym.isStable && sym.info.widenTermRefExpr.isInstanceOf[ConstantType])

That excludes defs with constant result. These have to be called anyway for their side effects.

@allanrenucci
Copy link
Contributor Author

This PR doesn't solve the underlying issue: inline accessor are not generated for private definition in companion objects.

However once the issue is fixed, it could be added as an optimisation since accessors should not be needed for constant vals

odersky added 2 commits July 6, 2018 09:49
Need to tweak ensureAccessible to use the constant type instead of
the reference.
@odersky odersky merged commit e94a2e1 into scala:master Jul 6, 2018
@allanrenucci allanrenucci deleted the fix-4754 branch July 6, 2018 11:42
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

Successfully merging this pull request may close these issues.

3 participants