From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 8C2283857C50; Fri, 10 Dec 2021 09:52:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8C2283857C50 From: "iains at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug objc/103639] [11/12 Regression] switch case with break in fast enumeration loop generates wrong code Date: Fri, 10 Dec 2021 09:52:41 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: objc X-Bugzilla-Version: 11.2.1 X-Bugzilla-Keywords: wrong-code X-Bugzilla-Severity: normal X-Bugzilla-Who: iains at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Dec 2021 09:52:41 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D103639 --- Comment #8 from Iain Sandoe --- Here's a version using the testsuite object etc. it does not need Foundation (it needs CoreFoundation on Darwin for NSString, but only libobjc on Linux) the issue repeats equally with gnu-runtime on Linux. gcc pr103639.m -I path/to/gcc-sources/gcc/testsuite/objc-obj-c++-shared -o t -lobjc =3D=3D=3D=3D=3D #import "TestsuiteObject.m" #ifndef __NEXT_RUNTIME__ #include #else #include "nsconstantstring-class.h" #endif extern int printf (const char *, ...); #include /* A mini-array implementation that can be used to test fast enumeration. You create the array with some objects; you can mutate the array, and you can fast-enumerate it. */ @interface MyArray : TestsuiteObject { unsigned int length; id *objects; unsigned long mutated; } - (id) initWithLength: (unsigned int)l objects: (id *)o; - (void) mutate; - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState *)state objects:(id *)stackbuf=20 count:(unsigned long)len; @end @implementation MyArray : TestsuiteObject - (id) initWithLength: (unsigned int)l objects: (id *)o { length =3D l; objects =3D o; mutated =3D 0; return self; } - (void) mutate { mutated =3D 1; } - (unsigned long)countByEnumeratingWithState: (struct __objcFastEnumerationState*)state=20 objects: (id*)stackbuf count: (unsigned long)len { unsigned long i, batch_size; /* We keep how many objects we served in the state->state counter. So the next batch will contain up to length - state->state objects. */ batch_size =3D length - state->state; /* Make obvious adjustments. */ if (batch_size < 0) batch_size =3D 0; if (batch_size > len) batch_size =3D len; /* Copy the objects. */ for (i =3D 0; i < batch_size; i++) stackbuf[i] =3D objects[i]; state->state +=3D batch_size; state->itemsPtr =3D stackbuf; state->mutationsPtr =3D &mutated; return batch_size; } @end int main() { id *objects =3D malloc (sizeof (id) * 2); objects[0] =3D @"a"; objects[1] =3D @"b"; MyArray *array =3D [[MyArray alloc] initWithLength: 2 objects: objects]; // NSArray *array =3D [NSArray arrayWithObjects: @"a", @"b", nil]; int someVar =3D 0; for (id object in array) { switch (someVar) { case 0: printf ("case 0: %s\n", [object cString]); break; } printf ("foo\n"); } return 0; }=