Skip to content

Commit 7b83592

Browse files
committed
Sema: Eliminate typeCheckExpression() calls from convertLiteralInPlace()
This code dates back to the dark ages when forming substitutions was something only the constraint solver could do. Now that findNamedWitnessImpl() returns a ConcreteDeclRef instead of a ValueDecl, we don't have to build a whole constraint system and type check it just to form substitutions for the call.
1 parent 2db6293 commit 7b83592

File tree

2 files changed

+23
-43
lines changed

2 files changed

+23
-43
lines changed

lib/Sema/CSApply.cpp

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7292,31 +7292,13 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
72927292
return nullptr;
72937293

72947294
// Form a reference to the builtin conversion function.
7295-
// FIXME: Bogus location info.
7296-
Expr *base = TypeExpr::createImplicitHack(literal->getLoc(), type,
7297-
tc.Context);
7298-
7299-
Expr *unresolvedDot = new (tc.Context) UnresolvedDotExpr(
7300-
base, SourceLoc(),
7301-
witness.getDecl()->getFullName(),
7302-
DeclNameLoc(base->getEndLoc()),
7303-
/*Implicit=*/true);
7304-
(void)tc.typeCheckExpression(unresolvedDot, dc);
7305-
7306-
cs.cacheExprTypes(unresolvedDot);
7307-
7308-
ConcreteDeclRef builtinRef = unresolvedDot->getReferencedDecl();
7309-
if (!builtinRef) {
7310-
tc.diagnose(base->getLoc(), brokenBuiltinProtocolDiag);
7311-
return nullptr;
7312-
}
73137295

73147296
// Set the builtin initializer.
73157297
if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
7316-
stringLiteral->setBuiltinInitializer(builtinRef);
7298+
stringLiteral->setBuiltinInitializer(witness);
73177299
else {
73187300
cast<MagicIdentifierLiteralExpr>(literal)
7319-
->setBuiltinInitializer(builtinRef);
7301+
->setBuiltinInitializer(witness);
73207302
}
73217303

73227304
// The literal expression has this type.
@@ -7356,30 +7338,11 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
73567338
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl()))
73577339
return nullptr;
73587340

7359-
// Form a reference to the conversion function.
7360-
// FIXME: Bogus location info.
7361-
Expr *base = TypeExpr::createImplicitHack(literal->getLoc(), type,
7362-
tc.Context);
7363-
7364-
Expr *unresolvedDot = new (tc.Context) UnresolvedDotExpr(
7365-
base, SourceLoc(),
7366-
witness.getDecl()->getFullName(),
7367-
DeclNameLoc(base->getEndLoc()),
7368-
/*Implicit=*/true);
7369-
(void)tc.typeCheckExpression(unresolvedDot, dc);
7370-
cs.cacheExprTypes(unresolvedDot);
7371-
7372-
ConcreteDeclRef ref = unresolvedDot->getReferencedDecl();
7373-
if (!ref) {
7374-
tc.diagnose(base->getLoc(), brokenProtocolDiag);
7375-
return nullptr;
7376-
}
7377-
73787341
// Set the initializer.
73797342
if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
7380-
stringLiteral->setInitializer(ref);
7343+
stringLiteral->setInitializer(witness);
73817344
else
7382-
cast<MagicIdentifierLiteralExpr>(literal)->setInitializer(ref);
7345+
cast<MagicIdentifierLiteralExpr>(literal)->setInitializer(witness);
73837346

73847347
// The literal expression has this type.
73857348
cs.setType(literal, type);

test/SILGen/generic_literals.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-swift-emit-silgen -enable-sil-ownership %s | %FileCheck %s
22

3-
// CHECK-LABEL: sil hidden @$S16generic_literals0A14IntegerLiteral1xyx_ts013ExpressibleBycD0RzlF
3+
// CHECK-LABEL: sil hidden @$S16generic_literals0A14IntegerLiteral1xyx_ts013ExpressibleBycD0RzlF : $@convention(thin) <T where T : ExpressibleByIntegerLiteral> (@in_guaranteed T) -> () {
44
func genericIntegerLiteral<T : ExpressibleByIntegerLiteral>(x: T) {
55
var x = x
66
// CHECK: [[ADDR:%.*]] = alloc_stack $T
@@ -16,7 +16,7 @@ func genericIntegerLiteral<T : ExpressibleByIntegerLiteral>(x: T) {
1616
x = 17
1717
}
1818

19-
// CHECK-LABEL: sil hidden @$S16generic_literals0A15FloatingLiteral{{[_0-9a-zA-Z]*}}F
19+
// CHECK-LABEL: sil hidden @$S16generic_literals0A15FloatingLiteral1xyx_ts018ExpressibleByFloatD0RzlF : $@convention(thin) <T where T : ExpressibleByFloatLiteral> (@in_guaranteed T) -> () {
2020
func genericFloatingLiteral<T : ExpressibleByFloatLiteral>(x: T) {
2121
var x = x
2222
// CHECK: [[TVAL:%.*]] = alloc_stack $T
@@ -31,3 +31,20 @@ func genericFloatingLiteral<T : ExpressibleByFloatLiteral>(x: T) {
3131

3232
x = 2.5
3333
}
34+
35+
// CHECK-LABEL: sil hidden @$S16generic_literals0A13StringLiteral1xyx_ts013ExpressibleBycD0RzlF : $@convention(thin) <T where T : ExpressibleByStringLiteral> (@in_guaranteed T) -> () {
36+
37+
func genericStringLiteral<T : ExpressibleByStringLiteral>(x: T) {
38+
var x = x
39+
// CHECK: [[LIT_VALUE:%.*]] = string_literal utf8 "hello"
40+
// CHECK: [[TSTR_META:%.*]] = metatype $@thick T.StringLiteralType.Type
41+
// CHECK: [[STR_VAL:%.*]] = alloc_stack $T.StringLiteralType
42+
// CHECK: [[BUILTIN_CONV:%.*]] = witness_method $T.StringLiteralType, #_ExpressibleByBuiltinStringLiteral.init!allocator.1
43+
// CHECK: apply [[BUILTIN_CONV]]<T.StringLiteralType>([[STR_VAL]], [[LIT_VALUE]], {{.*}}, [[TSTR_META]]) : $@convention(witness_method: _ExpressibleByBuiltinStringLiteral) <τ_0_0 where τ_0_0 : _ExpressibleByBuiltinStringLiteral> (Builtin.RawPointer, Builtin.Word, Builtin.Int1, @thick τ_0_0.Type) -> @out τ_0_0
44+
// CHECK: [[TMETA:%.*]] = metatype $@thick T.Type
45+
// CHECK: [[TVAL:%.*]] = alloc_stack $T
46+
// CHECK: [[CONV:%.*]] = witness_method $T, #ExpressibleByStringLiteral.init!allocator.1
47+
// CHECK: apply [[CONV]]<T>([[TVAL]], [[STR_VAL]], [[TMETA]]) : $@convention(witness_method: ExpressibleByStringLiteral) <τ_0_0 where τ_0_0 : ExpressibleByStringLiteral> (@in τ_0_0.StringLiteralType, @thick τ_0_0.Type) -> @out τ_0_0
48+
49+
x = "hello"
50+
}

0 commit comments

Comments
 (0)