-
-
Notifications
You must be signed in to change notification settings - Fork 499
fix: resolve Symbol::For overload ambiguity #1742
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
Open
umuoy1
wants to merge
4
commits into
nodejs:main
Choose a base branch
from
umuoy1:fix/symbol-for-overload-ambiguity
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.
+194
−2
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
This file contains hidden or 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 hidden or 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 hidden or 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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |||||
| #include <chrono> | ||||||
| #include <string> | ||||||
| #include <string_view> | ||||||
| #include <type_traits> | ||||||
| #include <utility> | ||||||
| #include <vector> | ||||||
|
|
||||||
| // VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known | ||||||
|
|
@@ -786,6 +788,41 @@ class String : public Name { | |||||
| const; ///< Converts a String value to a UTF-16 encoded C++ string. | ||||||
| }; | ||||||
|
|
||||||
| namespace details { | ||||||
|
|
||||||
| // This overload set must mirror the non-template Symbol::For overloads. | ||||||
| struct string_convertible_probe { | ||||||
| static void select(const std::string&); | ||||||
| static void select(std::string_view); | ||||||
| static void select(const char*); | ||||||
| static void select(String); | ||||||
| static void select(napi_value); | ||||||
| }; | ||||||
|
|
||||||
| template <typename T, typename = void> | ||||||
| struct has_unambiguous_symbol_for_overload : std::false_type {}; | ||||||
|
|
||||||
| template <typename T> | ||||||
| struct has_unambiguous_symbol_for_overload< | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| T, | ||||||
| std::void_t<decltype(string_convertible_probe::select(std::declval<T>()))>> | ||||||
| : std::true_type {}; | ||||||
|
|
||||||
| // Enable the template overload only for string-like arguments that have no | ||||||
| // unique best match among the non-template Symbol::For overloads. | ||||||
| // | ||||||
| // Exclude nullptr because it matches the pointer overloads equally well and | ||||||
| // cannot safely initialize a std::string_view. | ||||||
| template <typename T> | ||||||
| using enable_if_ambiguous_symbol_for_t = | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| std::enable_if_t<!std::is_null_pointer_v<std::decay_t<T>> && | ||||||
| std::is_convertible_v<T, const std::string&> && | ||||||
| std::is_convertible_v<T, std::string_view> && | ||||||
| !has_unambiguous_symbol_for_overload<T>::value, | ||||||
| int>; | ||||||
|
|
||||||
| } // namespace details | ||||||
|
|
||||||
| /// A JavaScript symbol value. | ||||||
| class Symbol : public Name { | ||||||
| public: | ||||||
|
|
@@ -831,6 +868,11 @@ class Symbol : public Name { | |||||
| // Create a symbol in the global registry, UTF-8 encoded cpp string view | ||||||
| static MaybeOrValue<Symbol> For(napi_env env, std::string_view description); | ||||||
|
|
||||||
| // Resolve otherwise ambiguous string-like arguments through the | ||||||
| // std::string_view overload | ||||||
| template <typename T, details::enable_if_ambiguous_symbol_for_t<T&&> = 0> | ||||||
| static MaybeOrValue<Symbol> For(napi_env env, T&& description); | ||||||
|
|
||||||
| // Create a symbol in the global registry, C style string (null terminated) | ||||||
| static MaybeOrValue<Symbol> For(napi_env env, const char* description); | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or 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 hidden or 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.