Skip to content

Commit 9fcfa06

Browse files
derekmaurocopybara-github
authored andcommitted
Change some nullability annotations in absl::Span to
absl_nullability_unknown to workaround a bug that makes nullability checks trigger in foreach loops, while still fixing the -Wnullability-completeness warnings. PiperOrigin-RevId: 755951074 Change-Id: Ia6eea53f381d9255856a3f85efa41f0dfbd5c684
1 parent cdd3d21 commit 9fcfa06

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

absl/types/span.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,11 @@ class ABSL_ATTRIBUTE_VIEW Span {
202202
public:
203203
using element_type = T;
204204
using value_type = absl::remove_cv_t<T>;
205-
// TODO(b/316099902) - pointer should be Nullable<T*>, but this makes it hard
206-
// to recognize foreach loops as safe.
207-
using pointer = T*;
208-
using const_pointer = const T*;
205+
// TODO(b/316099902) - pointer should be absl_nullable, but this makes it hard
206+
// to recognize foreach loops as safe. absl_nullability_unknown is currently
207+
// used to suppress -Wnullability-completeness warnings.
208+
using pointer = T* absl_nullability_unknown;
209+
using const_pointer = const T* absl_nullability_unknown;
209210
using reference = T&;
210211
using const_reference = const T&;
211212
using iterator = pointer;
@@ -220,7 +221,7 @@ class ABSL_ATTRIBUTE_VIEW Span {
220221
static const size_type npos = ~(size_type(0));
221222

222223
constexpr Span() noexcept : Span(nullptr, 0) {}
223-
constexpr Span(pointer absl_nullable array ABSL_ATTRIBUTE_LIFETIME_BOUND,
224+
constexpr Span(pointer array ABSL_ATTRIBUTE_LIFETIME_BOUND,
224225
size_type length) noexcept
225226
: ptr_(array), len_(length) {}
226227

@@ -310,7 +311,7 @@ class ABSL_ATTRIBUTE_VIEW Span {
310311
//
311312
// Returns a pointer to the span's underlying array of data (which is held
312313
// outside the span).
313-
constexpr pointer absl_nullable data() const noexcept { return ptr_; }
314+
constexpr pointer data() const noexcept { return ptr_; }
314315

315316
// Span::size()
316317
//
@@ -368,31 +369,27 @@ class ABSL_ATTRIBUTE_VIEW Span {
368369
//
369370
// Returns an iterator pointing to the first element of this span, or `end()`
370371
// if the span is empty.
371-
constexpr iterator absl_nullable begin() const noexcept { return data(); }
372+
constexpr iterator begin() const noexcept { return data(); }
372373

373374
// Span::cbegin()
374375
//
375376
// Returns a const iterator pointing to the first element of this span, or
376377
// `end()` if the span is empty.
377-
constexpr const_iterator absl_nullable cbegin() const noexcept {
378-
return begin();
379-
}
378+
constexpr const_iterator cbegin() const noexcept { return begin(); }
380379

381380
// Span::end()
382381
//
383382
// Returns an iterator pointing just beyond the last element at the
384383
// end of this span. This iterator acts as a placeholder; attempting to
385384
// access it results in undefined behavior.
386-
constexpr iterator absl_nullable end() const noexcept {
387-
return data() + size();
388-
}
385+
constexpr iterator end() const noexcept { return data() + size(); }
389386

390387
// Span::cend()
391388
//
392389
// Returns a const iterator pointing just beyond the last element at the
393390
// end of this span. This iterator acts as a placeholder; attempting to
394391
// access it results in undefined behavior.
395-
constexpr const_iterator absl_nullable cend() const noexcept { return end(); }
392+
constexpr const_iterator cend() const noexcept { return end(); }
396393

397394
// Span::rbegin()
398395
//
@@ -507,7 +504,7 @@ class ABSL_ATTRIBUTE_VIEW Span {
507504
}
508505

509506
private:
510-
pointer absl_nullable ptr_;
507+
pointer ptr_;
511508
size_type len_;
512509
};
513510

0 commit comments

Comments
 (0)