@@ -3273,31 +3273,33 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
3273
3273
assert (ownerNominal == baseTy->getAnyNominal ());
3274
3274
3275
3275
// Gather all of the substitutions for all levels of generic arguments.
3276
- GenericParamList *curGenericParams = dc->getGenericParamsOfContext ();
3277
- if (!curGenericParams )
3276
+ auto *genericSig = dc->getGenericSignatureOfContext ();
3277
+ if (!genericSig )
3278
3278
return substitutions;
3279
3279
3280
- while (baseTy && curGenericParams) {
3280
+ auto params = genericSig->getGenericParams ();
3281
+ unsigned n = params.size ();
3282
+
3283
+ while (baseTy && n > 0 ) {
3281
3284
// For a bound generic type, gather the generic parameter -> generic
3282
3285
// argument substitutions.
3283
3286
if (auto boundGeneric = baseTy->getAs <BoundGenericType>()) {
3284
- auto params = curGenericParams->getParams ();
3285
3287
auto args = boundGeneric->getGenericArgs ();
3286
- for (unsigned i = 0 , n = args.size (); i != n ; ++i) {
3287
- substitutions[params[i]-> getDeclaredInterfaceType () ->getCanonicalType ()
3288
+ for (unsigned i = 0 , e = args.size (); i < e ; ++i) {
3289
+ substitutions[params[n - e + i] ->getCanonicalType ()
3288
3290
->castTo <GenericTypeParamType>()] = args[i];
3289
3291
}
3290
3292
3291
3293
// Continue looking into the parent.
3292
3294
baseTy = boundGeneric->getParent ();
3293
- curGenericParams = curGenericParams-> getOuterParameters ();
3295
+ n -= args. size ();
3294
3296
continue ;
3295
3297
}
3296
3298
3297
3299
// Continue looking into the parent.
3298
3300
if (auto protocolTy = baseTy->getAs <ProtocolType>()) {
3299
3301
baseTy = protocolTy->getParent ();
3300
- curGenericParams = curGenericParams-> getOuterParameters () ;
3302
+ n-- ;
3301
3303
continue ;
3302
3304
}
3303
3305
@@ -3310,19 +3312,16 @@ TypeBase::getContextSubstitutions(const DeclContext *dc,
3310
3312
llvm_unreachable (" Bad base type" );
3311
3313
}
3312
3314
3313
- if (genericEnv) {
3314
- auto *parentDC = dc;
3315
- while (parentDC->isTypeContext ())
3316
- parentDC = parentDC->getParent ();
3317
- if (auto *outerSig = parentDC->getGenericSignatureOfContext ()) {
3318
- for (auto gp : outerSig->getGenericParams ()) {
3319
- auto result = substitutions.insert (
3320
- {gp->getCanonicalType ()->castTo <GenericTypeParamType>(),
3321
- genericEnv->mapTypeIntoContext (gp)});
3322
- assert (result.second );
3323
- (void ) result;
3324
- }
3325
- }
3315
+ while (n > 0 ) {
3316
+ auto *gp = params[--n];
3317
+ auto substTy = (genericEnv
3318
+ ? genericEnv->mapTypeIntoContext (gp)
3319
+ : gp);
3320
+ auto result = substitutions.insert (
3321
+ {gp->getCanonicalType ()->castTo <GenericTypeParamType>(),
3322
+ substTy});
3323
+ assert (result.second );
3324
+ (void ) result;
3326
3325
}
3327
3326
3328
3327
return substitutions;
0 commit comments