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

un-initialized constants report unresolved constant variable while they should get their default value #634

Closed
rarris opened this issue Nov 7, 2022 · 4 comments · Fixed by #645 or #669
Assignees
Labels
bug Something isn't working
Projects

Comments

@rarris
Copy link
Contributor

rarris commented Nov 7, 2022

program example

FUNCTION main : DINT
    VAR CONSTANT
    	cmd1 : commands;
        var2 : INT;
    END_VAR

END_FUNCTION

TYPE commands :
STRUCT
	ReInit : BOOL;
	Reset : BOOL;
	Stop : BOOL;
END_STRUCT
END_TYPE

error: Unresolved constant 'cmd1' variable
┌─ test.st:19:6

19 │ cmd1 : command;
│ ^^^^^

error: Unresolved constant 'var2' variable
┌─ test.st:20:6

20 │ var2 : int;
│ ^^^^^

@rarris rarris added the bug Something isn't working label Nov 7, 2022
@riederm
Copy link
Collaborator

riederm commented Nov 7, 2022

cmd1 & var2 need an initial value - the compiler message is really bad. We should improve the error message for this situation. Or do you think this code should work like this? the constants never get a value - or should they get default values?

@riederm
Copy link
Collaborator

riederm commented Nov 7, 2022

by the way - there is a typo I fixed so be careful when you test this again: I changed cmd1: command to cmd1: commands (adding the s in the end).

so the code would work like this:

FUNCTION main : DINT
    VAR CONSTANT
    	cmd1 : commands;
        var2 : INT;
    END_VAR

END_FUNCTION

TYPE commands :
STRUCT
	ReInit : BOOL;
	Reset : BOOL;
	Stop : BOOL;
END_STRUCT
END_TYPE

@riederm riederm changed the title unresolved constant variable poor error message with uninitialized constants: unresolved constant variable Nov 7, 2022
@ghaith
Copy link
Collaborator

ghaith commented Nov 8, 2022

Uninitialized constants should also get the default value. I've seen code that defines an uninitialized STRUCT as a "Reset" constant to easily reset other values during execution.

@riederm riederm changed the title poor error message with uninitialized constants: unresolved constant variable un-initialized constants report unresolved constant variable while they should get their default value Nov 10, 2022
riederm added a commit that referenced this issue Nov 12, 2022
constants with no initializers used to cause validation errors, from
this commit on they get thei'r datatypes default value.

note that alias and range-types will inherit their default from their
referenced types.

fixes #634
riederm added a commit that referenced this issue Nov 24, 2022
* uninitialized consts fall back to default value

constants with no initializers used to cause validation errors, from
this commit on they get their datatypes default value.

note that alias and range-types will inherit their default from their
referenced types.

added several test-szenarios
* test that shows the transitivity for range types
* test that covers enum default const evaluationF

fixes #634
@riederm riederm added this to To do in Next via automation Nov 24, 2022
@riederm riederm moved this from To do to Done in Next Nov 24, 2022
@ghaith
Copy link
Collaborator

ghaith commented Nov 28, 2022

This is still not solved, the code in the example still fails on codegen (Struct init does not get an initial value)

@ghaith ghaith reopened this Nov 28, 2022
Next automation moved this from Done to In progress Nov 28, 2022
@riederm riederm self-assigned this Nov 29, 2022
riederm added a commit that referenced this issue Nov 30, 2022
aggregate local constant variables (in functions) used to have
problems if they were not properly initialized. Instead of forcing an
initializer, we fallback to the type's default value or zeroinitializer.

added regression test for a previously failing situation

fixes #634
riederm added a commit that referenced this issue Dec 2, 2022
aggregate local constant variables (in functions) used to have
problems if they were not properly initialized. Instead of forcing an
initializer, we fallback to the type's default value or zeroinitializer.

added regression test for a previously failing situation

fixes #634
Next automation moved this from In progress to Done Dec 2, 2022
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
Status: Done
Next
Done
3 participants