Skip to content

Use FINAL for immutable variables creates syntax error in case of method call #96

Closed

Description

@cgrail

Given is the following code:

class lcl_test definition create private.
  public section.
    types: begin of test,
             id   type sysuuid_x16,
             name type string,
           end of test.
    methods show_data.
endclass.


class lcl_test implementation.
  method show_data.
    try.
        data(test_data) = value test( id   = cl_system_uuid=>create_uuid_x16_static( )
                                      name = 'Test' ).
        cl_demo_output=>display( test_data ).
      catch cx_uuid_error.
        raise exception new cx_sovy_fsi_event_dispatcher( ).
    endtry.
  endmethod.
endclass.

When I run the ABAP Cleaner with the rule Use FINAL for immutable variables I will get a syntax error and the declaration type of the variable test_data will change from DATA to FINAL.

Screenshot 2023-08-25 at 11 00 53

Manual solution is to either extract the creation of the id upfront:

        final(id) = cl_system_uuid=>create_uuid_x16_static( ).
        final(test_data) = value test( id   = id
                                       name = 'Test' ).

Or to move id generation to the last line:

        final(test_data) = value test( name = 'Test'
                                       id   = cl_system_uuid=>create_uuid_x16_static( ) ).

Which is weird, but works.

It would be great if you could add another check to either extract the result of inline calls into own variables or don't change the declaration type in that case.

Activity

  1. fabianlupa commented on Aug 26, 2023

    @fabianlupa

    Or to move id generation to the last line:

    Seriously? The weird edge cases of the chosen expression implementation strike again :(

    A variable inside the expression also works by the way:

            final(test_data) = value test( LET i = cl_system_uuid=>create_uuid_x16_static( ) IN
                                           id   = i
                                           name = 'Test' ).

    In my opinion this should be fixed or at least documented somewhere. Having to account for this in abap-cleaner seems unreasonable, especially when it's not obvious what works and what does not.

  2. self-assigned this
    on Sep 2, 2023
  3. jmgrassau commented on Sep 2, 2023

    @jmgrassau
    Member

    Hi Christian,

    that's an amazing finding indeed! And you can find more surprising examples, e.g.

    image

    I shall ask colleagues from the ABAP compiler team about this. However, even if the compiler learns to deal with those cases at some point in the future, I think for the time being it would be best to prevent introducing FINAL whenever any method calls are found inside of constructor expressions on the right-hand side.

    Kind regards,
    Jörg-Michael

  4. jmgrassau commented on Sep 2, 2023

    @jmgrassau
    Member

    Hi Christian and Fabian,

    with the next release, the workaround would be as mentioned: Whenever a method call is found inside of a constructor expression, DATA is kept:

    image

    Hope this solves the issue, even if it does a bit too much; please let me know if you find cases which still cause a syntax error!

    Kind regards,
    Jörg-Michael

  5. suynwa commented on Sep 4, 2023

    @suynwa

    I shall ask colleagues from the ABAP compiler team about this.

    Hi Jörg-Michael, done that already 👍🏼

  6. jmgrassau commented on Sep 4, 2023

    @jmgrassau
    Member

    Hi Christian, Fabian and Suhas,

    thanks again for reporting this! The workaround is now in place with version 1.5.3, which was just released. Closing this issue for now, but if there are news from the ABAP compiler team on this, we could see if the workaround can be modified accordingly or even removed at some point.

    Kind regards,
    Jörg-Michael

  7. cgrail commented on Sep 5, 2023

    @cgrail
    Author

    Cool. Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions