Skip to content

Commit ae5611a

Browse files
committed
crash once we get to code i want to better understand
1 parent 627bcc5 commit ae5611a

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6589,6 +6589,7 @@ ERROR(noimplicitcopy_attr_valid_only_on_local_let_params,
65896589
none, "'@_noImplicitCopy' attribute can only be applied to local lets and params", ())
65906590
ERROR(noimplicitcopy_attr_invalid_in_generic_context,
65916591
none, "'@_noImplicitCopy' attribute cannot be applied to entities in generic contexts", ())
6592+
ERROR(moveonly_generics, none, "move-only type %0 cannot be used with generics yet", (Type))
65926593

65936594
//------------------------------------------------------------------------------
65946595
// MARK: Type inference from default expressions

include/swift/Sema/CSFix.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ enum class FixKind : uint8_t {
421421

422422
/// Macro that has parameters but was not provided with any arguments.
423423
MacroMissingArguments,
424+
425+
/// Produce an error about a type that must be Copyable
426+
MustBeCopyable,
424427
};
425428

426429
class ConstraintFix {
@@ -2019,6 +2022,25 @@ class NotCompileTimeConst final : public ContextualMismatch {
20192022
}
20202023
};
20212024

2025+
class MustBeCopyable final : public ConstraintFix {
2026+
Type noncopyableTy;
2027+
2028+
MustBeCopyable(ConstraintSystem &cs, Type noncopyableTy, ConstraintLocator *locator);
2029+
2030+
public:
2031+
std::string getName() const override { return "remove move-only from type"; }
2032+
2033+
bool diagnose(const Solution &solution, bool asNote = false) const override;
2034+
2035+
static MustBeCopyable *create(ConstraintSystem &cs,
2036+
Type noncopyableTy,
2037+
ConstraintLocator *locator);
2038+
2039+
static bool classof(ConstraintFix *fix) {
2040+
return fix->getKind() == FixKind::MustBeCopyable;
2041+
}
2042+
};
2043+
20222044
class CollectionElementContextualMismatch final
20232045
: public ContextualMismatch,
20242046
private llvm::TrailingObjects<CollectionElementContextualMismatch,

lib/Sema/CSDiagnostics.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5803,6 +5803,11 @@ bool NotCompileTimeConstFailure::diagnoseAsError() {
58035803
return true;
58045804
}
58055805

5806+
bool NotCopyableFailure::diagnoseAsError() {
5807+
emitDiagnostic(diag::moveonly_generics, noncopyableTy);
5808+
return true;
5809+
}
5810+
58065811
bool CollectionElementContextualFailure::diagnoseAsError() {
58075812
auto anchor = getRawAnchor();
58085813
auto *locator = getLocator();

lib/Sema/CSDiagnostics.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,15 @@ class NotCompileTimeConstFailure final : public FailureDiagnostic {
18231823
bool diagnoseAsError() override;
18241824
};
18251825

1826+
class NotCopyableFailure final : public FailureDiagnostic {
1827+
Type noncopyableTy;
1828+
public:
1829+
NotCopyableFailure(const Solution &solution, Type noncopyableTy, ConstraintLocator *locator)
1830+
: FailureDiagnostic(solution, locator), noncopyableTy(noncopyableTy) {}
1831+
1832+
bool diagnoseAsError() override;
1833+
};
1834+
18261835
/// Diagnose a contextual mismatch between expected collection element type
18271836
/// and the one provided (e.g. source of the assignment or argument to a call)
18281837
/// e.g.:

lib/Sema/CSFix.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,21 @@ bool NotCompileTimeConst::diagnose(const Solution &solution, bool asNote) const
13261326
return failure.diagnose(asNote);
13271327
}
13281328

1329+
MustBeCopyable::MustBeCopyable(ConstraintSystem &cs, Type noncopyableTy, ConstraintLocator *locator)
1330+
: ConstraintFix(cs, FixKind::MustBeCopyable, locator, FixBehavior::Error),
1331+
noncopyableTy(noncopyableTy) {}
1332+
1333+
bool MustBeCopyable::diagnose(const Solution &solution, bool asNote) const {
1334+
NotCopyableFailure failure(solution, noncopyableTy, getLocator());
1335+
return failure.diagnose(asNote);
1336+
}
1337+
1338+
MustBeCopyable* MustBeCopyable::create(ConstraintSystem &cs,
1339+
Type noncopyableTy,
1340+
ConstraintLocator *locator) {
1341+
return new (cs.getAllocator()) MustBeCopyable(cs, noncopyableTy, locator);
1342+
}
1343+
13291344
bool CollectionElementContextualMismatch::diagnose(const Solution &solution,
13301345
bool asNote) const {
13311346
CollectionElementContextualFailure failure(

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4203,8 +4203,15 @@ ConstraintSystem::matchTypesBindTypeVar(
42034203

42044204
// If the type variable cannot bind to a move-only type, fail.
42054205
if (!typeVar->getImpl().canBindToMoveOnly() && type->isPureMoveOnly()) {
4206-
// TODO: record a fix that just emits an error to reject the substitution.
4207-
return getTypeMatchFailure(locator);
4206+
if (shouldAttemptFixes()) {
4207+
auto *fix = MustBeCopyable::create(*this, type,
4208+
getConstraintLocator(locator));
4209+
if (recordFix(fix))
4210+
return getTypeMatchFailure(locator);
4211+
4212+
} else {
4213+
return getTypeMatchFailure(locator);
4214+
}
42084215
}
42094216

42104217
// We do not allow keypaths to go through AnyObject. Let's create a fix
@@ -13810,6 +13817,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1381013817
case FixKind::RenameConflictingPatternVariables:
1381113818
case FixKind::MacroMissingPound:
1381213819
case FixKind::MacroMissingArguments: {
13820+
case FixKind::RenameConflictingPatternVariables:
13821+
case FixKind::MustBeCopyable: {
1381313822
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1381413823
}
1381513824
case FixKind::IgnoreInvalidASTNode: {

0 commit comments

Comments
 (0)