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

Fixing buffer_appender's ++ slicing. #1822

Merged
merged 5 commits into from
Aug 18, 2020
Merged

Conversation

brevzin
Copy link
Contributor

@brevzin brevzin commented Aug 14, 2020

buffer_appender's pre- and post-fix increment operators slice down to back_inserter, which means it actually breaks the iterator requirements (which would be actually checked in C++20, it is not weakly_incrementable) and also breaks any code that assumes that ++it and it have the same type (as illustrated in the test case, a reduced form of how we format our optional type, which no longer compiles).

buffer_appender tmp = *this;
++*this;
return tmp;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Given that back_insert_iterator only has like... 4 member functions, and I've already implemented 2 of them (and over-implemented them too, these could both just be return *this;), maybe we should just implement the whole thing in here without std?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine as is.

Copy link
Contributor

@vitaut vitaut left a comment

Choose a reason for hiding this comment

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

Thanks for the PR! Looks good but I suggest simplifying the test case and making it more portable.

@@ -2473,3 +2473,37 @@ TEST(FormatTest, FormatUTF8Precision) {
EXPECT_EQ(result.size(), 5);
EXPECT_EQ(from_u8str(result), from_u8str(str.substr(0, 5)));
}

#ifdef __cpp_decltype_auto
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we make the test work without this, e.g. checking if the types are the same manually?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. Just have a static_assert.

Comment on lines 2478 to 2481
struct lazy_optional {
bool engaged;
std::string value;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd minimize this even further into an empty struct and make the name reflect what we are trying to test here.

buffer_appender tmp = *this;
++*this;
return tmp;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's fine as is.

auto format(check_back_appender, Context& ctx) -> decltype(ctx.out()) {
// needs to satisfy weakly_incrementable
auto out = ctx.out();
static_assert(std::is_same<decltype(++out), decltype(out)&>::value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add a message to fix contbuild.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, done.

@vitaut vitaut merged commit 6be6544 into fmtlib:master Aug 18, 2020
@vitaut
Copy link
Contributor

vitaut commented Aug 18, 2020

Thanks!

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