@@ -1912,57 +1912,27 @@ namespace {
1912
1912
}
1913
1913
1914
1914
Expr *visitNilLiteralExpr (NilLiteralExpr *expr) {
1915
+ auto type = simplifyType (cs.getType (expr));
1916
+
1917
+ // By far the most common 'nil' literal is for Optional<T>.none.
1918
+ // We don't have to look up the witness in this case since SILGen
1919
+ // knows how to lower it directly.
1920
+ if (auto objectType = type->getOptionalObjectType ()) {
1921
+ cs.setType (expr, type);
1922
+ return expr;
1923
+ }
1924
+
1915
1925
auto &tc = cs.getTypeChecker ();
1916
1926
auto *protocol = tc.getProtocol (expr->getLoc (),
1917
1927
KnownProtocolKind::ExpressibleByNilLiteral);
1918
1928
1919
1929
// For type-sugar reasons, prefer the spelling of the default literal
1920
1930
// type.
1921
- auto type = simplifyType (cs.getType (expr));
1922
1931
if (auto defaultType = tc.getDefaultType (protocol, dc)) {
1923
1932
if (defaultType->isEqual (type))
1924
1933
type = defaultType;
1925
1934
}
1926
1935
1927
- // By far the most common 'nil' literal is for Optional<T>.none.
1928
- //
1929
- // Emit this case directly instead of calling Optional.init(nilLiteral:),
1930
- // since this generates more efficient SIL.
1931
- if (auto objectType = type->getOptionalObjectType ()) {
1932
- auto *nilDecl = tc.Context .getOptionalNoneDecl ();
1933
- tc.validateDecl (nilDecl);
1934
- if (!nilDecl->hasInterfaceType ())
1935
- return nullptr ;
1936
-
1937
- auto genericSig =
1938
- nilDecl->getDeclContext ()->getGenericSignatureOfContext ();
1939
- SubstitutionMap subs =
1940
- SubstitutionMap::get (genericSig, llvm::makeArrayRef (objectType),
1941
- { });
1942
- ConcreteDeclRef concreteDeclRef (nilDecl, subs);
1943
-
1944
- auto nilType = FunctionType::get (
1945
- {FunctionType::Param (MetatypeType::get (type))}, type);
1946
- auto *nilRefExpr = new (tc.Context ) DeclRefExpr (
1947
- concreteDeclRef, DeclNameLoc (expr->getLoc ()),
1948
- /* implicit=*/ true , AccessSemantics::Ordinary,
1949
- nilType);
1950
- cs.cacheType (nilRefExpr);
1951
-
1952
- auto *typeExpr = TypeExpr::createImplicitHack (
1953
- expr->getLoc (),
1954
- type,
1955
- tc.Context );
1956
- cs.cacheType (typeExpr);
1957
-
1958
- auto *callExpr = new (tc.Context ) DotSyntaxCallExpr (
1959
- nilRefExpr, expr->getLoc (), typeExpr, type);
1960
- callExpr->setImplicit (true );
1961
- cs.cacheType (callExpr);
1962
-
1963
- return callExpr;
1964
- }
1965
-
1966
1936
DeclName initName (tc.Context , DeclBaseName::createConstructor (),
1967
1937
{ tc.Context .Id_nilLiteral });
1968
1938
return convertLiteralInPlace (expr, type, protocol,
0 commit comments