@@ -1647,6 +1647,34 @@ namespace {
1647
1647
const auto DefaultParameterConvention = ParameterConvention::Direct_Unowned;
1648
1648
const auto DefaultResultConvention = ResultConvention::Unowned;
1649
1649
1650
+ // / A wrapper that ensures that the returned type from
1651
+ // / \c TypeResolver::resolveType is never the null \c Type. It otherwise
1652
+ // / tries to behave like \c Type, so it provides the proper conversion and
1653
+ // / arrow operators.
1654
+ class NeverNullType final {
1655
+ public:
1656
+ // / Forbid default construction.
1657
+ NeverNullType () = delete ;
1658
+ // / Forbid construction from \c nullptr.
1659
+ NeverNullType (std::nullptr_t ) = delete ;
1660
+
1661
+ public:
1662
+ // / Construct a never-null Type. If \p Ty is null, a fatal error is thrown.
1663
+ NeverNullType (Type Ty) : WrappedTy(Ty) {
1664
+ if (Ty.isNull ()) {
1665
+ llvm::report_fatal_error (" Resolved to null type!" );
1666
+ }
1667
+ }
1668
+
1669
+ operator Type () const { return WrappedTy; }
1670
+ Type get () const { return WrappedTy; }
1671
+
1672
+ TypeBase *operator ->() const { return WrappedTy.operator ->(); }
1673
+
1674
+ private:
1675
+ Type WrappedTy;
1676
+ };
1677
+
1650
1678
class TypeResolver {
1651
1679
ASTContext &Context;
1652
1680
TypeResolution resolution;
@@ -1660,7 +1688,7 @@ namespace {
1660
1688
{
1661
1689
}
1662
1690
1663
- Type resolveType (TypeRepr *repr, TypeResolutionOptions options);
1691
+ NeverNullType resolveType (TypeRepr *repr, TypeResolutionOptions options);
1664
1692
1665
1693
private:
1666
1694
template <typename ...ArgTypes>
@@ -1795,7 +1823,8 @@ Type ResolveTypeRequest::evaluate(Evaluator &evaluator,
1795
1823
return result;
1796
1824
}
1797
1825
1798
- Type TypeResolver::resolveType (TypeRepr *repr, TypeResolutionOptions options) {
1826
+ NeverNullType TypeResolver::resolveType (TypeRepr *repr,
1827
+ TypeResolutionOptions options) {
1799
1828
assert (repr && " Cannot validate null TypeReprs!" );
1800
1829
1801
1830
// If we know the type representation is invalid, just return an
@@ -1891,9 +1920,9 @@ Type TypeResolver::resolveType(TypeRepr *repr, TypeResolutionOptions options) {
1891
1920
options |= TypeResolutionFlags::SilenceErrors;
1892
1921
auto constraintType = resolveType (opaqueRepr->getConstraint (),
1893
1922
options);
1894
-
1895
- return constraintType && !constraintType->hasError ()
1896
- ? ErrorType::get (constraintType) : ErrorType::get (Context);
1923
+
1924
+ return !constraintType->hasError () ? ErrorType::get (constraintType )
1925
+ : ErrorType::get (Context);
1897
1926
}
1898
1927
1899
1928
case TypeReprKind::Fixed:
@@ -1976,7 +2005,7 @@ Type TypeResolver::resolveAttributedType(TypeAttributes &attrs,
1976
2005
instanceOptions -= TypeResolutionFlags::SILType;
1977
2006
1978
2007
auto instanceTy = resolveType (base, instanceOptions);
1979
- if (!instanceTy || instanceTy->hasError ())
2008
+ if (instanceTy->hasError ())
1980
2009
return instanceTy;
1981
2010
1982
2011
// Check for @thin.
@@ -2440,8 +2469,8 @@ TypeResolver::resolveASTFunctionTypeParams(TupleTypeRepr *inputRepr,
2440
2469
variadic = true ;
2441
2470
}
2442
2471
2443
- Type ty = resolveType (eltTypeRepr, thisElementOptions);
2444
- if (!ty || ty->hasError ()) {
2472
+ auto ty = resolveType (eltTypeRepr, thisElementOptions);
2473
+ if (ty->hasError ()) {
2445
2474
elements.emplace_back (ErrorType::get (Context));
2446
2475
continue ;
2447
2476
}
@@ -2550,7 +2579,7 @@ Type TypeResolver::resolveOpaqueReturnType(TypeRepr *repr,
2550
2579
for (auto argRepr : generic->getGenericArgs ()) {
2551
2580
auto argTy = resolveType (argRepr, options);
2552
2581
// If we cannot resolve the generic parameter, propagate the error out.
2553
- if (!argTy || argTy->hasError ()) {
2582
+ if (argTy->hasError ()) {
2554
2583
return ErrorType::get (Context);
2555
2584
}
2556
2585
TypeArgsBuf.push_back (argTy);
@@ -2594,8 +2623,8 @@ Type TypeResolver::resolveASTFunctionType(
2594
2623
2595
2624
auto resultOptions = options.withoutContext ();
2596
2625
resultOptions.setContext (TypeResolverContext::FunctionResult);
2597
- Type outputTy = resolveType (repr->getResultTypeRepr (), resultOptions);
2598
- if (!outputTy || outputTy->hasError ()) {
2626
+ auto outputTy = resolveType (repr->getResultTypeRepr (), resultOptions);
2627
+ if (outputTy->hasError ()) {
2599
2628
return ErrorType::get (Context);
2600
2629
}
2601
2630
@@ -3255,8 +3284,8 @@ Type TypeResolver::resolveSpecifierTypeRepr(SpecifierTypeRepr *repr,
3255
3284
3256
3285
Type TypeResolver::resolveArrayType (ArrayTypeRepr *repr,
3257
3286
TypeResolutionOptions options) {
3258
- Type baseTy = resolveType (repr->getBase (), options.withoutContext ());
3259
- if (!baseTy || baseTy->hasError ()) {
3287
+ auto baseTy = resolveType (repr->getBase (), options.withoutContext ());
3288
+ if (baseTy->hasError ()) {
3260
3289
return ErrorType::get (Context);
3261
3290
}
3262
3291
@@ -3272,13 +3301,13 @@ Type TypeResolver::resolveDictionaryType(DictionaryTypeRepr *repr,
3272
3301
TypeResolutionOptions options) {
3273
3302
options = adjustOptionsForGenericArgs (options);
3274
3303
3275
- Type keyTy = resolveType (repr->getKey (), options.withoutContext ());
3276
- if (!keyTy || keyTy->hasError ()) {
3304
+ auto keyTy = resolveType (repr->getKey (), options.withoutContext ());
3305
+ if (keyTy->hasError ()) {
3277
3306
return ErrorType::get (Context);
3278
3307
}
3279
3308
3280
- Type valueTy = resolveType (repr->getValue (), options.withoutContext ());
3281
- if (!valueTy || valueTy->hasError ()) {
3309
+ auto valueTy = resolveType (repr->getValue (), options.withoutContext ());
3310
+ if (valueTy->hasError ()) {
3282
3311
return ErrorType::get (Context);
3283
3312
}
3284
3313
@@ -3304,8 +3333,8 @@ Type TypeResolver::resolveOptionalType(OptionalTypeRepr *repr,
3304
3333
TypeResolutionOptions elementOptions = options.withoutContext (true );
3305
3334
elementOptions.setContext (TypeResolverContext::ImmediateOptionalTypeArgument);
3306
3335
3307
- Type baseTy = resolveType (repr->getBase (), elementOptions);
3308
- if (!baseTy || baseTy->hasError ()) {
3336
+ auto baseTy = resolveType (repr->getBase (), elementOptions);
3337
+ if (baseTy->hasError ()) {
3309
3338
return ErrorType::get (Context);
3310
3339
}
3311
3340
@@ -3376,12 +3405,12 @@ Type TypeResolver::resolveImplicitlyUnwrappedOptionalType(
3376
3405
TypeResolutionOptions elementOptions = options.withoutContext (true );
3377
3406
elementOptions.setContext (TypeResolverContext::ImmediateOptionalTypeArgument);
3378
3407
3379
- Type baseTy = resolveType (repr->getBase (), elementOptions);
3380
- if (!baseTy || baseTy->hasError ()) {
3408
+ auto baseTy = resolveType (repr->getBase (), elementOptions);
3409
+ if (baseTy->hasError ()) {
3381
3410
return ErrorType::get (Context);
3382
3411
}
3383
3412
3384
- Type uncheckedOptionalTy =
3413
+ auto uncheckedOptionalTy =
3385
3414
TypeChecker::getOptionalType (repr->getExclamationLoc (), baseTy);
3386
3415
if (uncheckedOptionalTy->hasError ()) {
3387
3416
return ErrorType::get (Context);
@@ -3416,8 +3445,8 @@ Type TypeResolver::resolveTupleType(TupleTypeRepr *repr,
3416
3445
for (unsigned i = 0 , end = repr->getNumElements (); i != end; ++i) {
3417
3446
auto *tyR = repr->getElementType (i);
3418
3447
3419
- Type ty = resolveType (tyR, elementOptions);
3420
- if (!ty || ty->hasError ())
3448
+ auto ty = resolveType (tyR, elementOptions);
3449
+ if (ty->hasError ())
3421
3450
hadError = true ;
3422
3451
3423
3452
auto eltName = repr->getElementName (i);
@@ -3485,8 +3514,8 @@ Type TypeResolver::resolveCompositionType(CompositionTypeRepr *repr,
3485
3514
};
3486
3515
3487
3516
for (auto tyR : repr->getTypes ()) {
3488
- Type ty = resolveType (tyR, options.withoutContext ());
3489
- if (!ty || ty->hasError ()) return ty;
3517
+ auto ty = resolveType (tyR, options.withoutContext ());
3518
+ if (ty->hasError ()) return ty;
3490
3519
3491
3520
auto nominalDecl = ty->getAnyNominal ();
3492
3521
if (nominalDecl && isa<ClassDecl>(nominalDecl)) {
@@ -3529,8 +3558,8 @@ Type TypeResolver::resolveCompositionType(CompositionTypeRepr *repr,
3529
3558
Type TypeResolver::resolveMetatypeType (MetatypeTypeRepr *repr,
3530
3559
TypeResolutionOptions options) {
3531
3560
// The instance type of a metatype is always abstract, not SIL-lowered.
3532
- Type ty = resolveType (repr->getBase (), options.withoutContext ());
3533
- if (!ty || ty->hasError ()) {
3561
+ auto ty = resolveType (repr->getBase (), options.withoutContext ());
3562
+ if (ty->hasError ()) {
3534
3563
return ErrorType::get (Context);
3535
3564
}
3536
3565
@@ -3562,8 +3591,8 @@ Type TypeResolver::buildMetatypeType(
3562
3591
Type TypeResolver::resolveProtocolType (ProtocolTypeRepr *repr,
3563
3592
TypeResolutionOptions options) {
3564
3593
// The instance type of a metatype is always abstract, not SIL-lowered.
3565
- Type ty = resolveType (repr->getBase (), options.withoutContext ());
3566
- if (!ty || ty->hasError ()) {
3594
+ auto ty = resolveType (repr->getBase (), options.withoutContext ());
3595
+ if (ty->hasError ()) {
3567
3596
return ErrorType::get (Context);
3568
3597
}
3569
3598
0 commit comments