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

Wrong implicit symbol declaration in "autodeclare index" #324

Closed
mgerlach-phys opened this issue Sep 16, 2019 · 2 comments
Closed

Wrong implicit symbol declaration in "autodeclare index" #324

mgerlach-phys opened this issue Sep 16, 2019 · 2 comments
Labels
bug Something isn't working

Comments

@mgerlach-phys
Copy link

The following, short program does not behave as expected:

`*symbol n;
autodeclare index randomIndex=n;
local A = n;

print +s;
.end`

Without declaring "n" the expected behavior would be to declare it implicitly as a symbol with a warning (as it is the case for the program without the "autodeclare" statement).

Funnily, the program runs without complains and returns i_.

@tueda
Copy link
Collaborator

tueda commented Sep 17, 2019

Thank you for your report!
It seems that this issue and #325 are caused by the fact that n (in the following example) is considered as a different object from the usual one when implicitly used via autodeclare index (20 vs. 0):

I x = n;
L F = n;
P "%r";
.end
test.frm Line 1 --> Warning: Implicit declaration of n as a symbol
8  1  4  20  1  1  1  3
Auto I x = n;
L F = n;
P "%r";
.end
8  1  4  0  1  1  1  3

The reason why you may get i_, pi_, coeff_, num_, den_, ... is because they are built-in objects defined in this order:

form/sources/startup.c

Lines 1029 to 1037 in 0b3ab5d

AddSymbol((UBYTE *)"i_",-MAXPOWER,MAXPOWER,VARTYPEIMAGINARY,0);
AddSymbol((UBYTE *)"pi_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);
AddSymbol((UBYTE *)"coeff_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);
AddSymbol((UBYTE *)"num_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);
AddSymbol((UBYTE *)"den_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);
AddSymbol((UBYTE *)"xarg_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);
AddSymbol((UBYTE *)"dimension_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);
AddSymbol((UBYTE *)"factor_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);
AddSymbol((UBYTE *)"sep_",-MAXPOWER,MAXPOWER,VARTYPENONE,0);

@benruijl
Copy link
Collaborator

benruijl commented Jul 7, 2022

The problem is caused by a rather error-prone way that auto-declared statements are processed. The entry point is:

	AC.Symbols = &(AC.AutoSymbolList);
...
	retval = CompileStatement(inp);

	AC.Symbols = &(AC.SymbolList);
...

So the AC.Symbols is overwritten. This array is used by AddSymbol to add symbols, and I believe AddSymbol expects that this array is always AC.SymbolList. For example, the function GetAutoName uses AddSymbol to register a new symbol when the auto-symbol wasn't used before.

What happens in a situation like:

Auto S n1;
Auto I mu = n1;

is that when n1 is used in the second auto-declare, it gets created and added to AC.AutoSymbolList and not AC.SymbolList. Since AC.AutoSymbolList is empty, we see a 0 popping up in the P "%r". Similarly, if we leave out Auto S n1, it creates the symbol in the wrong array.

To fix it, in DoDimension, I switch out the symbol list at the start:

AC.Symbols = &(AC.SymbolList);

and set it back to the original value at the end. This fixes this problem, and hopefully doesn't have side-effects. Basically any place where auto-declared variables is used for the first time in another auto-declared statement, this problem occurs. I believe DoDimension is the only place where this happens, but I haven't checked this thorougly.

tueda added a commit that referenced this issue Nov 2, 2022
benruijl pushed a commit that referenced this issue Nov 3, 2022
tueda pushed a commit that referenced this issue Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants