Visitar URL original
fix(compiler): generate correct code for safe method calls by JoostK · Pull Request #44088 · angular/angular · GitHub
The Wayback Machine - https://web.archive.org/web/20220321101747/https://github.com/angular/angular/pull/44088
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(compiler): generate correct code for safe method calls #44088

Closed
wants to merge 2 commits into from

Conversation

JoostK
Copy link
Member

@JoostK JoostK commented Nov 5, 2021 •

fix(compiler): generate correct code for safe method calls

When a safe method call such as person?.getName() is used, the
compiler would generate invalid code if the argument list also contained
a safe method call. For example, the following code:

person?.getName(config?.get('title').enabled)

would generate

let tmp;
ctx.person == null ? null : ctx.person.getName((tmp = tmp) == null ?
null : tmp.enabled)

Notice how the call to config.get('title') has completely disappeared,
with (tmp = tmp) having taken its place.

The issue occurred due to how the argument list would be converted
from expression AST to output AST twice. First, the outer safe method
call would first convert its arguments list. This resulted in a
temporary being allocated for config.get('title'), which was stored in
the internal _resultMap. Only after the argument list has been
converted would the outer safe method call realize that it should be
guarded by a safe access of person, entering the convertSafeAccess
procedure to convert itself. This would convert the argument list once
again, but this time the _resultMap would already contain the
temporary tmp for config?.get('title'). Consequently, the safe
method in the argument list would be emitted as tmp.

This commit fixes the issue by ensuring that nodes are only converted
once.

Closes #44069

@ngbot ngbot bot added this to the Backlog milestone Nov 5, 2021
@ngbot ngbot bot added this to the Backlog milestone Nov 5, 2021
@google-cla google-cla bot added the cla: yes label Nov 5, 2021
@JoostK JoostK force-pushed the fix-safe-method-calls branch from 9a23ee1 to ff17a6a Compare Nov 5, 2021
@JoostK JoostK marked this pull request as ready for review Nov 5, 2021
@JoostK JoostK requested a review from crisbeto Nov 5, 2021
<span [title]="person?.getName(config.get('title')?.enabled ?? true)"></span>
`
})
export class MyApp {
Copy link
Member

@crisbeto crisbeto Nov 6, 2021

Choose a reason for hiding this comment

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

Should we have a runtime equivalent of this test as well?

Copy link
Member

@petebacondarwin petebacondarwin Nov 6, 2021

Choose a reason for hiding this comment

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

Copy link
Member

@petebacondarwin petebacondarwin left a comment

Neat little fix @JoostK - Please can you add the acceptance test?

<span [title]="person?.getName(config.get('title')?.enabled ?? true)"></span>
`
})
export class MyApp {
Copy link
Member

@petebacondarwin petebacondarwin Nov 6, 2021

Choose a reason for hiding this comment

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

JoostK added 2 commits Nov 6, 2021
When a safe method call such as `person?.getName()` is used, the
compiler would generate invalid code if the argument list also contained
a safe method call. For example, the following code:

```
person?.getName(config?.get('title').enabled)
```

would generate

```
let tmp;
ctx.person == null ? null : ctx.person.getName((tmp = tmp) == null ?
null : tmp.enabled)
```

Notice how the call to `config.get('title')` has completely disappeared,
with `(tmp = tmp)` having taken its place.

The issue occurred due to how the argument list would be converted
from expression AST to output AST twice. First, the outer safe method
call would first convert its arguments list. This resulted in a
temporary being allocated for `config.get('title')`, which was stored in
the internal `_resultMap`. Only after the argument list has been
converted would the outer safe method call realize that it should be
guarded by a safe access of `person`, entering the `convertSafeAccess`
procedure to convert itself. This would convert the argument list once
again, but this time the `_resultMap` would already contain the
temporary `tmp` for `config?.get('title')`. Consequently, the safe
method in the argument list would be emitted as `tmp`.

This commit fixes the issue by ensuring that nodes are only converted
once.

Closes angular#44069
@JoostK JoostK force-pushed the fix-safe-method-calls branch from ff17a6a to 18252d0 Compare Nov 6, 2021
@pullapprove pullapprove bot requested a review from atscott Nov 6, 2021
const fixture = TestBed.createComponent(App);
fixture.detectChanges(/* checkNoChanges */ false);
expect(fixture.nativeElement.textContent).toContain('Hello, unknown!');
expect(log).toEqual(['getFallbackName()']);
Copy link
Member

@crisbeto crisbeto Nov 6, 2021

Choose a reason for hiding this comment

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

nit: I don't think that we get much out of this log. If the resulting value is correct, then we can pretty confident that the generated code is correct.

Copy link
Member Author

@JoostK JoostK Nov 6, 2021

Choose a reason for hiding this comment

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

I included it mainly to verify short-circuiting behavior, e.g. in this case the inner safe access should not have been evaluated, and to ensure that methods are not called multiple times.

@ngbot
Copy link

@ngbot ngbot bot commented Nov 6, 2021

I see that you just added the action: merge label, but the following checks are still failing:
    failure status "ci/circleci: legacy-unit-tests-saucelabs" is failing
    pending status "google3" is pending
    pending status "pullapprove" is pending
    pending 1 pending code review

If you want your PR to be merged, it has to pass all the CI checks.

If you can't get the PR to a green state due to flakes or broken master, please try rebasing to master and/or restarting the CI job. If that fails and you believe that the issue is not due to your change, please contact the caretaker and ask for help.

atscott
atscott approved these changes Nov 8, 2021
@JoostK
Copy link
Member Author

@JoostK JoostK commented Nov 8, 2021

merge-assistance: legacy-unit-test-saucelabs are almost impossible to get green.

@atscott
Copy link
Contributor

@atscott atscott commented Nov 8, 2021

@atscott
Copy link
Contributor

@atscott atscott commented Nov 8, 2021

This PR was merged into the repository by commit b249e24.

atscott added a commit that referenced this issue Nov 8, 2021
When a safe method call such as `person?.getName()` is used, the
compiler would generate invalid code if the argument list also contained
a safe method call. For example, the following code:

```
person?.getName(config?.get('title').enabled)
```

would generate

```
let tmp;
ctx.person == null ? null : ctx.person.getName((tmp = tmp) == null ?
null : tmp.enabled)
```

Notice how the call to `config.get('title')` has completely disappeared,
with `(tmp = tmp)` having taken its place.

The issue occurred due to how the argument list would be converted
from expression AST to output AST twice. First, the outer safe method
call would first convert its arguments list. This resulted in a
temporary being allocated for `config.get('title')`, which was stored in
the internal `_resultMap`. Only after the argument list has been
converted would the outer safe method call realize that it should be
guarded by a safe access of `person`, entering the `convertSafeAccess`
procedure to convert itself. This would convert the argument list once
again, but this time the `_resultMap` would already contain the
temporary `tmp` for `config?.get('title')`. Consequently, the safe
method in the argument list would be emitted as `tmp`.

This commit fixes the issue by ensuring that nodes are only converted
once.

Closes #44069

PR Close #44088
@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Dec 9, 2021

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Dec 9, 2021
dimakuba added a commit to dimakuba/angular that referenced this issue Dec 28, 2021
…4088)

When a safe method call such as `person?.getName()` is used, the
compiler would generate invalid code if the argument list also contained
a safe method call. For example, the following code:

```
person?.getName(config?.get('title').enabled)
```

would generate

```
let tmp;
ctx.person == null ? null : ctx.person.getName((tmp = tmp) == null ?
null : tmp.enabled)
```

Notice how the call to `config.get('title')` has completely disappeared,
with `(tmp = tmp)` having taken its place.

The issue occurred due to how the argument list would be converted
from expression AST to output AST twice. First, the outer safe method
call would first convert its arguments list. This resulted in a
temporary being allocated for `config.get('title')`, which was stored in
the internal `_resultMap`. Only after the argument list has been
converted would the outer safe method call realize that it should be
guarded by a safe access of `person`, entering the `convertSafeAccess`
procedure to convert itself. This would convert the argument list once
again, but this time the `_resultMap` would already contain the
temporary `tmp` for `config?.get('title')`. Consequently, the safe
method in the argument list would be emitted as `tmp`.

This commit fixes the issue by ensuring that nodes are only converted
once.

Closes angular#44069

PR Close angular#44088
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.