@@ -257,6 +257,17 @@ enumElementPayloadSubpattern(EnumElementDecl *enumElementDecl,
257
257
return pat;
258
258
}
259
259
260
+ // / Build a type-checked integer literal.
261
+ static IntegerLiteralExpr *buildIntegerLiteral (ASTContext &C, unsigned index) {
262
+ Type intType = C.getIntDecl ()->getDeclaredType ();
263
+
264
+ auto literal = IntegerLiteralExpr::createFromUnsigned (C, index);
265
+ literal->setType (intType);
266
+ literal->setBuiltinInitializer (C.getIntBuiltinInitDecl (C.getIntDecl ()));
267
+
268
+ return literal;
269
+ }
270
+
260
271
// / Create AST statements which convert from an enum to an Int with a switch.
261
272
// / \p stmts The generated statements are appended to this vector.
262
273
// / \p parentDC Either an extension or the enum itself.
@@ -298,15 +309,20 @@ static DeclRefExpr *convertEnumToIndex(SmallVectorImpl<ASTNode> &stmts,
298
309
SourceLoc (), SourceLoc (),
299
310
Identifier (), elt, nullptr );
300
311
pat->setImplicit ();
312
+ pat->setType (enumType);
301
313
302
314
auto labelItem = CaseLabelItem (pat);
303
315
304
316
// generate: indexVar = <index>
305
- auto indexExpr = IntegerLiteralExpr::createFromUnsigned (C, index++);
317
+ auto indexExpr = buildIntegerLiteral (C, index++);
318
+
306
319
auto indexRef = new (C) DeclRefExpr (indexVar, DeclNameLoc (),
307
- /* implicit*/ true );
320
+ /* implicit*/ true ,
321
+ AccessSemantics::Ordinary,
322
+ LValueType::get (intType));
308
323
auto assignExpr = new (C) AssignExpr (indexRef, SourceLoc (),
309
324
indexExpr, /* implicit*/ true );
325
+ assignExpr->setType (TupleType::getEmpty (C));
310
326
auto body = BraceStmt::create (C, SourceLoc (), ASTNode (assignExpr),
311
327
SourceLoc ());
312
328
cases.push_back (CaseStmt::create (C, SourceLoc (), labelItem, SourceLoc (),
@@ -316,7 +332,9 @@ static DeclRefExpr *convertEnumToIndex(SmallVectorImpl<ASTNode> &stmts,
316
332
317
333
// generate: switch enumVar { }
318
334
auto enumRef = new (C) DeclRefExpr (enumVarDecl, DeclNameLoc (),
319
- /* implicit*/ true );
335
+ /* implicit*/ true ,
336
+ AccessSemantics::Ordinary,
337
+ enumVarDecl->getType ());
320
338
auto switchStmt = SwitchStmt::create (LabeledStmtInfo (), SourceLoc (), enumRef,
321
339
SourceLoc (), cases, SourceLoc (), C);
322
340
@@ -445,16 +463,20 @@ deriveBodyEquatable_enum_noAssociatedValues_eq(AbstractFunctionDecl *eqDecl,
445
463
fnType);
446
464
}
447
465
466
+ TupleTypeElt abTupleElts[2 ] = { aIndex->getType (), bIndex->getType () };
448
467
TupleExpr *abTuple = TupleExpr::create (C, SourceLoc (), { aIndex, bIndex },
449
468
{ }, { }, SourceLoc (),
450
469
/* HasTrailingClosure*/ false ,
451
- /* Implicit*/ true );
470
+ /* Implicit*/ true ,
471
+ TupleType::get (abTupleElts, C));
452
472
453
- auto *cmpExpr = new (C) BinaryExpr (cmpFuncExpr, abTuple, /* implicit*/ true );
473
+ auto *cmpExpr = new (C) BinaryExpr (
474
+ cmpFuncExpr, abTuple, /* implicit*/ true ,
475
+ fnType->castTo <FunctionType>()->getResult ());
454
476
statements.push_back (new (C) ReturnStmt (SourceLoc (), cmpExpr));
455
477
456
478
BraceStmt *body = BraceStmt::create (C, SourceLoc (), statements, SourceLoc ());
457
- return { body, /* isTypeChecked=*/ false };
479
+ return { body, /* isTypeChecked=*/ true };
458
480
}
459
481
460
482
// / Derive the body for an '==' operator for an enum where at least one of the
0 commit comments