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

TC uses incorrect scope for (forward referenced) var in nested function #435

Closed
PaulKlint opened this issue Dec 11, 2013 · 4 comments
Closed

Comments

@PaulKlint
Copy link
Member

The following code is currently correct but is not accepted by the type checker.
Can this be fixed in the type checker?

public test bool  sideEffect1() {
             void One() { called = called + 1; return; }
             int called = 0;  
             One(); 
             One(); 
             One(); 
             return called == 3;
             }

gives

error("Name called is not in scope",|project://rascal-test/src/tests/functionality/CallTests.rsc|(4703,6,<125,31>,<125,37>))
@mahills
Copy link
Member

mahills commented Dec 11, 2013

I'll have to check, I believe the assumption right now is that variables
used inside nested functions are declared first. This should be possible
to change, but I'm really hoping we don't allow this with variables of
inferred type...

Paul Klint mailto:notifications@github.com
December 11, 2013 at 5:07 PM

The following code is currently correct but is not accepted by the
type checker.
Can this be fixed in the type checker?

|public test bool sideEffect1() {
void One() { called = called + 1; return; }
int called = 0;
One();
One();
One();
return called == 3;
}
|

gives

|error("Name called is not in
scope",|project://rascal-test/src/tests/functionality/CallTests.rsc|(4703,6,<125,31>,<125,37>))
|


Reply to this email directly or view it on GitHub
#435.

@PaulKlint
Copy link
Member Author

Personally, I think imposing the restriction that such variables are declared first is fine. Maybe there are other opinions. The type checker is already complex enough, so let's simplify where possible.

@mahills
Copy link
Member

mahills commented Jan 8, 2014

Okay. I'm fine with this. The current way the checker works is the following: when a statement sequence is checked, all the function declarations are found in the statement sequence and they are registered in the environment, without descending into their bodies. Then, each statement is processed in turn. When the function is reached, the body will also be checked. Nothing is done to specially handle variable declarations, so these just occur wherever they are encountered. I personally like this better, but we could modify the code to also add the variable names into the scope at the start, before processing the statements. If we decided to leave it as it is we should close the issue.

@PaulKlint
Copy link
Member Author

I think this fine, let's close this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants