@@ -173,18 +173,26 @@ Atom Atom::forLayout(LayoutConstraint layout,
173
173
// /
174
174
// / * For layout atoms, we use LayoutConstraint::compare().
175
175
int Atom::compare (Atom other, const ProtocolGraph &graph) const {
176
+ // Exit early if the atoms are equal.
177
+ if (Ptr == other.Ptr )
178
+ return 0 ;
179
+
176
180
auto kind = getKind ();
177
181
auto otherKind = other.getKind ();
178
182
179
183
if (kind != otherKind)
180
184
return int (kind) < int (otherKind) ? -1 : 1 ;
181
185
186
+ int result = 0 ;
187
+
182
188
switch (kind) {
183
189
case Kind::Name:
184
- return getName ().compare (other.getName ());
190
+ result = getName ().compare (other.getName ());
191
+ break ;
185
192
186
193
case Kind::Protocol:
187
- return graph.compareProtocols (getProtocol (), other.getProtocol ());
194
+ result = graph.compareProtocols (getProtocol (), other.getProtocol ());
195
+ break ;
188
196
189
197
case Kind::AssociatedType: {
190
198
auto protos = getProtocols ();
@@ -200,7 +208,8 @@ int Atom::compare(Atom other, const ProtocolGraph &graph) const {
200
208
return result;
201
209
}
202
210
203
- return getName ().compare (other.getName ());
211
+ result = getName ().compare (other.getName ());
212
+ break ;
204
213
}
205
214
206
215
case Kind::GenericParam: {
@@ -213,15 +222,16 @@ int Atom::compare(Atom other, const ProtocolGraph &graph) const {
213
222
if (param->getIndex () != otherParam->getIndex ())
214
223
return param->getIndex () < otherParam->getIndex () ? -1 : 1 ;
215
224
216
- return 0 ;
225
+ break ;
217
226
}
218
227
219
- case Kind::Layout: {
220
- return getLayoutConstraint ().compare (other.getLayoutConstraint ());
221
- }
228
+ case Kind::Layout:
229
+ result = getLayoutConstraint ().compare (other.getLayoutConstraint ());
230
+ break ;
222
231
}
223
232
224
- llvm_unreachable (" Bad atom kind" );
233
+ assert (result != 0 && " Two distinct atoms should not compare equal" );
234
+ return result;
225
235
}
226
236
227
237
// / Print the atom using our mnemonic representation.
0 commit comments