Skip to content

Commit 385f38b

Browse files
authored
Merge pull request #41442 from zoecarver/remove-noop-casts
[cxx-interop] Remove no-op casts.
2 parents 83be9ba + 970489b commit 385f38b

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

lib/Sema/CSApply.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,16 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
204204
paramRefExpr->setType(param->getType());
205205

206206
auto specParamTy = specializedFuncDecl->getParameters()->get(paramIndex)->getType();
207-
auto cast = ForcedCheckedCastExpr::createImplicit(
208-
ctx, paramRefExpr, specParamTy);
209207

210-
forwardingParams.push_back(Argument(SourceLoc(), Identifier(), cast));
208+
Expr *argExpr = nullptr;
209+
if (specParamTy->isEqual(paramRefExpr->getType())) {
210+
argExpr = paramRefExpr;
211+
} else {
212+
argExpr = ForcedCheckedCastExpr::createImplicit(ctx, paramRefExpr,
213+
specParamTy);
214+
}
215+
216+
forwardingParams.push_back(Argument(SourceLoc(), Identifier(), argExpr));
211217
paramIndex++;
212218
}
213219

@@ -220,10 +226,16 @@ synthesizeDependentTypeThunkParamForwarding(AbstractFunctionDecl *afd, void *con
220226
specializedFuncCallExpr->setType(specializedFuncDecl->getResultInterfaceType());
221227
specializedFuncCallExpr->setThrows(false);
222228

223-
auto cast = ForcedCheckedCastExpr::createImplicit(
224-
ctx, specializedFuncCallExpr, thunkDecl->getResultInterfaceType());
229+
Expr *resultExpr = nullptr;
230+
if (specializedFuncCallExpr->getType()->isEqual(thunkDecl->getResultInterfaceType())) {
231+
resultExpr = specializedFuncCallExpr;
232+
} else {
233+
resultExpr = ForcedCheckedCastExpr::createImplicit(
234+
ctx, specializedFuncCallExpr, thunkDecl->getResultInterfaceType());
235+
}
225236

226-
auto returnStmt = new (ctx) ReturnStmt(SourceLoc(), cast, /*implicit=*/true);
237+
auto returnStmt = new (ctx)
238+
ReturnStmt(SourceLoc(), resultExpr, /*implicit=*/true);
227239
auto body = BraceStmt::create(ctx, SourceLoc(), {returnStmt}, SourceLoc(),
228240
/*implicit=*/true);
229241
return {body, /*isTypeChecked=*/true};

test/Interop/Cxx/templates/dependent-types-silgen.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,9 @@ import DependentTypes
2727
// CHECK-LABEL: sil [transparent] [serialized] [ossa] @$sSC27dependantReturnTypeInfferedyyps5Int64VF : $@convention(thin) (Int64) -> @out Any
2828
// CHECK: bb0(%0 : $*Any, %1 : $Int64):
2929
// CHECK: [[SPEC_OUT:%.*]] = alloc_stack $__CxxTemplateInst1MIxE
30-
// CHECK: [[TMP_INT:%.*]] = alloc_stack $Int64
31-
// CHECK: store %1 to [trivial] [[TMP_INT]] : $*Int64
32-
// CHECK: [[TMP_INT_CASTED:%.*]] = alloc_stack $Int64
33-
// CHECK: unconditional_checked_cast_addr Int64 in [[TMP_INT]] : $*Int64 to Int64 in [[TMP_INT_CASTED]] : $*Int64
3430

35-
// CHECK: [[ARG:%.*]] = load [trivial] [[TMP_INT_CASTED]] : $*Int64
3631
// CHECK: [[FN:%.*]] = function_ref @{{_Z27dependantReturnTypeInfferedIxE1MIT_ES1_|\?\?\$dependantReturnTypeInffered@_J@@YA\?AU\?\$M@_J@@_J@Z}} : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE
37-
// CHECK: [[OUT:%.*]] = apply [[FN]]([[ARG]]) : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE
32+
// CHECK: [[OUT:%.*]] = apply [[FN]](%1) : $@convention(c) (Int64) -> __CxxTemplateInst1MIxE
3833

3934
// CHECK: store [[OUT]] to [trivial] [[SPEC_OUT]] : $*__CxxTemplateInst1MIxE
4035
// CHECK: unconditional_checked_cast_addr __CxxTemplateInst1MIxE in [[SPEC_OUT]] : $*__CxxTemplateInst1MIxE to Any in %0 : $*Any

0 commit comments

Comments
 (0)