@@ -389,15 +389,6 @@ static void desugarConformanceRequirement(
389389
390390 // Fast path.
391391 if (constraintType->is <ProtocolType>()) {
392- // Diagnose attempts to introduce a value generic like 'let N: P' where 'P'
393- // is some protocol in either the defining context or in an extension where
394- // clause.
395- if (req.getFirstType ()->isValueParameter ()) {
396- errors.push_back (
397- RequirementError::forInvalidValueGenericConformance (req, loc));
398- return ;
399- }
400-
401392 if (req.getFirstType ()->isTypeParameter ()) {
402393 result.push_back (req);
403394 return ;
@@ -538,7 +529,26 @@ void swift::rewriting::realizeTypeRequirement(DeclContext *dc,
538529 Type constraintType,
539530 SourceLoc loc,
540531 SmallVectorImpl<StructuralRequirement> &result,
541- SmallVectorImpl<RequirementError> &errors) {
532+ SmallVectorImpl<RequirementError> &errors,
533+ bool isFromInheritanceClause) {
534+ // Handle value generics first.
535+ if (subjectType->isValueParameter ()) {
536+ if (isFromInheritanceClause) {
537+ if (!constraintType->isLegalValueGenericType ()) {
538+ // The definition of a generic value parameter has an unsupported type,
539+ // e.g. `<let N: UInt8>`.
540+ errors.push_back (RequirementError::forInvalidValueGenericType (
541+ subjectType, constraintType, loc));
542+ }
543+ } else {
544+ // A generic value parameter was used as the subject of a subtype
545+ // constraint, e.g. `N: X` in `struct S<let N: Int> where N: X`.
546+ errors.push_back (RequirementError::forInvalidValueGenericConstraint (
547+ subjectType, constraintType, loc));
548+ }
549+ return ;
550+ }
551+
542552 // The GenericSignatureBuilder allowed the right hand side of a
543553 // conformance or superclass requirement to reference a protocol
544554 // typealias whose underlying type was a protocol or class.
@@ -574,22 +584,6 @@ void swift::rewriting::realizeTypeRequirement(DeclContext *dc,
574584 result.push_back ({Requirement (RequirementKind::Superclass,
575585 subjectType, constraintType),
576586 loc});
577- } else if (subjectType->isValueParameter () && !isa<ExtensionDecl>(dc)) {
578- // This is a correct value generic definition where 'let N: Int'.
579- //
580- // Note: This definition is only valid in non-extension contexts. If we are
581- // in an extension context then the user has written something like:
582- // 'extension T where N: Int' which is weird and not supported.
583- if (constraintType->isLegalValueGenericType ()) {
584- return ;
585- }
586-
587- // Otherwise, we're trying to define a value generic parameter with an
588- // unsupported type right now e.g. 'let N: UInt8'.
589- errors.push_back (
590- RequirementError::forInvalidValueGenericType (subjectType,
591- constraintType,
592- loc));
593587 } else {
594588 errors.push_back (
595589 RequirementError::forInvalidTypeRequirement (subjectType,
@@ -792,7 +786,8 @@ void swift::rewriting::realizeRequirement(
792786 inferRequirements (secondType, moduleForInference, dc, result);
793787 }
794788
795- realizeTypeRequirement (dc, firstType, secondType, loc, result, errors);
789+ realizeTypeRequirement (dc, firstType, secondType, loc, result, errors,
790+ /* isFromInheritanceClause*/ false );
796791 break ;
797792 }
798793
@@ -845,7 +840,8 @@ void swift::rewriting::realizeInheritedRequirements(
845840 auto *typeRepr = inheritedTypes.getTypeRepr (index);
846841 SourceLoc loc = (typeRepr ? typeRepr->getStartLoc () : SourceLoc ());
847842
848- realizeTypeRequirement (dc, type, inheritedType, loc, result, errors);
843+ realizeTypeRequirement (dc, type, inheritedType, loc, result, errors,
844+ /* isFromInheritanceClause*/ true );
849845 }
850846
851847 // Also check for `SynthesizedProtocolAttr`s with additional constraints added
@@ -856,7 +852,8 @@ void swift::rewriting::realizeInheritedRequirements(
856852 auto inheritedType = attr->getProtocol ()->getDeclaredType ();
857853 auto loc = attr->getLocation ();
858854
859- realizeTypeRequirement (dc, type, inheritedType, loc, result, errors);
855+ realizeTypeRequirement (dc, type, inheritedType, loc, result, errors,
856+ /* isFromInheritanceClause*/ false );
860857 }
861858}
862859
0 commit comments