Skip to content

NFC for SR-12085 Clean up TypeCheckType so it never returns Type(). #29780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2688,7 +2688,7 @@ bool ContextualFailure::trySequenceSubsequenceFixIts(
auto String = TypeChecker::getStringType(getASTContext());
auto Substring = TypeChecker::getSubstringType(getASTContext());

if (!String || !Substring)
if (String->hasError() || Substring->hasError())
return false;

// Substring -> String conversion
Expand Down
10 changes: 5 additions & 5 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1768,7 +1768,7 @@ namespace {
TVO_CanBindToNoEscape);

Type optTy = getOptionalType(expr->getSubExpr()->getLoc(), valueTy);
if (!optTy)
if (optTy->hasError())
return Type();

// Prior to Swift 5, 'try?' always adds an additional layer of optionality,
Expand Down Expand Up @@ -2828,7 +2828,7 @@ namespace {
// Try to build the appropriate type for a variadic argument list of
// the fresh element type. If that failed, just bail out.
auto array = TypeChecker::getArraySliceType(expr->getLoc(), element);
if (!array) return element;
if (array->hasError()) return element;

// Require the operand to be convertible to the array type.
CS.addConstraint(ConstraintKind::Conversion,
Expand Down Expand Up @@ -3200,9 +3200,9 @@ namespace {
/// worth QoI efforts.
Type getOptionalType(SourceLoc optLoc, Type valueTy) {
auto optTy = TypeChecker::getOptionalType(optLoc, valueTy);
if (!optTy ||
if (optTy->hasError() ||
TypeChecker::requireOptionalIntrinsics(CS.getASTContext(), optLoc))
return Type();
return ErrorType::get(CS.getASTContext());

return optTy;
}
Expand Down Expand Up @@ -3233,7 +3233,7 @@ namespace {
TVO_CanBindToNoEscape);

Type optTy = getOptionalType(expr->getSubExpr()->getLoc(), valueTy);
if (!optTy)
if (optTy->hasError())
return Type();

CS.addConstraint(ConstraintKind::Conversion,
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2122,9 +2122,9 @@ static Type validateParameterType(ParamDecl *decl) {

if (decl->isVariadic()) {
Ty = TypeChecker::getArraySliceType(decl->getStartLoc(), Ty);
if (Ty.isNull()) {
if (Ty->hasError()) {
decl->setInvalid();
return ErrorType::get(ctx);
return Ty;
}

// Disallow variadic parameters in enum elements.
Expand Down
86 changes: 36 additions & 50 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ Type TypeChecker::getArraySliceType(SourceLoc loc, Type elementType) {
ASTContext &ctx = elementType->getASTContext();
if (!ctx.getArrayDecl()) {
ctx.Diags.diagnose(loc, diag::sugar_type_not_found, 0);
return Type();
return ErrorType::get(ctx);
}

return ArraySliceType::get(elementType);
Expand All @@ -375,7 +375,7 @@ Type TypeChecker::getDictionaryType(SourceLoc loc, Type keyType,
ASTContext &ctx = keyType->getASTContext();
if (!ctx.getDictionaryDecl()) {
ctx.Diags.diagnose(loc, diag::sugar_type_not_found, 3);
return Type();
return ErrorType::get(ctx);
}

return DictionaryType::get(keyType, valueType);
Expand All @@ -385,7 +385,7 @@ Type TypeChecker::getOptionalType(SourceLoc loc, Type elementType) {
ASTContext &ctx = elementType->getASTContext();
if (!ctx.getOptionalDecl()) {
ctx.Diags.diagnose(loc, diag::sugar_type_not_found, 1);
return Type();
return ErrorType::get(ctx);
}

return OptionalType::get(elementType);
Expand All @@ -395,35 +395,35 @@ Type TypeChecker::getStringType(ASTContext &Context) {
if (auto typeDecl = Context.getStringDecl())
return typeDecl->getDeclaredInterfaceType();

return Type();
llvm_unreachable("Broken Standard library: TypeChecker::getStringType failed.");
}

Type TypeChecker::getSubstringType(ASTContext &Context) {
if (auto typeDecl = Context.getSubstringDecl())
return typeDecl->getDeclaredInterfaceType();

return Type();
llvm_unreachable("Broken Standard library: TypeChecker::getSubstringType failed.");
}

Type TypeChecker::getIntType(ASTContext &Context) {
if (auto typeDecl = Context.getIntDecl())
return typeDecl->getDeclaredInterfaceType();

return Type();
llvm_unreachable("Broken Standard library: TypeChecker::getIntType failed.");
}

Type TypeChecker::getInt8Type(ASTContext &Context) {
if (auto typeDecl = Context.getInt8Decl())
return typeDecl->getDeclaredInterfaceType();

return Type();
llvm_unreachable("Broken Standard library: TypeChecker::getInt8Type failed.");
}

Type TypeChecker::getUInt8Type(ASTContext &Context) {
if (auto typeDecl = Context.getUInt8Decl())
return typeDecl->getDeclaredInterfaceType();

return Type();
llvm_unreachable("Broken Standard library: TypeChecker::getUInt8Type failed.");
}

Type
Expand Down Expand Up @@ -1802,12 +1802,11 @@ namespace {
= nullptr,
DifferentiabilityKind diffKind
= DifferentiabilityKind::NonDifferentiable);
bool
SmallVector<AnyFunctionType::Param, 8>
resolveASTFunctionTypeParams(TupleTypeRepr *inputRepr,
TypeResolutionOptions options,
bool requiresMappingOut,
DifferentiabilityKind diffKind,
SmallVectorImpl<AnyFunctionType::Param> &ps);
DifferentiabilityKind diffKind);

Type resolveSILFunctionType(FunctionTypeRepr *repr,
TypeResolutionOptions options,
Expand Down Expand Up @@ -2523,11 +2522,12 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
return ty;
}

bool TypeResolver::resolveASTFunctionTypeParams(
TupleTypeRepr *inputRepr, TypeResolutionOptions options,
bool requiresMappingOut, DifferentiabilityKind diffKind,
SmallVectorImpl<AnyFunctionType::Param> &elements) {
elements.reserve(inputRepr->getNumElements());
SmallVector<AnyFunctionType::Param, 8> TypeResolver::resolveASTFunctionTypeParams(
TupleTypeRepr *inputRepr, TypeResolutionOptions options,
bool requiresMappingOut, DifferentiabilityKind diffKind) {

SmallVector<AnyFunctionType::Param, 8> params;
params.reserve(inputRepr->getNumElements());

auto elementOptions = options.withoutContext(true);
elementOptions.setContext(TypeResolverContext::FunctionInput);
Expand All @@ -2540,17 +2540,16 @@ bool TypeResolver::resolveASTFunctionTypeParams(
auto thisElementOptions = elementOptions;
bool variadic = false;
if (inputRepr->hasEllipsis() &&
elements.size() == inputRepr->getEllipsisIndex()) {
params.size() == inputRepr->getEllipsisIndex()) {
thisElementOptions = elementOptions.withoutContext();
thisElementOptions.setContext(TypeResolverContext::VariadicFunctionInput);
variadic = true;
}

Type ty = resolveType(eltTypeRepr, thisElementOptions);
if (!ty) return true;

if (ty->hasError()) {
elements.emplace_back(ErrorType::get(Context));
params.emplace_back(ErrorType::get(Context));
continue;
}

Expand Down Expand Up @@ -2607,7 +2606,7 @@ bool TypeResolver::resolveASTFunctionTypeParams(
auto paramFlags = ParameterTypeFlags::fromParameterType(
ty, variadic, autoclosure, /*isNonEphemeral*/ false, ownership,
noDerivative);
elements.emplace_back(ty, Identifier(), paramFlags);
params.emplace_back(ty, Identifier(), paramFlags);
}

// All non-`@noDerivative` parameters of `@differentiable` and
Expand All @@ -2619,15 +2618,15 @@ bool TypeResolver::resolveASTFunctionTypeParams(
// differentiability/linearity parameter. Otherwise, adding `@noDerivative`
// produces an ill-formed function type.
auto hasValidDifferentiabilityParam =
llvm::find_if(elements, [&](AnyFunctionType::Param param) {
llvm::find_if(params, [&](AnyFunctionType::Param param) {
if (param.isNoDerivative())
return false;
return isDifferentiable(param.getPlainType(),
/*tangentVectorEqualsSelf*/ isLinear);
}) != elements.end();
}) != params.end();
for (unsigned i = 0, end = inputRepr->getNumElements(); i != end; ++i) {
auto *eltTypeRepr = inputRepr->getElementType(i);
auto param = elements[i];
auto param = params[i];
if (param.isNoDerivative())
continue;
auto paramType = param.getPlainType();
Expand All @@ -2643,7 +2642,7 @@ bool TypeResolver::resolveASTFunctionTypeParams(
}
}

return false;
return params;
}

Type TypeResolver::resolveOpaqueReturnType(TypeRepr *repr,
Expand All @@ -2657,10 +2656,6 @@ Type TypeResolver::resolveOpaqueReturnType(TypeRepr *repr,
if (auto generic = dyn_cast<GenericIdentTypeRepr>(repr)) {
for (auto argRepr : generic->getGenericArgs()) {
auto argTy = resolveType(argRepr, options);
// If we cannot resolve the generic parameter, propagate the error out.
if (!argTy || argTy->hasError()) {
return ErrorType::get(Context);
}
TypeArgsBuf.push_back(argTy);
}
}
Expand Down Expand Up @@ -2697,12 +2692,11 @@ Type TypeResolver::resolveASTFunctionType(
TypeResolutionOptions options = None;
options |= parentOptions.withoutContext().getFlags();

SmallVector<AnyFunctionType::Param, 8> params;
if (resolveASTFunctionTypeParams(repr->getArgsTypeRepr(), options,
repr->getGenericEnvironment() != nullptr,
diffKind, params)) {
return Type();
}
SmallVector<AnyFunctionType::Param, 8> params =
resolveASTFunctionTypeParams(repr->getArgsTypeRepr(),
options,
repr->getGenericEnvironment() != nullptr,
diffKind);

Type outputTy = resolveType(repr->getResultTypeRepr(), options);
if (!outputTy || outputTy->hasError()) return outputTy;
Expand Down Expand Up @@ -3307,9 +3301,6 @@ Type TypeResolver::resolveArrayType(ArrayTypeRepr *repr,

auto sliceTy =
TypeChecker::getArraySliceType(repr->getBrackets().Start, baseTy);
if (!sliceTy)
return ErrorType::get(Context);

return sliceTy;
}

Expand All @@ -3324,22 +3315,21 @@ Type TypeResolver::resolveDictionaryType(DictionaryTypeRepr *repr,
if (!valueTy || valueTy->hasError()) return valueTy;

auto dictDecl = Context.getDictionaryDecl();

if (auto dictTy = TypeChecker::getDictionaryType(repr->getBrackets().Start,
keyTy, valueTy)) {

auto dictTy = TypeChecker::getDictionaryType(repr->getBrackets().Start,
keyTy, valueTy);
if (!dictTy->hasError()) {
auto unboundTy = dictDecl->getDeclaredType()->castTo<UnboundGenericType>();

Type args[] = {keyTy, valueTy};

if (!TypeChecker::applyUnboundGenericArguments(
unboundTy, dictDecl, repr->getStartLoc(), resolution, args)) {
return nullptr;
return ErrorType::get(Context);
}

return dictTy;
}

return ErrorType::get(Context);
return dictTy;
}

Type TypeResolver::resolveOptionalType(OptionalTypeRepr *repr,
Expand All @@ -3354,7 +3344,6 @@ Type TypeResolver::resolveOptionalType(OptionalTypeRepr *repr,

auto optionalTy = TypeChecker::getOptionalType(repr->getQuestionLoc(),
baseTy);
if (!optionalTy) return ErrorType::get(Context);

return optionalTy;
}
Expand Down Expand Up @@ -3426,9 +3415,6 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
uncheckedOptionalTy = TypeChecker::getOptionalType(repr->getExclamationLoc(),
baseTy);

if (!uncheckedOptionalTy)
return ErrorType::get(Context);

return uncheckedOptionalTy;
}

Expand Down Expand Up @@ -3877,13 +3863,13 @@ Type swift::resolveCustomAttrType(CustomAttr *attr, DeclContext *dc,
ASTContext &ctx = dc->getASTContext();
auto resolution = TypeResolution::forContextual(dc, options);
if (TypeChecker::validateType(attr->getTypeLoc(), resolution))
return Type();
return attr->getTypeLoc().getType();

// We always require the type to resolve to a nominal type.
Type type = attr->getTypeLoc().getType();
if (!type->getAnyNominal()) {
assert(ctx.Diags.hadAnyError());
return Type();
return ErrorType::get(ctx);
}

return type;
Expand Down