Skip to content

Commit ab2f47b

Browse files
committed
[CSFix] Add a fix to detect invalid specialization of non-generic types
1 parent cbaf558 commit ab2f47b

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

include/swift/Sema/CSFix.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ enum class FixKind : uint8_t {
454454
/// Ignore the fact that member couldn't be referenced within init accessor
455455
/// because its name doesn't appear in 'initializes' or 'accesses' attributes.
456456
AllowInvalidMemberReferenceInInitAccessor,
457+
458+
/// Ignore an attempt to specialize non-generic type.
459+
AllowConcreteTypeSpecialization,
457460
};
458461

459462
class ConstraintFix {
@@ -3647,6 +3650,33 @@ class AllowInvalidMemberReferenceInInitAccessor final : public ConstraintFix {
36473650
}
36483651
};
36493652

3653+
class AllowConcreteTypeSpecialization final : public ConstraintFix {
3654+
Type ConcreteType;
3655+
3656+
AllowConcreteTypeSpecialization(ConstraintSystem &cs, Type concreteTy,
3657+
ConstraintLocator *locator)
3658+
: ConstraintFix(cs, FixKind::AllowConcreteTypeSpecialization, locator),
3659+
ConcreteType(concreteTy) {}
3660+
3661+
public:
3662+
std::string getName() const override {
3663+
return "allow concrete type specialization";
3664+
}
3665+
3666+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3667+
3668+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
3669+
return diagnose(*commonFixes.front().first);
3670+
}
3671+
3672+
static AllowConcreteTypeSpecialization *
3673+
create(ConstraintSystem &cs, Type concreteTy, ConstraintLocator *locator);
3674+
3675+
static bool classof(const ConstraintFix *fix) {
3676+
return fix->getKind() == FixKind::AllowConcreteTypeSpecialization;
3677+
}
3678+
};
3679+
36503680
} // end namespace constraints
36513681
} // end namespace swift
36523682

lib/Sema/CSFix.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2829,3 +2829,15 @@ AllowInvalidMemberReferenceInInitAccessor::create(ConstraintSystem &cs,
28292829
return new (cs.getAllocator())
28302830
AllowInvalidMemberReferenceInInitAccessor(cs, memberName, locator);
28312831
}
2832+
2833+
bool AllowConcreteTypeSpecialization::diagnose(const Solution &solution,
2834+
bool asNote) const {
2835+
return false;
2836+
}
2837+
2838+
AllowConcreteTypeSpecialization *
2839+
AllowConcreteTypeSpecialization::create(ConstraintSystem &cs, Type concreteTy,
2840+
ConstraintLocator *locator) {
2841+
return new (cs.getAllocator())
2842+
AllowConcreteTypeSpecialization(cs, concreteTy, locator);
2843+
}

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13657,6 +13657,10 @@ ConstraintSystem::simplifyExplicitGenericArgumentsConstraint(
1365713657
}
1365813658
}
1365913659
}
13660+
13661+
if (openedGenericParams.empty())
13662+
return SolutionKind::Error;
13663+
1366013664
assert(openedGenericParams.size() == genericParams->size());
1366113665

1366213666
// Match the opened generic parameters to the specialized arguments.
@@ -14764,7 +14768,8 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
1476414768
case FixKind::MacroMissingPound:
1476514769
case FixKind::AllowGlobalActorMismatch:
1476614770
case FixKind::AllowAssociatedValueMismatch:
14767-
case FixKind::GenericArgumentsMismatch: {
14771+
case FixKind::GenericArgumentsMismatch:
14772+
case FixKind::AllowConcreteTypeSpecialization: {
1476814773
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
1476914774
}
1477014775
case FixKind::IgnoreInvalidASTNode: {

0 commit comments

Comments
 (0)