Skip to content

[Sema] Fix stale reference passed to PlaceholderType::get #36948

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

Merged
merged 1 commit into from
Apr 19, 2021
Merged
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 include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -5037,7 +5037,7 @@ class HandlePlaceholderType {
this->locator = cs.getConstraintLocator(locator);
}

Type operator()(PlaceholderTypeRepr *placeholderRepr) const {
Type operator()(ASTContext &ctx, PlaceholderTypeRepr *placeholderRepr) const {
return cs.createTypeVariable(
cs.getConstraintLocator(
locator, LocatorPathElt::PlaceholderType(placeholderRepr)),
Expand Down
18 changes: 6 additions & 12 deletions lib/Sema/PreCheckExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1467,12 +1467,9 @@ TypeExpr *PreCheckExpression::simplifyNestedTypeExpr(UnresolvedDotExpr *UDE) {
// For now, just return the unbound generic type.
return unboundTy;
},
/*placeholderHandler*/
[&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(getASTContext(), placeholderRepr);
});
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
PlaceholderType::get);
const auto BaseTy = resolution.resolveType(InnerTypeRepr);

if (BaseTy->mayHaveMembers()) {
Expand Down Expand Up @@ -2004,12 +2001,9 @@ Expr *PreCheckExpression::simplifyTypeConstructionWithLiteralArg(Expr *E) {
// For now, just return the unbound generic type.
return unboundTy;
},
/*placeholderHandler*/
[&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(getASTContext(), placeholderRepr);
});
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
PlaceholderType::get);
const auto result = resolution.resolveType(typeExpr->getTypeRepr());
if (result->hasError())
return nullptr;
Expand Down
17 changes: 6 additions & 11 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2184,11 +2184,9 @@ static Type validateParameterType(ParamDecl *decl) {
// For now, just return the unbound generic type.
return unboundTy;
};
placeholderHandler = [&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(ctx, placeholderRepr);
};
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
placeholderHandler = PlaceholderType::get;
} else if (isa<AbstractFunctionDecl>(dc)) {
options = TypeResolutionOptions(TypeResolverContext::AbstractFunctionDecl);
} else if (isa<SubscriptDecl>(dc)) {
Expand Down Expand Up @@ -2818,12 +2816,9 @@ ExtendedTypeRequest::evaluate(Evaluator &eval, ExtensionDecl *ext) const {
// For now, just return the unbound generic type.
return unboundTy;
},
/*placeholderHandler*/
[&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(ext->getASTContext(), placeholderRepr);
});
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
PlaceholderType::get);

const auto extendedType = resolution.resolveType(extendedRepr);

Expand Down
34 changes: 12 additions & 22 deletions lib/Sema/TypeCheckPattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,12 +475,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
// For now, just return the unbound generic type.
return unboundTy;
},
/*placeholderHandler*/
[&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(Context, placeholderRepr);
});
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
PlaceholderType::get);
const auto ty = resolution.resolveType(repr);
auto *enumDecl = dyn_cast_or_null<EnumDecl>(ty->getAnyNominal());
if (!enumDecl)
Expand Down Expand Up @@ -600,12 +597,9 @@ class ResolvePattern : public ASTVisitor<ResolvePattern,
// resolution. For now, just return the unbound generic type.
return unboundTy;
},
/*placeholderHandler*/
[&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(Context, placeholderRepr);
})
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
PlaceholderType::get)
.resolveType(prefixRepr);
auto *enumDecl = dyn_cast_or_null<EnumDecl>(enumTy->getAnyNominal());
if (!enumDecl)
Expand Down Expand Up @@ -813,11 +807,9 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
// For now, just return the unbound generic type.
return unboundTy;
};
placeholderHandler = [&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(Context, placeholderRepr);
};
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
placeholderHandler = PlaceholderType::get;
}
return validateTypedPattern(
cast<TypedPattern>(P),
Expand Down Expand Up @@ -883,11 +875,9 @@ Type PatternTypeRequest::evaluate(Evaluator &evaluator,
// For now, just return the unbound generic type.
return unboundTy;
};
placeholderHandler = [&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(Context, placeholderRepr);
};
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
placeholderHandler = PlaceholderType::get;
}
TypedPattern *TP = cast<TypedPattern>(somePat->getSubPattern());
const auto type = validateTypedPattern(
Expand Down
7 changes: 4 additions & 3 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2020,15 +2020,16 @@ NeverNullType TypeResolver::resolveType(TypeRepr *repr,
}

case TypeReprKind::Placeholder: {
auto &ctx = getASTContext();
// Fill in the placeholder if there's an appropriate handler.
if (const auto handlerFn = resolution.getPlaceholderHandler())
if (const auto ty = handlerFn(cast<PlaceholderTypeRepr>(repr)))
if (const auto ty = handlerFn(ctx, cast<PlaceholderTypeRepr>(repr)))
return ty;

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

return ErrorType::get(resolution.getASTContext());
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckType.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ using OpenUnboundGenericTypeFn = llvm::function_ref<Type(UnboundGenericType *)>;

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

/// Handles the resolution of types within a given declaration context,
/// which might involve resolving generic parameters to a particular
Expand Down
9 changes: 3 additions & 6 deletions lib/Sema/TypeChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,9 @@ Type swift::performTypeResolution(TypeRepr *TyR, ASTContext &Ctx,
// For now, just return the unbound generic type.
return unboundTy;
},
/*placeholderHandler*/
[&](auto placeholderRepr) {
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
return PlaceholderType::get(Ctx, placeholderRepr);
});
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
PlaceholderType::get);

Optional<DiagnosticSuppression> suppression;
if (!ProduceDiagnostics)
Expand Down