Re: [PATCH] support ptrdiff_t if it has the same storage size as int, but does not alias it

"'Robin Haberkorn' via scintilla-interest" <[email protected]> Tue, 21 Oct 2025 21:29:10 +0000
Newsgroups gmane.comp.lib.scintilla.devel
Message-ID <[email protected]>
On Mon Oct 13, 2025 at 02:50:03 GMT +02, Neil Hodgson wrote:
> If a platform isn't interested in consistency then it will lose in the
> market. It is unreasonable to expect other projects to adapt to this.

I disagree. NetBSD and Haiku aren't violating the C/C++ standards. Also,
authors of future libcs/platforms might well not be aware that you think that
ptrdiff_t should alias int under certain conditions and cause the same issue.

But it's your project and your decision.

> I suspect that it is possible to templatize the instantiations over
> is_convertible_v, possibly using enable_if, replacing the #if but that
> needs someone more motivated than me.

Unfortunately you can't. It doesn't work in explicit instantiations.
Perhaps declaring RunStyles<int> __attribute__((weak)) might have
worked - but it's neither compiler-, nor platform-agnostic.

So I prepared another version of my patch that introduces
PTRDIFF_DOESNT_ALIAS_INT, which you may have to pass in via CXXFLAGS.

For SciTECO I wrote an Autoconf macro AX_PTRDIFF_ALIASES_INT [1]
to check for this condition.

Yours sincerely,
Robin

[1]: https://git.fmsbw.de/sciteco/tree/m4/ax_ptrdiff_aliases_int.m4

-- 
You received this message because you are subscribed to the Google Groups "scintilla-interest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/scintilla-interest/DDOBIR02928X.1O8WKU057M2FA%40googlemail.com.
v3-0001-support-ptrdiff_t-if-it-has-the-same-storage-size.patch (text/x-patch, 2.9 KB)
From 333dd3153850d05a219731dee0b899155442b191 Mon Sep 17 00:00:00 2001
From: Robin Haberkorn <[email protected]>
Date: Sun, 19 Oct 2025 23:40:08 +0200
Subject: [PATCH v3] support ptrdiff_t if it has the same storage size as int,
 but does *not* alias it

* This is the case e.g. on NetBSD 10 for ARMv6 where Sci::Position == ptrdiff_t == long int,
  but obviously for other platforms as well, where it causes "invalid conversion"
  and "undefined symbol" errors.
  Scintilla was testing for aliasability by comparing the storage size with sizeof()
  or PTRDIFF_MAX == INT_MAX at the preprocessor level.
  This was fundamentally flawed.
* In LineVector<T>::InsertLines() we are now using the C++17 construct
  std::is_convertible_v<From*,To*> instead.
* We need RunStyles<ptrdiff_t> as well on the affected platforms.
  This is impossible to test for in a constant expression that can be used
  with the preprocessor.
  Also, it's not possible to conditionally instantiate templates.
  We tried to instantiate RunStyles for all scalar types that could be behind ptrdiff_t,
  but it was causing warnings on MSVC.
  Implicitly instantiating RunStyles would be possible, but is not desired.
  Therefore as a workaround, you can now define the PTRDIFF_DOESNT_ALIAS_INT
  macro when invoking the build system, to force instantiating RunStyles<ptrdiff_t>.
  When writing portable applications, you may have to use a compile-time check
  for checking aliasability of ptrdiff_t and int in order to define PTRDIFF_DOESNT_ALIAS_INT.
---
 src/CellBuffer.cxx | 3 ++-
 src/RunStyles.cxx  | 2 +-
 2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/CellBuffer.cxx b/src/CellBuffer.cxx
index 04486d4c6..3e9deb934 100644
--- a/src/CellBuffer.cxx
+++ b/src/CellBuffer.cxx
@@ -21,6 +21,7 @@
 #include <optional>
 #include <algorithm>
 #include <memory>
+#include <type_traits>
 
 #include "ScintillaTypes.h"
 
@@ -215,7 +216,7 @@ public:
 	}
 	void InsertLines(Sci::Line line, const Sci::Position *positions, size_t lines, bool lineStart) override {
 		const POS lineAsPos = pos_cast(line);
-		if constexpr (sizeof(Sci::Position) == sizeof(POS)) {
+		if constexpr (std::is_convertible_v<Sci::Position *, POS *>) {
 			starts.InsertPartitions(lineAsPos, positions, lines);
 		} else {
 			starts.InsertPartitionsWithCast(lineAsPos, positions, lines);
diff --git a/src/RunStyles.cxx b/src/RunStyles.cxx
index 848670ba9..bca45c889 100644
--- a/src/RunStyles.cxx
+++ b/src/RunStyles.cxx
@@ -321,7 +321,7 @@ void RunStyles<DISTANCE, STYLE>::Check() const {
 
 template class Scintilla::Internal::RunStyles<int, int>;
 template class Scintilla::Internal::RunStyles<int, char>;
-#if (PTRDIFF_MAX != INT_MAX) || defined(__HAIKU__)
+#if (PTRDIFF_MAX != INT_MAX) || defined(__HAIKU__) || defined(PTRDIFF_DOESNT_ALIAS_INT)
 template class Scintilla::Internal::RunStyles<ptrdiff_t, int>;
 template class Scintilla::Internal::RunStyles<ptrdiff_t, char>;
 #endif
-- 
2.50.1
signature.asc (application/pgp-signature, 488 B)
-----BEGIN PGP SIGNATURE-----

iQEzBAABCAAdFiEEB/6AD3js1eGB4raKarMDYY4q6voFAmj3+ygACgkQarMDYY4q
6voOAwgAo4EaugoB+mKOmgGlWsaUrKEbWMQHrf8UGiNnk/Ji5fYu1r+OODDt1zrf
2mjCGImoWOHgaZDYxd8vAliM5dYBfbeTID2icMPhdO64t2rhs3XiHVkL56sYQvV+
3NABisSkgI6v39daEYtHm/prFOGOEvY2zfjJPkcIaMzi5a/e29bLO8DOKqS3gwUV
N02JsQH/B6elyIVreKkEy1fyElZHNcc/+7C8ILKAiXKHxOCiX84CTSnUbX90sUjN
CMhWFhBNujI/0IAAa/Q6b4p2jainxcyFlAKYuswnijLr8Oy/u2XEldGW4aDoRUJG
Mrqajal0SY4txxDRi2pTM+zE9Qn2BA==
=JqZW
-----END PGP SIGNATURE-----