Skip to content

[CS][Experiment] Re-activate all inactive constraints #30487

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

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 4 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,10 @@ ERROR(missing_unwrap_optional_try,none,
ERROR(missing_forced_downcast,none,
"%0 is not convertible to %1; "
"did you mean to use 'as!' to force downcast?", (Type, Type))
WARNING(coercion_may_fail_warning,none,
"coercion from %0 to %1 may fail; use 'as?' or 'as!' instead",
(Type, Type))

ERROR(missing_explicit_conversion,none,
"%0 is not implicitly convertible to %1; "
"did you mean to use 'as' to explicitly convert?", (Type, Type))
Expand Down
8 changes: 8 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6184,3 +6184,11 @@ bool MissingQuialifierInMemberRefFailure::diagnoseAsError() {
emitDiagnostic(choice, diag::decl_declared_here, choice->getFullName());
return true;
}

bool CoercionAsForceCastFailure::diagnoseAsError() {
auto *coercion = cast<CoerceExpr>(getRawAnchor());
emitDiagnostic(coercion->getLoc(), diag::coercion_may_fail_warning,
getFromType(), getToType())
.highlight(coercion->getSourceRange());
return true;
}
10 changes: 10 additions & 0 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,16 @@ class MissingQuialifierInMemberRefFailure final : public FailureDiagnostic {
bool diagnoseAsError();
};

/// Emits a warning about an attempt to use the 'as' operator as the 'as!'
/// operator.
class CoercionAsForceCastFailure final : public ContextualFailure {
public:
CoercionAsForceCastFailure(const Solution &solution, Type fromType,
Type toType, ConstraintLocator *locator)
: ContextualFailure(solution, fromType, toType, locator) {}

bool diagnoseAsError() override;
};
} // end namespace constraints
} // end namespace swift

Expand Down
14 changes: 14 additions & 0 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1276,3 +1276,17 @@ AddQualifierToAccessTopLevelName::create(ConstraintSystem &cs,
ConstraintLocator *locator) {
return new (cs.getAllocator()) AddQualifierToAccessTopLevelName(cs, locator);
}

bool AllowCoercionToForceCast::diagnose(const Solution &solution,
bool asNote) const {
CoercionAsForceCastFailure failure(solution, getFromType(), getToType(),
getLocator());
return failure.diagnose(asNote);
}

AllowCoercionToForceCast *
AllowCoercionToForceCast::create(ConstraintSystem &cs, Type fromType,
Type toType, ConstraintLocator *locator) {
return new (cs.getAllocator())
AllowCoercionToForceCast(cs, fromType, toType, locator);
}
31 changes: 31 additions & 0 deletions lib/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,9 @@ enum class FixKind : uint8_t {
/// Member shadows a top-level name, such a name could only be accessed by
/// prefixing it with a module name.
AddQualifierToAccessTopLevelName,

/// A warning fix that allows a coercion to perform a force-cast.
AllowCoercionToForceCast,
};

class ConstraintFix {
Expand Down Expand Up @@ -1728,6 +1731,34 @@ class AllowNonClassTypeToConvertToAnyObject final : public ContextualMismatch {
create(ConstraintSystem &cs, Type type, ConstraintLocator *locator);
};

/// A warning fix to maintain compatibility with the following:
///
/// \code
/// func foo(_ arr: [Any]?) {
/// _ = (arr ?? []) as [NSObject]
/// }
/// \endcode
///
/// which performs a force-cast of the array's elements, despite being spelled
/// as a coercion.
class AllowCoercionToForceCast final : public ContextualMismatch {
AllowCoercionToForceCast(ConstraintSystem &cs, Type fromType, Type toType,
ConstraintLocator *locator)
: ContextualMismatch(cs, FixKind::AllowCoercionToForceCast, fromType,
toType, locator, /*warning*/ true) {}

public:
std::string getName() const {
return "allow coercion to be treated as a force-cast";
}

bool diagnose(const Solution &solution, bool asNote = false) const;

static AllowCoercionToForceCast *create(ConstraintSystem &cs, Type fromType,
Type toType,
ConstraintLocator *locator);
};

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

Expand Down
Loading