Skip to content

Commit 8bdfb91

Browse files
committed
[NFC] Move the non-materializable variable check
Move this from the validation phase into the phase where we check the binding. The type checker no longer has to reset the specifier for VarDecls.
1 parent ebdd126 commit 8bdfb91

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,28 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
21792179

21802180
auto resultTy = typeCheckExpression(initializer, DC, contextualType,
21812181
contextualPurpose, flags, &listener);
2182+
2183+
// If we detected that the initializer would have a non-materializable type,
2184+
// complain.
2185+
if (resultTy && listener.isInOut()) {
2186+
diagnose(pattern->getStartLoc(), diag::var_type_not_materializable,
2187+
resultTy);
2188+
2189+
pattern->setType(ErrorType::get(Context));
2190+
pattern->forEachVariable([&](VarDecl *var) {
2191+
// Don't change the type of a variable that we've been able to
2192+
// compute a type for.
2193+
if (var->hasType() &&
2194+
!var->getType()->hasUnboundGenericType() &&
2195+
!var->getType()->hasError())
2196+
return;
2197+
2198+
var->markInvalid();
2199+
});
2200+
2201+
return true;
2202+
}
2203+
21822204
if (resultTy) {
21832205
TypeResolutionOptions options;
21842206
options |= TR_OverrideType;
@@ -2196,7 +2218,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
21962218

21972219
// Apply the solution to the pattern as well.
21982220
if (coercePatternToType(pattern, DC, initTy, options,
2199-
nullptr, TypeLoc(), listener.isInOut())) {
2221+
nullptr, TypeLoc())) {
22002222
return true;
22012223
}
22022224
}

lib/Sema/TypeCheckDecl.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3984,12 +3984,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
39843984

39853985
void visitBoundVariable(VarDecl *VD) {
39863986
TC.validateDecl(VD);
3987-
3988-
if (!VD->getType()->isMaterializable()) {
3989-
TC.diagnose(VD->getStartLoc(), diag::var_type_not_materializable,
3990-
VD->getType());
3991-
VD->markInvalid();
3992-
}
39933987

39943988
// Check the behavior.
39953989
checkVarBehavior(VD, TC);

lib/Sema/TypeCheckPattern.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -914,8 +914,7 @@ bool TypeChecker::typeCheckPattern(Pattern *P, DeclContext *dc,
914914
bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
915915
TypeResolutionOptions options,
916916
GenericTypeResolver *resolver,
917-
TypeLoc tyLoc,
918-
bool forceInOut) {
917+
TypeLoc tyLoc) {
919918
recur:
920919
if (tyLoc.isNull()) {
921920
tyLoc = TypeLoc::withoutLoc(type);
@@ -1001,9 +1000,6 @@ bool TypeChecker::coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
10011000
VarDecl *var = NP->getDecl();
10021001
if (var->isInvalid())
10031002
type = ErrorType::get(Context);
1004-
if (forceInOut) {
1005-
var->setSpecifier(VarDecl::Specifier::InOut);
1006-
}
10071003
var->setType(type->getInOutObjectType());
10081004
// FIXME: wtf
10091005
if (type->hasTypeParameter())

lib/Sema/TypeChecker.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,8 +1799,7 @@ class TypeChecker final : public LazyResolver {
17991799
bool coercePatternToType(Pattern *&P, DeclContext *dc, Type type,
18001800
TypeResolutionOptions options,
18011801
GenericTypeResolver *resolver = nullptr,
1802-
TypeLoc tyLoc = TypeLoc(),
1803-
bool forceInOut = false);
1802+
TypeLoc tyLoc = TypeLoc());
18041803
bool typeCheckExprPattern(ExprPattern *EP, DeclContext *DC,
18051804
Type type);
18061805

0 commit comments

Comments
 (0)