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

Fix init checker on secondary constructor parameter access #13776

Merged
merged 1 commit into from
Nov 15, 2021

Conversation

natsukagami
Copy link
Contributor

Insert the parameters of the secondary constructor into the
Objekt of thisV, so that they can be looked up when it is
referenced.

An example is provided as a test file.

Upon calling a secondary constructor, we add the
parameters into the cache as if they were fields of
the constructed object (similar to a primary constructor).

Due to the actual fields referenced being resolved to
fully qualified names (but references to parameters are not),
we also change the accessLocal logic to look at the
object cache first, if we are in a Constructor context.

@natsukagami natsukagami changed the title Temporary fix for the secondary constructor parameters bug Fix for the secondary constructor parameters bug Oct 19, 2021
@natsukagami natsukagami changed the title Fix for the secondary constructor parameters bug Fix init checker on secondary constructor parameter access Oct 19, 2021
@natsukagami
Copy link
Contributor Author

Fixed! Please have another look.

@natsukagami natsukagami marked this pull request as ready for review October 27, 2021 01:32
Copy link
Contributor

@liufengyun liufengyun 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 🎉

@@ -652,6 +652,16 @@ object Semantic {
}

def callConstructor(ctor: Symbol, args: List[ArgInfo], source: Tree): Contextual[Result] = log("call " + ctor.show + ", args = " + args, printer, (_: Result).show) {
// init "fake" param fields for the secondary constructor
def addEnvToRef(env: Env, ref: Ref) = {
val ctorDef = ctor.defTree.asInstanceOf[DefDef]
Copy link
Contributor

Choose a reason for hiding this comment

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

Nitpick: We can avoid ctor.defTree.asInstanceOf[DefDef] by passing it from call sites --- there we already have ddef.

Maybe rename addEnvToRef to something like addParamAsField to be more clear?

def this(b: B, m: Int) = {
this(b)
def foo = m // resolved to parameter `m`
class C { foo } // resolved to parameter `m`, as hidden field of `A`
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this comment belong on the previous line? I don't see how it applies to class C.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The call to foo here will cause the interpreter to look up m (and from the PR's changes, find m as a hidden field of A)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, got it, thanks.

@natsukagami natsukagami force-pushed the fix-secondary-constructor-crash branch 2 times, most recently from 2e3d0fc to 0bafae2 Compare November 3, 2021 01:50
- Insert the parameters of the secondary constructor into the
`Objekt` of `thisV`, so that they can be looked up when it is
referenced.

- Add tests
@natsukagami natsukagami force-pushed the fix-secondary-constructor-crash branch from 8f67d07 to 1971882 Compare November 9, 2021 19:04
@natsukagami
Copy link
Contributor Author

@liufengyun @olhotak I think it is ready, please merge if it looks OK.

@olhotak olhotak merged commit dcd4249 into scala:master Nov 15, 2021
@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
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.

4 participants