Discard stack variable

How about a command that notifies compiler that you dont need a stack variable anymore?

It could be something like a syntax sugar to use instead of:

uint myVariable = 1;

something like:

uint myVariable = 1;
discard myVariable;

Can you elaborate a bit more on your use case and on why would you need this?

The motivation is actually explained pretty well in the question.

In order to avoid ‘Stack Too Deep’, you sometimes need to “scope” a variable.

This is demonstrated in the question via the rather “dirty” syntax:

    uint myVariable = 1;

So the author suggests an alternative syntax for that, which would look “less dirty”.

I’m not sure how exactly compiler works, but it definitely pops variables that get out of scope.

So if it is possible, this operator should pop a variable you dont need anymore.

If not, it should just act like a syntax sugar like i described above.

The goal - to get rid of a variable that you dont need anymore but it still occupies a slot inside stack.

This will allow not only avoid “stack to deep” but maybe make all the next operations more gas effective (because you have one less slot in stack you need to keep about).

Though i’m not sure in my last statement, it is more to the compiler guys.

But it will at least could help to solve “stack to deep” issue.

The compiler should be able to do so without any additional hint (such as the discard sugar syntax in your example).

It obviously doesn’t, otherwise you wouldn’t need that “scoping hack” in order to dodge the ‘Stack Too Deep’ problem.

But instead of supporting this sugar syntax, the compiler team may as well just augment the compiler to detect that.

Note that the ‘Stack Too Deep’ problem isn’t really a common problem in all compilers (at least AFAIK). Although it sounds similar in name to the ‘Stack Overflow’ problem, the latter is a runtime error which has nothing to do with the compiler (it can also occur on the EVM if you “try hard enough”, BTW).

IMO the proposal from the OP looks like “syntactic sugar” to me which is something we frankly won’t pursue and like @barakman said it might no even be a compiler problem.

This syntax is most certainly not conventional for strongly-typed languages (and is not needed for weakly-typed languages), so it is most likely not going to be implemented, I would presume.

Alternative ways to circumvent (work-around) the ‘Stack Too Deep’ problem are:

  1. Scoping the “temporary” variable, as demonstrated in the original question
  2. Splitting the function into several functions (which is essentially just a different way of scoping)
  3. Adding viaIR: true in the project’s compiler setting
1 Like