Skip to content

Commit 2b5a691

Browse files
authored
Merge pull request #23924 from slavapestov/small-csapply-cleanup
Small CSApply cleanup
2 parents 189a38d + 7fe577f commit 2b5a691

File tree

9 files changed

+59
-82
lines changed

9 files changed

+59
-82
lines changed

lib/SILGen/LValue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ class LValue {
413413
}
414414

415415
void addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
416-
VarDecl *var, Optional<SubstitutionMap> subs,
416+
VarDecl *var, SubstitutionMap subs,
417417
LValueOptions options,
418418
SGFAccessKind accessKind,
419419
AccessStrategy strategy,

lib/SILGen/SILGen.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,6 @@ class LLVM_LIBRARY_VISIBILITY SILGenModule : public ASTVisitor<SILGenModule> {
439439
void emitMarkFunctionEscapeForTopLevelCodeGlobals(SILLocation loc,
440440
const CaptureInfo &captureInfo);
441441

442-
/// Get the substitutions necessary to invoke a non-member (global or local)
443-
/// property.
444-
SubstitutionMap
445-
getNonMemberVarDeclSubstitutions(VarDecl *var);
446-
447442
/// Map the substitutions for the original declaration to substitutions for
448443
/// the overridden declaration.
449444
static SubstitutionMap mapSubstitutionsForWitnessOverride(

lib/SILGen/SILGenExpr.cpp

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -866,23 +866,12 @@ emitRValueForDecl(SILLocation loc, ConcreteDeclRef declRef, Type ncRefType,
866866
return emitUndefRValue(loc, refType);
867867
}
868868

869-
// If this is a reference to a type, produce a metatype.
870-
if (isa<TypeDecl>(decl)) {
871-
assert(refType->is<MetatypeType>() &&
872-
"type declref does not have metatype type?!");
873-
return RValue(*this, loc, refType,
874-
ManagedValue::forUnmanaged(
875-
B.createMetatype(loc, getLoweredType(refType))));
876-
}
877-
878869
// If this is a reference to a var, emit it as an l-value and then load.
879-
if (auto *var = dyn_cast<VarDecl>(decl)) {
880-
assert(!declRef.isSpecialized() &&
881-
"Cannot handle specialized variable references");
870+
if (auto *var = dyn_cast<VarDecl>(decl))
871+
return emitRValueForNonMemberVarDecl(loc, declRef, refType, semantics, C);
872+
873+
assert(!isa<TypeDecl>(decl));
882874

883-
return emitRValueForNonMemberVarDecl(loc, var, refType, semantics, C);
884-
}
885-
886875
// If the referenced decl isn't a VarDecl, it should be a constant of some
887876
// sort.
888877
SILDeclRef silDeclRef(decl);
@@ -2016,14 +2005,7 @@ RValue RValueEmitter::visitMemberRefExpr(MemberRefExpr *e,
20162005
SGFContext resultCtx) {
20172006
assert(!e->getType()->is<LValueType>() &&
20182007
"RValueEmitter shouldn't be called on lvalues");
2019-
2020-
if (isa<TypeDecl>(e->getMember().getDecl())) {
2021-
// Emit the metatype for the associated type.
2022-
visit(e->getBase());
2023-
SILValue mt =
2024-
SGF.B.createMetatype(e, SGF.getLoweredLoadableType(e->getType()));
2025-
return RValue(SGF, e, ManagedValue::forUnmanaged(mt));
2026-
}
2008+
assert(isa<VarDecl>(e->getMember().getDecl()));
20272009

20282010
// Everything else should use the l-value logic.
20292011

lib/SILGen/SILGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,7 +1874,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
18741874
LValue emitLValue(Expr *E, SGFAccessKind accessKind,
18751875
LValueOptions options = LValueOptions());
18761876

1877-
RValue emitRValueForNonMemberVarDecl(SILLocation loc, VarDecl *var,
1877+
RValue emitRValueForNonMemberVarDecl(SILLocation loc,
1878+
ConcreteDeclRef declRef,
18781879
CanType formalRValueType,
18791880
AccessSemantics semantics,
18801881
SGFContext C);

lib/SILGen/SILGenLValue.cpp

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,37 +2408,34 @@ namespace {
24082408
};
24092409
}
24102410

2411-
SubstitutionMap
2412-
SILGenModule::getNonMemberVarDeclSubstitutions(VarDecl *var) {
2413-
auto *dc = var->getDeclContext();
2414-
if (auto *genericEnv = dc->getGenericEnvironmentOfContext())
2415-
return genericEnv->getForwardingSubstitutionMap();
2416-
2417-
return SubstitutionMap();
2418-
}
2419-
24202411
static LValue emitLValueForNonMemberVarDecl(SILGenFunction &SGF,
2421-
SILLocation loc, VarDecl *var,
2412+
SILLocation loc,
2413+
ConcreteDeclRef declRef,
24222414
CanType formalRValueType,
24232415
SGFAccessKind accessKind,
24242416
LValueOptions options,
24252417
AccessSemantics semantics) {
24262418
LValue lv;
24272419

2420+
auto *var = cast<VarDecl>(declRef.getDecl());
2421+
auto subs = declRef.getSubstitutions();
2422+
if (!subs)
2423+
subs = SGF.F.getForwardingSubstitutionMap();
2424+
24282425
auto access = getFormalAccessKind(accessKind);
24292426
auto strategy = var->getAccessStrategy(semantics, access,
24302427
SGF.SGM.M.getSwiftModule(),
24312428
SGF.F.getResilienceExpansion());
24322429

2433-
lv.addNonMemberVarComponent(SGF, loc, var, /*be lazy*/ None,
2430+
lv.addNonMemberVarComponent(SGF, loc, var, subs,
24342431
options, accessKind, strategy, formalRValueType);
24352432

24362433
return lv;
24372434
}
24382435

24392436
void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24402437
VarDecl *var,
2441-
Optional<SubstitutionMap> subs,
2438+
SubstitutionMap subs,
24422439
LValueOptions options,
24432440
SGFAccessKind accessKind,
24442441
AccessStrategy strategy,
@@ -2447,17 +2444,11 @@ void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24472444
AccessEmitter<NonMemberVarAccessEmitter, VarDecl> {
24482445
LValue &LV;
24492446
SILLocation Loc;
2450-
Optional<SubstitutionMap> Subs;
2447+
SubstitutionMap Subs;
24512448
LValueOptions Options;
24522449

2453-
SubstitutionMap getSubs() {
2454-
if (Subs) return *Subs;
2455-
Subs = SGF.SGM.getNonMemberVarDeclSubstitutions(Storage);
2456-
return *Subs;
2457-
}
2458-
24592450
NonMemberVarAccessEmitter(SILGenFunction &SGF, SILLocation loc,
2460-
VarDecl *var, Optional<SubstitutionMap> subs,
2451+
VarDecl *var, SubstitutionMap subs,
24612452
SGFAccessKind accessKind,
24622453
CanType formalRValueType,
24632454
LValueOptions options, LValue &lv)
@@ -2469,7 +2460,7 @@ void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24692460
SILType storageType =
24702461
SGF.getLoweredType(Storage->getType()).getAddressType();
24712462
LV.add<AddressorComponent>(Storage, addressor,
2472-
/*isSuper=*/false, isDirect, getSubs(),
2463+
/*isSuper=*/false, isDirect, Subs,
24732464
CanType(), typeData, storageType, nullptr,
24742465
PreparedArguments(),
24752466
/* isOnSelfParameter */ false);
@@ -2479,23 +2470,23 @@ void LValue::addNonMemberVarComponent(SILGenFunction &SGF, SILLocation loc,
24792470
LValueTypeData typeData) {
24802471
LV.add<CoroutineAccessorComponent>(
24812472
Storage, accessor,
2482-
/*isSuper*/ false, isDirect, getSubs(), CanType(), typeData, nullptr,
2473+
/*isSuper*/ false, isDirect, Subs, CanType(), typeData, nullptr,
24832474
PreparedArguments(), /*isOnSelfParameter*/ false);
24842475
}
24852476

24862477
void emitUsingGetterSetter(SILDeclRef accessor, bool isDirect,
24872478
LValueTypeData typeData) {
24882479
LV.add<GetterSetterComponent>(
24892480
Storage, accessor,
2490-
/*isSuper=*/false, isDirect, getSubs(), CanType(), typeData, nullptr,
2481+
/*isSuper=*/false, isDirect, Subs, CanType(), typeData, nullptr,
24912482
PreparedArguments(), /* isOnSelfParameter */ false);
24922483
}
24932484

24942485
void emitUsingMaterialization(AccessStrategy readStrategy,
24952486
AccessStrategy writeStrategy,
24962487
LValueTypeData typeData) {
24972488
LV.add<MaterializeToTemporaryComponent>(
2498-
Storage, /*super*/ false, getSubs(), Options, readStrategy,
2489+
Storage, /*super*/ false, Subs, Options, readStrategy,
24992490
writeStrategy,
25002491
/*base type*/ CanType(), typeData, nullptr, PreparedArguments(),
25012492
/* isOnSelfParameter */ false);
@@ -2574,13 +2565,14 @@ SILGenFunction::emitAddressOfLocalVarDecl(SILLocation loc, VarDecl *var,
25742565
}
25752566

25762567
RValue SILGenFunction::emitRValueForNonMemberVarDecl(SILLocation loc,
2577-
VarDecl *var,
2568+
ConcreteDeclRef declRef,
25782569
CanType formalRValueType,
25792570
AccessSemantics semantics,
25802571
SGFContext C) {
25812572
// Any writebacks for this access are tightly scoped.
25822573
FormalEvaluationScope scope(*this);
25832574

2575+
auto *var = cast<VarDecl>(declRef.getDecl());
25842576
auto localValue = maybeEmitValueOfLocalVarDecl(var);
25852577

25862578
// If this VarDecl is represented as an address, emit it as an lvalue, then
@@ -2678,7 +2670,8 @@ RValue SILGenFunction::emitRValueForNonMemberVarDecl(SILLocation loc,
26782670
? Result : Result.copyUnmanaged(*this, loc));
26792671
}
26802672

2681-
LValue lv = emitLValueForNonMemberVarDecl(*this, loc, var, formalRValueType,
2673+
LValue lv = emitLValueForNonMemberVarDecl(*this, loc, declRef,
2674+
formalRValueType,
26822675
SGFAccessKind::OwnedObjectRead,
26832676
LValueOptions(), semantics);
26842677
return emitLoadOfLValue(loc, std::move(lv), C);
@@ -2703,8 +2696,7 @@ LValue SILGenLValue::visitDiscardAssignmentExpr(DiscardAssignmentExpr *e,
27032696

27042697
LValue SILGenLValue::visitDeclRefExpr(DeclRefExpr *e, SGFAccessKind accessKind,
27052698
LValueOptions options) {
2706-
// The only non-member decl that can be an lvalue is VarDecl.
2707-
return emitLValueForNonMemberVarDecl(SGF, e, cast<VarDecl>(e->getDecl()),
2699+
return emitLValueForNonMemberVarDecl(SGF, e, e->getDeclRef(),
27082700
getSubstFormalRValueType(e),
27092701
accessKind, options,
27102702
e->getAccessSemantics());

lib/Sema/CSApply.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -551,18 +551,20 @@ namespace {
551551
/*isDynamic=*/false);
552552
}
553553

554-
SubstitutionMap substitutions;
554+
auto type = solution.simplifyType(openedType);
555+
556+
if (isa<TypeDecl>(decl) && !isa<ModuleDecl>(decl)) {
557+
auto typeExpr = TypeExpr::createImplicitHack(
558+
loc.getBaseNameLoc(), type->getMetatypeInstanceType(),
559+
ctx);
560+
cs.cacheType(typeExpr);
561+
return typeExpr;
562+
}
555563

556-
// Due to a SILGen quirk, unqualified property references do not
557-
// need substitutions.
558-
if (!isa<VarDecl>(decl)) {
559-
substitutions =
564+
auto substitutions =
560565
solution.computeSubstitutions(
561566
decl->getInnermostDeclContext()->getGenericSignatureOfContext(),
562567
locator);
563-
}
564-
565-
auto type = solution.simplifyType(openedType);
566568
auto declRefExpr =
567569
new (ctx) DeclRefExpr(ConcreteDeclRef(decl, substitutions),
568570
loc, implicit, semantics, type);
@@ -842,6 +844,20 @@ namespace {
842844
return forceUnwrapIfExpected(DSBI, choice, memberLocator);
843845
}
844846

847+
// If we're referring to a member type, it's just a type
848+
// reference.
849+
if (isa<TypeDecl>(member)) {
850+
Type refType = simplifyType(openedType);
851+
auto ref =
852+
TypeExpr::createImplicitHack(memberLoc.getBaseNameLoc(),
853+
refType, context);
854+
cs.setType(ref, refType);
855+
auto *result = new (context) DotSyntaxBaseIgnoredExpr(
856+
base, dotLoc, ref, refType);
857+
cs.setType(result, refType);
858+
return result;
859+
}
860+
845861
// The formal type of the 'self' value for the member's declaration.
846862
Type containerTy = getBaseType(refTy->castTo<FunctionType>());
847863

@@ -983,8 +999,8 @@ namespace {
983999
member->getAttrs().hasAttribute<OptionalAttr>());
9841000
}
9851001

986-
// For types and properties, build member references.
987-
if (isa<TypeDecl>(member) || isa<VarDecl>(member)) {
1002+
// For properties, build member references.
1003+
if (isa<VarDecl>(member)) {
9881004
assert(!dynamicSelfFnType && "Converted type doesn't make sense here");
9891005
if (!baseIsInstance && member->isInstanceMember()) {
9901006
assert(memberLocator.getBaseLocator() &&

lib/Sema/ConstraintSystem.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,19 +1206,6 @@ ConstraintSystem::getTypeOfMemberReference(
12061206

12071207
FunctionType::Param baseObjParam(baseObjTy);
12081208

1209-
// Don't open existentials when accessing typealias members of
1210-
// protocols.
1211-
if (auto *alias = dyn_cast<TypeAliasDecl>(value)) {
1212-
if (baseObjTy->isExistentialType()) {
1213-
auto memberTy = alias->getInterfaceType();
1214-
// If we end up with a protocol typealias here, it's underlying
1215-
// type must be fully concrete.
1216-
assert(!memberTy->hasTypeParameter());
1217-
auto openedType = FunctionType::get({baseObjParam}, memberTy);
1218-
return { openedType, memberTy };
1219-
}
1220-
}
1221-
12221209
if (auto *typeDecl = dyn_cast<TypeDecl>(value)) {
12231210
assert(!isa<ModuleDecl>(typeDecl) && "Nested module?");
12241211

test/Constraints/protocols.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ protocol P : Initable {
110110

111111
typealias E = Int
112112
typealias F = Self.E
113+
typealias G = Array
113114
}
114115

115116
protocol ClassP : class {
@@ -226,6 +227,9 @@ func staticExistential(_ p: P.Type, pp: P.Protocol) {
226227

227228
_ = pp.F.self
228229
_ = p.F.self
230+
231+
// Make sure that we open generics
232+
let _: [Int].Type = p.G.self
229233
}
230234

231235
protocol StaticP {

test/Sema/typo_correction.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protocol P {} // expected-error {{invalid redeclaration of 'P'}}
121121
// expected-note@-1 {{did you mean 'P'?}}
122122

123123
func hasTypo() {
124-
_ = P.a.a // expected-error {{type 'P.a' (aka 'Generic') has no member 'a'}}
124+
_ = P.a.a // expected-error {{type 'Generic<_>' has no member 'a'}}
125125
}
126126

127127
// Typo correction with AnyObject.

0 commit comments

Comments
 (0)