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

Update idioms.md with changes to match the code now #4587

Merged
merged 4 commits into from
Nov 25, 2024

Conversation

danakj
Copy link
Contributor

@danakj danakj commented Nov 25, 2024

  • Rename TypedInstArgsInfo references to InstLikeTypeInfo. The type was renamed in 07efa02.
  • Update and correct the link to search for ValueStore (and similar) in the Context class. We must avoid the google-doc-style checks rewriting repo: to repository: in the URL.
  • Mention the existance of many types of Store collections now.
  • Remove pre-C++20 idioms in Field detection. Correct the concept based idioms to work.
  • Fix code indenting consistency.

- Rename TypedInstArgsInfo references to InstLikeTypeInfo. The type was
  renamed in 07efa02.
- Update and correct the link to search for ValueStore (and similar) in
  the Context class. We must avoid the google-doc-style checks
  rewriting `repo:` to `repository:` in the URL.
- Mention the existance of many types of Store collections now.
- Remove pre-C++20 idioms in Field detection. Correct the concept based
  idioms to work.
- Fix code indenting consistency.
@danakj
Copy link
Contributor Author

danakj commented Nov 25, 2024

@zygoloid Maybe you could review? The concept examples here are based on your changes in toolchain/sem_ir/typed_insts.h, but the derived-from version was something I didn't see in the code yet, so I just put it together on godbolt to make sure it would work.

Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

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

Nice, tiny tweak to the comment update at the end though.

toolchain/docs/idioms.md Outdated Show resolved Hide resolved
@danakj danakj requested a review from chandlerc November 25, 2024 19:41
toolchain/docs/idioms.md Outdated Show resolved Hide resolved
Comment on lines 221 to 237
To detect a field with a specific name with a type derived from a specified base
type, use this idiom:

```cpp
// HasField<T> is true if T has a `U field` field,
// where `U` extends `BaseClass`.
template <typename T, bool Enabled = true>
inline constexpr bool HasField = false;
template <typename T>
inline constexpr bool HasField<
T, bool(std::is_base_of_v<BaseClass, decltype(T::field)>)> = true;
```
namespace Internal {

The equivalent concept is:
template <typename T, typename From> concept FieldDerivedFrom =
std::derived_from<std::remove_cvref_t<T>, From>;

```cpp
}

// HasField<T> is true if T has a `U field` field, where `U` extends
// `BaseClass`.
template <typename T> concept HasField = requires (T x) {
{ x.field } -> std::derived_from<BaseClass>;
{ x.field } -> Internal::FieldDerivedFrom<BaseClass>;
};
```
Copy link
Contributor

Choose a reason for hiding this comment

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

If we're not using this idiom, maybe we should remove it from the documentation for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, especially since it needs a helper. SGTM

Copy link
Contributor

@chandlerc chandlerc left a comment

Choose a reason for hiding this comment

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

(LG to me, but good to make sure the template stuff LG to Richard as well before merging.)

@danakj danakj added this pull request to the merge queue Nov 25, 2024
Merged via the queue into carbon-language:trunk with commit 2cb6507 Nov 25, 2024
8 checks passed
@danakj danakj deleted the InstLikeTypeInfo branch November 25, 2024 20:12
bricknerb pushed a commit to bricknerb/carbon-lang that referenced this pull request Nov 28, 2024
…4587)

- Rename TypedInstArgsInfo references to InstLikeTypeInfo. The type was
renamed in 07efa02.
- Update and correct the link to search for ValueStore (and similar) in
the Context class. We must avoid the google-doc-style checks rewriting
`repo:` to `repository:` in the URL.
- Mention the existance of many types of Store collections now.
- Remove pre-C++20 idioms in Field detection. Correct the concept based
idioms to work.
- Fix code indenting consistency.

---------

Co-authored-by: Richard Smith <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants