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

optional: Import tl's optional type #2327

Merged
merged 2 commits into from
Jul 9, 2023

Conversation

CohenArthur
Copy link
Member

This is directly adapted from https://github.com/TartanLlama/optional.
This class is tested for C++11 and on GCC 4.8, which is our target mimimum
GCC version.

This optional class provides multiple advantages over our existing
implementation of Optional:

  • Easier API (no static constructors)
  • Better method chaining (and_then, map...)
  • Harsher semantics (no assign-through on Optional<T&>)

Over the std::optional type present in C++'s standard library, this type offers:

  • Optional references without going through std::reference_wrapper
  • Better method chaining

For these reasons, I am in favor of importing it directly rather than
adapt C++14's std::optional type to work with C++11 and GCC 4.8.

gcc/rust/ChangeLog:

* util/optional.h: New file.

@tschwinge
Copy link
Member

#2317 (comment) and following messages apply here, too.

Copy link
Member

@philberty philberty left a comment

Choose a reason for hiding this comment

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

Goooo for it....

Project Pineapple automation moved this from In progress to Reviewer approved Jun 27, 2023
static constexpr nullopt_t nullopt{nullopt_t::do_not_use{},
nullopt_t::do_not_use{}};

class bad_optional_access : public std::exception
Copy link
Member

Choose a reason for hiding this comment

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

can this be removed then

#define TL_OPTIONAL_VERSION_PATCH 0

// TODO: Are these okay?
#include <exception>
Copy link
Member

Choose a reason for hiding this comment

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

A bootstrap build will show if it works, I say we should include this into the rust-session-manager and merge it and see how MArks build farm copes with it before merging the other code so we can fix anything we find

Copy link
Member

Choose a reason for hiding this comment

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

IIRC, gcc is built without exceptions https://gcc.gnu.org/codingconventions.html#Exceptions

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it's okay to modify the file so it includes "rust-system.h" instead of the poisoned headers. As mentioned on the #2317 PR, I'll split up the commit into adding the original file and then modifying it.

This is directly adapted from https://github.com/TartanLlama/optional.
This class is tested for C++11 and on GCC 4.8, which is our target mimimum
GCC version.

This optional class provides multiple advantages over our existing
implementation of Optional<T>:

- Easier API (no static constructors)
- Better method chaining (and_then, map...)
- Harsher semantics (no assign-through on Optional<T&>)

Over the std::optional type present in C++'s standard library, this type offers:

- Optional references without going through std::reference_wrapper
- Better method chaining

For these reasons, I am in favor of importing it directly rather than
adapt C++14's std::optional type to work with C++11 and GCC 4.8.

gcc/rust/ChangeLog:

	* util/optional.h: New file.
@tschwinge
Copy link
Member

Re commit "optional: Adapt class to GCC's standards": "it also formats the file according to our .clang-format": that's generally frowned upon, as it makes it nearly impossible to see what actually has been changed. Instead, how about making such files exempt from clang-format, via some magic in .github/workflows/clang-format.yml (is there an option to skip/exclude individual files -- or if only directories, put such things into an gcc/rust/imported or similar directory?), or use markers to have clang-format ignore this file, like // clang-format off on top of the file, https://clang.llvm.org/docs/ClangFormatStyleOptions.html#disabling-formatting-on-a-piece-of-code? (That's however just my opinion/preference).

@CohenArthur
Copy link
Member Author

@tschwinge I would rather split this commit in two, then - one to format the file, and one to make the necessary changes. Any option other than adding // clang-format off at the top of the file will cause unaware contributors (like me) to autoformat the file regularly. I'm also in favor of having it formatted

@tschwinge
Copy link
Member

OK, fair point about local auto-formatting. But why not // clang-format off at the top of the file then, which ought to take care of this issue?

@CohenArthur
Copy link
Member Author

Oh, I'm good with that too! But is it okay for GCC?

@tschwinge
Copy link
Member

is it okay for GCC?

What problem would you anticipate?

@CohenArthur
Copy link
Member Author

just that the lack of respect of the coding style could be one of the review comments on the patch. but I'm good with dealing with that problem when it arrives :) I'll amend those commits

@CohenArthur CohenArthur force-pushed the optional-type branch 2 times, most recently from fdd984f to c5beebf Compare June 29, 2023 09:53
This commit removes the poisoned includes as well as the exception throwing
mechanism of the original class.

gcc/rust/ChangeLog:

	* util/optional.h: Adapt for GCC.
Comment on lines +1283 to +1284

gcc_unreachable();
Copy link
Member

Choose a reason for hiding this comment

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

Just to verify: does gcc_unreachable here (and likewise elsewhere) have the correct semantics:

gccrs/gcc/system.h

Lines 828 to 831 in 7af86ea

/* Use gcc_unreachable() to mark unreachable locations (like an
unreachable default case of a switch. Do not use gcc_assert(0). */
#if (GCC_VERSION >= 4005) && !ENABLE_ASSERT_CHECKING
#define gcc_unreachable() __builtin_unreachable ()

..., that is: an unreachable code path (..., and the compiler should optimized based on that knowledge), or should the original throw rather map onto an abort:

gccrs/gcc/system.h

Lines 789 to 795 in 7af86ea

/* Redefine 'abort' to report an internal error w/o coredump, and
reporting the location of the error in the source file.
Instead of directly calling 'abort' or 'fancy_abort', GCC code
should normally call 'internal_error' with a specific message. */
extern void fancy_abort (const char *, int, const char *)
ATTRIBUTE_NORETURN ATTRIBUTE_COLD;
#define abort() fancy_abort (__FILE__, __LINE__, __FUNCTION__)

..., or thus internal_error?

Copy link
Member Author

Choose a reason for hiding this comment

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

ah, I know I have used it in other places not as an optimization unreachable but more as a "should not reach here unreachable", so under the impression that it would always do the fancy_abort. I guess we could have a rust_unreachable, at least for now, because we're nowhere close to caring about optimization and always want those fancy aborts

Copy link
Member Author

Choose a reason for hiding this comment

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

@tschwinge I've created a branch so the changes are made. I've added it to tomorrow's community call because I want to discuss it with you. I think there is value in having a macro that always fancy_aborts and in one that is used for optimization only in releases, but I don't think we are there yet

Copy link
Member Author

Choose a reason for hiding this comment

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

In any way, these changes aren't specific to this PR, so for now I think tl::optional should stay similar to the rest of the gccrs codebase and use gcc_unreachable (even if I now think it is "invalid" and should be replaced by fancy_abort). I will fix it once we've reached a decision on the matter tomorrow :)

@CohenArthur CohenArthur added this pull request to the merge queue Jul 9, 2023
Merged via the queue into Rust-GCC:master with commit e74cb82 Jul 9, 2023
9 checks passed
Project Pineapple automation moved this from Reviewer approved to Done Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

None yet

4 participants