Skip to content

[BUG] for loop don't allow several assignments in next #448

Closed

Description

@realgdman

for loop (and other loops) don't accept several assignments inside next

Reproduce Code
https://cpp2.godbolt.org/z/aqYqr63z5

main: () = {
	(copy a := 0, copy b := 0)
	//for "abc" next a++ do (i) { //ok
	for "abc" next { a++; b++; } do (i) { //error
	//for "abc' next a do (i) { //unused a
		std::cout << i << a << b << '\n';
	}
}

Version
latest (38aec57)

Command lines
cppfront/cppfront $1.cpp2 -p
clang++-15 -Icppfront/include $1.cpp -std=c++20 -o $1

Expected result
Some block syntax for that assignment or diagnostic

Actual result
Compilation error

Additional notes
In grammar only single expr allowed
//G next-clause:
//G 'next' assignment-expression

Additionally, logical-or-expression could be logical.
Which can lead to compiled, but unused logical expression
for "abc' next a do (i) { //unused a

Activity

  1. realgdman commented on May 11, 2023

    @realgdman
    Author

    Temporary workaround was found

    main: () = {
    	(copy a := 0, copy b := 0, next :=:()={ a&$*++; b&$*++; })
    	for "abc" next next() do (c) {
    		std::cout << c << a << b << '\n';
    	}	
    }
    
    First attempt ``` main: () = { a:=0; b:=0; l := :() = { a&$*++; b&$*++; }; for "abc" next l() do (c) { std::cout << c << a << b << '\n'; } } ```
    Thanks for idea from here, https://github.com//issues/432 currently lambda cannot be declared inside `next`
  2. msadeqhe commented on May 11, 2023

    @msadeqhe

    The example should be written like this:

    main: () = {
    	(copy a: = 0, copy b: = 0)
    	for "abc" next (a++, b++) do (i) {
    		std::cout << i << a << b << '\n';
    	}
    }

    Cpp2 generates this:

    auto main() -> int{
    {
    auto a = 0;
    auto b = 0;
    
     { auto const& cpp2_range = "abc"; for ( auto const& i : cpp2_range )  { do {
      std::cout << i << a << b << '\n';
     } while (false); (++a, ++b); }}
    }
    }
  3. realgdman commented on May 11, 2023

    @realgdman
    Author

    Well I should have guessed. Even this works.
    for "abc" next ( a++, b++, std::cout << a, foo()) do (c)

  4. JohelEGP commented on May 11, 2023

    @JohelEGP
    Contributor

    See #449 (comment). It may work by accident.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions