Skip to content

Commit 7cd4248

Browse files
committed
Move some value checking to desugaring
1 parent 71fee06 commit 7cd4248

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

lib/AST/RequirementMachine/Diagnostics.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,9 @@ struct RequirementError {
166166
return {Kind::InvalidValueGenericType, requirement, loc};
167167
}
168168

169-
static RequirementError forInvalidValueGenericConformance(Type subjectType,
170-
Type constraint,
169+
static RequirementError forInvalidValueGenericConformance(Requirement req,
171170
SourceLoc loc) {
172-
Requirement requirement(RequirementKind::Conformance, subjectType, constraint);
173-
return {Kind::InvalidValueGenericConformance, requirement, loc};
171+
return {Kind::InvalidValueGenericConformance, req, loc};
174172
}
175173

176174
static RequirementError forInvalidValueGenericSameType(Type subjectType,

lib/AST/RequirementMachine/RequirementLowering.cpp

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,15 @@ static void desugarConformanceRequirement(
355355

356356
// Fast path.
357357
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+
358367
if (req.getFirstType()->isTypeParameter()) {
359368
result.push_back(req);
360369
return;
@@ -521,50 +530,30 @@ static void realizeTypeRequirement(DeclContext *dc,
521530
}
522531
}
523532

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)) {
525542
// This is a correct value generic definition where 'let N: Int'.
526543
//
527544
// Note: This definition is only valid in non-extension contexts. If we are
528545
// in an extension context then the user has written something like:
529546
// 'extension T where N: Int' which is weird and not supported.
530-
if (constraintType->isLegalValueGenericType() && !isa<ExtensionDecl>(dc)) {
547+
if (constraintType->isLegalValueGenericType()) {
531548
return;
532549
}
533550

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));
552551
// Otherwise, we're trying to define a value generic parameter with an
553552
// unsupported type right now e.g. 'let N: UInt8'.
554-
} else {
555-
errors.push_back(
553+
errors.push_back(
556554
RequirementError::forInvalidValueGenericType(subjectType,
557555
constraintType,
558556
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});
568557
} else {
569558
errors.push_back(
570559
RequirementError::forInvalidTypeRequirement(subjectType,

0 commit comments

Comments
 (0)