Skip to content

Commit f33b2eb

Browse files
authored
Fix -Wconversion warnings (#1508)
* add -Wconversion * mark generic constructors as explicit to resolve ambiguities * restrict ListOf constructor to resolve ambiguities * expand use of R_xlen_t * remove unnecesary use of R_xlen_t * fix several implicit casts * update ChangeLog * move stuff up * enable implicit conversion from List * enable implicit conversion when List can be constructed from U * enable implicit conversion for any wrap-able type without an explicit operator DataFrame() * directly exclude InputParameter types to avoid self-referencing DataFrame_Impl, which doesn't play well with older gcc, probably clang either
1 parent db4971e commit f33b2eb

38 files changed

Lines changed: 279 additions & 183 deletions

‎.github/workflows/werror.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
env:
1010
_R_CHECK_FORCE_SUGGESTS_: "false"
11-
RCPP_CXXFLAGS: "-Werror -Wall -Wextra"
11+
RCPP_CXXFLAGS: "-Werror -Wall -Wextra -Wconversion"
1212

1313
jobs:
1414
ci:

‎ChangeLog‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
2026-09-05 Iñaki Ucar <iucar@fedoraproject.org>
2+
3+
* .github/workflows/werror.yaml: Add -Wconversion
4+
5+
* inst/include/Rcpp/macros/interface.h: Mark GenericProxy constructor as
6+
explicit to resolve ambiguities with validated SEXP constructors
7+
* inst/include/Rcpp/vector/Vector.h: Idem
8+
* inst/include/Rcpp/DataFrame.h: Idem with ad hoc constructor
9+
* inst/include/Rcpp/vector/ListOf.h: In this case, the implicit conversion
10+
of ListOf<T> e.g. from List is intentional, so the previously generic
11+
constructor now gets restricted to cases that are convertible to SEXP
12+
13+
* inst/include/Rcpp/stats/dpq/dpq.h: Expand and homogenize R_xlen_t use
14+
* inst/include/Rcpp/sugar/functions/head.h: Idem
15+
* inst/include/Rcpp/vector/MatrixColumn.h: Idem
16+
* inst/include/Rcpp/vector/MatrixRow.h: Idem
17+
* inst/include/Rcpp/vector/RangeIndexer.h: Idem
18+
* inst/include/Rcpp/internal/wrap.h: Idem and fix potential integer overflow
19+
* inst/include/Rcpp/sugar/operators/Comparator_With_One_Value.h: Idem and
20+
fix a bug where rhs_is_na may return NaN instead of NA_INTEGER
21+
* inst/include/Rcpp/Language.h: Revert incorrect use of R_xlen_t
22+
* inst/include/Rcpp/vector/SubMatrix.h: Idem, because Matrix rows and cols
23+
are restricted to int anyway
24+
25+
* src/date.cpp: Add missing explicit integer narrowing
26+
* inst/tinytest/cpp/dates.cpp: Add static_cast to time_t
27+
* inst/tinytest/cpp/language.cpp: Fix implicit cast to int
28+
* inst/tinytest/cpp/Matrix.cpp: Idem
29+
* inst/tinytest/cpp/Module.cpp: Idem
30+
* inst/tinytest/cpp/String.cpp: Idem
31+
* inst/tinytest/cpp/Vector.cpp: Idem
32+
* inst/include/Rcpp/vector/Matrix.h: Idem
33+
* inst/include/Rcpp/vector/Vector.h: Idem
34+
* inst/include/Rcpp/sugar/Range.h: Idem
35+
* inst/include/Rcpp/sugar/functions/seq_along.h: Idem
36+
* inst/include/Rcpp/sugar/functions/rowSums.h: Idem
37+
* inst/include/Rcpp/sugar/functions/sample.h: Idem
38+
* inst/include/Rcpp/sugar/matrix/col.h: Idem
39+
* inst/include/Rcpp/sugar/matrix/diag.h: Idem
40+
* inst/include/Rcpp/sugar/matrix/lower_tri.h: Idem
41+
* inst/include/Rcpp/sugar/matrix/outer.h: Idem
42+
* inst/include/Rcpp/sugar/matrix/row.h: Idem
43+
* inst/include/Rcpp/sugar/matrix/upper_tri.h: Idem
44+
* inst/include/Rcpp/sugar/functions/var.h: Add static_cast to double
45+
* inst/include/Rcpp/vector/Subsetter.h: Add several necessary static_cast
46+
* inst/include/Rcpp/vector/MatrixBase.h: Idem
47+
148
2026-09-04 Dirk Eddelbuettel <edd@debian.org>
249

350
* DESCRIPTION (Version, Date): Roll micro version and date

‎inst/include/Rcpp/DataFrame.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11

22
// DataFrame.h: Rcpp R/C++ interface class library -- data frames
33
//
4-
// Copyright (C) 2010 - 2026 Dirk Eddelbuettel and Romain Francois
4+
// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
56
//
67
// This file is part of Rcpp.
78
//
@@ -46,7 +47,12 @@ namespace Rcpp{
4647
set__(other) ;
4748
}
4849

49-
template <typename T>
50+
// enabled for everything except the InputParameter family (which
51+
// each offer their own operator T() conversion), so this doesn't
52+
// compete with that conversion, while still accepting anything
53+
// wrap()-able, including third-party types (e.g. arma::mat)
54+
template <typename T, typename std::enable_if<
55+
!traits::is_input_parameter<T>::value, int>::type = 0>
5056
DataFrame_Impl( const T& obj ) ;
5157

5258
DataFrame_Impl& operator=( DataFrame_Impl& other){

‎inst/include/Rcpp/InputParameter.h‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
//
33
// InputParameter.h: Rcpp R/C++ interface class library --
44
//
5-
// Copyright (C) 2013 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2013 - 2025 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
67
//
78
// This file is part of Rcpp.
89
//
@@ -94,6 +95,21 @@ namespace Rcpp {
9495
struct input_parameter<const T&> {
9596
typedef typename Rcpp::ConstReferenceInputParameter<T> type ;
9697
} ;
98+
99+
// detects the InputParameter family, which each offer their own
100+
// operator T() converting to whatever type they wrap. Generic
101+
// wrap()-based converting constructors (e.g. DataFrame_Impl) use this
102+
// to exclude these types, so that they don't compete with them
103+
template <typename T>
104+
struct is_input_parameter : false_type {};
105+
template <typename T>
106+
struct is_input_parameter<InputParameter<T>> : true_type {};
107+
template <typename T>
108+
struct is_input_parameter<ReferenceInputParameter<T>> : true_type {};
109+
template <typename T>
110+
struct is_input_parameter<ConstInputParameter<T>> : true_type {};
111+
template <typename T>
112+
struct is_input_parameter<ConstReferenceInputParameter<T>> : true_type {};
97113
}
98114

99115
}

‎inst/include/Rcpp/Language.h‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Language.h: Rcpp R/C++ interface class library -- language objects (calls)
33
//
44
// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and Iñaki Ucar
56
//
67
// This file is part of Rcpp.
78
//
@@ -189,7 +190,7 @@ namespace Rcpp{
189190
class unary_call : public std::function<RESULT_TYPE(T)> {
190191
public:
191192
unary_call( Language call_ ) : call(call_), proxy(call_,1) {}
192-
unary_call( Language call_, R_xlen_t index ) : call(call_), proxy(call_,index){}
193+
unary_call( Language call_, int index ) : call(call_), proxy(call_,index){}
193194
unary_call( Function fun ) : call( fun, R_NilValue), proxy(call,1) {}
194195

195196
RESULT_TYPE operator()( const T& object ){
@@ -206,7 +207,7 @@ namespace Rcpp{
206207
class binary_call : public std::function<RESULT_TYPE(T1,T2)> {
207208
public:
208209
binary_call( Language call_ ) : call(call_), proxy1(call_,1), proxy2(call_,2) {}
209-
binary_call( Language call_, R_xlen_t index1, R_xlen_t index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){}
210+
binary_call( Language call_, int index1, int index2 ) : call(call_), proxy1(call_,index1), proxy2(call_,index2){}
210211
binary_call( Function fun) : call(fun, R_NilValue, R_NilValue), proxy1(call,1), proxy2(call,2){}
211212

212213
RESULT_TYPE operator()( const T1& o1, const T2& o2 ){

‎inst/include/Rcpp/api/meat/DataFrame.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Copyright (C) 2013 Romain Francois
1+
// Copyright (C) 2013 - 2025 Romain Francois
2+
// Copyright (C) 2026 Romain Francois and Iñaki Ucar
23
//
34
// This file is part of Rcpp.
45
//
@@ -21,7 +22,8 @@
2122
namespace Rcpp{
2223

2324
template <template <class> class StoragePolicy>
24-
template <class T>
25+
template <class T, typename std::enable_if<
26+
!traits::is_input_parameter<T>::value, int>::type>
2527
DataFrame_Impl<StoragePolicy>::DataFrame_Impl( const T& obj){
2628
set__(Shield<SEXP>(wrap(obj)));
2729
}

‎inst/include/Rcpp/internal/wrap.h‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ namespace Rcpp {
564564
template <typename T>
565565
inline SEXP wrap_dispatch_matrix_logical(const T& object, ::Rcpp::traits::true_type) {
566566
int nr = object.nrow(), nc = object.ncol();
567-
Shield<SEXP> res(Rf_allocVector(LGLSXP, nr * nc));
567+
Shield<SEXP> res(Rf_allocVector(LGLSXP, static_cast<R_xlen_t>(nr) * nc));
568568
int k=0;
569569
int* p = LOGICAL(res);
570570
for (int j=0; j<nc; j++)
@@ -581,7 +581,7 @@ namespace Rcpp {
581581
inline SEXP wrap_dispatch_matrix_primitive(const T& object) {
582582
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<STORAGE>::rtype;
583583
int nr = object.nrow(), nc = object.ncol();
584-
Shield<SEXP> res(Rf_allocVector(RTYPE, nr*nc));
584+
Shield<SEXP> res(Rf_allocVector(RTYPE, static_cast<R_xlen_t>(nr)*nc));
585585

586586
int k=0;
587587
STORAGE* p = r_vector_start< RTYPE>(res);
@@ -603,7 +603,7 @@ namespace Rcpp {
603603
template <typename T>
604604
inline SEXP wrap_dispatch_matrix_not_logical(const T& object, ::Rcpp::traits::r_type_string_tag) {
605605
int nr = object.nrow(), nc = object.ncol();
606-
Shield<SEXP> res(Rf_allocVector(STRSXP, nr*nc));
606+
Shield<SEXP> res(Rf_allocVector(STRSXP, static_cast<R_xlen_t>(nr)*nc));
607607

608608
int k=0;
609609
for (int j=0; j<nc; j++)
@@ -619,7 +619,7 @@ namespace Rcpp {
619619
template <typename T>
620620
inline SEXP wrap_dispatch_matrix_not_logical(const T& object, ::Rcpp::traits::r_type_generic_tag) {
621621
int nr = object.nrow(), nc = object.ncol();
622-
Shield<SEXP> res(Rf_allocVector(VECSXP, nr*nc));
622+
Shield<SEXP> res(Rf_allocVector(VECSXP, static_cast<R_xlen_t>(nr)*nc));
623623

624624
int k=0;
625625
for (int j=0; j<nc; j++)
@@ -665,12 +665,12 @@ namespace Rcpp {
665665

666666
template <typename T, typename elem_type>
667667
inline SEXP wrap_dispatch_importer__impl__prim(const T& object, ::Rcpp::traits::false_type) {
668-
int size = object.size();
668+
R_xlen_t size = object.size();
669669
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<elem_type>::rtype;
670670
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
671671
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE;
672672
CTYPE* start = r_vector_start<RTYPE>(x);
673-
for (int i=0; i<size; i++) {
673+
for (R_xlen_t i=0; i<size; i++) {
674674
start[i] = object.get(i);
675675
}
676676
return x;
@@ -679,12 +679,12 @@ namespace Rcpp {
679679

680680
template <typename T, typename elem_type>
681681
inline SEXP wrap_dispatch_importer__impl__prim(const T& object, ::Rcpp::traits::true_type) {
682-
int size = object.size();
682+
R_xlen_t size = object.size();
683683
const int RTYPE = ::Rcpp::traits::r_sexptype_traits<elem_type>::rtype;
684684
Shield<SEXP> x(Rf_allocVector(RTYPE, size));
685685
typedef typename ::Rcpp::traits::storage_type<RTYPE>::type CTYPE;
686686
CTYPE* start = r_vector_start<RTYPE>(x);
687-
for (int i=0; i<size; i++) {
687+
for (R_xlen_t i=0; i<size; i++) {
688688
start[i] = caster<elem_type,CTYPE>(object.get(i));
689689
}
690690
return x;
@@ -698,19 +698,19 @@ namespace Rcpp {
698698

699699
template <typename T, typename elem_type>
700700
inline SEXP wrap_dispatch_importer__impl(const T& object, ::Rcpp::traits::r_type_string_tag) {
701-
int size = object.size();
701+
R_xlen_t size = object.size();
702702
Shield<SEXP> x(Rf_allocVector(STRSXP, size));
703-
for (int i=0; i<size; i++) {
703+
for (R_xlen_t i=0; i<size; i++) {
704704
SET_STRING_ELT(x, i, make_charsexp(object.get(i)));
705705
}
706706
return x;
707707
}
708708

709709
template <typename T, typename elem_type>
710710
inline SEXP wrap_dispatch_importer__impl(const T& object, ::Rcpp::traits::r_type_generic_tag) {
711-
int size = object.size();
711+
R_xlen_t size = object.size();
712712
Shield<SEXP> x(Rf_allocVector(VECSXP, size));
713-
for (int i=0; i<size; i++) {
713+
for (R_xlen_t i=0; i<size; i++) {
714714
SET_VECTOR_ELT(x, i, object.wrap(i));
715715
}
716716
return x;

‎inst/include/Rcpp/macros/interface.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Copyright (C) 2013 Romain Francois
1+
// Copyright (C) 2013 - 2025 Romain Francois
2+
// Copyright (C) 2026 Romain Francois and Iñaki Ucar
23
//
34
// This file is part of Rcpp.
45
//
@@ -31,7 +32,7 @@ __CLASS__& operator=(const __CLASS__& rhs) { \
3132
return Storage::copy__(rhs) ; \
3233
} \
3334
template <typename Proxy> \
34-
__CLASS__( const GenericProxy<Proxy>& proxy ){ \
35+
explicit __CLASS__( const GenericProxy<Proxy>& proxy ){ \
3536
Storage::set__( proxy.get() ) ; \
3637
}
3738

@@ -42,7 +43,7 @@ __CLASS__& operator=(const __CLASS__& rhs) { \
4243
return Storage::copy__(rhs) ; \
4344
} \
4445
template <typename Proxy> \
45-
__CLASS__( const GenericProxy<Proxy>& proxy ){ \
46+
explicit __CLASS__( const GenericProxy<Proxy>& proxy ){ \
4647
Storage::set__( proxy.get() ) ; \
4748
}
4849

0 commit comments

Comments
 (0)