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)) confRef.getConcrete()->getWitnessDecl(operatorReq))
memberOpDecl = concreteMemberMethodDecl; memberOpDecl = concreteMemberMethodDecl;
assert(memberOpDecl && "Member operator declaration must exist"); assert(memberOpDecl && "Member operator declaration must exist");
auto memberOpDRE =
new (C) DeclRefExpr(memberOpDecl, DeclNameLoc(), /*Implicit*/ true);
auto *memberTypeExpr = TypeExpr::createImplicit(memberType, C); auto *memberTypeExpr = TypeExpr::createImplicit(memberType, C);
auto memberOpExpr = auto memberOpExpr =
new (C) DotSyntaxCallExpr(memberOpDRE, SourceLoc(), memberTypeExpr); new (C) MemberRefExpr(memberTypeExpr, SourceLoc(), memberOpDecl,
DeclNameLoc(), /*Implicit*/ true);
// Create expression `lhs.member <op> rhs.member`. // Create expression `lhs.member <op> rhs.member`.
// NOTE(TF-1054): create new `DeclRefExpr`s per loop iteration to avoid // 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; SmallVector<Expr *, 8> elExprs;
for (EnumElementDecl *elt : parentEnum->getAllElements()) { for (EnumElementDecl *elt : parentEnum->getAllElements()) {
auto *ref = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit*/true);
auto *base = TypeExpr::createImplicit(enumTy, C); 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); elExprs.push_back(apply);
} }
auto *arrayExpr = ArrayExpr::create(C, SourceLoc(), elExprs, {}, SourceLoc()); auto *arrayExpr = ArrayExpr::create(C, SourceLoc(), elExprs, {}, SourceLoc());

View File

@@ -619,9 +619,9 @@ deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl, void *) {
DeclNameLoc(), /*Implicit=*/true); DeclNameLoc(), /*Implicit=*/true);
// CodingKeys.x // CodingKeys.x
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit=*/true);
auto *metaTyRef = TypeExpr::createImplicit(codingKeysType, C); 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:) // encode(_:forKey:)/encodeIfPresent(_:forKey:)
auto methodName = useIfPresentVariant ? C.Id_encodeIfPresent : C.Id_encode; auto methodName = useIfPresentVariant ? C.Id_encodeIfPresent : C.Id_encode;
@@ -901,9 +901,9 @@ deriveBodyDecodable_init(AbstractFunctionDecl *initDecl, void *) {
SourceLoc(), varType); SourceLoc(), varType);
// CodingKeys.x // CodingKeys.x
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit=*/true);
metaTyRef = TypeExpr::createImplicit(codingKeysType, C); 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:) // decode(_:forKey:)/decodeIfPresent(_:forKey:)
SmallVector<Identifier, 2> argNames{Identifier(), C.Id_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 labelItem = CaseLabelItem(litPat);
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*Implicit=*/true);
auto *metaTyRef = TypeExpr::createImplicit(enumType, C); 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, auto *assignment = new (C) AssignExpr(selfRef, SourceLoc(), valueExpr,
/*Implicit=*/true); /*Implicit=*/true);

View File

@@ -74,39 +74,21 @@ deriveBodyComparable_enum_noAssociatedValues_lt(AbstractFunctionDecl *ltDecl,
FuncDecl *cmpFunc = C.getLessThanIntDecl(); FuncDecl *cmpFunc = C.getLessThanIntDecl();
assert(cmpFunc && "should have a < for int as we already checked for it"); 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 }, TupleExpr *abTuple = TupleExpr::create(C, SourceLoc(), { aIndex, bIndex },
{ }, { }, SourceLoc(), { }, { }, SourceLoc(),
/*HasTrailingClosure*/ false, /*HasTrailingClosure*/ false,
/*Implicit*/ true, /*Implicit*/ true);
TupleType::get(abTupleElts, C));
auto *cmpExpr = new (C) BinaryExpr( auto *cmpExpr = new (C) BinaryExpr(
cmpFuncExpr, abTuple, /*implicit*/ true, cmpFuncExpr, abTuple, /*implicit*/ true);
fnType->castTo<FunctionType>()->getResult());
statements.push_back(new (C) ReturnStmt(SourceLoc(), cmpExpr)); statements.push_back(new (C) ReturnStmt(SourceLoc(), cmpExpr));
BraceStmt *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc()); 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 /// 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)) if (auto *witness = confRef.getConcrete()->getWitnessDecl(requirement))
memberWitnessDecl = witness; memberWitnessDecl = witness;
assert(memberWitnessDecl && "Member witness declaration must exist"); 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:)`. // Create reference to member method: `self.<member>.move(along:)`.
Expr *memberExpr = Expr *memberExpr =
new (C) MemberRefExpr(selfDRE, SourceLoc(), member, DeclNameLoc(), new (C) MemberRefExpr(selfDRE, SourceLoc(), member, DeclNameLoc(),
/*Implicit*/ true); /*Implicit*/ true);
auto *memberMethodExpr = auto *memberMethodExpr =
new (C) DotSyntaxCallExpr(memberMethodDRE, SourceLoc(), memberExpr); new (C) MemberRefExpr(memberExpr, SourceLoc(), memberWitnessDecl,
DeclNameLoc(), /*Implicit*/ true);
// Create reference to parameter member: `direction.<member>`. // Create reference to parameter member: `direction.<member>`.
VarDecl *paramMember = nullptr; VarDecl *paramMember = nullptr;

View File

@@ -386,19 +386,17 @@ deriveBodyEquatable_struct_eq(AbstractFunctionDecl *eqDecl, void *) {
if (!propertyDecl->isUserAccessible()) if (!propertyDecl->isUserAccessible())
continue; continue;
auto aPropertyRef = new (C) DeclRefExpr(propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto aParamRef = new (C) DeclRefExpr(aParam, DeclNameLoc(), auto aParamRef = new (C) DeclRefExpr(aParam, DeclNameLoc(),
/*implicit*/ true); /*implicit*/ true);
auto aPropertyExpr = new (C) DotSyntaxCallExpr(aPropertyRef, SourceLoc(), auto aPropertyExpr = new (C) MemberRefExpr(aParamRef, SourceLoc(),
aParamRef); propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto bPropertyRef = new (C) DeclRefExpr(propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto bParamRef = new (C) DeclRefExpr(bParam, DeclNameLoc(), auto bParamRef = new (C) DeclRefExpr(bParam, DeclNameLoc(),
/*implicit*/ true); /*implicit*/ true);
auto bPropertyExpr = new (C) DotSyntaxCallExpr(bPropertyRef, SourceLoc(), auto bPropertyExpr = new (C) MemberRefExpr(bParamRef, SourceLoc(),
bParamRef); propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto guardStmt = DerivedConformance::returnFalseIfNotEqualGuard(C, auto guardStmt = DerivedConformance::returnFalseIfNotEqualGuard(C,
aPropertyExpr, bPropertyExpr); aPropertyExpr, bPropertyExpr);
@@ -873,12 +871,12 @@ deriveBodyHashable_struct_hashInto(AbstractFunctionDecl *hashIntoDecl, void *) {
if (!propertyDecl->isUserAccessible()) if (!propertyDecl->isUserAccessible())
continue; continue;
auto propertyRef = new (C) DeclRefExpr(propertyDecl, DeclNameLoc(),
/*implicit*/ true);
auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(), auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(),
/*implicit*/ true); /*implicit*/ true);
auto selfPropertyExpr = new (C) DotSyntaxCallExpr(propertyRef, SourceLoc(), auto selfPropertyExpr = new (C) MemberRefExpr(selfRef, SourceLoc(),
selfRef); propertyDecl, DeclNameLoc(),
/*implicit*/ true);
// Generate: hasher.combine(self.<property>) // Generate: hasher.combine(self.<property>)
auto combineExpr = createHasherCombineCall(C, hasherParam, selfPropertyExpr); auto combineExpr = createHasherCombineCall(C, hasherParam, selfPropertyExpr);
statements.emplace_back(ASTNode(combineExpr)); 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. // Create a statement which assigns the case to self.
// valueExpr = "\(enumType).\(elt)" // valueExpr = "\(enumType).\(elt)"
auto eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit*/true);
auto metaTyRef = TypeExpr::createImplicit(enumType, C); 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)" // assignment = "self = \(valueExpr)"
auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(), auto selfRef = new (C) DeclRefExpr(selfDecl, DeclNameLoc(),

View File

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