Skip to content

Commit af933c6

Browse files
committed
Sema: remove TypeResolver::diagnoseMoveOnlyGeneric
1 parent 8f2ed59 commit af933c6

File tree

3 files changed

+54
-50
lines changed

3 files changed

+54
-50
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,9 +2106,6 @@ namespace {
21062106
repr->setInvalid();
21072107
return diags.diagnose(std::forward<ArgTypes>(Args)...);
21082108
}
2109-
2110-
bool diagnoseMoveOnlyGeneric(TypeRepr *repr,
2111-
Type unboundTy, Type genericArgTy);
21122109

21132110
bool diagnoseDisallowedExistential(TypeRepr *repr);
21142111

@@ -2500,33 +2497,6 @@ bool TypeResolver::diagnoseInvalidPlaceHolder(OpaqueReturnTypeRepr *repr) {
25002497
return false;
25012498
}
25022499

2503-
/// Checks the given type, assuming that it appears as an argument for a
2504-
/// generic parameter in the \c repr, to see if it is move-only.
2505-
///
2506-
/// Because generic type parameters currently all assume copyability of
2507-
/// the substituted type, it's an error for a move-only type to appear
2508-
/// as an argument for type parameters.
2509-
///
2510-
/// returns true if an error diagnostic was emitted
2511-
bool TypeResolver::diagnoseMoveOnlyGeneric(TypeRepr *repr,
2512-
Type unboundTy,
2513-
Type genericArgTy) {
2514-
if (getASTContext().LangOpts.hasFeature(Feature::NoncopyableGenerics))
2515-
return false;
2516-
2517-
if (genericArgTy->isNoncopyable()) {
2518-
if (unboundTy) {
2519-
diagnoseInvalid(repr, repr->getLoc(), diag::noncopyable_generics_specific,
2520-
genericArgTy, unboundTy);
2521-
} else {
2522-
diagnoseInvalid(repr, repr->getLoc(), diag::noncopyable_generics,
2523-
genericArgTy);
2524-
}
2525-
return true;
2526-
}
2527-
return false;
2528-
}
2529-
25302500

25312501
bool swift::diagnoseMissingOwnership(ParamSpecifier ownership,
25322502
TypeRepr *repr, Type ty,
@@ -5011,17 +4981,26 @@ NeverNullType TypeResolver::resolveArrayType(ArrayTypeRepr *repr,
50114981
// If the standard library isn't loaded, we ought to let the user know
50124982
// something has gone terribly wrong, since the rest of the compiler is going
50134983
// to assume it can canonicalize [T] to Array<T>.
5014-
if (!ctx.getArrayDecl()) {
5015-
ctx.Diags.diagnose(repr->getBrackets().Start,
5016-
diag::sugar_type_not_found, 0);
5017-
return ErrorType::get(ctx);
5018-
}
4984+
{
4985+
// Check that we can validly substitute the baseTy into an array. We do not
4986+
// actually resolve to that valid array type, as we want to return the
4987+
// sugared Type node ArraySliceType instead!
4988+
auto *arrayDecl = ctx.getArrayDecl();
4989+
if (!arrayDecl) {
4990+
ctx.Diags.diagnose(repr->getBrackets().Start,
4991+
diag::sugar_type_not_found, 0);
4992+
return ErrorType::get(ctx);
4993+
}
50194994

5020-
// do not allow move-only types in an array
5021-
if (diagnoseMoveOnlyGeneric(repr,
5022-
ctx.getArrayDecl()->getDeclaredInterfaceType(),
5023-
baseTy)) {
5024-
return ErrorType::get(ctx);
4995+
Type genericArgs[1] = {baseTy};
4996+
auto arrayTy =
4997+
resolution.applyUnboundGenericArguments(arrayDecl,
4998+
/*parentTy=*/nullptr,
4999+
repr->getBrackets().Start,
5000+
genericArgs);
5001+
if (arrayTy->hasError()) {
5002+
return ErrorType::get(ctx);
5003+
}
50255004
}
50265005

50275006
return ArraySliceType::get(baseTy);
@@ -5116,11 +5095,17 @@ NeverNullType TypeResolver::resolveOptionalType(OptionalTypeRepr *repr,
51165095
return ErrorType::get(ctx);
51175096
}
51185097

5119-
// do not allow move-only types in an optional
5120-
if (diagnoseMoveOnlyGeneric(repr,
5121-
ctx.getOptionalDecl()->getDeclaredInterfaceType(),
5122-
baseTy)) {
5123-
return ErrorType::get(ctx);
5098+
{
5099+
// Check that we can validly substitute the baseTy into an Optional
5100+
Type genericArgs[1] = {baseTy};
5101+
auto substTy =
5102+
resolution.applyUnboundGenericArguments(ctx.getOptionalDecl(),
5103+
/*parentTy=*/nullptr,
5104+
repr->getQuestionLoc(),
5105+
genericArgs);
5106+
if (substTy->hasError()) {
5107+
return ErrorType::get(ctx);
5108+
}
51245109
}
51255110

51265111
return optionalTy;
@@ -5221,11 +5206,17 @@ NeverNullType TypeResolver::resolveImplicitlyUnwrappedOptionalType(
52215206
return ErrorType::get(ctx);
52225207
}
52235208

5224-
// do not allow move-only types in an implicitly-unwrapped optional
5225-
if (diagnoseMoveOnlyGeneric(repr,
5226-
ctx.getOptionalDecl()->getDeclaredInterfaceType(),
5227-
baseTy)) {
5228-
return ErrorType::get(ctx);
5209+
{
5210+
// Check that we can validly substitute the baseTy into an Optional
5211+
Type genericArgs[1] = {baseTy};
5212+
auto substTy =
5213+
resolution.applyUnboundGenericArguments(ctx.getOptionalDecl(),
5214+
/*parentTy=*/nullptr,
5215+
repr->getExclamationLoc(),
5216+
genericArgs);
5217+
if (substTy->hasError()) {
5218+
return ErrorType::get(ctx);
5219+
}
52295220
}
52305221

52315222
return uncheckedOptionalTy;

lib/Sema/TypeChecker.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ enum class CheckedCastContextKind {
286286
};
287287

288288
namespace TypeChecker {
289+
// DANGER: callers must verify that elementType satisfies the requirements of
290+
// the Wrapped generic parameter, as this function will not do so!
289291
Type getOptionalType(SourceLoc loc, Type elementType);
290292

291293
/// Bind an UnresolvedDeclRefExpr by performing name lookup and

test/Generics/inverse_generics.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,14 @@ struct SSS: ~Copyable, PPP {}
484484
protocol PPP: ~Copyable {}
485485
let global__old__: any PPP = SSS() // expected-error {{value of type 'SSS' does not conform to specified type 'Copyable'}}
486486
let global__new__: any PPP & ~Copyable = SSS()
487+
488+
489+
struct Example<T> {}
490+
491+
struct TestResolution {
492+
var maybeNC: NC? = nil // expected-error {{type 'NC' does not conform to protocol 'Copyable'}}
493+
var maybeIOUNC: NC! = nil // expected-error {{type 'NC' does not conform to protocol 'Copyable'}}
494+
var arrayNC: [NC] = [] // expected-error {{type 'NC' does not conform to protocol 'Copyable'}}
495+
var dictNC: [String: NC] = [:] // expected-error {{type 'NC' does not conform to protocol 'Copyable'}}
496+
var exampleNC: Example<NC> = Example() // expected-error {{type 'NC' does not conform to protocol 'Copyable'}}
497+
}

0 commit comments

Comments
 (0)