Skip to content

Revert "[ConstraintSystem] Use missing conformance fix to diagnose contextual failures" #24870

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
// system had a contextual type specified, we use it - it will have a purpose
// indicator which allows us to give a very "to the point" diagnostic.
Diag<Type, Type> diagID;
Diag<Type, Type> diagIDProtocol;
Diag<Type> nilDiag;
std::function<void(void)> nilFollowup;

Expand All @@ -2068,6 +2069,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
"contextual type");
case CTP_Initialization:
diagID = diag::cannot_convert_initializer_value;
diagIDProtocol = diag::cannot_convert_initializer_value_protocol;
nilDiag = diag::cannot_convert_initializer_value_nil;
nilFollowup = [this] {
TypeRepr *patternTR = CS.getContextualTypeLoc().getTypeRepr();
Expand All @@ -2093,6 +2095,7 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
}

diagID = diag::cannot_convert_to_return_type;
diagIDProtocol = diag::cannot_convert_to_return_type_protocol;
nilDiag = diag::cannot_convert_to_return_type_nil;
break;
case CTP_ThrowStmt: {
Expand Down Expand Up @@ -2140,10 +2143,12 @@ bool FailureDiagnosis::diagnoseContextualConversionError(

case CTP_EnumCaseRawValue:
diagID = diag::cannot_convert_raw_initializer_value;
diagIDProtocol = diag::cannot_convert_raw_initializer_value;
nilDiag = diag::cannot_convert_raw_initializer_value_nil;
break;
case CTP_DefaultParameter:
diagID = diag::cannot_convert_default_arg_value;
diagIDProtocol = diag::cannot_convert_default_arg_value_protocol;
nilDiag = diag::cannot_convert_default_arg_value_nil;
break;

Expand All @@ -2163,34 +2168,42 @@ bool FailureDiagnosis::diagnoseContextualConversionError(
return true;
case CTP_YieldByValue:
diagID = diag::cannot_convert_yield_value;
diagIDProtocol = diag::cannot_convert_yield_value_protocol;
nilDiag = diag::cannot_convert_yield_value_nil;
break;
case CTP_CallArgument:
diagID = diag::cannot_convert_argument_value;
diagIDProtocol = diag::cannot_convert_argument_value_protocol;
nilDiag = diag::cannot_convert_argument_value_nil;
break;
case CTP_ClosureResult:
diagID = diag::cannot_convert_closure_result;
diagIDProtocol = diag::cannot_convert_closure_result_protocol;
nilDiag = diag::cannot_convert_closure_result_nil;
break;
case CTP_ArrayElement:
diagID = diag::cannot_convert_array_element;
diagIDProtocol = diag::cannot_convert_array_element_protocol;
nilDiag = diag::cannot_convert_array_element_nil;
break;
case CTP_DictionaryKey:
diagID = diag::cannot_convert_dict_key;
diagIDProtocol = diag::cannot_convert_dict_key_protocol;
nilDiag = diag::cannot_convert_dict_key_nil;
break;
case CTP_DictionaryValue:
diagID = diag::cannot_convert_dict_value;
diagIDProtocol = diag::cannot_convert_dict_value_protocol;
nilDiag = diag::cannot_convert_dict_value_nil;
break;
case CTP_CoerceOperand:
diagID = diag::cannot_convert_coerce;
diagIDProtocol = diag::cannot_convert_coerce_protocol;
nilDiag = diag::cannot_convert_coerce_nil;
break;
case CTP_AssignSource:
diagID = diag::cannot_convert_assign;
diagIDProtocol = diag::cannot_convert_assign_protocol;
nilDiag = diag::cannot_convert_assign_nil;
break;
}
Expand Down Expand Up @@ -2302,13 +2315,10 @@ bool FailureDiagnosis::diagnoseContextualConversionError(

// When complaining about conversion to a protocol type, complain about
// conformance instead of "conversion".
if (contextualType->isExistentialType()) {
MissingContextualConformanceFailure failure(
expr, CS, CTP, exprType, contextualType,
CS.getConstraintLocator(expr, ConstraintLocator::ContextualType));
return failure.diagnoseAsError();
}

if (contextualType->is<ProtocolType>() ||
contextualType->is<ProtocolCompositionType>())
diagID = diagIDProtocol;

// Try to simplify irrelevant details of function types. For example, if
// someone passes a "() -> Float" function to a "() throws -> Int"
// parameter, then uttering the "throws" may confuse them into thinking that
Expand Down
80 changes: 0 additions & 80 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2812,83 +2812,3 @@ bool CollectionElementContextualFailure::diagnoseAsError() {
eltType, contextualType, anchor);
return true;
}

bool MissingContextualConformanceFailure::diagnoseAsError() {
auto *anchor = getAnchor();
auto path = getLocator()->getPath();

Optional<Diag<Type, Type>> diagnostic;
if (path.empty()) {
assert(isa<AssignExpr>(anchor));
diagnostic = getDiagnosticFor(CTP_AssignSource);
} else {
const auto &last = path.back();
switch (last.getKind()) {
case ConstraintLocator::ContextualType:
assert(Context != CTP_Unused);
diagnostic = getDiagnosticFor(Context);
break;

default:
break;
}
}

if (!diagnostic)
return false;

auto srcType = getFromType();
auto dstType = getToType();

emitDiagnostic(anchor->getLoc(), *diagnostic, srcType, dstType);

if (isa<InOutExpr>(anchor))
return true;

if (srcType->isAny() && dstType->isAnyObject()) {
emitDiagnostic(anchor->getLoc(), diag::any_as_anyobject_fixit)
.fixItInsertAfter(anchor->getEndLoc(), " as AnyObject");
}

return true;
}

Optional<Diag<Type, Type>>
MissingContextualConformanceFailure::getDiagnosticFor(
ContextualTypePurpose context) {
switch (context) {
case CTP_Initialization:
return diag::cannot_convert_initializer_value_protocol;
case CTP_ReturnStmt:
case CTP_ReturnSingleExpr:
return diag::cannot_convert_to_return_type_protocol;
case CTP_EnumCaseRawValue:
return diag::cannot_convert_raw_initializer_value;
case CTP_DefaultParameter:
return diag::cannot_convert_default_arg_value_protocol;
case CTP_YieldByValue:
return diag::cannot_convert_yield_value_protocol;
case CTP_CallArgument:
return diag::cannot_convert_argument_value_protocol;
case CTP_ClosureResult:
return diag::cannot_convert_closure_result_protocol;
case CTP_ArrayElement:
return diag::cannot_convert_array_element_protocol;
case CTP_DictionaryKey:
return diag::cannot_convert_dict_key_protocol;
case CTP_DictionaryValue:
return diag::cannot_convert_dict_value_protocol;
case CTP_CoerceOperand:
return diag::cannot_convert_coerce_protocol;
case CTP_AssignSource:
return diag::cannot_convert_assign_protocol;

case CTP_ThrowStmt:
case CTP_Unused:
case CTP_CannotFail:
case CTP_YieldByReference:
case CTP_CalleeResult:
break;
}
return None;
}
30 changes: 4 additions & 26 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,14 @@ class RequirementFailure : public FailureDiagnostic {
/// ```
class MissingConformanceFailure final : public RequirementFailure {
Type NonConformingType;
Type ProtocolType;
ProtocolDecl *Protocol;

public:
MissingConformanceFailure(Expr *expr, ConstraintSystem &cs,
ConstraintLocator *locator,
std::pair<Type, Type> conformance)
std::pair<Type, ProtocolDecl *> conformance)
: RequirementFailure(cs, expr, RequirementKind::Conformance, locator),
NonConformingType(conformance.first), ProtocolType(conformance.second) {
}
NonConformingType(conformance.first), Protocol(conformance.second) {}

bool diagnoseAsError() override;

Expand All @@ -342,7 +341,7 @@ class MissingConformanceFailure final : public RequirementFailure {
Type getLHS() const override { return NonConformingType; }

/// The protocol generic requirement expected associated type to conform to.
Type getRHS() const override { return ProtocolType; }
Type getRHS() const override { return Protocol->getDeclaredType(); }

protected:
DiagOnDecl getDiagnosticOnDecl() const override {
Expand Down Expand Up @@ -1203,27 +1202,6 @@ class CollectionElementContextualFailure final : public ContextualFailure {
bool diagnoseAsError() override;
};

class MissingContextualConformanceFailure final : public ContextualFailure {
ContextualTypePurpose Context;

public:
MissingContextualConformanceFailure(Expr *root, ConstraintSystem &cs,
ContextualTypePurpose context, Type type,
Type protocolType,
ConstraintLocator *locator)
: ContextualFailure(root, cs, type, protocolType, locator),
Context(context) {
assert(protocolType->is<ProtocolType>() ||
protocolType->is<ProtocolCompositionType>());
}

bool diagnoseAsError() override;

private:
static Optional<Diag<Type, Type>>
getDiagnosticFor(ContextualTypePurpose purpose);
};

} // end namespace constraints
} // end namespace swift

Expand Down
33 changes: 7 additions & 26 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,35 +160,16 @@ RelabelArguments::create(ConstraintSystem &cs,
}

bool MissingConformance::diagnose(Expr *root, bool asNote) const {
auto &cs = getConstraintSystem();
auto *locator = getLocator();

if (IsContextual) {
auto context = cs.getContextualTypePurpose();
MissingContextualConformanceFailure failure(
root, cs, context, NonConformingType, ProtocolType, locator);
return failure.diagnose(asNote);
}

MissingConformanceFailure failure(
root, cs, locator, std::make_pair(NonConformingType, ProtocolType));
MissingConformanceFailure failure(root, getConstraintSystem(), getLocator(),
{NonConformingType, Protocol});
return failure.diagnose(asNote);
}

MissingConformance *
MissingConformance::forContextual(ConstraintSystem &cs, Type type,
Type protocolType,
ConstraintLocator *locator) {
return new (cs.getAllocator()) MissingConformance(
cs, /*isContextual=*/true, type, protocolType, locator);
}

MissingConformance *
MissingConformance::forRequirement(ConstraintSystem &cs, Type type,
Type protocolType,
ConstraintLocator *locator) {
return new (cs.getAllocator()) MissingConformance(
cs, /*isContextual=*/false, type, protocolType, locator);
MissingConformance *MissingConformance::create(ConstraintSystem &cs, Type type,
ProtocolDecl *protocol,
ConstraintLocator *locator) {
return new (cs.getAllocator())
MissingConformance(cs, type, protocol, locator);
}

bool SkipSameTypeRequirement::diagnose(Expr *root, bool asNote) const {
Expand Down
26 changes: 8 additions & 18 deletions lib/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,19 +363,13 @@ class RelabelArguments final

/// Add a new conformance to the type to satisfy a requirement.
class MissingConformance final : public ConstraintFix {
// Determines whether given protocol type comes from the context e.g.
// assignment destination or argument comparison.
bool IsContextual;

Type NonConformingType;
// This could either be a protocol or protocol composition.
Type ProtocolType;
ProtocolDecl *Protocol;

MissingConformance(ConstraintSystem &cs, bool isContextual, Type type,
Type protocolType, ConstraintLocator *locator)
MissingConformance(ConstraintSystem &cs, Type type, ProtocolDecl *protocol,
ConstraintLocator *locator)
: ConstraintFix(cs, FixKind::AddConformance, locator),
IsContextual(isContextual), NonConformingType(type),
ProtocolType(protocolType) {}
NonConformingType(type), Protocol(protocol) {}

public:
std::string getName() const override {
Expand All @@ -384,17 +378,13 @@ class MissingConformance final : public ConstraintFix {

bool diagnose(Expr *root, bool asNote = false) const override;

static MissingConformance *forRequirement(ConstraintSystem &cs, Type type,
Type protocolType,
ConstraintLocator *locator);

static MissingConformance *forContextual(ConstraintSystem &cs, Type type,
Type protocolType,
ConstraintLocator *locator);
static MissingConformance *create(ConstraintSystem &cs, Type type,
ProtocolDecl *protocol,
ConstraintLocator *locator);

Type getNonConformingType() { return NonConformingType; }

Type getProtocolType() { return ProtocolType; }
ProtocolDecl *getProtocol() { return Protocol; }
};

/// Skip same-type generic requirement constraint,
Expand Down
Loading