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

fix: should avoid implicit conversions in pushKV params #5719

Merged
merged 1 commit into from
Nov 21, 2023

Conversation

UdjinM6
Copy link

@UdjinM6 UdjinM6 commented Nov 20, 2023

Issue being fixed or feature implemented

Should fix compilation errors like

masternode/meta.cpp:43:9: error: call to member function 'pushKV' is ambiguous
    ret.pushKV("lastOutboundAttemptElapsed", now - lastOutboundAttempt);
    ^~
masternode/meta.cpp:45:9: error: call to member function 'pushKV' is ambiguous
    ret.pushKV("lastOutboundSuccessElapsed", now - lastOutboundSuccess);
    ^~

on FreeBSD + clang-15

kudos to @MrDefacto for finding the issue and testing the fix

What was done?

Specify now variable type explicitly instead of relying on auto

How Has This Been Tested?

MrDefacto confirmed it compiles with no issues on FreeBSD now

Breaking Changes

n/a

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone (for repository code-owners and collaborators only)

should fix compilation errors like
```
masternode/meta.cpp:43:9: error: call to member function 'pushKV' is ambiguous
    ret.pushKV("lastOutboundAttemptElapsed", now - lastOutboundAttempt);
    ^~
masternode/meta.cpp:45:9: error: call to member function 'pushKV' is ambiguous
    ret.pushKV("lastOutboundSuccessElapsed", now - lastOutboundSuccess);
    ^~
```
on FreeBSD + clang-15

kudos to MrDefacto for finding the issue and testing the fix
@UdjinM6 UdjinM6 added this to the 20.1 milestone Nov 20, 2023
Copy link
Collaborator

@knst knst left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@ogabrielides ogabrielides left a comment

Choose a reason for hiding this comment

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

utACK

Copy link
Member

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

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

utACK; that's a simpler solution than I figured out!

I had figured out this "fix" but this is simplier

Subject: [PATCH] fix: conceptize univalue
---
Index: src/univalue/include/univalue.h
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/univalue/include/univalue.h b/src/univalue/include/univalue.h
--- a/src/univalue/include/univalue.h	(revision 76a086f3e8414a7b1953ee0b879d45e579edb4fc)
+++ b/src/univalue/include/univalue.h	(date 1700371705464)
@@ -23,18 +23,9 @@
         typ = initialType;
         val = initialStr;
     }
-    UniValue(uint64_t val_) {
-        setInt(val_);
-    }
-    UniValue(int64_t val_) {
-        setInt(val_);
-    }
     UniValue(bool val_) {
         setBool(val_);
     }
-    UniValue(int val_) {
-        setInt(val_);
-    }
     UniValue(double val_) {
         setFloat(val_);
     }
@@ -45,6 +36,9 @@
         std::string s(val_);
         setStr(s);
     }
+    UniValue(std::integral auto val_) {
+        setInt(val_);
+    }
 
     void clear();
 
@@ -53,7 +47,12 @@
     bool setNumStr(const std::string& val);
     bool setInt(uint64_t val);
     bool setInt(int64_t val);
-    bool setInt(int val_) { return setInt((int64_t)val_); }
+    bool setInt(std::integral auto val_) {
+        if constexpr (std::is_unsigned_v<decltype(val)>) {
+            return setInt((uint64_t)val_);
+        }
+        return setInt((int64_t)val_);
+    }
     bool setFloat(double val);
     bool setStr(const std::string& val);
     bool setArray();
@@ -122,26 +121,17 @@
         std::string _val(val_);
         return pushKV(key, _val);
     }
-    bool pushKV(const std::string& key, int64_t val_) {
+    template<class T>
+    bool pushKV(const std::string& key, T val_) {
+        static_assert(std::is_trivially_copy_constructible_v<T>, "Type is not trivially copy constructable");
         UniValue tmpVal(val_);
         return pushKV(key, tmpVal);
     }
-    bool pushKV(const std::string& key, uint64_t val_) {
-        UniValue tmpVal(val_);
-        return pushKV(key, tmpVal);
-    }
-    bool pushKV(const std::string& key, bool val_) {
-        UniValue tmpVal(val_);
-        return pushKV(key, tmpVal);
+    template<class T>
+    bool pushKV(const std::string& key, const std::atomic<T>& val_) {
+        return pushKV(key, val_.load());
     }
-    bool pushKV(const std::string& key, int val_) {
-        UniValue tmpVal((int64_t)val_);
-        return pushKV(key, tmpVal);
-    }
-    bool pushKV(const std::string& key, double val_) {
-        UniValue tmpVal(val_);
-        return pushKV(key, tmpVal);
-    }
+
     bool pushKVs(const UniValue& obj);
 
     std::string write(unsigned int prettyIndent = 0,

@PastaPastaPasta PastaPastaPasta merged commit 9861bc7 into dashpay:develop Nov 21, 2023
8 of 9 checks passed
ogabrielides pushed a commit to ogabrielides/dash that referenced this pull request Dec 4, 2023
## Issue being fixed or feature implemented
Should fix compilation errors like
```
masternode/meta.cpp:43:9: error: call to member function 'pushKV' is ambiguous
    ret.pushKV("lastOutboundAttemptElapsed", now - lastOutboundAttempt);
    ^~
masternode/meta.cpp:45:9: error: call to member function 'pushKV' is ambiguous
    ret.pushKV("lastOutboundSuccessElapsed", now - lastOutboundSuccess);
    ^~
```
on FreeBSD + clang-15

kudos to @MrDefacto for finding the issue and testing the fix


## What was done?
Specify `now` variable type explicitly instead of relying on `auto`


## How Has This Been Tested?
MrDefacto confirmed it compiles with no issues on FreeBSD now

## Breaking Changes
n/a

## Checklist:
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have added or updated relevant unit/integration/functional/e2e
tests
- [ ] I have made corresponding changes to the documentation
- [x] I have assigned this pull request to a milestone _(for repository
code-owners and collaborators only)_
@UdjinM6 UdjinM6 modified the milestones: 20.1, 20.0.2 Dec 4, 2023
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.

4 participants