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

Options: Delete copy constructor and copy assignment #2861

Merged
merged 13 commits into from
Feb 16, 2024

Conversation

bendudson
Copy link
Contributor

Accidentally copying Options leads to unexpected bugs where changes (e.g documentation) changes a copy rather than the original. Usually what is intended is a reference, but the author omits an &.

Example: bendudson/hermes-3#204 (comment)

Copy assignment is also ambiguous: Is it the value, the attributes, or the children that should be copied?

Now if a copy is really intended, there is a .copy() method (thanks Rust!) that makes a deep copy.

Accidentally copying Options leads to unexpected bugs where
changes (e.g documentation) changes a copy rather than the
original. Usually what is intended is a reference, but the
author omits an `&`.

Copy assignment is also ambiguous: Is it the value, the
attributes, or the children that should be copied?

Now if a copy is really intended, there is a `.copy()` method
(thanks Rust!)
Copy link
Contributor

github-actions bot commented Feb 7, 2024

clang-tidy review says "All clean, LGTM! 👍"

Should be a reference. Caught because copy constructor is
deleted.
Copy link
Contributor

github-actions bot commented Feb 7, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

github-actions bot commented Feb 7, 2024

clang-tidy review says "All clean, LGTM! 👍"

Copy link
Contributor

github-actions bot commented Feb 7, 2024

clang-tidy review says "All clean, LGTM! 👍"

bendudson and others added 3 commits February 7, 2024 21:14
C++ initializer_lists don't play nicely with uncopyable types
https://blog.knatten.org/2018/10/05/why-you-cant-list-initialize-containers-of-non-copyable-types/

List initialization of Options now constructs nested initializer lists
of `Options::CopyableOptions`, then walks this tree to move data into
Options. This simplifies some of the logic, since we're now traversing
the tree of Options starting from the root.
Tests that use copy constructor changed to test copy method.

Unit tests compile but failing. Probably initializer_list
construction issue.
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clang-tidy made some suggestions

include/bout/options.hxx Show resolved Hide resolved
include/bout/options.hxx Show resolved Hide resolved
include/bout/options.hxx Outdated Show resolved Hide resolved
include/bout/options.hxx Outdated Show resolved Hide resolved
include/bout/options.hxx Outdated Show resolved Hide resolved
include/bout/options.hxx Outdated Show resolved Hide resolved
bendudson and others added 2 commits February 8, 2024 10:43
When setting a value, the `is_section` member should be `false`,
and the "source" attribute must be set.
Copy link
Contributor

github-actions bot commented Feb 8, 2024

clang-tidy review says "All clean, LGTM! 👍"

@ZedThree
Copy link
Member

Looks like the failing tests are just because a fedora mirror 404'd, rerunning now.

LGTM, thanks @bendudson! In C++26 we might even be able to do =delete("Use .copy() instead"), so that'll be nice in about 10 years 😄

Please could you put a line under "breaking changes" in CHANGELOG.md?

@ZedThree
Copy link
Member

For some reason, only the Fedora build is failing with:

/home/test/BOUT-dev/src/solver/impls/cvode/cvode.cxx: In instantiation of 'std::vector<double> CvodeSolver::create_constraints(const std::vector<Solver::VarStr<T> >&) [with FieldType = Field3D]':
/home/test/BOUT-dev/src/solver/impls/cvode/cvode.cxx:358:46:   required from here
/home/test/BOUT-dev/src/solver/impls/cvode/cvode.cxx:488:25: note:   358 |     auto f3d_constraints = create_constraints(f3d);
/home/test/BOUT-dev/src/solver/impls/cvode/cvode.cxx:488:25: note:       |                            ~~~~~~~~~~~~~~~~~~^~~~~
/home/test/BOUT-dev/src/solver/impls/cvode/cvode.cxx:488:25: error: use of deleted function 'Options::Options(const Options&)'

Not sure why the other compilers aren't catching that! Will investigate locally

@ZedThree
Copy link
Member

Oh, this is because most of our builds use sundials 4.1.0, and we have a guard

#if not(SUNDIALS_VERSION_MAJOR >= 3 and SUNDIALS_VERSION_MINOR >= 2)
if (apply_positivity_constraints) {
throw BoutException("The apply_positivity_constraints option is only available with "
"SUNDIALS>=3.2.0");
}
#else
if (apply_positivity_constraints) {
auto f2d_constraints = create_constraints(f2d);
auto f3d_constraints = create_constraints(f3d);

which is not correct. I'll make a separate PR for that fix (we also have some deprecations to fix, I'll have a stab at those too)

In `create_constraints` the variable options were being copied,
so that the `positivity_constraint` documentation was in a copy
that was discarded. Should use a reference rather than copy.
When a compiler error is issued, it will usually print the line
where the method is deleted.
Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

bendudson and others added 2 commits February 15, 2024 18:45
C++ compilers can cast char* to bool, rather than std::string.
Copy link
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@bendudson bendudson merged commit 3a29bcd into next Feb 16, 2024
1 check passed
@bendudson bendudson deleted the next-options-nocopy branch February 16, 2024 04:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants