Skip to content

Commit 5cd45c2

Browse files
committed
Replace defensive Rf_error() with ASSERT() in impl code
These guards are unreachable when exported wrappers validate correctly. ASSERT() is active in debug builds and compiles to nothing in release.
1 parent 6e6ad56 commit 5cd45c2

File tree

5 files changed

+18
-20
lines changed

5 files changed

+18
-20
lines changed

src/day_1985.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ using namespace Rcpp;
33

44
#include "tree_distances.h" /* includes <TreeTools/SplitList.h> */
55
#include "information.h"
6+
#include <cassert>
7+
#include <TreeTools/assert.h>
68

79
#include <TreeTools.h> /* for root_on_node() */
810
#include <TreeTools/root_tree.h> /* for root_on_node() */
@@ -96,7 +98,7 @@ double calc_consensus_info(const List &trees, const LogicalVector &phylo,
9698

9799
std::vector<ClusterTable> tables;
98100
if (std::size_t(n_trees) > tables.max_size()) {
99-
Rf_error("Not enough memory available to compute consensus of so many trees"); // LCOV_EXCL_LINE
101+
ASSERT(false && "Not enough memory for consensus of so many trees"); // LCOV_EXCL_LINE
100102
}
101103

102104
tables.reserve(n_trees);

src/hpart.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ size_t build_node_from_list(const RObject& node,
126126
if (Rf_isInteger(node) || Rf_isReal(node)) {
127127
const IntegerVector leaf_vec(node);
128128
if (leaf_vec.size() != 1) {
129-
Rf_error("List must only contain integers, not vectors of integers"); // #nocov
129+
ASSERT(false && "List must only contain integers, not vectors of integers"); // #nocov
130130
}
131131
const int leaf_label = leaf_vec[0]; // 1-based R leaf label
132132
const size_t leaf_idx = leaf_label - 1; // 0-based label for HNode
@@ -172,7 +172,7 @@ size_t build_node_from_list(const RObject& node,
172172
}
173173

174174
// Invalid node type
175-
Rf_error("Invalid node type"); // #nocov
175+
ASSERT(false && "Invalid node type"); // #nocov
176176
}
177177

178178

src/hpart_relabel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ void recompute_bitsets_postorder(TreeDist::HPart &hpart, const size_t node_idx,
1313
if (node.children.empty()) {
1414
// Leaf node
1515
if (node.leaf_count != 1) {
16-
Rf_error("Leaf node has leaf_count != 1"); // #nocov
16+
ASSERT(node.leaf_count == 1 && "Leaf node has leaf_count != 1"); // #nocov
1717
}
1818
int new_index = mapping[node.label]; // mapping is 0-based
1919
node.label = new_index;

src/information.h

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _TREEDIST_INFO_H
33

44
#include <Rcpp/Lightest>
5-
#include <cassert> /* for log2() */
5+
#include <TreeTools/assert.h>
66
#include <cmath> /* for log2() */
77
#include "ints.h" /* for int16 */
88

@@ -48,14 +48,12 @@ __attribute__((constructor))
4848

4949
inline double split_phylo_info(const int32 n_in, const int32 *n_tip,
5050
const double p) {
51-
if (*n_tip > CT_MAX_LEAVES) {
52-
Rf_error("This many leaves are not yet supported.");
53-
}
51+
ASSERT(*n_tip <= CT_MAX_LEAVES && "This many leaves are not yet supported.");
5452
const int32 n_out = *n_tip - n_in;
55-
assert(p > 0);
56-
assert(p <= 1);
57-
assert(n_in > 1);
58-
assert(n_out > 1);
53+
ASSERT(p > 0);
54+
ASSERT(p <= 1);
55+
ASSERT(n_in > 1);
56+
ASSERT(n_out > 1);
5957
if (p == 1) {
6058
return (l2unrooted[*n_tip] - l2rooted[n_in] - l2rooted[n_out]);
6159
} else {
@@ -74,14 +72,12 @@ inline double split_phylo_info(const int32 n_in, const int32 *n_tip,
7472

7573
inline double split_clust_info(const int32 n_in, const int32 *n_tip,
7674
const double p) {
77-
if (*n_tip > CT_MAX_LEAVES) {
78-
Rf_error("This many leaves are not yet supported.");
79-
}
75+
ASSERT(*n_tip <= CT_MAX_LEAVES && "This many leaves are not yet supported.");
8076
const int32 n_out = *n_tip - n_in;
81-
assert(p > 0);
82-
assert(p <= 1);
83-
assert(n_in > 1);
84-
assert(n_out > 1);
77+
ASSERT(p > 0);
78+
ASSERT(p <= 1);
79+
ASSERT(n_in > 1);
80+
ASSERT(n_out > 1);
8581
return -p * (
8682
(((n_in * l2[n_in]) + (n_out * l2[n_out])) / *n_tip) - l2[*n_tip]
8783
);

src/spr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ IntegerVector confusion(const RawMatrix& x, const RawMatrix& y) {
119119

120120
IntegerMatrix reverse (const IntegerMatrix x) {
121121
if (double(x.nrow()) > double(std::numeric_limits<intx>::max())) {
122-
Rf_error("This many edges are not (yet) supported."); // impl guard; wrapper validates
122+
ASSERT(false && "This many edges are not (yet) supported."); // wrapper validates
123123
}
124124
const intx n_edge = intx(x.nrow());
125125
ASSERT(n_edge % 2 == 0); // Tree is binary

0 commit comments

Comments
 (0)