public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106837] New: False compilation error "inconsistent begin/end types in range-based 'for' statement"
@ 2022-09-05 13:17 ofekshilon at gmail dot com
  2022-09-05 13:22 ` [Bug c++/106837] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: ofekshilon at gmail dot com @ 2022-09-05 13:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106837

            Bug ID: 106837
           Summary: False compilation error "inconsistent begin/end types
                    in range-based 'for' statement"
           Product: gcc
           Version: 12.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ofekshilon at gmail dot com
  Target Milestone: ---

The following fails to build with all gcc versions at least since 6.1, when
built with "-std=c++17" or (for supporting versions) with "-std=c++20".  

  struct myDat {
    myDat() {}
    struct Iter {
      Iter &operator++();
      int operator*();
      bool operator!=(int& other);
    };
    Iter begin() ;
    int end() ;
  };

int main() { 
    for (int pos : myDat()) 
        ; 
}

Here's a godbolt link, with comparison to a successful build by clang:
https://godbolt.org/z/1fsfvxzb7


Note this patch by Jason Merrill:
https://gcc.gnu.org/legacy-ml/gcc-patches/2016-03/msg00792.html

+           {
+             if (cxx_dialect >= cxx1z
+                 && (build_x_binary_op (input_location, NE_EXPR,
+                                        *begin, ERROR_MARK,
+                                        *end, ERROR_MARK,
+                                        NULL, tf_none)
+                     != error_mark_node))
+               /* P08184R0 allows __begin and __end to have different types,
+                  but make sure they are comparable so we can give a better
+                  diagnostic.  */;
+             else
+               error ("inconsistent begin/end types in range-based %<for%> "
+                      "statement: %qT and %qT",
+                      TREE_TYPE (*begin), TREE_TYPE (*end));
+           }

In this case the begin/end iter types *are* comparable, but the
build_x_binary_op test fails.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/106837] False compilation error "inconsistent begin/end types in range-based 'for' statement"
  2022-09-05 13:17 [Bug c++/106837] New: False compilation error "inconsistent begin/end types in range-based 'for' statement" ofekshilon at gmail dot com
@ 2022-09-05 13:22 ` pinskia at gcc dot gnu.org
  2022-09-05 13:30 ` ofekshilon at gmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-05 13:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106837

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
> In this case the begin/end iter types *are* comparable
Not exactly as the operator!= requires a lvalue. And end() is not an lvalue.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/106837] False compilation error "inconsistent begin/end types in range-based 'for' statement"
  2022-09-05 13:17 [Bug c++/106837] New: False compilation error "inconsistent begin/end types in range-based 'for' statement" ofekshilon at gmail dot com
  2022-09-05 13:22 ` [Bug c++/106837] " pinskia at gcc dot gnu.org
@ 2022-09-05 13:30 ` ofekshilon at gmail dot com
  2022-09-05 13:35 ` jakub at gcc dot gnu.org
  2022-09-06  9:12 ` ofekshilon at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: ofekshilon at gmail dot com @ 2022-09-05 13:30 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106837

--- Comment #2 from Ofek Shilon <ofekshilon at gmail dot com> ---
(In reply to Andrew Pinski from comment #1)
> > In this case the begin/end iter types *are* comparable
> Not exactly as the operator!= requires a lvalue. And end() is not an lvalue.

Then enable comparison also via the other iterator type:

  struct myDat {
    myDat() {}
    struct EndIter;
    struct Iter {
      Iter &operator++();
      int operator*();
      bool operator!=(EndIter& other);
    };
    Iter begin() ;
    struct EndIter {
        bool operator!=(Iter other);
    };
    EndIter end() ;
  };

  int main() { 
    for (int pos : myDat()) 
        ; 
  }

This still fails with the same message

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/106837] False compilation error "inconsistent begin/end types in range-based 'for' statement"
  2022-09-05 13:17 [Bug c++/106837] New: False compilation error "inconsistent begin/end types in range-based 'for' statement" ofekshilon at gmail dot com
  2022-09-05 13:22 ` [Bug c++/106837] " pinskia at gcc dot gnu.org
  2022-09-05 13:30 ` ofekshilon at gmail dot com
@ 2022-09-05 13:35 ` jakub at gcc dot gnu.org
  2022-09-06  9:12 ` ofekshilon at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-09-05 13:35 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106837

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
True.  But in the end the operator!= will be called on __begin and __end vars
which are lvalues.
So I think the test needs to be called on some trees with types of the *begin
and *end return values for which lvalue_kind will say it is an lvalue.
Dunno if that means trying to create a dummy VAR_DECL, CONST_DECL,
PLACEHOLDER_EXPR, COMPOUND_LITERAL_EXPR or what else (and whether to do that
only if *begin or *end aren't lvalues already.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug c++/106837] False compilation error "inconsistent begin/end types in range-based 'for' statement"
  2022-09-05 13:17 [Bug c++/106837] New: False compilation error "inconsistent begin/end types in range-based 'for' statement" ofekshilon at gmail dot com
                   ` (2 preceding siblings ...)
  2022-09-05 13:35 ` jakub at gcc dot gnu.org
@ 2022-09-06  9:12 ` ofekshilon at gmail dot com
  3 siblings, 0 replies; 5+ messages in thread
From: ofekshilon at gmail dot com @ 2022-09-06  9:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106837

--- Comment #4 from Ofek Shilon <ofekshilon at gmail dot com> ---
This can be tested empirically. Remove the entire build_x_binary_op check,
build gcc and run on this snippet. If it builds correctly than the begin/end
types are indeed comparable and the emitted error is false.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-09-06  9:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-05 13:17 [Bug c++/106837] New: False compilation error "inconsistent begin/end types in range-based 'for' statement" ofekshilon at gmail dot com
2022-09-05 13:22 ` [Bug c++/106837] " pinskia at gcc dot gnu.org
2022-09-05 13:30 ` ofekshilon at gmail dot com
2022-09-05 13:35 ` jakub at gcc dot gnu.org
2022-09-06  9:12 ` ofekshilon at gmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).