mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[CS] NFC: Store ContextualTypeInfo in SyntacticElementTarget
Move the contextual type locator onto ContextualTypeInfo, and consolidate the separate fields in SyntacticElementTarget into storing a ContextualTypeInfo. This then lets us plumb down the locator for the branch contextual type of an if/switch expression from the initial constraint generation, rather than introducing it later. This should be NFC.
This commit is contained in:
@@ -25,6 +25,7 @@
|
||||
#include "swift/AST/TypeLoc.h"
|
||||
#include "swift/Basic/Debug.h"
|
||||
#include "swift/Sema/ConstraintLocator.h"
|
||||
#include "swift/Sema/ContextualTypeInfo.h"
|
||||
#include "swift/Sema/OverloadChoice.h"
|
||||
#include "llvm/ADT/ArrayRef.h"
|
||||
#include "llvm/ADT/ilist.h"
|
||||
@@ -50,23 +51,6 @@ class ConstraintLocator;
|
||||
class ConstraintSystem;
|
||||
enum class TrailingClosureMatching;
|
||||
|
||||
/// Describes contextual type information about a particular element
|
||||
/// (expression, statement etc.) within a constraint system.
|
||||
struct ContextualTypeInfo {
|
||||
TypeLoc typeLoc;
|
||||
ContextualTypePurpose purpose;
|
||||
|
||||
ContextualTypeInfo() : typeLoc(TypeLoc()), purpose(CTP_Unused) {}
|
||||
|
||||
ContextualTypeInfo(Type contextualTy, ContextualTypePurpose purpose)
|
||||
: typeLoc(TypeLoc::withoutLoc(contextualTy)), purpose(purpose) {}
|
||||
|
||||
ContextualTypeInfo(TypeLoc typeLoc, ContextualTypePurpose purpose)
|
||||
: typeLoc(typeLoc), purpose(purpose) {}
|
||||
|
||||
Type getType() const { return typeLoc.getType(); }
|
||||
};
|
||||
|
||||
/// Describes the kind of constraint placed on one or more types.
|
||||
enum class ConstraintKind : char {
|
||||
/// The two types must be bound to the same type. This is the only
|
||||
|
||||
@@ -3114,12 +3114,11 @@ public:
|
||||
return E;
|
||||
}
|
||||
|
||||
void setContextualType(ASTNode node, TypeLoc T,
|
||||
ContextualTypePurpose purpose) {
|
||||
void setContextualInfo(ASTNode node, ContextualTypeInfo info) {
|
||||
assert(bool(node) && "Expected non-null expression!");
|
||||
assert(contextualTypes.count(node) == 0 &&
|
||||
"Already set this contextual type");
|
||||
contextualTypes[node] = {{T, purpose}, Type()};
|
||||
contextualTypes[node] = {info, Type()};
|
||||
}
|
||||
|
||||
llvm::Optional<ContextualTypeInfo> getContextualTypeInfo(ASTNode node) const {
|
||||
|
||||
55
include/swift/Sema/ContextualTypeInfo.h
Normal file
55
include/swift/Sema/ContextualTypeInfo.h
Normal file
@@ -0,0 +1,55 @@
|
||||
//===--- ContextualTypeInfo.h - Contextual Type Info ------------*- C++ -*-===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2023 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This file provides the \c ContextualTypeInfo class.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef SWIFT_SEMA_CONTEXTUAL_TYPE_INFO_H
|
||||
#define SWIFT_SEMA_CONTEXTUAL_TYPE_INFO_H
|
||||
|
||||
#include "swift/AST/TypeLoc.h"
|
||||
#include "swift/Sema/ConstraintLocator.h"
|
||||
|
||||
namespace swift {
|
||||
namespace constraints {
|
||||
|
||||
/// Describes contextual type information about a particular element
|
||||
/// (expression, statement etc.) within a constraint system.
|
||||
struct ContextualTypeInfo {
|
||||
TypeLoc typeLoc;
|
||||
ContextualTypePurpose purpose;
|
||||
|
||||
/// The locator for the contextual type conversion constraint, or
|
||||
/// \c nullptr to use the default locator which is anchored directly on
|
||||
/// the expression.
|
||||
ConstraintLocator *locator;
|
||||
|
||||
ContextualTypeInfo()
|
||||
: typeLoc(TypeLoc()), purpose(CTP_Unused), locator(nullptr) {}
|
||||
|
||||
ContextualTypeInfo(Type contextualTy, ContextualTypePurpose purpose,
|
||||
ConstraintLocator *locator = nullptr)
|
||||
: typeLoc(TypeLoc::withoutLoc(contextualTy)), purpose(purpose),
|
||||
locator(locator) {}
|
||||
|
||||
ContextualTypeInfo(TypeLoc typeLoc, ContextualTypePurpose purpose,
|
||||
ConstraintLocator *locator = nullptr)
|
||||
: typeLoc(typeLoc), purpose(purpose), locator(locator) {}
|
||||
|
||||
Type getType() const { return typeLoc.getType(); }
|
||||
};
|
||||
|
||||
} // end namespace constraints
|
||||
} // end namespace swift
|
||||
|
||||
#endif // SWIFT_SEMA_CONTEXTUAL_TYPE_INFO_H
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "swift/AST/Stmt.h"
|
||||
#include "swift/AST/TypeLoc.h"
|
||||
#include "swift/Sema/ConstraintLocator.h"
|
||||
#include "swift/Sema/ContextualTypeInfo.h"
|
||||
|
||||
namespace swift {
|
||||
|
||||
@@ -71,18 +72,8 @@ private:
|
||||
/// type-checked.
|
||||
DeclContext *dc;
|
||||
|
||||
// TODO: Fold the 3 below fields into ContextualTypeInfo
|
||||
|
||||
/// The purpose of the contextual type.
|
||||
ContextualTypePurpose contextualPurpose;
|
||||
|
||||
/// The type to which the expression should be converted.
|
||||
TypeLoc convertType;
|
||||
|
||||
/// The locator for the contextual type conversion constraint, or
|
||||
/// \c nullptr to use the default locator which is anchored directly on
|
||||
/// the expression.
|
||||
ConstraintLocator *convertTypeLocator;
|
||||
/// The contextual type info for the expression.
|
||||
ContextualTypeInfo contextualInfo;
|
||||
|
||||
/// When initializing a pattern from the expression, this is the
|
||||
/// pattern.
|
||||
@@ -169,26 +160,15 @@ private:
|
||||
void maybeApplyPropertyWrapper();
|
||||
|
||||
public:
|
||||
SyntacticElementTarget(Expr *expr, DeclContext *dc,
|
||||
ContextualTypePurpose contextualPurpose,
|
||||
Type convertType,
|
||||
ConstraintLocator *convertTypeLocator,
|
||||
bool isDiscarded)
|
||||
: SyntacticElementTarget(expr, dc, contextualPurpose,
|
||||
TypeLoc::withoutLoc(convertType),
|
||||
convertTypeLocator, isDiscarded) {}
|
||||
|
||||
SyntacticElementTarget(Expr *expr, DeclContext *dc,
|
||||
ContextualTypePurpose contextualPurpose,
|
||||
Type convertType, bool isDiscarded)
|
||||
: SyntacticElementTarget(expr, dc, contextualPurpose, convertType,
|
||||
/*convertTypeLocator*/ nullptr, isDiscarded) {}
|
||||
: SyntacticElementTarget(
|
||||
expr, dc, ContextualTypeInfo(convertType, contextualPurpose),
|
||||
isDiscarded) {}
|
||||
|
||||
SyntacticElementTarget(Expr *expr, DeclContext *dc,
|
||||
ContextualTypePurpose contextualPurpose,
|
||||
TypeLoc convertType,
|
||||
ConstraintLocator *convertTypeLocator,
|
||||
bool isDiscarded);
|
||||
ContextualTypeInfo contextualInfo, bool isDiscarded);
|
||||
|
||||
SyntacticElementTarget(ClosureExpr *closure, Type convertType) {
|
||||
kind = Kind::closure;
|
||||
@@ -375,26 +355,31 @@ public:
|
||||
llvm_unreachable("invalid decl context type");
|
||||
}
|
||||
|
||||
ContextualTypePurpose getExprContextualTypePurpose() const {
|
||||
/// Get the contextual type info for an expression target.
|
||||
ContextualTypeInfo getExprContextualTypeInfo() const {
|
||||
assert(kind == Kind::expression);
|
||||
return expression.contextualPurpose;
|
||||
return expression.contextualInfo;
|
||||
}
|
||||
|
||||
/// Get the contextual type purpose for an expression target.
|
||||
ContextualTypePurpose getExprContextualTypePurpose() const {
|
||||
return getExprContextualTypeInfo().purpose;
|
||||
}
|
||||
|
||||
/// Get the contextual type for an expression target.
|
||||
Type getExprContextualType() const {
|
||||
return getExprContextualTypeLoc().getType();
|
||||
}
|
||||
|
||||
/// Get the contextual type for an expression target.
|
||||
TypeLoc getExprContextualTypeLoc() const {
|
||||
assert(kind == Kind::expression);
|
||||
|
||||
// For an @autoclosure parameter, the conversion type is
|
||||
// the result of the function type.
|
||||
if (FunctionType *autoclosureParamType = getAsAutoclosureParamType()) {
|
||||
return TypeLoc(expression.convertType.getTypeRepr(),
|
||||
autoclosureParamType->getResult());
|
||||
}
|
||||
auto typeLoc = getExprContextualTypeInfo().typeLoc;
|
||||
if (FunctionType *autoclosureParamType = getAsAutoclosureParamType())
|
||||
return TypeLoc(typeLoc.getTypeRepr(), autoclosureParamType->getResult());
|
||||
|
||||
return expression.convertType;
|
||||
return typeLoc;
|
||||
}
|
||||
|
||||
/// Retrieve the type to which an expression should be converted, or
|
||||
@@ -408,33 +393,32 @@ public:
|
||||
/// Retrieve the conversion type locator for the expression, or \c nullptr
|
||||
/// if it has not been set.
|
||||
ConstraintLocator *getExprConvertTypeLocator() const {
|
||||
assert(kind == Kind::expression);
|
||||
return expression.convertTypeLocator;
|
||||
return getExprContextualTypeInfo().locator;
|
||||
}
|
||||
|
||||
/// Returns the autoclosure parameter type, or \c nullptr if the
|
||||
/// expression has a different kind of context.
|
||||
FunctionType *getAsAutoclosureParamType() const {
|
||||
assert(kind == Kind::expression);
|
||||
if (expression.contextualPurpose == CTP_AutoclosureDefaultParameter)
|
||||
return expression.convertType.getType()->castTo<FunctionType>();
|
||||
if (getExprContextualTypePurpose() == CTP_AutoclosureDefaultParameter)
|
||||
return getExprContextualTypeInfo().getType()->castTo<FunctionType>();
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void setExprConversionType(Type type) {
|
||||
assert(kind == Kind::expression);
|
||||
expression.convertType = TypeLoc::withoutLoc(type);
|
||||
expression.contextualInfo.typeLoc = TypeLoc::withoutLoc(type);
|
||||
}
|
||||
|
||||
void setExprConversionTypeLoc(TypeLoc type) {
|
||||
assert(kind == Kind::expression);
|
||||
expression.convertType = type;
|
||||
expression.contextualInfo.typeLoc = type;
|
||||
}
|
||||
|
||||
/// Whether this target is for an initialization expression and pattern.
|
||||
bool isForInitialization() const {
|
||||
return kind == Kind::expression &&
|
||||
expression.contextualPurpose == CTP_Initialization;
|
||||
getExprContextualTypePurpose() == CTP_Initialization;
|
||||
}
|
||||
|
||||
/// For a pattern initialization target, retrieve the pattern.
|
||||
@@ -448,7 +432,7 @@ public:
|
||||
|
||||
ExprPattern *getExprPattern() const {
|
||||
assert(kind == Kind::expression);
|
||||
assert(expression.contextualPurpose == CTP_ExprPattern);
|
||||
assert(getExprContextualTypePurpose() == CTP_ExprPattern);
|
||||
return cast<ExprPattern>(expression.pattern);
|
||||
}
|
||||
|
||||
@@ -575,11 +559,16 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
assert(kind == Kind::expression);
|
||||
assert(expression.contextualPurpose == CTP_Initialization ||
|
||||
expression.contextualPurpose == CTP_ForEachStmt ||
|
||||
expression.contextualPurpose == CTP_ForEachSequence ||
|
||||
expression.contextualPurpose == CTP_ExprPattern);
|
||||
switch (getExprContextualTypePurpose()) {
|
||||
case CTP_Initialization:
|
||||
case CTP_ForEachStmt:
|
||||
case CTP_ForEachSequence:
|
||||
case CTP_ExprPattern:
|
||||
break;
|
||||
default:
|
||||
assert(false && "Unexpected contextual type purpose");
|
||||
break;
|
||||
}
|
||||
expression.pattern = pattern;
|
||||
}
|
||||
|
||||
|
||||
@@ -4334,8 +4334,9 @@ bool ConstraintSystem::generateWrappedPropertyTypeConstraints(
|
||||
ConstraintKind::Equal, propertyType, wrappedValueType,
|
||||
getConstraintLocator(
|
||||
wrappedVar, LocatorPathElt::ContextualType(CTP_WrappedProperty)));
|
||||
setContextualType(wrappedVar, TypeLoc::withoutLoc(wrappedValueType),
|
||||
CTP_WrappedProperty);
|
||||
|
||||
ContextualTypeInfo contextInfo(wrappedValueType, CTP_WrappedProperty);
|
||||
setContextualInfo(wrappedVar, contextInfo);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -4419,10 +4420,9 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
|
||||
makeIteratorCall, dc, /*patternType=*/Type(), PB, /*index=*/0,
|
||||
/*shouldBindPatternsOneWay=*/false);
|
||||
|
||||
cs.setContextualType(
|
||||
sequenceExpr,
|
||||
TypeLoc::withoutLoc(sequenceProto->getDeclaredInterfaceType()),
|
||||
CTP_ForEachSequence);
|
||||
ContextualTypeInfo contextInfo(sequenceProto->getDeclaredInterfaceType(),
|
||||
CTP_ForEachSequence);
|
||||
cs.setContextualInfo(sequenceExpr, contextInfo);
|
||||
|
||||
if (cs.generateConstraints(makeIteratorTarget))
|
||||
return llvm::None;
|
||||
@@ -4480,10 +4480,9 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
|
||||
if (!iteratorProto)
|
||||
return llvm::None;
|
||||
|
||||
cs.setContextualType(
|
||||
nextRef->getBase(),
|
||||
TypeLoc::withoutLoc(iteratorProto->getDeclaredInterfaceType()),
|
||||
CTP_ForEachSequence);
|
||||
ContextualTypeInfo contextInfo(iteratorProto->getDeclaredInterfaceType(),
|
||||
CTP_ForEachSequence);
|
||||
cs.setContextualInfo(nextRef->getBase(), contextInfo);
|
||||
}
|
||||
|
||||
SyntacticElementTarget nextTarget(nextCall, dc, CTP_Unused,
|
||||
@@ -4549,8 +4548,9 @@ generateForEachStmtConstraints(ConstraintSystem &cs,
|
||||
return llvm::None;
|
||||
|
||||
cs.setTargetFor(whereExpr, whereTarget);
|
||||
cs.setContextualType(whereExpr, TypeLoc::withoutLoc(boolType),
|
||||
CTP_Condition);
|
||||
|
||||
ContextualTypeInfo contextInfo(boolType, CTP_Condition);
|
||||
cs.setContextualInfo(whereExpr, contextInfo);
|
||||
}
|
||||
|
||||
// Populate all of the information for a for-each loop.
|
||||
|
||||
@@ -323,10 +323,8 @@ void ConstraintSystem::applySolution(const Solution &solution) {
|
||||
|
||||
// Add the contextual types.
|
||||
for (const auto &contextualType : solution.contextualTypes) {
|
||||
if (!getContextualTypeInfo(contextualType.first)) {
|
||||
setContextualType(contextualType.first, contextualType.second.typeLoc,
|
||||
contextualType.second.purpose);
|
||||
}
|
||||
if (!getContextualTypeInfo(contextualType.first))
|
||||
setContextualInfo(contextualType.first, contextualType.second);
|
||||
}
|
||||
|
||||
// Register the statement condition targets.
|
||||
@@ -1731,8 +1729,7 @@ bool ConstraintSystem::solveForCodeCompletion(
|
||||
SyntacticElementTarget &target, SmallVectorImpl<Solution> &solutions) {
|
||||
if (auto *expr = target.getAsExpr()) {
|
||||
// Tell the constraint system what the contextual type is.
|
||||
setContextualType(expr, target.getExprContextualTypeLoc(),
|
||||
target.getExprContextualTypePurpose());
|
||||
setContextualInfo(expr, target.getExprContextualTypeInfo());
|
||||
|
||||
// Set up the expression type checker timer.
|
||||
Timer.emplace(expr, *this);
|
||||
|
||||
@@ -1224,8 +1224,7 @@ private:
|
||||
|
||||
auto contextualResultInfo = getContextualResultInfo();
|
||||
SyntacticElementTarget target(resultExpr, context.getAsDeclContext(),
|
||||
contextualResultInfo.purpose,
|
||||
contextualResultInfo.getType(),
|
||||
contextualResultInfo,
|
||||
/*isDiscarded=*/false);
|
||||
|
||||
if (cs.generateConstraints(target)) {
|
||||
@@ -1233,9 +1232,7 @@ private:
|
||||
return;
|
||||
}
|
||||
|
||||
cs.setContextualType(target.getAsExpr(),
|
||||
TypeLoc::withoutLoc(contextualResultInfo.getType()),
|
||||
contextualResultInfo.purpose);
|
||||
cs.setContextualInfo(target.getAsExpr(), contextualResultInfo);
|
||||
cs.setTargetFor(returnStmt, target);
|
||||
}
|
||||
|
||||
@@ -1391,9 +1388,15 @@ bool ConstraintSystem::generateConstraints(SingleValueStmtExpr *E) {
|
||||
// Assign contextual types for each of the expression branches.
|
||||
SmallVector<Expr *, 4> scratch;
|
||||
auto branches = E->getSingleExprBranches(scratch);
|
||||
for (auto *branch : branches) {
|
||||
setContextualType(branch, TypeLoc::withoutLoc(resultTy),
|
||||
CTP_SingleValueStmtBranch);
|
||||
for (auto idx : indices(branches)) {
|
||||
auto *branch = branches[idx];
|
||||
|
||||
auto ctpElt = LocatorPathElt::ContextualType(CTP_SingleValueStmtBranch);
|
||||
auto *loc = getConstraintLocator(
|
||||
E, {LocatorPathElt::SingleValueStmtBranch(idx), ctpElt});
|
||||
|
||||
ContextualTypeInfo info(resultTy, CTP_SingleValueStmtBranch, loc);
|
||||
setContextualInfo(branch, info);
|
||||
}
|
||||
|
||||
TypeJoinExpr *join = nullptr;
|
||||
@@ -1546,26 +1549,8 @@ ConstraintSystem::simplifySyntacticElementConstraint(
|
||||
getConstraintLocator(locator));
|
||||
|
||||
if (auto *expr = element.dyn_cast<Expr *>()) {
|
||||
auto ctpElt = LocatorPathElt::ContextualType(contextInfo.purpose);
|
||||
auto *contextualTypeLoc = getConstraintLocator(expr, {ctpElt});
|
||||
|
||||
// If this is a branch expression in a SingleValueStmtExpr, form a locator
|
||||
// based on the branch index.
|
||||
if (auto *SVE = getAsExpr<SingleValueStmtExpr>(locator.getAnchor())) {
|
||||
SmallVector<Expr *, 4> scratch;
|
||||
auto branches = SVE->getSingleExprBranches(scratch);
|
||||
for (auto idx : indices(branches)) {
|
||||
if (expr == branches[idx]) {
|
||||
contextualTypeLoc = getConstraintLocator(
|
||||
SVE, {LocatorPathElt::SingleValueStmtBranch(idx), ctpElt});
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SyntacticElementTarget target(expr, context->getAsDeclContext(),
|
||||
contextInfo.purpose, contextInfo.getType(),
|
||||
contextualTypeLoc, isDiscarded);
|
||||
contextInfo, isDiscarded);
|
||||
|
||||
if (generateConstraints(target))
|
||||
return SolutionKind::Error;
|
||||
|
||||
@@ -25,22 +25,23 @@ using namespace constraints;
|
||||
#define DEBUG_TYPE "SyntacticElementTarget"
|
||||
|
||||
SyntacticElementTarget::SyntacticElementTarget(
|
||||
Expr *expr, DeclContext *dc, ContextualTypePurpose contextualPurpose,
|
||||
TypeLoc convertType, ConstraintLocator *convertTypeLocator,
|
||||
Expr *expr, DeclContext *dc, ContextualTypeInfo contextualInfo,
|
||||
bool isDiscarded) {
|
||||
auto contextualPurpose = contextualInfo.purpose;
|
||||
|
||||
// Verify that a purpose was specified if a convertType was. Note that it is
|
||||
// ok to have a purpose without a convertType (which is used for call
|
||||
// return types).
|
||||
assert((!convertType.getType() || contextualPurpose != CTP_Unused) &&
|
||||
assert((!contextualInfo.getType() || contextualPurpose != CTP_Unused) &&
|
||||
"Purpose for conversion type was not specified");
|
||||
|
||||
// Take a look at the conversion type to check to make sure it is sensible.
|
||||
if (auto type = convertType.getType()) {
|
||||
if (auto type = contextualInfo.getType()) {
|
||||
// If we're asked to convert to an UnresolvedType, then ignore the request.
|
||||
// This happens when CSDiags nukes a type.
|
||||
if (type->is<UnresolvedType>() ||
|
||||
(type->is<MetatypeType>() && type->hasUnresolvedType())) {
|
||||
convertType = TypeLoc();
|
||||
contextualInfo.typeLoc = TypeLoc();
|
||||
contextualPurpose = CTP_Unused;
|
||||
}
|
||||
}
|
||||
@@ -48,9 +49,7 @@ SyntacticElementTarget::SyntacticElementTarget(
|
||||
kind = Kind::expression;
|
||||
expression.expression = expr;
|
||||
expression.dc = dc;
|
||||
expression.contextualPurpose = contextualPurpose;
|
||||
expression.convertType = convertType;
|
||||
expression.convertTypeLocator = convertTypeLocator;
|
||||
expression.contextualInfo = contextualInfo;
|
||||
expression.pattern = nullptr;
|
||||
expression.propertyWrapper.wrappedVar = nullptr;
|
||||
expression.propertyWrapper.innermostWrappedValueInit = nullptr;
|
||||
@@ -62,8 +61,7 @@ SyntacticElementTarget::SyntacticElementTarget(
|
||||
}
|
||||
|
||||
void SyntacticElementTarget::maybeApplyPropertyWrapper() {
|
||||
assert(kind == Kind::expression);
|
||||
assert(expression.contextualPurpose == CTP_Initialization);
|
||||
assert(getExprContextualTypePurpose() == CTP_Initialization);
|
||||
|
||||
VarDecl *singleVar;
|
||||
if (auto *pattern = expression.pattern) {
|
||||
@@ -130,8 +128,8 @@ void SyntacticElementTarget::maybeApplyPropertyWrapper() {
|
||||
// the initializer type later.
|
||||
expression.propertyWrapper.wrappedVar = singleVar;
|
||||
expression.expression = backingInitializer;
|
||||
expression.convertType = {outermostWrapperAttr->getTypeRepr(),
|
||||
outermostWrapperAttr->getType()};
|
||||
expression.contextualInfo.typeLoc = {outermostWrapperAttr->getTypeRepr(),
|
||||
outermostWrapperAttr->getType()};
|
||||
}
|
||||
|
||||
SyntacticElementTarget
|
||||
@@ -157,9 +155,9 @@ SyntacticElementTarget::forInitialization(Expr *initializer, DeclContext *dc,
|
||||
}
|
||||
}
|
||||
|
||||
SyntacticElementTarget target(
|
||||
initializer, dc, CTP_Initialization, contextualType,
|
||||
/*convertTypeLocator*/ nullptr, /*isDiscarded=*/false);
|
||||
ContextualTypeInfo contextInfo(contextualType, CTP_Initialization);
|
||||
SyntacticElementTarget target(initializer, dc, contextInfo,
|
||||
/*isDiscarded=*/false);
|
||||
target.expression.pattern = pattern;
|
||||
target.expression.bindPatternVarsOneWay = bindPatternVarsOneWay;
|
||||
target.maybeApplyPropertyWrapper();
|
||||
@@ -226,10 +224,10 @@ ContextualPattern SyntacticElementTarget::getContextualPattern() const {
|
||||
forEachStmt.dc);
|
||||
}
|
||||
|
||||
assert(kind == Kind::expression);
|
||||
assert(expression.contextualPurpose == CTP_Initialization);
|
||||
if (expression.contextualPurpose == CTP_Initialization &&
|
||||
expression.initialization.patternBinding) {
|
||||
auto ctp = getExprContextualTypePurpose();
|
||||
assert(ctp == CTP_Initialization);
|
||||
|
||||
if (ctp == CTP_Initialization && expression.initialization.patternBinding) {
|
||||
return ContextualPattern::forPatternBindingDecl(
|
||||
expression.initialization.patternBinding,
|
||||
expression.initialization.patternBindingIndex);
|
||||
@@ -239,12 +237,11 @@ ContextualPattern SyntacticElementTarget::getContextualPattern() const {
|
||||
}
|
||||
|
||||
bool SyntacticElementTarget::infersOpaqueReturnType() const {
|
||||
assert(kind == Kind::expression);
|
||||
switch (expression.contextualPurpose) {
|
||||
switch (getExprContextualTypePurpose()) {
|
||||
case CTP_Initialization:
|
||||
case CTP_ReturnStmt:
|
||||
case CTP_ReturnSingleExpr:
|
||||
if (Type convertType = expression.convertType.getType())
|
||||
if (Type convertType = getExprContextualType())
|
||||
return convertType->hasOpaqueArchetype();
|
||||
return false;
|
||||
default:
|
||||
@@ -253,8 +250,7 @@ bool SyntacticElementTarget::infersOpaqueReturnType() const {
|
||||
}
|
||||
|
||||
bool SyntacticElementTarget::contextualTypeIsOnlyAHint() const {
|
||||
assert(kind == Kind::expression);
|
||||
switch (expression.contextualPurpose) {
|
||||
switch (getExprContextualTypePurpose()) {
|
||||
case CTP_Initialization:
|
||||
return !infersOpaqueReturnType() && !isOptionalSomePatternInit();
|
||||
case CTP_ForEachStmt:
|
||||
|
||||
@@ -458,8 +458,7 @@ TypeChecker::typeCheckTarget(SyntacticElementTarget &target,
|
||||
if (auto *expr = target.getAsExpr()) {
|
||||
// Tell the constraint system what the contextual type is. This informs
|
||||
// diagnostics and is a hint for various performance optimizations.
|
||||
cs.setContextualType(expr, target.getExprContextualTypeLoc(),
|
||||
target.getExprContextualTypePurpose());
|
||||
cs.setContextualInfo(expr, target.getExprContextualTypeInfo());
|
||||
|
||||
// Try to shrink the system by reducing disjunction domains. This
|
||||
// goes through every sub-expression and generate its own sub-system, to
|
||||
@@ -751,9 +750,8 @@ Type TypeChecker::typeCheckParameterDefault(Expr *&defaultValue,
|
||||
}
|
||||
|
||||
defaultExprTarget.setExprConversionType(contextualTy);
|
||||
cs.setContextualType(defaultValue,
|
||||
defaultExprTarget.getExprContextualTypeLoc(),
|
||||
defaultExprTarget.getExprContextualTypePurpose());
|
||||
cs.setContextualInfo(defaultValue,
|
||||
defaultExprTarget.getExprContextualTypeInfo());
|
||||
|
||||
auto viable = cs.solve(defaultExprTarget, FreeTypeVariableBinding::Disallow);
|
||||
if (!viable)
|
||||
|
||||
@@ -36,7 +36,8 @@ TEST_F(SemaTest, TestPlaceholderInferenceForArrayLiteral) {
|
||||
arrayExpr, DC, arrayTy, typedPattern, /*bindPatternVarsOneWay=*/false);
|
||||
|
||||
ConstraintSystem cs(DC, ConstraintSystemOptions());
|
||||
cs.setContextualType(arrayExpr, {arrayRepr, arrayTy}, CTP_Initialization);
|
||||
ContextualTypeInfo contextualInfo({arrayRepr, arrayTy}, CTP_Initialization);
|
||||
cs.setContextualInfo(arrayExpr, contextualInfo);
|
||||
auto failed = cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
|
||||
ASSERT_FALSE(failed);
|
||||
|
||||
@@ -78,7 +79,8 @@ TEST_F(SemaTest, TestPlaceholderInferenceForDictionaryLiteral) {
|
||||
dictExpr, DC, dictTy, typedPattern, /*bindPatternVarsOneWay=*/false);
|
||||
|
||||
ConstraintSystem cs(DC, ConstraintSystemOptions());
|
||||
cs.setContextualType(dictExpr, {dictRepr, dictTy}, CTP_Initialization);
|
||||
ContextualTypeInfo contextualInfo({dictRepr, dictTy}, CTP_Initialization);
|
||||
cs.setContextualInfo(dictExpr, contextualInfo);
|
||||
auto failed = cs.generateConstraints(target, FreeTypeVariableBinding::Disallow);
|
||||
ASSERT_FALSE(failed);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user