Andriamanitra / coctus

Command line tool for playing Clash of Code locally
MIT License
4 stars 2 forks source link

Stub generator generates invalid code when named variables conflict with loop variable #52

Open Andriamanitra opened 2 months ago

Andriamanitra commented 2 months ago

Describe the bug Template generator produces invalid code in languages that don't allow shadowing and use a loop variable that is named the same as one of the names in the stub generator. Currently this only affects C++, but as more languages get added some of them will likely have similar issues.

To Reproduce

$ echo 'loopline N i:int' | clash generate-stub cpp --from-file -
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    for (int i = 0; i < n; i++) {
        int i;
        cin >> i; cin.ignore();
    }

    return 0;
}
$ echo 'loopline N i:int' | clash generate-stub cpp --from-file - | gcc -x c++ -fsyntax-only -
<stdin>: In function ‘int main()’:
<stdin>:10:25: error: ‘n’ was not declared in this scope
<stdin>:11:13: error: redeclaration of ‘int i’
<stdin>:10:14: note: ‘int i’ previously declared here

CodinGame itself suffers from the exact same issue so we don't have plans to fix this on our side either.