Skip to content

Commit bc1161d

Browse files
committed
improve error when using noncopyable type with subscript
Subscripts today don't support any form of ownership specifier for its parameters Since noncopyable types require such a specifier, it's not helpful to emit an error suggesting to add the specifier. rdar://109233314 (cherry picked from commit 3ce8244)
1 parent fbe13bb commit bc1161d

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7138,6 +7138,8 @@ ERROR(moveonly_cannot_conform_to_type, none,
71387138
(DescriptiveDeclKind, DeclName, Type))
71397139
ERROR(moveonly_parameter_missing_ownership, none,
71407140
"noncopyable parameter must specify its ownership", ())
7141+
ERROR(moveonly_parameter_subscript_unsupported, none,
7142+
"subscripts cannot have noncopyable parameters yet", ())
71417143
NOTE(moveonly_parameter_ownership_suggestion, none,
71427144
"add '%0' %1", (StringRef, StringRef))
71437145
ERROR(ownership_specifier_copyable,none,

lib/Sema/TypeCheckType.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2306,31 +2306,39 @@ bool TypeResolver::diagnoseMoveOnlyMissingOwnership(
23062306
if (options.contains(TypeResolutionFlags::HasOwnership))
23072307
return false;
23082308

2309-
// Do not run this on SIL files since there is currently a bug where we are
2310-
// trying to parse it in SILBoxes.
2311-
//
2312-
// To track what we want to do long term: rdar://105635373.
2309+
// Don't diagnose in SIL; ownership is already required there.
23132310
if (options.contains(TypeResolutionFlags::SILType))
23142311
return false;
23152312

2316-
diagnose(repr->getLoc(),
2317-
diag::moveonly_parameter_missing_ownership);
2313+
//////////////////
2314+
// At this point, we know we have a noncopyable parameter that is missing an
2315+
// ownership specifier, so we need to emit an error
2316+
2317+
// We don't yet support any ownership specifiers for parameters of subscript
2318+
// decls, give a tailored error message saying you simply can't use a
2319+
// noncopyable type here.
2320+
if (options.hasBase(TypeResolverContext::SubscriptDecl)) {
2321+
diagnose(repr->getLoc(), diag::moveonly_parameter_subscript_unsupported);
2322+
} else {
2323+
// general error diagnostic
2324+
diagnose(repr->getLoc(),
2325+
diag::moveonly_parameter_missing_ownership);
23182326

2319-
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2320-
"borrowing", "for an immutable reference")
2321-
.fixItInsert(repr->getStartLoc(), "borrowing ");
2327+
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2328+
"borrowing", "for an immutable reference")
2329+
.fixItInsert(repr->getStartLoc(), "borrowing ");
23222330

2323-
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2324-
"inout", "for a mutable reference")
2325-
.fixItInsert(repr->getStartLoc(), "inout ");
2331+
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2332+
"inout", "for a mutable reference")
2333+
.fixItInsert(repr->getStartLoc(), "inout ");
23262334

2327-
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2328-
"consuming", "to take the value from the caller")
2329-
.fixItInsert(repr->getStartLoc(), "consuming ");
2335+
diagnose(repr->getLoc(), diag::moveonly_parameter_ownership_suggestion,
2336+
"consuming", "to take the value from the caller")
2337+
.fixItInsert(repr->getStartLoc(), "consuming ");
2338+
}
23302339

23312340
// to avoid duplicate diagnostics
23322341
repr->setInvalid();
2333-
23342342
return true;
23352343
}
23362344

test/Sema/moveonly_require_ownership_specifier.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,12 @@ func takeInstantiated(_ x: NoncopyableWrapper<Int>) {}
8484
// expected-note@-2 {{add 'borrowing' for an immutable reference}}{{28-28=borrowing }}
8585
// expected-note@-3 {{add 'inout' for a mutable reference}}{{28-28=inout }}
8686
// expected-note@-4 {{add 'consuming' to take the value from the caller}}{{28-28=consuming }}
87+
88+
struct O: ~Copyable {}
89+
90+
public struct M: ~Copyable {
91+
subscript(_ i: O) -> Int { // expected-error {{subscripts cannot have noncopyable parameters}}
92+
get { fatalError() }
93+
set { }
94+
}
95+
}

0 commit comments

Comments
 (0)