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

Improper compiler error when checked variable is out of scope #1017

Closed
2000jedi opened this issue Mar 30, 2021 · 2 comments
Closed

Improper compiler error when checked variable is out of scope #1017

2000jedi opened this issue Mar 30, 2021 · 2 comments
Assignees
Labels
bug This labels issues that are bugs. priority:1 This labels bugs that are blocking customers.

Comments

@2000jedi
Copy link

Problem

The following code should compile

int main() {
  int arr _Checked[5];
  int len = 5;
  {
    _Array_ptr<int> dest : count(len) = arr;
  }
  len = 4;
  return 0;
}

But instead, the 0.8 (release) version of the compiler generates the following error:

error: inferred bounds for 'dest' are unknown after statement
len = 4;

Note that the variable dest is already out of scope.

This should not be an error.

@sulekhark sulekhark added bug This labels issues that are bugs. priority:1 This labels bugs that are blocking customers. labels Mar 30, 2021
@sulekhark sulekhark self-assigned this Mar 30, 2021
sulekhark added a commit that referenced this issue Apr 17, 2021
… out-of-scope variable. (#1031)

* Fix for the crash during CFG construction when CFGLifetimeEnds elements are added to the CFG.

* Make VariableOccurrenceCount take a ValueDecl rather than DeclRefExpr argument

* Removing checked pointers that are no longer in scope from ObservedBounds.
Also removing expressions that refer to out-of-scope variables from AvailableExpressions.

* Added a test case to test the modifications to equivalent expressions
when variables go out of scope within a basic block.

* Fix for a crash on Windows caused by an iterator access out of range. If one iterates through a loop and removes elements from it at the same time, the end of the iterator has to be re-evaluated in each iteration.

* Incorporated review comments.

* Incorporated review comments.

Co-authored-by: kakje <[email protected]>
@sulekhark
Copy link
Contributor

Intersecting the set of in-scope variables across the predecessors of a basic block excludes most variables that are not in-scope for the basic block. However, an exception is when a variable goes out of scope within the basic block. When this happens, the bug noted above manifests.

Fix:
Enabling the AddLifetime flag during CFG construction produces the CFGLifetimeEnds CFGElement for every variable that goes out of scope. When we encounter this CFGElement, we remove the variable that goes out of scope from the ObservedBounds map if it is a checked pointer, and also delete all available expressions that use this out-of-scope variable.

@sulekhark
Copy link
Contributor

Fixed by PR #1031.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This labels issues that are bugs. priority:1 This labels bugs that are blocking customers.
Projects
None yet
Development

No branches or pull requests

2 participants