rusty icon indicating copy to clipboard operation
rusty copied to clipboard

bit-access on pointers will panic without error/validation

Open mhasel opened this issue 2 years ago • 2 comments

We are missing validations for the following examples, all of which end either in an unreachable or panic.

For regular pointer variables, I don't think bit-access makes much sense, but what about structs etc.? In some ST dialects/implementations it is possible to directly bit-access into a struct, but I guess the structs in these variants are packed? Should we also support this or just return an error and abort codegen?

TYPE struct_t : STRUCT
	field : DINT;
END_STRUCT END_TYPE

FUNCTION_BLOCK fb_t
END_FUNCTION_BLOCK

PROGRAM mainProg
	VAR
		st : struct_t;
		fb : fb_t;
		ptr : REF_TO DINT;
	END_VAR
		// None of these are currently validated
		st.%X1;   // hits unreachable in cast_if_needed
		fb.%X1;   // hits unreachable in cast_if_needed
		ptr.%X1;  // panics in Inkwell
END_PROGRAM

mhasel avatar Jul 21 '23 10:07 mhasel

Has this been fixed? I'm unable to reproduce on the current master

volsa avatar Apr 02 '24 08:04 volsa

Looks like a validation for this has been added with #951. That fixes the issue of hitting unreachables/panics, but we are still unable to directly bit-access into structs, so I guess this is now more of a discussion of whether or not we want to support that functionality.

Updated example:

TYPE struct_t : STRUCT
	field : DINT;
END_STRUCT END_TYPE

PROGRAM mainProg
	VAR
		st : struct_t := (field := 2);
	END_VAR
		st.%X1; // should this return the second bit of `st.field`, i.e. 1?
END_PROGRAM

It might make sense to close this issue and open a new one for this use-case?

mhasel avatar Apr 02 '24 14:04 mhasel