Skip to content

Fix conversion warnings in the library and tests - #2953

Open
gouarin wants to merge 5 commits into
xtensor-stack:masterfrom
gouarin:fix-conversion-warnings
Open

gouarin wants to merge 5 commits into
xtensor-stack:masterfrom
gouarin:fix-conversion-warnings

Conversation

@gouarin

@gouarin gouarin commented Oct 29, 2026

Copy link
Copy Markdown
Member

Checklist

  • The title and commit message(s) are descriptive.
  • Small commits made to fix your PR have been squashed to avoid history pollution.
  • Tests have been added for new features or bug fixes.
  • API of new functions and classes are documented (no public API changes).

Description

The library and the tests now build without -Wconversion, -Wsign-conversion and -Wold-style-cast warnings, including -Wfloat-conversion and clang's -Wimplicit-int-float-conversion. Unique warning sites in the test build:

master this PR
GCC 13 35 0
GCC 15 35 (36 with xsimd) 0
clang 20 42 (43 with xsimd) 0 with SKIP_ON_WERROR

The xbuilder.hpp sites are not in this PR: they hid a bug, fixed in #2952. The 7 clang sites left come from test_xnan_functions checks that already sit under #ifndef SKIP_ON_WERROR because they request lossy result types.

Most fixes change a type: unsigned permutations in swapaxes/moveaxis, unsigned index types in quantile, a uint32_t npy 1.0 header length like the 2.0 reader, numeric_constants defined from std::numbers. Each remaining static_cast has a comment explaining why the value fits. xfunction::operator() gets back the static_cast<size_type>(args) that #2876 dropped; the comment above it still described it. In test_xfft, the test signal no longer mixes integer and float tensors.

Behavior change: numeric_constants<long double> now has full long double precision, and numeric_constants<T> requires a floating-point T. The float and double values are bit-identical.

- swapaxes and moveaxis build their permutation with the unsigned index
  type of the shape instead of the signed strides type. The values are
  axes, and transpose converts them to size_type anyway.
- quantile builds its index list and intermediate shapes with unsigned
  index types, and passes the original signed axis to partition,
  fancy_indexing and moveaxis instead of converting the normalized one
  back.
- call_over_leading_axis computes the number of slices as a ptrdiff_t,
  since it is only used in pointer arithmetic with the signed stride.
- argsort_iter converts the sorted indices to the difference type of
  the data iterator. They come from iota over the data range, so they
  fit.
- xindex_view::element converts the index it receives to size_type.
  reshape_view passes signed indices (from unravel_index) that are never
  negative.
The refactoring to std::apply in xtensor-stack#2876 dropped the
static_cast<size_type>(args)... that the comment above it still
describes. Calling an xfunction with int indices, for example t(0) on
an xindex_view expression, then warned about sign conversion in the
operands' access methods. Restore the cast.
The constants were double literals, so numeric_constants<float>
converted them from double, which -Wfloat-conversion reports. Use the
C++20 std::numbers variable templates instead. The values for float and
double are bit-identical to the previous ones (pi_v / 2, 2 * inv_pi_v
and so on are exact operations). numeric_constants<long double> now has
full long double precision instead of double precision.
xfft divided complex expressions by sizes (N, m, n) and multiplied pi
by an integer tensor, so xoperation.hpp converted size_t to float or
double inside the library. Convert explicitly to the precision the
expression already used, which keeps the results unchanged. N and m
are powers of 2, so they are exact. The bluestein angle constant now
uses numeric_constants<precision>::PI.

read_header_1_0 assembled the 16-bit header length in uint16_t, which
is promoted to int and narrowed back. Use uint32_t like
read_header_2_0.
The tests multiplied float tensors by size_t values and divided
complex results by size_t, which made the library headers warn. Use a
float amplitude and convert the frequency and length once.
@codspeed

codspeed Bot commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ Unable to measure the execution of 1 benchmark

This benchmark measured no execution time, so its result is not comparable and counts as unchanged. This usually happens when the compiler removes the code under test.

Prevent compiler optimizations

See affected benchmarks

✅ 255 untouched benchmarks


Comparing gouarin:fix-conversion-warnings (a8ef72f) with master (57f3d07)

Open in CodSpeed

This branch has not been deployed

No deployments
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.

1 participant