Skip to content

Sema: Eliminate typeCheckExpression() calls from convertLiteralInPlace() #16278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/SIL.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2881,7 +2881,7 @@ reference-counted types, this can produce a different value from the operand
if the block is copied from the stack to the heap.

copy_block_without_escaping
``````````
```````````````````````````
::

sil-instruction :: 'copy_block_without_escaping' sil-operand 'withoutEscaping' sil-operand
Expand Down
4 changes: 3 additions & 1 deletion lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4541,6 +4541,8 @@ static RValue emitApplyAllocatingInitializer(SILGenFunction &SGF,
callee.emplace(Callee::forWitnessMethod(
SGF, selfMetaVal.getType().getSwiftRValueType(),
initRef, subs, loc));
} else if (getMethodDispatch(ctor) == MethodDispatch::Class) {
callee.emplace(Callee::forClassMethod(SGF, initRef, subs, loc));
} else {
callee.emplace(Callee::forDirect(SGF, initRef, subs, loc));
}
Expand All @@ -4550,7 +4552,7 @@ static RValue emitApplyAllocatingInitializer(SILGenFunction &SGF,
// For an inheritable initializer, determine whether we'll need to adjust the
// result type.
bool requiresDowncast = false;
if (ctor->isInheritable() && overriddenSelfType) {
if (ctor->isRequired() && overriddenSelfType) {
CanType substResultType = substFormalType;
for (unsigned i : range(ctor->getNumParameterLists())) {
(void)i;
Expand Down
55 changes: 12 additions & 43 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,14 @@ ConcreteDeclRef findNamedWitnessImpl(

// For a type with dependent conformance, just return the requirement from
// the protocol. There are no protocol conformance tables.
if (!conformance->isConcrete())
return requirement;
if (!conformance->isConcrete()) {
auto subMap = SubstitutionMap::getProtocolSubstitutions(proto, type,
*conformance);
SmallVector<Substitution, 2> subs;
proto->getGenericSignature()->getSubstitutions(subMap, subs);
return ConcreteDeclRef(tc.Context, requirement, subs);
}

auto concrete = conformance->getConcrete();
return concrete->getWitnessDeclRef(requirement, &tc);
}
Expand Down Expand Up @@ -7286,31 +7292,13 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
return nullptr;

// Form a reference to the builtin conversion function.
// FIXME: Bogus location info.
Expr *base = TypeExpr::createImplicitHack(literal->getLoc(), type,
tc.Context);

Expr *unresolvedDot = new (tc.Context) UnresolvedDotExpr(
base, SourceLoc(),
witness.getDecl()->getFullName(),
DeclNameLoc(base->getEndLoc()),
/*Implicit=*/true);
(void)tc.typeCheckExpression(unresolvedDot, dc);

cs.cacheExprTypes(unresolvedDot);

ConcreteDeclRef builtinRef = unresolvedDot->getReferencedDecl();
if (!builtinRef) {
tc.diagnose(base->getLoc(), brokenBuiltinProtocolDiag);
return nullptr;
}

// Set the builtin initializer.
if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
stringLiteral->setBuiltinInitializer(builtinRef);
stringLiteral->setBuiltinInitializer(witness);
else {
cast<MagicIdentifierLiteralExpr>(literal)
->setBuiltinInitializer(builtinRef);
->setBuiltinInitializer(witness);
}

// The literal expression has this type.
Expand Down Expand Up @@ -7350,30 +7338,11 @@ Expr *ExprRewriter::convertLiteralInPlace(Expr *literal,
if (!witness || !isa<AbstractFunctionDecl>(witness.getDecl()))
return nullptr;

// Form a reference to the conversion function.
// FIXME: Bogus location info.
Expr *base = TypeExpr::createImplicitHack(literal->getLoc(), type,
tc.Context);

Expr *unresolvedDot = new (tc.Context) UnresolvedDotExpr(
base, SourceLoc(),
witness.getDecl()->getFullName(),
DeclNameLoc(base->getEndLoc()),
/*Implicit=*/true);
(void)tc.typeCheckExpression(unresolvedDot, dc);
cs.cacheExprTypes(unresolvedDot);

ConcreteDeclRef ref = unresolvedDot->getReferencedDecl();
if (!ref) {
tc.diagnose(base->getLoc(), brokenProtocolDiag);
return nullptr;
}

// Set the initializer.
if (auto stringLiteral = dyn_cast<StringLiteralExpr>(literal))
stringLiteral->setInitializer(ref);
stringLiteral->setInitializer(witness);
else
cast<MagicIdentifierLiteralExpr>(literal)->setInitializer(ref);
cast<MagicIdentifierLiteralExpr>(literal)->setInitializer(witness);

// The literal expression has this type.
cs.setType(literal, type);
Expand Down
21 changes: 19 additions & 2 deletions test/SILGen/generic_literals.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %target-swift-emit-silgen -enable-sil-ownership %s | %FileCheck %s

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

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

x = 2.5
}

// CHECK-LABEL: sil hidden @$S16generic_literals0A13StringLiteral1xyx_ts013ExpressibleBycD0RzlF : $@convention(thin) <T where T : ExpressibleByStringLiteral> (@in_guaranteed T) -> () {

func genericStringLiteral<T : ExpressibleByStringLiteral>(x: T) {
var x = x
// CHECK: [[LIT_VALUE:%.*]] = string_literal utf8 "hello"
// CHECK: [[TSTR_META:%.*]] = metatype $@thick T.StringLiteralType.Type
// CHECK: [[STR_VAL:%.*]] = alloc_stack $T.StringLiteralType
// CHECK: [[BUILTIN_CONV:%.*]] = witness_method $T.StringLiteralType, #_ExpressibleByBuiltinStringLiteral.init!allocator.1
// 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
// CHECK: [[TMETA:%.*]] = metatype $@thick T.Type
// CHECK: [[TVAL:%.*]] = alloc_stack $T
// CHECK: [[CONV:%.*]] = witness_method $T, #ExpressibleByStringLiteral.init!allocator.1
// 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

x = "hello"
}
43 changes: 43 additions & 0 deletions test/SILGen/literals.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// RUN: %target-swift-emit-silgen -enable-sil-ownership %s | %FileCheck %s

func takesOptionalFunction(_: (() -> ())?) {}

struct CustomNull : ExpressibleByNilLiteral {
init(nilLiteral: ()) {}
}

func takesANull(_: CustomNull) {}

// CHECK-LABEL: sil hidden @$S8literals4testyyF : $@convention(thin) () -> ()
func test() {
// CHECK: [[NIL:%.*]] = enum $Optional<@callee_guaranteed () -> ()>, #Optional.none!enumelt
// CHECK: [[FN:%.*]] = function_ref @$S8literals21takesOptionalFunctionyyyycSgF
// CHECK: apply [[FN]]([[NIL]])
_ = takesOptionalFunction(nil)

// CHECK: [[METATYPE:%.*]] = metatype $@thin CustomNull.Type
// CHECK: [[NIL_FN:%.*]] = function_ref @$S8literals10CustomNullV10nilLiteralACyt_tcfC
// CHECK: [[NIL:%.*]] = apply [[NIL_FN]]([[METATYPE]])
// CHECK: [[FN:%.*]] = function_ref @$S8literals10takesANullyyAA10CustomNullVF
// CHECK: apply [[FN]]([[NIL]])
_ = takesANull(nil)
}

class CustomStringClass : ExpressibleByStringLiteral {
required init(stringLiteral value: String) {}
required init(extendedGraphemeClusterLiteral value: String) {}
required init(unicodeScalarLiteral value: String) {}
}

class CustomStringSubclass : CustomStringClass {}

// CHECK-LABEL: sil hidden @$S8literals27returnsCustomStringSubclassAA0cdE0CyF : $@convention(thin) () -> @owned CustomStringSubclass
// CHECK: [[METATYPE:%.*]] = metatype $@thick CustomStringSubclass.Type
// CHECK: [[UPCAST:%.*]] = upcast [[METATYPE]] : $@thick CustomStringSubclass.Type to $@thick CustomStringClass.Type
// CHECK: [[CTOR:%.*]] = class_method [[UPCAST]] : $@thick CustomStringClass.Type, #CustomStringClass.init!allocator.1 : (CustomStringClass.Type) -> (String) -> CustomStringClass, $@convention(method) (@owned String, @thick CustomStringClass.Type) -> @owned CustomStringClass
// CHECK: [[RESULT:%.*]] = apply [[CTOR]]({{%.*}}, [[UPCAST]])
// CHECK: [[DOWNCAST:%.*]] = unchecked_ref_cast [[RESULT]] : $CustomStringClass to $CustomStringSubclass
// CHECK: return [[DOWNCAST]]
func returnsCustomStringSubclass() -> CustomStringSubclass {
return "hello world"
}
25 changes: 0 additions & 25 deletions test/SILGen/nil_literal.swift

This file was deleted.