@@ -355,6 +355,15 @@ static void desugarConformanceRequirement(
355
355
356
356
// Fast path.
357
357
if (constraintType->is <ProtocolType>()) {
358
+ // Diagnose attempts to introduce a value generic like 'let N: P' where 'P'
359
+ // is some protocol in either the defining context or in an extension where
360
+ // clause.
361
+ if (req.getFirstType ()->isValueParameter ()) {
362
+ errors.push_back (
363
+ RequirementError::forInvalidValueGenericConformance (req, loc));
364
+ return ;
365
+ }
366
+
358
367
if (req.getFirstType ()->isTypeParameter ()) {
359
368
result.push_back (req);
360
369
return ;
@@ -521,50 +530,30 @@ static void realizeTypeRequirement(DeclContext *dc,
521
530
}
522
531
}
523
532
524
- if (subjectType->isValueParameter ()) {
533
+ if (constraintType->isConstraintType ()) {
534
+ result.push_back ({Requirement (RequirementKind::Conformance,
535
+ subjectType, constraintType),
536
+ loc});
537
+ } else if (constraintType->getClassOrBoundGenericClass ()) {
538
+ result.push_back ({Requirement (RequirementKind::Superclass,
539
+ subjectType, constraintType),
540
+ loc});
541
+ } else if (subjectType->isValueParameter () && !isa<ExtensionDecl>(dc)) {
525
542
// This is a correct value generic definition where 'let N: Int'.
526
543
//
527
544
// Note: This definition is only valid in non-extension contexts. If we are
528
545
// in an extension context then the user has written something like:
529
546
// 'extension T where N: Int' which is weird and not supported.
530
- if (constraintType->isLegalValueGenericType () && !isa<ExtensionDecl>(dc) ) {
547
+ if (constraintType->isLegalValueGenericType ()) {
531
548
return ;
532
549
}
533
550
534
- // Diagnose attempts to introduce a value generic like 'let N: P' where 'P'
535
- // is some protocol in either the defining context or in an extension where
536
- // clause.
537
- if (constraintType->isConstraintType ()) {
538
- errors.push_back (
539
- RequirementError::forInvalidValueGenericConformance (subjectType,
540
- constraintType,
541
- loc));
542
- // OK, this is not a constraint type. If we're in an extension complain
543
- // about constraining to non-protocol type.
544
- //
545
- // FIXME: Maybe a better diagnostic here saying you can't constrain a value
546
- // generic parameter to a different value type in an extension?
547
- } else if (isa<ExtensionDecl>(dc)) {
548
- errors.push_back (
549
- RequirementError::forInvalidTypeRequirement (subjectType,
550
- constraintType,
551
- loc));
552
551
// Otherwise, we're trying to define a value generic parameter with an
553
552
// unsupported type right now e.g. 'let N: UInt8'.
554
- } else {
555
- errors.push_back (
553
+ errors.push_back (
556
554
RequirementError::forInvalidValueGenericType (subjectType,
557
555
constraintType,
558
556
loc));
559
- }
560
- } else if (constraintType->isConstraintType ()) {
561
- result.push_back ({Requirement (RequirementKind::Conformance,
562
- subjectType, constraintType),
563
- loc});
564
- } else if (constraintType->getClassOrBoundGenericClass ()) {
565
- result.push_back ({Requirement (RequirementKind::Superclass,
566
- subjectType, constraintType),
567
- loc});
568
557
} else {
569
558
errors.push_back (
570
559
RequirementError::forInvalidTypeRequirement (subjectType,
0 commit comments