Skip to content

Commit bd6724c

Browse files
committed
[NFC] Collapse TypeChecker::getDictionaryType
This method's only caller was using this to form a substituted DictionaryType, but also using TypeChecker::applyUnboundGenericArguments. We need to use the latter unconditionally so we actually check the requirements on DictionaryType and emit diagnostics. Also clean up resolveDictionaryType so it consistently returns ErrorTypes.
1 parent 4c0c7da commit bd6724c

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -370,17 +370,6 @@ Type TypeChecker::getArraySliceType(SourceLoc loc, Type elementType) {
370370
return ArraySliceType::get(elementType);
371371
}
372372

373-
Type TypeChecker::getDictionaryType(SourceLoc loc, Type keyType,
374-
Type valueType) {
375-
ASTContext &ctx = keyType->getASTContext();
376-
if (!ctx.getDictionaryDecl()) {
377-
ctx.Diags.diagnose(loc, diag::sugar_type_not_found, 3);
378-
return Type();
379-
}
380-
381-
return DictionaryType::get(keyType, valueType);
382-
}
383-
384373
Type TypeChecker::getOptionalType(SourceLoc loc, Type elementType) {
385374
ASTContext &ctx = elementType->getASTContext();
386375
if (!ctx.getOptionalDecl()) {
@@ -3305,11 +3294,13 @@ Type TypeResolver::resolveSpecifierTypeRepr(SpecifierTypeRepr *repr,
33053294
Type TypeResolver::resolveArrayType(ArrayTypeRepr *repr,
33063295
TypeResolutionOptions options) {
33073296
Type baseTy = resolveType(repr->getBase(), options.withoutContext());
3308-
if (!baseTy || baseTy->hasError()) return baseTy;
3297+
if (!baseTy || baseTy->hasError()) {
3298+
return ErrorType::get(Context);
3299+
}
33093300

33103301
auto sliceTy =
33113302
TypeChecker::getArraySliceType(repr->getBrackets().Start, baseTy);
3312-
if (!sliceTy)
3303+
if (sliceTy->hasError())
33133304
return ErrorType::get(Context);
33143305

33153306
return sliceTy;
@@ -3320,28 +3311,30 @@ Type TypeResolver::resolveDictionaryType(DictionaryTypeRepr *repr,
33203311
options = adjustOptionsForGenericArgs(options);
33213312

33223313
Type keyTy = resolveType(repr->getKey(), options.withoutContext());
3323-
if (!keyTy || keyTy->hasError()) return keyTy;
3314+
if (!keyTy || keyTy->hasError()) {
3315+
return ErrorType::get(Context);
3316+
}
33243317

33253318
Type valueTy = resolveType(repr->getValue(), options.withoutContext());
3326-
if (!valueTy || valueTy->hasError()) return valueTy;
3319+
if (!valueTy || valueTy->hasError()) {
3320+
return ErrorType::get(Context);
3321+
}
33273322

33283323
auto dictDecl = Context.getDictionaryDecl();
3329-
3330-
if (auto dictTy = TypeChecker::getDictionaryType(repr->getBrackets().Start,
3331-
keyTy, valueTy)) {
3332-
auto unboundTy = dictDecl->getDeclaredType()->castTo<UnboundGenericType>();
3333-
3334-
Type args[] = {keyTy, valueTy};
3335-
3336-
if (!TypeChecker::applyUnboundGenericArguments(
3337-
unboundTy, dictDecl, repr->getStartLoc(), resolution, args)) {
3338-
return nullptr;
3339-
}
3340-
3341-
return dictTy;
3324+
if (!dictDecl) {
3325+
Context.Diags.diagnose(repr->getBrackets().Start,
3326+
diag::sugar_type_not_found, 3);
3327+
return ErrorType::get(Context);
33423328
}
33433329

3344-
return ErrorType::get(Context);
3330+
auto unboundTy = dictDecl->getDeclaredType()->castTo<UnboundGenericType>();
3331+
Type args[] = {keyTy, valueTy};
3332+
if (!TypeChecker::applyUnboundGenericArguments(
3333+
unboundTy, dictDecl, repr->getStartLoc(), resolution, args)) {
3334+
assert(Context.Diags.hadAnyError());
3335+
return ErrorType::get(Context);
3336+
}
3337+
return DictionaryType::get(keyTy, valueTy);
33453338
}
33463339

33473340
Type TypeResolver::resolveOptionalType(OptionalTypeRepr *repr,

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ enum class CheckedCastContextKind {
345345

346346
namespace TypeChecker {
347347
Type getArraySliceType(SourceLoc loc, Type elementType);
348-
Type getDictionaryType(SourceLoc loc, Type keyType, Type valueType);
349348
Type getOptionalType(SourceLoc loc, Type elementType);
350349
Type getStringType(ASTContext &ctx);
351350
Type getSubstringType(ASTContext &ctx);

0 commit comments

Comments
 (0)