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

wip: try to fix initialization warnings in NameKinds #15462

Closed
wants to merge 10 commits into from

Conversation

olhotak
Copy link
Contributor

@olhotak olhotak commented Jun 16, 2022

Based on #15364.

Do not merge.

This is an attempt to refactor NameKinds to fix the initialization warnings.

The overall approach is to separate the two concerns in NameKinds: the definitions of the NameKind class hierarchy and the various vals, instances of various kinds of NameKinds. The PR moves the former out of object NameKinds and leaves the latter there. From the point of view of initialization, the goal is to remove the dependence of the classes in the NameKind class hierarchy on the outer object NameKinds.

Such refactorings would be easier if we had some static-class annotation that would signal to the init checker that a particular class is not intended to use its outer. The init checker could then check that it indeed does not use its outer, and allow this to be promoted even though outer is not yet initialized.

Some of the classes in the NameKind hierarchy were anonymous classes. Moving them out required making them named classes. A static-class annotation would enable us to avoid moving them and giving them names.

Since it was possible for me to move all the NameKind classes out of object NameKinds, it must be that they do not actually depend on their outer. Then it seems there must be some bug in the init checker: it should have been able to prove that they do not depend on their outer and allow this to be promoted, but it didn't. More detailed warnings about the reasons why the init checker thinks a class depends on outer would help diagnose this bug and also help programmers understand why the init checker cannot promote this.

Two classes, ExpandedNameKind and UniqueNameKind, still give a warning because they have the pattern described in #15459 : a superclass that needs to leak this and a subclass that adds fields. It would be good to find some way of expressing this pattern, for a superclass to specify an action to be performed after subclasses have been initialized.

A third class, AvoidNameKind, also gives the same warning, probably for the same reason, but one can't be sure because it involves code generated from an enum and until we fix #15459, the warning does not provide enough information.

The overall outcome is that we remove the four initialization warnings from NameKinds about the leaking of this, but we introduce the three new ones described in the last two paragraphs.

@olhotak
Copy link
Contributor Author

olhotak commented Jun 16, 2022

The three new warnings are:

[error] -- Error: /home/olhotak/git/dotty/compiler/src/dotty/tools/dotc/core/NameKinds.scala:77:16 
[error] 77 |  NameKinds.add(this)
[error]    |                ^^^^
[error]    |Cannot prove the argument is fully initialized. Only fully initialized values are safe to leak. Calling trace:
[error]    |-> enum AvoidNameKind(tag: Int, prefix: String) extends PrefixNameKind(tag, prefix):	[ NameKinds.scala:384 ]
[error]    |   ^
[error]    |-> case UpperBound extends AvoidNameKind(AVOIDUPPER, "(upper)")	[ NameKinds.scala:386 ]
[error]    |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]    |-> enum AvoidNameKind(tag: Int, prefix: String) extends PrefixNameKind(tag, prefix):	[ NameKinds.scala:384 ]
[error]    |   ^
[error]    |-> class PrefixNameKind(tag: Int, prefix: String, optInfoString: String = "")	[ NameKinds.scala:81 ]
[error]    |   ^
[error]    |-> abstract class ClassifiedNameKind(tag: Int, val infoString: String) extends NameKind(tag) {	[ NameKinds.scala:64 ]
[error]    |   ^
[error]    |-> NameKinds.add(this)	[ NameKinds.scala:77 ]
[error]    |                 ^^^^
[error] -- Error: /home/olhotak/git/dotty/compiler/src/dotty/tools/dotc/core/NameKinds.scala:182:16 
[error] 182 |  NameKinds.add(this)
[error]     |                ^^^^
[error]     |Cannot prove the argument is fully initialized. Only fully initialized values are safe to leak. Calling trace:
[error]     |-> class UniqueExtMethNameKind extends UniqueNameKind("$extension") {	[ NameKinds.scala:278 ]
[error]     |   ^
[error]     |-> case class UniqueNameKind(val separator: String)	[ NameKinds.scala:198 ]
[error]     |   ^
[error]     |-> abstract class NumberedNameKind(tag: Int, val infoString: String) extends NameKind(tag) { self =>	[ NameKinds.scala:162 ]
[error]     |   ^
[error]     |-> NameKinds.add(this)	[ NameKinds.scala:182 ]
[error]     |                 ^^^^
[error] -- Error: /home/olhotak/git/dotty/compiler/src/dotty/tools/dotc/core/NameKinds.scala:144:16 
[error] 144 |  NameKinds.add(this)
[error]     |                ^^^^
[error]     |Cannot prove the argument is fully initialized. Only fully initialized values are safe to leak. Calling trace:
[error]     |-> class ExpandedNameKind extends QualifiedNameKind(EXPANDED, str.EXPAND_SEPARATOR) {	[ NameKinds.scala:236 ]
[error]     |   ^
[error]     |-> class QualifiedNameKind(tag: Int, val separator: String)	[ NameKinds.scala:110 ]
[error]     |   ^
[error]     |-> NameKinds.add(this)	[ NameKinds.scala:144 ]
[error]     |                 ^^^^

@liufengyun
Copy link
Contributor

@olhotak Could you rebase against #15467, and see if there is any difference?

@olhotak
Copy link
Contributor Author

olhotak commented Jun 17, 2022

#15467 seems to not make a difference to NameKinds, both to the 4 warnings before and the 3 warnings after this PR. But it could be that there are multiple reasons why this is not fully initialized and #15467 fixes one reason but not other reason(s).

@olhotak
Copy link
Contributor Author

olhotak commented Jul 2, 2022

Closing in favour of #15560.

@olhotak olhotak closed this Jul 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

initialization checker should list uninitialized fields in warning
3 participants