Skip to content

Commit 2f4ed5a

Browse files
committed
Sema: Clean up diagnoseMissingOwnership()
- Pass down the TypeResolution instance so we can get the generic signature. This ensures we always use the correct signature in SIL mode. - Don't diagnose if the type contains error types.
1 parent 3fe15fe commit 2f4ed5a

File tree

8 files changed

+29
-40
lines changed

8 files changed

+29
-40
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,17 +2323,20 @@ static Type validateParameterType(ParamDecl *decl) {
23232323
break;
23242324
}
23252325

2326+
// If the element is a variadic parameter, resolve the parameter type as if
2327+
// it were in non-parameter position, since we want functions to be
2328+
// @escaping in this case.
2329+
options.setContext(isa<VarargTypeRepr>(nestedRepr)
2330+
? TypeResolverContext::VariadicFunctionInput
2331+
: TypeResolverContext::FunctionInput);
2332+
options |= TypeResolutionFlags::Direct;
2333+
2334+
const auto resolution =
2335+
TypeResolution::forInterface(dc, options, unboundTyOpener,
2336+
PlaceholderType::get,
2337+
/*packElementOpener*/ nullptr);
2338+
23262339
if (auto *varargTypeRepr = dyn_cast<VarargTypeRepr>(nestedRepr)) {
2327-
// If the element is a variadic parameter, resolve the parameter type as if
2328-
// it were in non-parameter position, since we want functions to be
2329-
// @escaping in this case.
2330-
options.setContext(TypeResolverContext::VariadicFunctionInput);
2331-
options |= TypeResolutionFlags::Direct;
2332-
2333-
const auto resolution =
2334-
TypeResolution::forInterface(dc, options, unboundTyOpener,
2335-
PlaceholderType::get,
2336-
/*packElementOpener*/ nullptr);
23372340
Ty = resolution.resolveType(nestedRepr);
23382341

23392342
// Monovariadic types (T...) for <T> resolve to [T].
@@ -2347,13 +2350,6 @@ static Type validateParameterType(ParamDecl *decl) {
23472350
return ErrorType::get(ctx);
23482351
}
23492352
} else {
2350-
options.setContext(TypeResolverContext::FunctionInput);
2351-
options |= TypeResolutionFlags::Direct;
2352-
2353-
const auto resolution =
2354-
TypeResolution::forInterface(dc, options, unboundTyOpener,
2355-
PlaceholderType::get,
2356-
/*packElementOpener*/ nullptr);
23572353
Ty = resolution.resolveType(decl->getTypeRepr());
23582354
}
23592355

@@ -2364,8 +2360,7 @@ static Type validateParameterType(ParamDecl *decl) {
23642360

23652361
// Validate the presence of ownership for a parameter with an inverse applied.
23662362
if (!Ty->hasUnboundGenericType() &&
2367-
diagnoseMissingOwnership(ctx, dc, ownership,
2368-
decl->getTypeRepr(), Ty, options)) {
2363+
diagnoseMissingOwnership(ownership, decl->getTypeRepr(), Ty, resolution)) {
23692364
decl->setInvalid();
23702365
return ErrorType::get(ctx);
23712366
}

lib/Sema/TypeCheckType.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2526,23 +2526,30 @@ bool TypeResolver::diagnoseMoveOnlyGeneric(TypeRepr *repr,
25262526
}
25272527

25282528

2529-
bool swift::diagnoseMissingOwnership(ASTContext &ctx, DeclContext *dc,
2530-
ParamSpecifier ownership,
2529+
bool swift::diagnoseMissingOwnership(ParamSpecifier ownership,
25312530
TypeRepr *repr, Type ty,
2532-
TypeResolutionOptions options) {
2531+
const TypeResolution &resolution) {
2532+
auto options = resolution.getOptions();
2533+
25332534
assert(!ty->hasError());
25342535
assert(!options.contains(TypeResolutionFlags::SILType));
25352536

25362537
if (options.hasBase(TypeResolverContext::EnumElementDecl))
25372538
return false; // no need for ownership in enum cases.
25382539

2539-
if (!isInterfaceTypeNoncopyable(ty, dc->getGenericEnvironmentOfContext()))
2540+
// The parameter type is written with respect to the surrounding
2541+
// generic environment.
2542+
ty = GenericEnvironment::mapTypeIntoContext(
2543+
resolution.getGenericSignature().getGenericEnvironment(),
2544+
ty);
2545+
2546+
if (ty->hasError() || !ty->isNoncopyable())
25402547
return false; // copyable types do not need ownership
25412548

25422549
if (ownership != ParamSpecifier::Default)
25432550
return false; // it has ownership
25442551

2545-
auto &diags = ctx.Diags;
2552+
auto &diags = resolution.getASTContext().Diags;
25462553
auto loc = repr->getLoc();
25472554
repr->setInvalid();
25482555

@@ -3776,8 +3783,7 @@ TypeResolver::resolveASTFunctionTypeParams(TupleTypeRepr *inputRepr,
37763783
if (inStage(TypeResolutionStage::Interface)
37773784
&& !ty->hasUnboundGenericType()
37783785
&& !options.contains(TypeResolutionFlags::SILMode)) {
3779-
diagnoseMissingOwnership(getASTContext(), dc, ownership,
3780-
eltTypeRepr, ty, options);
3786+
diagnoseMissingOwnership(ownership, eltTypeRepr, ty, resolution);
37813787

37823788
// @_staticExclusiveOnly types cannot be passed as 'inout' in function
37833789
// types.

lib/Sema/TypeCheckType.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,12 +674,10 @@ void diagnoseInvalidGenericArguments(SourceLoc loc, ValueDecl *decl,
674674
/// \param repr the repr for the type of the parameter.
675675
/// \param ty the non-error resolved type of the repr.
676676
/// \param ownership the ownership kind of the parameter
677-
/// \param dc the decl context used for resolving the type
678677
/// \returns true iff a diagnostic was emitted and the \c repr was invalidated.
679-
bool diagnoseMissingOwnership(ASTContext &ctx, DeclContext *dc,
680-
ParamSpecifier ownership,
678+
bool diagnoseMissingOwnership(ParamSpecifier ownership,
681679
TypeRepr *repr, Type ty,
682-
TypeResolutionOptions options);
680+
const TypeResolution &resolution);
683681

684682
} // end namespace swift
685683

test/Generics/function_defs.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
// XFAIL: noncopyable_generics
4-
53
//===----------------------------------------------------------------------===//
64
// Type-check function definitions
75
//===----------------------------------------------------------------------===//

test/Generics/occurs_check_diagnostic.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
// XFAIL: noncopyable_generics
4-
53
protocol P {
64
associatedtype A
75
}

test/decl/typealias/protocol.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
// XFAIL: noncopyable_generics
4-
53
// Tests for typealias inside protocols
64

75
protocol Bad {

validation-test/compiler_crashers_2_fixed/issue-61031.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
// XFAIL: noncopyable_generics
4-
53
public struct Wrapped<Values>: Sequence where Values: Sequence {
64
public var values: Values
75

validation-test/compiler_crashers_2_fixed/issue-63997.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// RUN: %target-typecheck-verify-swift
22

3-
// XFAIL: noncopyable_generics
4-
53
protocol P {
64
associatedtype A
75
}

0 commit comments

Comments
 (0)