Sema: Fix a few places where we built AST that had to go through SanitizeExpr before type checking

This commit is contained in:
Slava Pestov
2020-06-25 23:37:34 -04:00
parent b663c9b79a
commit b50789404b
9 changed files with 35 additions and 62 deletions

View File

@@ -126,11 +126,10 @@ deriveBodyMathOperator(AbstractFunctionDecl *funcDecl, MathOperator op) {
confRef.getConcrete()->getWitnessDecl(operatorReq))
memberOpDecl = concreteMemberMethodDecl;
assert(memberOpDecl && "Member operator declaration must exist");
auto memberOpDRE =
new (C) DeclRefExpr(memberOpDecl, DeclNameLoc(), /*Implicit*/ true);
auto *memberTypeExpr = TypeExpr::createImplicit(memberType, C);
auto memberOpExpr =
new (C) DotSyntaxCallExpr(memberOpDRE, SourceLoc(), memberTypeExpr);
new (C) MemberRefExpr(memberTypeExpr, SourceLoc(), memberOpDecl,
DeclNameLoc(), /*Implicit*/ true);
// Create expression `lhs.member <op> rhs.member`.
// NOTE(TF-1054): create new `DeclRefExpr`s per loop iteration to avoid

View File

@@ -48,9 +48,9 @@ deriveCaseIterable_enum_getter(AbstractFunctionDecl *funcDecl, void *) {
SmallVector<Expr *, 8> elExprs;
for (EnumElementDecl *elt : parentEnum->getAllElements()) {
auto *ref = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit*/true);
auto *base = TypeExpr::createImplicit(enumTy, C);
auto *apply = new (C) DotSyntaxCallExpr(ref, SourceLoc(), base);
auto *apply = new (C) MemberRefExpr(base, SourceLoc(),
elt, DeclNameLoc(), /*implicit*/true);
elExprs.push_back(apply);
}
auto *arrayExpr = ArrayExpr::create(C, SourceLoc(), elExprs, {}, SourceLoc());

View File

@@ -619,9 +619,9 @@ deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl, void *) {
DeclNameLoc(), /*Implicit=*/true);
// CodingKeys.x
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit=*/true);
auto *metaTyRef = TypeExpr::createImplicit(codingKeysType, C);
auto *keyExpr = new (C) DotSyntaxCallExpr(eltRef, SourceLoc(), metaTyRef);
auto *keyExpr = new (C) MemberRefExpr(metaTyRef, SourceLoc(), elt,
DeclNameLoc(), /*Implicit=*/true);
// encode(_:forKey:)/encodeIfPresent(_:forKey:)
auto methodName = useIfPresentVariant ? C.Id_encodeIfPresent : C.Id_encode;
@@ -901,9 +901,9 @@ deriveBodyDecodable_init(AbstractFunctionDecl *initDecl, void *) {
SourceLoc(), varType);
// CodingKeys.x
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit=*/true);
metaTyRef = TypeExpr::createImplicit(codingKeysType, C);
auto *keyExpr = new (C) DotSyntaxCallExpr(eltRef, SourceLoc(), metaTyRef);
auto *keyExpr = new (C) MemberRefExpr(metaTyRef, SourceLoc(),
elt, DeclNameLoc(), /*Implicit=*/true);
// decode(_:forKey:)/decodeIfPresent(_:forKey:)
SmallVector<Identifier, 2> argNames{Identifier(), C.Id_forKey};

View File

@@ -284,9 +284,9 @@ deriveBodyCodingKey_init_stringValue(AbstractFunctionDecl *initDecl, void *) {
auto labelItem = CaseLabelItem(litPat);
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*Implicit=*/true);
auto *metaTyRef = TypeExpr::createImplicit(enumType, C);
auto *valueExpr = new (C) DotSyntaxCallExpr(eltRef, SourceLoc(), metaTyRef);
auto *valueExpr = new (C) MemberRefExpr(metaTyRef, SourceLoc(), elt,
DeclNameLoc(), /*Implicit=*/true);
auto *assignment = new (C) AssignExpr(selfRef, SourceLoc(), valueExpr,
/*Implicit=*/true);

View File

@@ -74,39 +74,21 @@ deriveBodyComparable_enum_noAssociatedValues_lt(AbstractFunctionDecl *ltDecl,
FuncDecl *cmpFunc = C.getLessThanIntDecl();
assert(cmpFunc && "should have a < for int as we already checked for it");
auto fnType = cmpFunc->getInterfaceType()->castTo<FunctionType>();
Expr *cmpFuncExpr = new (C) DeclRefExpr(cmpFunc, DeclNameLoc(),
/*implicit*/ true,
AccessSemantics::Ordinary);
Expr *cmpFuncExpr;
if (cmpFunc->getDeclContext()->isTypeContext()) {
auto contextTy = cmpFunc->getDeclContext()->getSelfInterfaceType();
Expr *base = TypeExpr::createImplicitHack(SourceLoc(), contextTy, C);
Expr *ref = new (C) DeclRefExpr(cmpFunc, DeclNameLoc(), /*Implicit*/ true,
AccessSemantics::Ordinary, fnType);
fnType = fnType->getResult()->castTo<FunctionType>();
cmpFuncExpr = new (C) DotSyntaxCallExpr(ref, SourceLoc(), base, fnType);
cmpFuncExpr->setImplicit();
} else {
cmpFuncExpr = new (C) DeclRefExpr(cmpFunc, DeclNameLoc(),
/*implicit*/ true,
AccessSemantics::Ordinary,
fnType);
}
TupleTypeElt abTupleElts[2] = { aIndex->getType(), bIndex->getType() };
TupleExpr *abTuple = TupleExpr::create(C, SourceLoc(), { aIndex, bIndex },
{ }, { }, SourceLoc(),
/*HasTrailingClosure*/ false,
/*Implicit*/ true,
TupleType::get(abTupleElts, C));
/*Implicit*/ true);
auto *cmpExpr = new (C) BinaryExpr(
cmpFuncExpr, abTuple, /*implicit*/ true,
fnType->castTo<FunctionType>()->getResult());
cmpFuncExpr, abTuple, /*implicit*/ true);
statements.push_back(new (C) ReturnStmt(SourceLoc(), cmpExpr));
BraceStmt *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc());
return { body, /*isTypeChecked=*/true };
return { body, /*isTypeChecked=*/false };
}
/// Derive the body for an '==' operator for an enum where at least one of the

View File

@@ -268,16 +268,14 @@ deriveBodyDifferentiable_move(AbstractFunctionDecl *funcDecl, void *) {
if (auto *witness = confRef.getConcrete()->getWitnessDecl(requirement))
memberWitnessDecl = witness;
assert(memberWitnessDecl && "Member witness declaration must exist");
auto *memberMethodDRE = new (C)
DeclRefExpr(memberWitnessDecl, DeclNameLoc(), /*Implicit*/ true);
memberMethodDRE->setFunctionRefKind(FunctionRefKind::SingleApply);
// Create reference to member method: `self.<member>.move(along:)`.
Expr *memberExpr =
new (C) MemberRefExpr(selfDRE, SourceLoc(), member, DeclNameLoc(),
/*Implicit*/ true);
auto *memberMethodExpr =
new (C) DotSyntaxCallExpr(memberMethodDRE, SourceLoc(), memberExpr);
new (C) MemberRefExpr(memberExpr, SourceLoc(), memberWitnessDecl,
DeclNameLoc(), /*Implicit*/ true);
// Create reference to parameter member: `direction.<member>`.
VarDecl *paramMember = nullptr;

View File

@@ -386,19 +386,17 @@ deriveBodyEquatable_struct_eq(AbstractFunctionDecl *eqDecl, void *) {
if (!propertyDecl->isUserAccessible())
continue;
auto aPropertyRef = new (C) DeclRefExpr(propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto aParamRef = new (C) DeclRefExpr(aParam, DeclNameLoc(),
/*implicit*/ true);
auto aPropertyExpr = new (C) DotSyntaxCallExpr(aPropertyRef, SourceLoc(),
aParamRef);
auto aPropertyExpr = new (C) MemberRefExpr(aParamRef, SourceLoc(),
propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto bPropertyRef = new (C) DeclRefExpr(propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto bParamRef = new (C) DeclRefExpr(bParam, DeclNameLoc(),
/*implicit*/ true);
auto bPropertyExpr = new (C) DotSyntaxCallExpr(bPropertyRef, SourceLoc(),
bParamRef);
auto bPropertyExpr = new (C) MemberRefExpr(bParamRef, SourceLoc(),
propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto guardStmt = DerivedConformance::returnFalseIfNotEqualGuard(C,
aPropertyExpr, bPropertyExpr);
@@ -873,12 +871,12 @@ deriveBodyHashable_struct_hashInto(AbstractFunctionDecl *hashIntoDecl, void *) {
if (!propertyDecl->isUserAccessible())
continue;
auto propertyRef = new (C) DeclRefExpr(propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(),
/*implicit*/ true);
auto selfPropertyExpr = new (C) DotSyntaxCallExpr(propertyRef, SourceLoc(),
selfRef);
auto selfPropertyExpr = new (C) MemberRefExpr(selfRef, SourceLoc(),
propertyDecl, DeclNameLoc(),
/*implicit*/ true);
// Generate: hasher.combine(self.<property>)
auto combineExpr = createHasherCombineCall(C, hasherParam, selfPropertyExpr);
statements.emplace_back(ASTNode(combineExpr));

View File

@@ -332,9 +332,9 @@ deriveBodyRawRepresentable_init(AbstractFunctionDecl *initDecl, void *) {
// Create a statement which assigns the case to self.
// valueExpr = "\(enumType).\(elt)"
auto eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit*/true);
auto metaTyRef = TypeExpr::createImplicit(enumType, C);
auto valueExpr = new (C) DotSyntaxCallExpr(eltRef, SourceLoc(), metaTyRef);
auto valueExpr = new (C) MemberRefExpr(metaTyRef, SourceLoc(),
elt, DeclNameLoc(), /*implicit*/true);
// assignment = "self = \(valueExpr)"
auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(),

View File

@@ -1900,17 +1900,13 @@ void AttributeChecker::visitMainTypeAttr(MainTypeAttr *attr) {
}
auto funcDeclRef = ConcreteDeclRef(mainFunction, substitutionMap);
auto *funcDeclRefExpr = new (context) DeclRefExpr(
funcDeclRef, DeclNameLoc(location), /*Implicit*/ true);
funcDeclRefExpr->setImplicit(true);
funcDeclRefExpr->setType(mainFunction->getInterfaceType());
auto *dotSyntaxCallExpr = new (context) DotSyntaxCallExpr(
funcDeclRefExpr, /*DotLoc*/ SourceLoc(), typeExpr, voidToVoidFunctionType);
dotSyntaxCallExpr->setImplicit(true);
dotSyntaxCallExpr->setThrows(mainFunctionThrows);
auto *memberRefExpr = new (context) MemberRefExpr(
typeExpr, SourceLoc(), funcDeclRef, DeclNameLoc(location),
/*Implicit*/ true);
memberRefExpr->setImplicit(true);
auto *callExpr = CallExpr::createImplicit(context, dotSyntaxCallExpr, {}, {});
auto *callExpr = CallExpr::createImplicit(context, memberRefExpr, {}, {});
callExpr->setImplicit(true);
callExpr->setThrows(mainFunctionThrows);
callExpr->setType(context.TheEmptyTupleType);