-
Notifications
You must be signed in to change notification settings - Fork 252
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
fix: emit optimizing inspect
#529
Open
JohelEGP
wants to merge
7
commits into
hsutter:main
Choose a base branch
from
JohelEGP:is_as
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c23251a
refactor: return `std::bool_constant` from built-in `is`
JohelEGP f4cb90e
refactor: emit inspect alternative wildcard without `else`
JohelEGP 789a73e
fix: emit optimizing `inspect`
JohelEGP 080b563
test: add unit test `pure2-bugfix-for-optimizing-inspect`
JohelEGP 6101c60
test: add more test cases
JohelEGP 00e6846
test: add more test cases
JohelEGP 06d8e5f
test: add test cases for `is` overloads of `std::` types
JohelEGP File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
regression-tests/pure2-bugfix-for-optimizing-inspect.cpp2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
main: () = { | ||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
// Condition is `false_type` for `int`, alternative not instantiated. | ||
is std::string = x.substr(0); | ||
is _ = true; | ||
}]] | ||
}(1); | ||
|
||
{ | ||
pred := :<T> (y: T) -> std::true_type = { | ||
std::terminate(); | ||
return (); | ||
}; | ||
[[assert Testing: inspect 1 -> bool { | ||
// Condition is `true_type` for `int`, statement unconditionally executed. | ||
is (pred) = true; | ||
is _ = false; | ||
}]] | ||
} | ||
|
||
_ = :(x) = { | ||
pred := :<T> (y: T) -> bool requires std::is_same_v<T, long> = { | ||
std::terminate(); | ||
return true; | ||
}; | ||
[[assert Testing: inspect x -> bool { | ||
// Condition is non-viable for `int`, alternative not instantiated. | ||
is (pred) = x.substr(0); | ||
is _ = true; | ||
}]] | ||
}(1); | ||
|
||
// Exercise all built-in `is` overloads. | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is std::vector = x.substr(0); | ||
is std::array = x.substr(0); | ||
is std::string = x.substr(0); | ||
is std::exception = x.substr(0); | ||
is cpp2::empty = x.substr(0); | ||
is (std::ranges::empty) = x.substr(0); | ||
is ("") = x.substr(0); | ||
is _ = true; | ||
}]] | ||
}(1); | ||
_ = :(x) = { | ||
[[assert Testing: inspect x& -> bool { | ||
is std::exception = x.substr(0); | ||
is _ = true; | ||
}]] | ||
}(1); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is std::vector = true; | ||
is _ = false; | ||
}]] | ||
}(:std::vector = (1)); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is std::array = true; | ||
is _ = false; | ||
}]] | ||
}(:std::array = (1)); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is int = true; | ||
is _ = false; | ||
}]] | ||
}(1); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
// FIXME | ||
// Using `std::ranges::subrange<std::add_pointer_t<i32>>::view_interface` fails. | ||
// The alternative is elided due to the `is` being ambiguous. | ||
// Like in P2392, the cases of the built-in `is` should be a chain of conditions. | ||
// Using overloads to implement that is tedious and error-prone. | ||
is std::ranges::view_interface<std::ranges::subrange<std::add_pointer_t<i32>>> = true; | ||
is _ = false; | ||
}]] | ||
}(:std::ranges::subrange<std::add_pointer_t<i32>> = ()); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is std::exception = true; | ||
is _ = false; | ||
}]] | ||
}(:std::bad_exception = ()); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x& -> bool { | ||
is std::exception = true; | ||
is _ = false; | ||
}]] | ||
}(:std::bad_exception = ()); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is cpp2::empty = true; | ||
is _ = false; | ||
}]] | ||
}(:std::optional<i32> = ()); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is (std::ranges::empty) = true; | ||
is _ = false; | ||
}]] | ||
}(:std::vector<i32> = ()); | ||
|
||
_ = :(x) = { | ||
[[assert Testing: inspect x -> bool { | ||
is (1) = true; | ||
is _ = false; | ||
}]] | ||
}(1); | ||
|
||
// Exercise `is` overloads for `std::` types. | ||
|
||
{ | ||
f := :(x) = { | ||
(pred := :<T> (y: T) -> bool requires std::is_same_v<T, i32> = { | ||
std::terminate(); | ||
return true; | ||
}) | ||
[[assert Testing: inspect x -> bool { | ||
is (:std::span<i32> = ()) = x.substr(0); | ||
is (pred) = x.substr(0); | ||
is (1) = true; | ||
is _ = false; | ||
}]] | ||
|
||
(pred := :<T> (y: T) -> std::true_type = { | ||
std::terminate(); | ||
return (); | ||
}) { | ||
[[assert Testing: inspect x -> bool { | ||
is (pred) = true; | ||
is _ = false; | ||
}]] | ||
} | ||
}; | ||
f(:std::any = 1); | ||
f(:std::optional = 1); | ||
} | ||
} |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a [BUG].
This is not a [SUGGESTION] (but could be taken as one).
I have thought of having the function expression's signature be
<T> (x: T)
so that I could use
T::view_interface
in the line I'm commenting on.But it doesn't work because it's missing the lowered Cpp1
typename
in front of it.Cppfront already recognizes a template function's template parameters in its function parameter list to omit wrapping a function parameter's type in
cpp2::in
.So do you think it'd make sense to extend Cpp2 to not require
typename
in such type-only contexts?I'm also wondering how you plan to support such disambiguating
typename
in Cpp2.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The more I think about it,
the more I'm convinced that
"yes, this should be a thing!"
This is basically Cpp1's optional
typename
,but for Cpp2's type-only contexts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #531 for this.