Skip to content

Commit 7e406a7

Browse files
authored
Merge pull request #36948 from Jumhyn/placeholder-type-get-fix
[Sema] Fix stale reference passed to PlaceholderType::get
2 parents f171428 + 237b73d commit 7e406a7

File tree

7 files changed

+33
-56
lines changed

7 files changed

+33
-56
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5075,7 +5075,7 @@ class HandlePlaceholderType {
50755075
this->locator = cs.getConstraintLocator(locator);
50765076
}
50775077

5078-
Type operator()(PlaceholderTypeRepr *placeholderRepr) const {
5078+
Type operator()(ASTContext &ctx, PlaceholderTypeRepr *placeholderRepr) const {
50795079
return cs.createTypeVariable(
50805080
cs.getConstraintLocator(
50815081
locator, LocatorPathElt::PlaceholderType(placeholderRepr)),

lib/Sema/PreCheckExpr.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,12 +1467,9 @@ TypeExpr *PreCheckExpression::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
14671467
// For now, just return the unbound generic type.
14681468
return unboundTy;
14691469
},
1470-
/*placeholderHandler*/
1471-
[&](auto placeholderRepr) {
1472-
// FIXME: Don't let placeholder types escape type resolution.
1473-
// For now, just return the placeholder type.
1474-
return PlaceholderType::get(getASTContext(), placeholderRepr);
1475-
});
1470+
// FIXME: Don't let placeholder types escape type resolution.
1471+
// For now, just return the placeholder type.
1472+
PlaceholderType::get);
14761473
const auto BaseTy = resolution.resolveType(InnerTypeRepr);
14771474

14781475
if (BaseTy->mayHaveMembers()) {
@@ -2004,12 +2001,9 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
20042001
// For now, just return the unbound generic type.
20052002
return unboundTy;
20062003
},
2007-
/*placeholderHandler*/
2008-
[&](auto placeholderRepr) {
2009-
// FIXME: Don't let placeholder types escape type resolution.
2010-
// For now, just return the placeholder type.
2011-
return PlaceholderType::get(getASTContext(), placeholderRepr);
2012-
});
2004+
// FIXME: Don't let placeholder types escape type resolution.
2005+
// For now, just return the placeholder type.
2006+
PlaceholderType::get);
20132007
const auto result = resolution.resolveType(typeExpr->getTypeRepr());
20142008
if (result->hasError())
20152009
return nullptr;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2184,11 +2184,9 @@ static Type validateParameterType(ParamDecl *decl) {
21842184
// For now, just return the unbound generic type.
21852185
return unboundTy;
21862186
};
2187-
placeholderHandler = [&](auto placeholderRepr) {
2188-
// FIXME: Don't let placeholder types escape type resolution.
2189-
// For now, just return the placeholder type.
2190-
return PlaceholderType::get(ctx, placeholderRepr);
2191-
};
2187+
// FIXME: Don't let placeholder types escape type resolution.
2188+
// For now, just return the placeholder type.
2189+
placeholderHandler = PlaceholderType::get;
21922190
} else if (isa<AbstractFunctionDecl>(dc)) {
21932191
options = TypeResolutionOptions(TypeResolverContext::AbstractFunctionDecl);
21942192
} else if (isa<SubscriptDecl>(dc)) {
@@ -2818,12 +2816,9 @@ ExtendedTypeRequest::evaluate(Evaluator &eval, ExtensionDecl *ext) const {
28182816
// For now, just return the unbound generic type.
28192817
return unboundTy;
28202818
},
2821-
/*placeholderHandler*/
2822-
[&](auto placeholderRepr) {
2823-
// FIXME: Don't let placeholder types escape type resolution.
2824-
// For now, just return the placeholder type.
2825-
return PlaceholderType::get(ext->getASTContext(), placeholderRepr);
2826-
});
2819+
// FIXME: Don't let placeholder types escape type resolution.
2820+
// For now, just return the placeholder type.
2821+
PlaceholderType::get);
28272822

28282823
const auto extendedType = resolution.resolveType(extendedRepr);
28292824

lib/Sema/TypeCheckPattern.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
475475
// For now, just return the unbound generic type.
476476
return unboundTy;
477477
},
478-
/*placeholderHandler*/
479-
[&](auto placeholderRepr) {
480-
// FIXME: Don't let placeholder types escape type resolution.
481-
// For now, just return the placeholder type.
482-
return PlaceholderType::get(Context, placeholderRepr);
483-
});
478+
// FIXME: Don't let placeholder types escape type resolution.
479+
// For now, just return the placeholder type.
480+
PlaceholderType::get);
484481
const auto ty = resolution.resolveType(repr);
485482
auto *enumDecl = dyn_cast_or_null<EnumDecl>(ty->getAnyNominal());
486483
if (!enumDecl)
@@ -600,12 +597,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
600597
// resolution. For now, just return the unbound generic type.
601598
return unboundTy;
602599
},
603-
/*placeholderHandler*/
604-
[&](auto placeholderRepr) {
605-
// FIXME: Don't let placeholder types escape type resolution.
606-
// For now, just return the placeholder type.
607-
return PlaceholderType::get(Context, placeholderRepr);
608-
})
600+
// FIXME: Don't let placeholder types escape type resolution.
601+
// For now, just return the placeholder type.
602+
PlaceholderType::get)
609603
.resolveType(prefixRepr);
610604
auto *enumDecl = dyn_cast_or_null<EnumDecl>(enumTy->getAnyNominal());
611605
if (!enumDecl)
@@ -813,11 +807,9 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
813807
// For now, just return the unbound generic type.
814808
return unboundTy;
815809
};
816-
placeholderHandler = [&](auto placeholderRepr) {
817-
// FIXME: Don't let placeholder types escape type resolution.
818-
// For now, just return the placeholder type.
819-
return PlaceholderType::get(Context, placeholderRepr);
820-
};
810+
// FIXME: Don't let placeholder types escape type resolution.
811+
// For now, just return the placeholder type.
812+
placeholderHandler = PlaceholderType::get;
821813
}
822814
return validateTypedPattern(
823815
cast<TypedPattern>(P),
@@ -883,11 +875,9 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
883875
// For now, just return the unbound generic type.
884876
return unboundTy;
885877
};
886-
placeholderHandler = [&](auto placeholderRepr) {
887-
// FIXME: Don't let placeholder types escape type resolution.
888-
// For now, just return the placeholder type.
889-
return PlaceholderType::get(Context, placeholderRepr);
890-
};
878+
// FIXME: Don't let placeholder types escape type resolution.
879+
// For now, just return the placeholder type.
880+
placeholderHandler = PlaceholderType::get;
891881
}
892882
TypedPattern *TP = cast<TypedPattern>(somePat->getSubPattern());
893883
const auto type = validateTypedPattern(

lib/Sema/TypeCheckType.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,15 +2020,16 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
20202020
}
20212021

20222022
case TypeReprKind::Placeholder: {
2023+
auto &ctx = getASTContext();
20232024
// Fill in the placeholder if there's an appropriate handler.
20242025
if (const auto handlerFn = resolution.getPlaceholderHandler())
2025-
if (const auto ty = handlerFn(cast<PlaceholderTypeRepr>(repr)))
2026+
if (const auto ty = handlerFn(ctx, cast<PlaceholderTypeRepr>(repr)))
20262027
return ty;
20272028

20282029
// Complain if we're allowed to and bail out with an error.
20292030
if (!options.contains(TypeResolutionFlags::SilenceErrors))
2030-
getASTContext().Diags.diagnose(repr->getLoc(),
2031-
diag::placeholder_type_not_allowed);
2031+
ctx.Diags.diagnose(repr->getLoc(),
2032+
diag::placeholder_type_not_allowed);
20322033

20332034
return ErrorType::get(resolution.getASTContext());
20342035
}

lib/Sema/TypeCheckType.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ using OpenUnboundGenericTypeFn = llvm::function_ref<Type(UnboundGenericType *)>;
280280

281281
/// A function reference used to handle a PlaceholderTypeRepr.
282282
using HandlePlaceholderTypeReprFn =
283-
llvm::function_ref<Type(PlaceholderTypeRepr *)>;
283+
llvm::function_ref<Type(ASTContext &, PlaceholderTypeRepr *)>;
284284

285285
/// Handles the resolution of types within a given declaration context,
286286
/// which might involve resolving generic parameters to a particular

lib/Sema/TypeChecker.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,9 @@ Type swift::performTypeResolution(TypeRepr *TyR, ASTContext &Ctx,
389389
// For now, just return the unbound generic type.
390390
return unboundTy;
391391
},
392-
/*placeholderHandler*/
393-
[&](auto placeholderRepr) {
394-
// FIXME: Don't let placeholder types escape type resolution.
395-
// For now, just return the placeholder type.
396-
return PlaceholderType::get(Ctx, placeholderRepr);
397-
});
392+
// FIXME: Don't let placeholder types escape type resolution.
393+
// For now, just return the placeholder type.
394+
PlaceholderType::get);
398395

399396
Optional<DiagnosticSuppression> suppression;
400397
if (!ProduceDiagnostics)

0 commit comments

Comments
 (0)