Skip to content

Commit

Permalink
Report a clearer error for non-integral iterator differences
Browse files Browse the repository at this point in the history
  • Loading branch information
VoxSciurorum committed Jan 20, 2025
1 parent 359ddfd commit 637e623
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/Sema/SemaStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4002,7 +4002,7 @@ StmtResult Sema::BuildCilkForRangeStmt(CXXForRangeStmt *ForRange) {
}
ExprResult LimitExpr = ActOnBinOp(S, ForRange->getColonLoc(), tok::minus,
EndRef.get(), BeginRef.get());
if (LimitExpr.isInvalid()) {
if (LimitExpr.isInvalid() || !LimitExpr.get()->getType()->isIntegerType()) {
// CilkForRange currently only supports random access iterators.
Diag(ForRange->getForLoc(), diag::err_cilk_for_range_end_minus_begin);
return StmtError();
Expand Down
36 changes: 36 additions & 0 deletions clang/test/Cilk/cilkfor-range-error.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// RUN: %clang_cc1 %s -std=c++20 -triple x86_64-unknown-linux-gnu -fopencilk -fsyntax-only -verify

template<class Datum>
struct Iterator {
void *data;
Iterator operator+(long);
bool operator!=(const Iterator &) const;
Iterator &operator++();
Datum &operator*();
Datum *operator->();
};

struct S {
int data;
struct I : public Iterator<S> {
float operator-(const I &) const;
};
I begin(), end();
};

struct T {
int data;
struct I : public Iterator<T> {
long operator-(const I &) const;
};
I begin(), end();
};

void sum(S &s, T &t) {
_Cilk_for (auto i : s) { // expected-warning{{experimental}}
// expected-error@-1{{cannot determine length}}
}
_Cilk_for (auto i : _Cilk_spawn t) { // expected-warning{{experimental}}
// expected-error@-1{{'cilk_spawn' not allowed in this scope}}
}
}

0 comments on commit 637e623

Please sign in to comment.