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

Variable is wrongfully captured by macro expansion #80

Open
lschuetze opened this issue Feb 5, 2021 · 1 comment
Open

Variable is wrongfully captured by macro expansion #80

lschuetze opened this issue Feb 5, 2021 · 1 comment
Labels
bug Something isn't working m2m Related to the model-to-model transformation

Comments

@lschuetze
Copy link
Collaborator

A macro expansion captures a variable wrongfully leading to code compilation errors.
The first expansion of CELL_MEMBAL(,) is okay, because shift is not defined, yet. The second expansion results in the error shown below.

The code is the generated code from the LJ_paper/Lennard Jones example.

  auto particles_cellList_0 = particles.getCellList<CELL_MEMBAL(3, double)>(r_cut);
...
  double shift = (2.0 * ((sigma12 / (((((r_cut2 * r_cut2) * r_cut2) * r_cut2) * r_cut2) * r_cut2)) - (sigma6 / ((r_cut2 * r_cut2) * r_cut2))));
...
  auto cellList = particles.getCellList<CELL_MEMBAL(3, double)>(r_cut);
In file included from main.cpp:2:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:76:53: error: the value of ‘shift’ is not usable in a constant expression
   76 |   auto cellList = particles.getCellList<CELL_MEMBAL(3, double)>(r_cut);
      |                                                     ^
/net/home/lschuetze/openfpm/bin/openfpm_pdata/include/Vector/vector_dist.hpp:177:86: note: in definition of macro ‘CELL_MEMBAL’
  177 | CELL_MEMBAL(dim,St) CellList_gen<dim, St, Process_keys_lin, Mem_bal<>, shift<dim, St> >
      |                                                                              ^~~

main.cpp:73:10: note: ‘shift’ was not declared ‘constexpr73 |   double shift = (2.0 * ((sigma12 / (((((r_cut2 * r_cut2) * r_cut2) * r_cut2) * r_cut2) * r_cut2)) - (sigma6 / ((r_cut2 * r_cut2) * r_cut2))));
      |          ^~~~~
In file included from main.cpp:2:
/net/home/lschuetze/openfpm/bin/openfpm_pdata/include/Vector/vector_dist.hpp:177:93: error: type/value mismatch at argument 5 in template parameter list fortemplate<unsigned int dim, class T, template<unsigned int <anonymous>, class> class Prock, class Mem_type, class transform, class vector_pos_type> class CellList_gen177 | ,St) CellList_gen<dim, St, Process_keys_lin, Mem_bal<>, shift<dim, St> >
      |                                                                      ^

main.cpp:76:41: note: in expansion of macro ‘CELL_MEMBAL’
   76 |   auto cellList = particles.getCellList<CELL_MEMBAL(3, double)>(r_cut);
      |                                         ^~~~~~~~~~~
/net/home/lschuetze/openfpm/bin/openfpm_pdata/include/Vector/vector_dist.hpp:177:93: note:   expected a type, got ‘(shift < (double)3)’
  177 | ,St) CellList_gen<dim, St, Process_keys_lin, Mem_bal<>, shift<dim, St> >
      |                                                                      ^

main.cpp:76:41: note: in expansion of macro ‘CELL_MEMBAL’
   76 |   auto cellList = particles.getCellList<CELL_MEMBAL(3, double)>(r_cut);
      |                                         ^~~~~~~~~~~
main.cpp:76:63: error: invalid operands of types ‘<unresolved overloaded function type>’ anddouble’ to binary ‘operator>’
   76 |   auto cellList = particles.getCellList<CELL_MEMBAL(3, double)>(r_cut);
      |                                                               ^~~~~~~~
      |                                                                 |
      |                                                                 double
@lschuetze
Copy link
Collaborator Author

The variable definition shift in line 73 masks the definition of the shift introduced by the macro CELL_MEMBRAL which is expanded to CellList_gen<dim, St, Process_keys_lin, Mem_bal<>, shift<dim, St> >.

We could change our code generator such that the definition of cell lists is always before user defined variables so there is no mismatch anymore. I applied this solution by hand and it is sufficient.

@lschuetze lschuetze added bug Something isn't working m2m Related to the model-to-model transformation labels Feb 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working m2m Related to the model-to-model transformation
Projects
None yet
Development

No branches or pull requests

1 participant