Skip to content

Commit 8afef07

Browse files
committed
[NFC] Move resolveIdentifierType from TypeChecker to TypeResolver
Reduce the scope of this function since its remaining callers can just make use of type resolution directly.
1 parent 7bdc402 commit 8afef07

File tree

3 files changed

+56
-65
lines changed

3 files changed

+56
-65
lines changed

lib/Sema/TypeCheckPattern.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,7 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
467467
auto *repr = IdentTypeRepr::create(Context, components);
468468

469469
// See if the repr resolves to a type.
470-
Type ty = TypeChecker::resolveIdentifierType(
471-
TypeResolution::forContextual(DC, options), repr);
472-
470+
auto ty = TypeResolution::forContextual(DC, options).resolveType(repr);
473471
auto *enumDecl = dyn_cast_or_null<EnumDecl>(ty->getAnyNominal());
474472
if (!enumDecl)
475473
return nullptr;
@@ -566,8 +564,8 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
566564
auto *prefixRepr = IdentTypeRepr::create(Context, components);
567565

568566
// See first if the entire repr resolves to a type.
569-
Type enumTy = TypeChecker::resolveIdentifierType(
570-
TypeResolution::forContextual(DC, options), prefixRepr);
567+
Type enumTy = TypeResolution::forContextual(DC, options)
568+
.resolveType(prefixRepr);
571569
if (!dyn_cast_or_null<EnumDecl>(enumTy->getAnyNominal()))
572570
return nullptr;
573571

lib/Sema/TypeCheckType.cpp

Lines changed: 53 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,60 +1632,6 @@ static Type applyNonEscapingFromContext(DeclContext *DC,
16321632
return ty;
16331633
}
16341634

1635-
/// Returns a valid type or ErrorType in case of an error.
1636-
Type TypeChecker::resolveIdentifierType(TypeResolution resolution,
1637-
IdentTypeRepr *IdType) {
1638-
const auto options = resolution.getOptions();
1639-
auto DC = resolution.getDeclContext();
1640-
ASTContext &ctx = DC->getASTContext();
1641-
auto &diags = ctx.Diags;
1642-
auto ComponentRange = IdType->getComponentRange();
1643-
auto Components = llvm::makeArrayRef(ComponentRange.begin(),
1644-
ComponentRange.end());
1645-
Type result = resolveIdentTypeComponent(resolution, Components);
1646-
if (!result) return nullptr;
1647-
1648-
if (auto moduleTy = result->getAs<ModuleType>()) {
1649-
// Allow module types only if flag is specified.
1650-
if (options.contains(TypeResolutionFlags::AllowModule))
1651-
return moduleTy;
1652-
// Otherwise, emit an error.
1653-
if (!options.contains(TypeResolutionFlags::SilenceErrors)) {
1654-
auto moduleName = moduleTy->getModule()->getName();
1655-
diags.diagnose(Components.back()->getNameLoc(),
1656-
diag::cannot_find_type_in_scope, DeclNameRef(moduleName));
1657-
diags.diagnose(Components.back()->getNameLoc(),
1658-
diag::note_module_as_type, moduleName);
1659-
}
1660-
Components.back()->setInvalid();
1661-
return ErrorType::get(ctx);
1662-
}
1663-
1664-
// Hack to apply context-specific @escaping to a typealias with an underlying
1665-
// function type.
1666-
if (result->is<FunctionType>())
1667-
result = applyNonEscapingFromContext(DC, result, options);
1668-
1669-
// Check the availability of the type.
1670-
1671-
// We allow a type to conform to a protocol that is less available than
1672-
// the type itself. This enables a type to retroactively model or directly
1673-
// conform to a protocol only available on newer OSes and yet still be used on
1674-
// older OSes.
1675-
// To support this, inside inheritance clauses we allow references to
1676-
// protocols that are unavailable in the current type refinement context.
1677-
1678-
if (!options.contains(TypeResolutionFlags::SilenceErrors) &&
1679-
!options.contains(TypeResolutionFlags::AllowUnavailable) &&
1680-
diagnoseAvailability(IdType, DC,
1681-
options.contains(TypeResolutionFlags::AllowUnavailableProtocol))) {
1682-
Components.back()->setInvalid();
1683-
return ErrorType::get(ctx);
1684-
}
1685-
1686-
return result;
1687-
}
1688-
16891635
/// Validate whether type associated with @autoclosure attribute is correct,
16901636
/// it supposed to be a function type with no parameters.
16911637
/// \returns true if there was an error, false otherwise.
@@ -1831,6 +1777,8 @@ namespace {
18311777
SmallVectorImpl<SILYieldInfo> &yields,
18321778
SmallVectorImpl<SILResultInfo> &results,
18331779
Optional<SILResultInfo> &errorResult);
1780+
Type resolveIdentifierType(IdentTypeRepr *IdType,
1781+
TypeResolutionOptions options);
18341782
Type resolveSpecifierTypeRepr(SpecifierTypeRepr *repr,
18351783
TypeResolutionOptions options);
18361784
Type resolveArrayType(ArrayTypeRepr *repr,
@@ -1949,8 +1897,7 @@ Type TypeResolver::resolveType(TypeRepr *repr, TypeResolutionOptions options) {
19491897
case TypeReprKind::SimpleIdent:
19501898
case TypeReprKind::GenericIdent:
19511899
case TypeReprKind::CompoundIdent:
1952-
return TypeChecker::resolveIdentifierType(resolution.withOptions(options),
1953-
cast<IdentTypeRepr>(repr));
1900+
return resolveIdentifierType(cast<IdentTypeRepr>(repr), options);
19541901

19551902
case TypeReprKind::Function: {
19561903
if (!(options & TypeResolutionFlags::SILType)) {
@@ -3258,6 +3205,56 @@ bool TypeResolver::resolveSILResults(TypeRepr *repr,
32583205
yields, ordinaryResults, errorResult);
32593206
}
32603207

3208+
Type TypeResolver::resolveIdentifierType(IdentTypeRepr *IdType,
3209+
TypeResolutionOptions options) {
3210+
auto ComponentRange = IdType->getComponentRange();
3211+
auto Components = llvm::makeArrayRef(ComponentRange.begin(),
3212+
ComponentRange.end());
3213+
Type result = resolveIdentTypeComponent(resolution.withOptions(options),
3214+
Components);
3215+
if (!result) return nullptr;
3216+
3217+
if (auto moduleTy = result->getAs<ModuleType>()) {
3218+
// Allow module types only if flag is specified.
3219+
if (options.contains(TypeResolutionFlags::AllowModule))
3220+
return moduleTy;
3221+
// Otherwise, emit an error.
3222+
if (!options.contains(TypeResolutionFlags::SilenceErrors)) {
3223+
auto moduleName = moduleTy->getModule()->getName();
3224+
diagnose(Components.back()->getNameLoc(),
3225+
diag::cannot_find_type_in_scope, DeclNameRef(moduleName));
3226+
diagnose(Components.back()->getNameLoc(),
3227+
diag::note_module_as_type, moduleName);
3228+
}
3229+
Components.back()->setInvalid();
3230+
return ErrorType::get(Context);
3231+
}
3232+
3233+
// Hack to apply context-specific @escaping to a typealias with an underlying
3234+
// function type.
3235+
if (result->is<FunctionType>())
3236+
result = applyNonEscapingFromContext(DC, result, options);
3237+
3238+
// Check the availability of the type.
3239+
3240+
// We allow a type to conform to a protocol that is less available than
3241+
// the type itself. This enables a type to retroactively model or directly
3242+
// conform to a protocol only available on newer OSes and yet still be used on
3243+
// older OSes.
3244+
// To support this, inside inheritance clauses we allow references to
3245+
// protocols that are unavailable in the current type refinement context.
3246+
3247+
if (!options.contains(TypeResolutionFlags::SilenceErrors) &&
3248+
!options.contains(TypeResolutionFlags::AllowUnavailable) &&
3249+
diagnoseAvailability(IdType, DC,
3250+
options.contains(TypeResolutionFlags::AllowUnavailableProtocol))) {
3251+
Components.back()->setInvalid();
3252+
return ErrorType::get(Context);
3253+
}
3254+
3255+
return result;
3256+
}
3257+
32613258
Type TypeResolver::resolveSpecifierTypeRepr(SpecifierTypeRepr *repr,
32623259
TypeResolutionOptions options) {
32633260
// inout is only valid for (non-Subscript and non-EnumCaseDecl)

lib/Sema/TypeChecker.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,6 @@ Type getIntType(ASTContext &ctx);
353353
Type getInt8Type(ASTContext &ctx);
354354
Type getUInt8Type(ASTContext &ctx);
355355

356-
/// Try to resolve an IdentTypeRepr, returning either the referenced
357-
/// Type or an ErrorType in case of error.
358-
Type resolveIdentifierType(TypeResolution resolution, IdentTypeRepr *IdType);
359-
360356
/// Bind an UnresolvedDeclRefExpr by performing name lookup and
361357
/// returning the resultant expression. Context is the DeclContext used
362358
/// for the lookup.

0 commit comments

Comments
 (0)