-
Notifications
You must be signed in to change notification settings - Fork 396
Refactor: Make FieldName a composite of ClassName and SimpleFieldName. #4946
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
Conversation
* (And no, this is not dead code.) | ||
*/ | ||
val recordField = recordType.findField(field.name.simpleName) | ||
val sel = RecordSelect(newQual, SimpleFieldIdent(recordField.name))(recordField.tpe) |
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.
This snippet is worth paying particular attention to. The change does not alter the logic AFAICT, but it does highlight that there is/was something fishy going on.
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.
Looks like records only use simple field names. Maybe this will blow up:
class A {
private var x: Int
def foo = x
}
@inline
class B extends A {
private var x: String
}
new B().foo
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.
I have checked this, and it seems to work fine. What I've tested:
class A {
private var x: Int = 1
def foo = x
}
@inline
class B extends A {
private var x: String = "f"
def bar = x
}
object HelloWorld {
@noinline
def p(x: Any) = println(x)
def main(args: Array[String]): Unit = {
val b = new B()
p(b.foo)
p(b.bar)
}
}
Relevant generated code:
$c_Lhelloworld_HelloWorld$.prototype.main__AT__V = (function(args) {
var x = 0;
var x$1 = null;
x = 1;
x$1 = "f";
this.p__O__V(x);
this.p__O__V(x$1);
});
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.
Weird. Perhaps that specific example does not run into that particular code path.
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.
NICE! Just some minor stuff.
linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/NameGen.scala
Show resolved
Hide resolved
linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/FunctionEmitter.scala
Show resolved
Hide resolved
linker/shared/src/main/scala/org/scalajs/linker/backend/emitter/VarGen.scala
Show resolved
Hide resolved
@@ -1738,9 +1754,28 @@ object Serializers { | |||
LabelIdent(readLabelName()) | |||
} | |||
|
|||
def readSimpleFieldIdent(): SimpleFieldIdent = { |
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.
Unused? Seems like we do not actually ever read records :P
* (And no, this is not dead code.) | ||
*/ | ||
val recordField = recordType.findField(field.name.simpleName) | ||
val sel = RecordSelect(newQual, SimpleFieldIdent(recordField.name))(recordField.tpe) |
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.
Looks like records only use simple field names. Maybe this will blow up:
class A {
private var x: Int
def foo = x
}
@inline
class B extends A {
private var x: String
}
new B().foo
genFieldNameCache.getOrElseUpdate(name, { | ||
genName(name.className) + "__f_" + genName(name.simpleName) | ||
}) | ||
} |
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.
I would not cache this for now, to avoid changing performance behavior. (Also see comments RE duplicate caching of simple field names).
Previously, `FieldName` only represented the *simple* name of a field. It was complemented everywhere with the enclosing `ClassName`, for namespacing purposes. We now make `FieldName` a composite, like `MethodName`. It contains a `ClassName` and a `SimpleFieldName`. Structurally, `SimpleFieldName` is the same as the old `FieldName`. This removes the need to pass additional, out-of-band `ClassName`s everywhere a `FieldName` or `FieldIdent` was used. In addition to the readability improvements, this might improve performance. We previously often had to create (temporary) pairs of `(ClassName, FieldName)` as keys of maps. Now, we can directly use the `FieldName`s instead. While the IR names, types and trees are significantly impacted by this change, the `.sjsir` format is unchanged.
61302e8
to
723663b
Compare
Previously,
FieldName
only represented the simple name of a field. It was complemented everywhere with the enclosingClassName
, for namespacing purposes.We now make
FieldName
a composite, likeMethodName
. It contains aClassName
and aSimpleFieldName
. Structurally,SimpleFieldName
is the same as the oldFieldName
.This removes the need to pass additional, out-of-band
ClassName
s everywhere aFieldName
orFieldIdent
was used.In addition to the readability improvements, this might improve performance. We previously often had to create (temporary) pairs of
(ClassName, FieldName)
as keys of maps. Now, we can directly use theFieldName
s instead.While the IR names, types and trees are significantly impacted by this change, the
.sjsir
format is unchanged.