uqbar-project / wollok

Wollok Programming Language
GNU General Public License v3.0
60 stars 16 forks source link

Try-Catch Scope #612

Open flbulgarelli opened 8 years ago

flbulgarelli commented 8 years ago

Hi!

While implementing the wollok mumuki runner console, I wrote the following code:

program mumuki {
  try { var x = 1 } catch e : Exception {  }
  try { x += 1 } catch e : Exception {  }
  try { x += 1 } catch e : Exception {  }
  console.println('=> ' + (x).toString())
}

Although I may look contrived, I actually need to write something like that in order to recreate a program state, by executing previous lines (1..3) before I actually execute the console statement x. And I need to ensure that such lines do not abort the program flow, so I am using try-catch's.

The program above works, and outputs the following line:

franco@xinaiu:~/Downloads/wdk$ ./bin/winterpreter.sh other.wpgm 
/home/franco/Downloads/wdk/other.wpgm:2:29 WARNING Unused variable
=> 3

But, as you can see, it generates an strange warning Unused variable. And I can figure out why: try blocks, like most Java like languages - introduce a new scope, so var x = 1 has no effect.

But this is inconsistent with the runtime behavior, because the program executed correctly.

So, either the checker is too strict, or the runtime is too tolerant. How should it actually work?

javierfernandes commented 8 years ago

Yes. There are basically two aspects of this.

One is a bug. Seems like the scope is not correctly delimited for cross-references (well not actually cross files, but ..). A var inside a try shouldn't be available to be used anywhere else outside of the try. We don't have that, and that's why the checker is not reporting that problem. Then somehow the used/unused algorythm messes around, but just a side-effect.

Then on the other hand there's the runtime semantic of wollok. You see on this side of wollok the runtime environment is flexible, and resembles to dynamic languages. If there was a syntax to declare you could even do monkey patching and add methods or remove methods, / variables etc. Just that we didn't want that in the language. But the interpreter ""could"" support it.

This is low-level but the way the interpreter works it that when a var declaration is found it will add it to the current scope. Seems like in this case the scope is associated with the whole method. So the first try puts "x" in the method scope, then further tries can access them.

We create static checks in order to minimize the number of runtime errors. But there is one, for example when you try to access a variable which is not currently in the scope.

Anyway, just notes on the impl. At the end we need to go to the ScopeProvider classes and review/write some definition to delimit the scope of the WTry construction.

Regards

On Wed, Mar 9, 2016 at 2:54 PM, Franco Leonardo Bulgarelli < notifications@github.com> wrote:

Hi!

While implementing the wollok mumuki runner console, I wrote the following code:

program mumuki { try { var x = 1 } catch e : Exception { } try { x += 1 } catch e : Exception { } try { x += 1 } catch e : Exception { } console.println('=> ' + (x).toString()) }

Although I may look contrived, I actually need to write something like that in order to recreate a program state, by executing previous lines (1..3) before I actually execute the console statement x. And I need to ensure that such lines do not abort the program flow, so I am using try-catch's.

The program above works, and outputs the following line:

franco@xinaiu:~/Downloads/wdk$ ./bin/winterpreter.sh other.wpgm /home/franco/Downloads/wdk/other.wpgm:2:29 WARNING Unused variable => 3

But, as you can see, it generates an strange warning Unused variable. And I can figure out why: try blocks, like most Java like languages - introduce a new scope, so var x = 1 has no effect.

But this is inconsistent with the runtime behavior, because the program executed correctly.

So, either the checker is too strict, or the runtime is too tolerant. How should it actually work?

— Reply to this email directly or view it on GitHub https://github.com/uqbar-project/wollok/issues/612.

flbulgarelli commented 8 years ago

So, to sum up, my code is basically wrong according to Wollok semantics, isn't it?

Is there any present or future way to accomplish what I need - recreating state by reevaluation of previous statements? Any suggestion?

alete89 commented 4 years ago

@fdodino There was already this old issue regarding "unused variable" warning being raised when using outside a var/const defined inside a try/catch block.

fdodino commented 4 years ago

Yes, we must fix the visitor, at least...

npasserini commented 4 years ago

Che, pero estamos de acuerdo en que ese programa está mal y no se debería poder usar la variable definida dentro del try fuera de él, verdad?

El mié., 4 de dic. de 2019 a la(s) 21:54, Fernando Dodino ( notifications@github.com) escribió:

Yes, we must fix the visitor, at least...

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/uqbar-project/wollok/issues/612?email_source=notifications&email_token=ABDLKOOWB5KFADYR4BZPRVDQXBGNBA5CNFSM4B5T53A2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEF7CUFA#issuecomment-561916436, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDLKOKOLYD2GLPYL2XKDU3QXBGNBANCNFSM4B5T53AQ .

fdodino commented 4 years ago

No me había quedado claro eso, la explicación de Javi respecto de los scopes me parecía interesante pero no hay nada que indique que el programa Wollok es incorrecto, al menos dentro de este issue. Si no queremos eso, por mí está perfecto, veamos de aclarar bien el acceptance criteria para evitar dudas a la hora de encarar el ticket.

npasserini commented 4 years ago

Para mí todos los usos de x salvo su declaración están mal.

El mié., 4 de dic. de 2019 a la(s) 23:54, Fernando Dodino ( notifications@github.com) escribió:

No me había quedado claro eso, la explicación de Javi respecto de los scopes me parecía interesante pero no hay nada que indique que el programa Wollok es incorrecto, al menos dentro de este issue. Si no queremos eso, por mí está perfecto, veamos de aclarar bien el acceptance criteria para evitar dudas a la hora de encarar el ticket.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/uqbar-project/wollok/issues/612?email_source=notifications&email_token=ABDLKOKAKE2TSQHB2GB6WY3QXBUNXA5CNFSM4B5T53A2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEF7JK4Q#issuecomment-561943922, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABDLKOKG4BXRQHCNXCIVOKLQXBUNXANCNFSM4B5T53AQ .