@@ -97,7 +97,8 @@ static TypeRepr *buildTypeRepr(DeclContext *typeContext,
97
97
// / first parameter is always `self` (with or without `inout`).
98
98
static ClosureExpr *synthesizeMethodThunk (DeclContext *thunkDC,
99
99
NominalTypeDecl *nominal,
100
- FuncDecl *method) {
100
+ FuncDecl *method,
101
+ SourceLoc thunkLoc) {
101
102
auto &ctx = method->getASTContext ();
102
103
103
104
// If this is a method, let's form a thunk so that attribute initializer
@@ -212,17 +213,19 @@ static ClosureExpr *synthesizeMethodThunk(DeclContext *thunkDC,
212
213
}
213
214
214
215
DeclAttributes attrs;
215
- auto *closure = new (ctx) ClosureExpr (
216
- attrs, /* bracketRange=*/ SourceRange (),
217
- /* capturedSelf=*/ nullptr , ParameterList::create (ctx, closureParams),
218
- /* asyncLoc=*/ SourceLoc (),
219
- /* throwsLoc=*/ SourceLoc (),
220
- /* arrowLoc=*/ SourceLoc (),
221
- /* inLoc=*/ SourceLoc (),
222
- /* explicitResultType=*/ nullptr , thunkDC);
223
-
224
- closure->setBody (BraceStmt::createImplicit (ctx, body),
225
- /* isSingleExpr=*/ true );
216
+ auto *closure = new (ctx)
217
+ ClosureExpr (attrs, /* bracketRange=*/ SourceRange (),
218
+ /* capturedSelf=*/ nullptr ,
219
+ ParameterList::create (ctx, thunkLoc, closureParams, thunkLoc),
220
+ /* asyncLoc=*/ SourceLoc (),
221
+ /* throwsLoc=*/ SourceLoc (),
222
+ /* arrowLoc=*/ SourceLoc (),
223
+ /* inLoc=*/ SourceLoc (),
224
+ /* explicitResultType=*/ nullptr , thunkDC);
225
+
226
+ closure->setBody (
227
+ BraceStmt::create (ctx, thunkLoc, body, thunkLoc, /* implicit=*/ true ),
228
+ /* isSingleExpr=*/ true );
226
229
closure->setImplicit ();
227
230
228
231
return closure;
@@ -246,6 +249,14 @@ Expr *SynthesizeRuntimeMetadataAttrGenerator::evaluate(
246
249
247
250
auto *initContext = new (ctx) RuntimeAttributeInitializer (attr, attachedTo);
248
251
252
+ SourceRange sourceRange;
253
+ if (auto *repr = attr->getTypeRepr ()) {
254
+ sourceRange = repr->getSourceRange ();
255
+ } else {
256
+ sourceRange = SourceRange (
257
+ attachedTo->getAttributeInsertionLoc (/* forModifier=*/ false ));
258
+ }
259
+
249
260
Expr *initArgument = nullptr ;
250
261
if (auto *nominal = dyn_cast<NominalTypeDecl>(attachedTo)) {
251
262
// Registry attributes on protocols are only used for
@@ -254,26 +265,28 @@ Expr *SynthesizeRuntimeMetadataAttrGenerator::evaluate(
254
265
return nullptr ;
255
266
256
267
// Form an initializer call passing in the metatype
257
- auto *metatype = TypeExpr::createImplicit (nominal->getDeclaredType (), ctx);
268
+ auto *metatype = TypeExpr::createImplicitHack (
269
+ sourceRange.Start , nominal->getDeclaredType (), ctx);
258
270
initArgument = new (ctx)
259
271
DotSelfExpr (metatype, /* dot=*/ SourceLoc (), /* self=*/ SourceLoc ());
260
272
} else if (auto *func = dyn_cast<FuncDecl>(attachedTo)) {
261
273
if (auto *nominal = func->getDeclContext ()->getSelfNominalTypeDecl ()) {
262
- initArgument = synthesizeMethodThunk (initContext, nominal, func);
274
+ initArgument =
275
+ synthesizeMethodThunk (initContext, nominal, func, sourceRange.Start );
263
276
} else {
264
- initArgument = new (ctx)
265
- DeclRefExpr ( {func}, /* Loc=*/ DeclNameLoc (), /* implicit=*/ true );
277
+ initArgument = new (ctx) DeclRefExpr (
278
+ {func}, /* Loc=*/ DeclNameLoc (sourceRange. Start ), /* implicit=*/ true );
266
279
}
267
280
} else {
268
281
auto *var = cast<VarDecl>(attachedTo);
269
282
assert (!var->isStatic ());
270
283
271
284
auto *keyPath =
272
- KeyPathExpr::createImplicit (ctx, /* backslashLoc=*/ SourceLoc () ,
285
+ KeyPathExpr::createImplicit (ctx, /* backslashLoc=*/ sourceRange. Start ,
273
286
{KeyPathExpr::Component::forProperty (
274
287
{var}, var->getValueInterfaceType (),
275
- /* Loc=*/ SourceLoc () )},
276
- /* endLoc=*/ SourceLoc () );
288
+ /* Loc=*/ sourceRange. Start )},
289
+ /* endLoc=*/ sourceRange. Start );
277
290
278
291
// Build a type repr for base of the key path, since attribute
279
292
// could be attached to an inner type, we need to go up decl
@@ -283,20 +296,13 @@ Expr *SynthesizeRuntimeMetadataAttrGenerator::evaluate(
283
296
initArgument = keyPath;
284
297
}
285
298
286
- SourceRange sourceRange;
287
- if (auto *repr = attr->getTypeRepr ()) {
288
- sourceRange = repr->getSourceRange ();
289
- } else {
290
- sourceRange = SourceRange (
291
- attachedTo->getAttributeInsertionLoc (/* forModifier=*/ false ));
292
- }
293
-
294
299
auto typeExpr =
295
300
TypeExpr::createImplicitHack (sourceRange.Start , attrType, ctx);
296
301
297
302
// Add the initializer argument at the front of the argument list
298
303
SmallVector<Argument, 4 > newArgs;
299
- newArgs.push_back ({/* loc=*/ SourceLoc (), ctx.Id_attachedTo , initArgument});
304
+ newArgs.push_back (
305
+ {/* loc=*/ sourceRange.Start , ctx.Id_attachedTo , initArgument});
300
306
if (auto *attrArgs = attr->getArgs ())
301
307
newArgs.append (attrArgs->begin (), attrArgs->end ());
302
308
0 commit comments